var nObj = false;
var nMain = false;
var nTmr = false;

function nInit()
{
   if (document.getElementById && document.getElementsByTagName)
   {
      if (document.getElementById('navigation'))
      {
         var n = document.getElementById('navigation').getElementsByTagName('ul')[0].childNodes;
         for (var i=0;i<n.length;i++)
         {
            if (n[i].nodeType==1 && n[i].tagName.toLowerCase()=='li')
            {
               DOMAddEventHandler(n[i].getElementsByTagName('img')[0], 'mouseover', nEnt);
               if (n[i].getElementsByTagName('ul').length>0)
               {
                  var s = n[i].getElementsByTagName('ul')[0].getElementsByTagName('a');
                  for (var k=0;k<s.length;k++)
                  {
                     DOMAddEventHandler(s[k], 'mouseover', nOvr);
                     DOMAddEventHandler(s[k], 'mouseout', nOut);
                  }
               }
            }
         }
      }
   }

   if (document.comfr)
   {
      if (cookieValue('comment'))
      {
         var p = new RegExp('[+]', 'ig');
          var i = 	unescape(cookieValue('comment')).split('>');
          document.comfr.name.value = unescape(i[0]).replace(p,' ');
          document.comfr.email.value = unescape(i[2]).replace(p,' ');
          document.comfr.place.value = unescape(i[1]).replace(p,' ');
          document.comfr.url.value = unescape(i[3]).replace(p,' ');
      }
   }

}

window.onload = nInit; 

function DOMAddEventHandler(o, tp, f)
{
   if (o.addEventListener)
      o.addEventListener(tp, f, false);
   else if (o.attachEvent)
      o.attachEvent('on'+ tp, f );
} 

function nEnt(e)
{
   if (!e) var e = window.event;
   if (e.target)
      var obj = e.target;
   else if (e.srcElement)
      var obj = e.srcElement;
   if (obj)
   {
      if (nTmr)
         clearTimeout(nTmr);
      if (nMain != obj)
         nHd();
      nObj = false;
      nMain = obj;
      if (obj.src.toString().indexOf('/')>0)
         var sr = obj.src.toString().substr(obj.src.toString().lastIndexOf('/')+1);
      else
         var sr = obj.src.toString();
      if (sr.charAt(0)!='h' && sr.charAt(0)!='o')
         obj.src = 'h' + sr;
      if (obj.parentNode.parentNode.getElementsByTagName('ul').length>0)
      {
         var sObj = obj.parentNode.parentNode.getElementsByTagName('ul')[0];
         nShw(sObj);
      }
   }
}

function nOvr()
{
   if (nTmr) 
      clearTimeout(nTmr);
}

function nOut()
{
   if (nTmr) 
      clearTimeout(nTmr);
   nTmr = setTimeout("nHd()", 200);
}

function nShw(obj)
{
   if (nObj && nObj != obj)
      nHd();
   nObj = obj;
   if (nObj)
      nObj.style.display = 'block';
}

function nHd()
{
   if (nMain)
   {
      if (nObj)
       nObj.style.display='none';
      var sr = nMain.src.toString().substr(nMain.src.toString().lastIndexOf('/')+1);
      if (sr.charAt(0)=='h')
         nMain.src = sr.substr(1);
   }   
   nMain = false;
   nObj = false;
   nTmr = false;
}

function nShow(obj)
{
   var t = obj.parentNode.getElementsByTagName('span')[0];
   t.style.display=='block';
   
}

function nHide(obj)
{
   obj.style.display=='none';
}

function trim(s)
{
   return s.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function cookieValue(s)
{
    var c = document.cookie.split('; ');
    for (var i=0; i<c.length; i++)
      if (c[i].indexOf(s+'=')==0)
         return c[i].split('=')[1];
    return false;
}

function f_valCom(obj)
{
   obj.comment.value = trim(obj.comment.value);
   obj.name.value    = trim(obj.name.value);
   obj.place.value   = trim(obj.place.value);
   obj.email.value   = trim(obj.email.value);
   if (obj.comment.value.length==0)
   {
      alert("Please enter your comments.");
      obj.comment.focus();
      return false;
   }
   if (obj.name.value.length==0)
   {
      alert("Please enter your name.");
      obj.name.focus();
      return false;
   }
   if (obj.place.value.length==0)
   {
      alert("Please enter a town/city and country.");
      obj.place.focus();
      return false;
   }
   if (obj.email.value.length==0)
   {
      alert("Please enter your email address.");
      obj.email.focus();
      return false;
   }
   v_pattern   =  new RegExp("^[^@]+@[^@]+.[a-zA-Z]{2,}$");
   if (!obj.email.value.match(v_pattern))
   {
      alert("This email address seems not valid. Please re-enter it");
      obj.email.focus();
      return false;
   }
   
   
   
   var c = document.getElementById('cform');
   var p = document.getElementById('cpreview');
   
   if (c.style.display!='none')
   {
      var pc = document.getElementById('cpreview-comment');
      var pn = document.getElementById('cpreview-name');
      pc.innerHTML = formatComment(trim(obj.comment.value));
      if (trim(obj.url.value).length>0)
      {
         if (trim(obj.url.value).indexOf('http://')<0)
            obj.url.value = 'http://'+ trim(obj.url.value);
         pn.innerHTML = '<strong><a href="'+ obj.url.value +'" target="_blank">'+ trim(obj.name.value) +'</a></strong> from '+ trim(obj.place.value);
      }else
         pn.innerHTML = '<strong>'+ trim(obj.name.value) +'</strong> '+ trim(obj.place.value);
      c.style.display = 'none';
      p.style.display = 'block';
      return false;
   }
   return true;
}

function editCom()
{
   var c = document.getElementById('cform');
   var p = document.getElementById('cpreview');
   p.style.display = 'none';
   c.style.display = 'block';
}

function formatComment(s)
{
   s = s.replace(/\n+/g, '<br />');
   return s.replace(/(http|https):\/\/[\S]+(\b|$)/gim, '<a href="$&" class="my_link" target="_blank">$&</a>').replace(/([^\/])(www[\S]+(\b|$))/gim,'$1<a href="http://$2" class="my_link" target="_blank">$2</a>');
}

function mail(v_id)
{
   mailwin = window.open('mailafriend.php?id='+ v_id,'mailwin','width=335,height=450,left=50,top=25');
   mailwin.focus();
}

function rate(v_id)
{
   ratewin = window.open('rate.php?id='+ v_id,'ratewin','width=270,height=300,left=50,top=25');
   ratewin.focus();
}

function f_validate(v_id)
{
   var v_pattern = new RegExp("_", "g");
   for (var i=0;i<v_id.length;i++)
   {
      if (v_id[i].name)
      {
         if (v_id[i].name.indexOf('_')==0)
         {
            if (v_id[i].name=='_mustaccept')
            {
               if (!v_id[i].checked)
               {
                  alert("You must agree to these terms and conditions.");
                  v_id[i].focus();
                  return false;
               }
            } else {
               if (f_trim(v_id[i].value)=="")
               {
                  alert(v_id[i].name.replace(v_pattern," ") + " is a required field.");
                  v_id[i].focus();
                  return false;
               }
            }
         }
      }
   }
   return true;
}

function f_maxlength(v_id, v_length)
{
   if (v_id.value.length>v_length)
      v_id.value = v_id.value.substr(0,v_length);
}

function f_trim(v_src)
{
   var v_pat0 = new RegExp("^[ ]+");
   var v_pat1 = new RegExp("[ ]+$");
   v_src = v_src.replace(v_pat0,"");
   v_src = v_src.replace(v_pat1,"");
   return v_src;
}

function f_updatePal()
{
   var temp = document.pre.country.value.split('|');
   if (parseInt(document.pre.quantity.value)==0 || parseFloat(temp[0])==0 || parseFloat(temp[1])==0)
   {
      alert('Please make a selection');
      return false;         
   }
   if (parseInt(document.pre.quantity.value)==1)
   {
      document.paypal.item_name.value = 'and the love is free: book ('+ parseInt(document.pre.quantity.value) +' copy)';
   } else {
      document.paypal.item_name.value = 'and the love is free: book ('+ parseInt(document.pre.quantity.value) +' copies)';
   }



   document.paypal.amount.value = parseInt(document.pre.quantity.value) * temp[0];
   document.paypal.shipping.value = parseInt(document.pre.quantity.value) * temp[1];

//   document.paypal.amount.value = "179.70";
   return true;
}
