function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else {begin += 2;}
  var end = document.cookie.indexOf(";", begin);
  if (end == -1) {end = dc.length;}
  return unescape(dc.substring(begin + prefix.length, end));
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function gotoURL(form)
{
  for (i=0; i<form.catURL.length; i++) 
    if (form.catURL.options[i].selected)
      newURL=form.catURL.options[i].value;
  window.location.href=newURL;
}

function checkBillingForm(form) 
{
  //Temporary Disable
  //return true;
  var blnCheck=false;
  var i = 0;
  if (form.payment_method_uuid.checked) blnCheck=true;
  for (i=0; i<form.payment_method_uuid.length; i++)
    if (form.payment_method_uuid[i].checked) blnCheck=true; 
  if (!blnCheck){
    alert("Please choose Payment Mode !!");
    return false;
  }  
  if (form.bl_first_name.value == ""){
    alert("Billing Name cannot be blank !!");
    form.bl_first_name.focus();
    return false;
  }
  if (form.bl_address1.value == ""){
    alert("Billing Address cannot be blank !!");
    form.bl_address1.focus();
    return false;
  }
  if (form.bl_city.value == ""){
    alert("Billing City cannot be blank !!");
    form.bl_city.focus();
    return false;
  }
  if (form.bl_state.value == ""){
    alert("Billing State cannot be blank !!");
    form.bl_state.focus();
    return false;
  }
  if (form.bl_country.value == ""){
    alert("Billing Country cannot be blank !!");
    form.bl_country.focus();
    return false;
  }
  if (form.bl_email.value == ""){
    alert("Billing Email cannot be blank !!");
    form.bl_email.focus();
    return false;
  }
  txt=form.bl_email.value;
  if (txt.indexOf("@")<3){
      alert("I'm sorry. This email address seems wrong. Please"+" check the prefix and '@' sign.");
      form.bl_email.focus();
      return false;
  }

  if (form.sameaddress.checked) {
    form.sp_first_name.value  = form.bl_first_name.value;
    form.sp_address1.value    = form.bl_address1.value;
    form.sp_city.value        = form.bl_city.value;
    form.sp_state.value       = form.bl_state.value;
    form.sp_postcode.value    = form.bl_postcode.value;
    form.sp_contact_no.value  = form.bl_contact_no.value;
    form.sp_fax_no.value      = form.bl_fax_no.value;
    form.sp_email.value       = form.bl_email.value;
    //for (i=0; i<form.bl_country.length; i++) {
    //  if (form.bl_country.options[i].selected) {
    //    form.sp_country.options[i].selected = true; 
    //  };
    //};
    
    var x=document.getElementById("sp_country");
    var y=document.getElementById("bl_country"); 
    var inCountry=false;
    if (x.options[x.selectedIndex].text.match("--Select--")) 
    { 
    	for (i=1; i<form.sp_country.length; i++) {
    		if (form.sp_country.options[i].value.match(y.options[y.selectedIndex].text)) {
    			form.sp_country.options[i].selected = true;
    			inCountry = true;
    		};
    	};
    	
    	if (! inCountry) {
    	alert ("Cannot deliver good(s) to the selected Country!");
    	return false;
    	}
    	
    } else {
    	if (! x.options[x.selectedIndex].text.match(y.options[y.selectedIndex].text)) {
    	alert ("Different Country! Untick the 'Same As Above'!! ");
    	return false;	
    	}
    }	
    
  } else {
    if (form.sp_first_name.value == ""){
      alert("Shipping Name cannot be blank !!");
      form.sp_first_name.focus();
      return false;
    }
    if (form.sp_address1.value == ""){
      alert("Shipping Address cannot be blank !!");
      form.sp_address1.focus();
      return false;
    }
    if (form.sp_city.value == ""){
      alert("Shipping City cannot be blank !!");
      form.sp_city.focus();
      return false;
    }
    if (form.sp_state.value == ""){
      alert("Shipping State cannot be blank !!");
      form.sp_state.focus();
      return false;
    }
    if (form.sp_country.value == ""){
      alert("Shipping Country cannot be blank !!");
      form.sp_country.focus();
      return false;
    }
    if (form.sp_email.value == ""){
      alert("Shipping Email cannot be blank !!");
      form.sp_email.focus();
      return false;
    }
    txt=form.sp_email.value;
    if (txt.indexOf("@")<3){
      alert("I'm sorry. This email address seems wrong. Please"+" check the prefix and '@' sign.");
      form.sp_email.focus();
      return false;
    }
  }
    
    if (form.bl_address1.value.length > 255) 
    {
        diff=form.bl_address1.value.length - 255;
        if (diff>1)
            diff = diff + " characters";
        else
            diff = diff + " character";
            
        alert("The Billing Address is limited to " + 255 + " characters\n" + "Please reduce the text by " + diff);
        form.bl_address1.focus();
        return (false);
    }

    
    if (form.sp_address1.value.length > 255) 
    {
        diff=form.sp_address1.value.length - 255;
        if (diff>1)
            diff = diff + " characters";
        else
            diff = diff + " character";
            
        alert("The Shipping Address is limited to " + 255 + " characters\n" + "Please reduce the text by " + diff);
        form.sp_address1.focus();
        return (false);
    }  
  return true;
}

function checkShippingForm(form) 
{
  //Temporary Disable
  //return true;
  var blnCheck=false;
  var i = 0;
  if (form.shipping_method_uuid.checked) blnCheck=true;
  for (i=0; i<form.shipping_method_uuid.length; i++)
    if (form.shipping_method_uuid[i].checked) blnCheck=true; 
  if (!blnCheck){
    alert("Please choose Shipping Mode !!");
    return false;
  }
  blnCheck=false;
  if (form.payment_method_uuid.checked) blnCheck=true;
  for (i=0; i<form.payment_method_uuid.length; i++)
    if (form.payment_method_uuid[i].checked) blnCheck=true; 
  if (!blnCheck){
    alert("Please choose Payment Mode !!");
    return false;
  }  
  if (form.bl_first_name.value == ""){
    alert("Billing Name cannot be blank !!");
    form.bl_first_name.focus();
    return false;
  }
  if (form.bl_address1.value == ""){
    alert("Billing Address cannot be blank !!");
    form.bl_address1.focus();
    return false;
  }
  if (form.bl_city.value == ""){
    alert("Billing City cannot be blank !!");
    form.bl_city.focus();
    return false;
  }
  if (form.bl_state.value == ""){
    alert("Billing State cannot be blank !!");
    form.bl_state.focus();
    return false;
  }
  if (form.bl_country.value == ""){
    alert("Billing Country cannot be blank !!");
    form.bl_country.focus();
    return false;
  }
  if (form.bl_email.value == ""){
    alert("Billing Email cannot be blank !!");
    form.bl_email.focus();
    return false;
  }
  txt=form.bl_email.value;
  if (txt.indexOf("@")<3){
      alert("I'm sorry. This email address seems wrong. Please"+" check the prefix and '@' sign.");
      form.bl_email.focus();
      return false;
  }

  if (form.sameaddress.checked) {
    form.sp_first_name.value  = form.bl_first_name.value;
    form.sp_address1.value    = form.bl_address1.value;
    form.sp_city.value        = form.bl_city.value;
    form.sp_state.value       = form.bl_state.value;
    form.sp_postcode.value    = form.bl_postcode.value;
    form.sp_contact_no.value  = form.bl_contact_no.value;
    form.sp_fax_no.value      = form.bl_fax_no.value;
    form.sp_email.value       = form.bl_email.value;
    //for (i=0; i<form.bl_country.length; i++) {
    //  if (form.bl_country.options[i].selected) {
    //    form.sp_country.options[i].selected = true; 
    //  };
    //};
    
    var x=document.getElementById("sp_country");
    var y=document.getElementById("bl_country"); 
    var inCountry=false;
    if (x.options[x.selectedIndex].text.match("--Select--")) 
    { 
    	for (i=1; i<form.sp_country.length; i++) {
    		if (form.sp_country.options[i].value.match(y.options[y.selectedIndex].text)) {
    			form.sp_country.options[i].selected = true;
    			inCountry = true;
    		};
    	};
    	
    	if (! inCountry) {
    	alert ("Cannot deliver good(s) to the selected Country!");
    	return false;
    	}
    	
    } else {
    	if (! x.options[x.selectedIndex].text.match(y.options[y.selectedIndex].text)) {
    	alert ("Different Country! Untick the 'Same As Above'!! ");
    	return false;	
    	}
    }	
    
  } else {
    if (form.sp_first_name.value == ""){
      alert("Shipping Name cannot be blank !!");
      form.sp_first_name.focus();
      return false;
    }
    if (form.sp_address1.value == ""){
      alert("Shipping Address cannot be blank !!");
      form.sp_address1.focus();
      return false;
    }
    if (form.sp_city.value == ""){
      alert("Shipping City cannot be blank !!");
      form.sp_city.focus();
      return false;
    }
    if (form.sp_state.value == ""){
      alert("Shipping State cannot be blank !!");
      form.sp_state.focus();
      return false;
    }
    if (form.sp_country.value == ""){
      alert("Shipping Country cannot be blank !!");
      form.sp_country.focus();
      return false;
    }
    if (form.sp_email.value == ""){
      alert("Shipping Email cannot be blank !!");
      form.sp_email.focus();
      return false;
    }
    txt=form.sp_email.value;
    if (txt.indexOf("@")<3){
      alert("I'm sorry. This email address seems wrong. Please"+" check the prefix and '@' sign.");
      form.sp_email.focus();
      return false;
    }
  }
    
    if (form.bl_address1.value.length > 255) 
    {
        diff=form.bl_address1.value.length - 255;
        if (diff>1)
            diff = diff + " characters";
        else
            diff = diff + " character";
            
        alert("The Billing Address is limited to " + 255 + " characters\n" + "Please reduce the text by " + diff);
        form.bl_address1.focus();
        return (false);
    }

    
    if (form.sp_address1.value.length > 255) 
    {
        diff=form.sp_address1.value.length - 255;
        if (diff>1)
            diff = diff + " characters";
        else
            diff = diff + " character";
            
        alert("The Shipping Address is limited to " + 255 + " characters\n" + "Please reduce the text by " + diff);
        form.sp_address1.focus();
        return (false);
    }  
  return true;
}

function checkCreditCardForm(form) {
  var valid = "0123456789";
  var ok = 1; var checktemp;

  data=form.card_no.value;
  if (data == ""){
    alert("Credit Card Number cannot be blank !!");
    form.card_no.focus()
    return false;
  }
  if (data.length <= 13){
    alert("Invalid Credit Card Number !!");
    form.card_no.focus()
    return false;
  }
  for (var i=0; i<data.length; i++) {
    checktemp = "" + data.substring(i, i+1);
    if (valid.indexOf(checktemp) == "-1") {
      alert("Credit Card Number must be all in Digit !!");
      form.card_no.focus()
      return false; 
    }
  }
  data=form.card_cvc.value;
  if (data == ""){
    alert("Card Identification No cannot be blank !!");
    form.card_cvc.focus()
    return false;
  }
  if (data.length < 3){
    alert("Invalid Card Identification No !!");
    form.card_cvc.focus()
    return false;
  }
  for (var i=0; i<data.length; i++) {
    checktemp = "" + data.substring(i, i+1);
    if (valid.indexOf(checktemp) == "-1") {
      alert("Card Identification No must be all in Digit !!");
      form.card_cvc.focus()
      return false; 
    }
  }
  return true;
}


function checkMyAccount(form) 
{
  if (isBlank(form.user_name,'User Name')) return false; 
  if (isBlank(form.user_password,'User Password')) return false; 
  if (isBlank(form.email,'Email Address')) return false; 
  if (!isValidEmail(form.email)) return false; 
  return true;
}

function checkLogin(form)
{
  if (isBlank(form.user_name,'User Name')) return false; 
  if (isBlank(form.user_password,'User Password')) return false; 
  return true;
}

function checkEmail(form)
{
  if (isBlank(form.email,'Email Adress')) return false; 
  if (!isValidEmail(form.email)) return false; 
  return true;
}

function radioResetter(obj, defVal) {   
	var i=""
	var setDefault=true;
  if (typeof obj == 'undefined')
  {
    return;
  }
	obj.checked = true;
	if (obj.length >= 0 && defVal == "true")
	{
    for (i in obj) 
    {
	    if (obj[i].checked) setDefault=false;
	  }
    if (setDefault) obj[0].checked=true;
  }
}

function isValidNumeric(InStr, StrMsg) 
{
 parseFloat(InStr);
 if (isNaN(InStr))
 {
  alert("Not a valid number!" +"(" + StrMsg + ")" );
  return false;
 }
 return true;
}
function isValidInteger(InStr, StrMsg)  {
     var elmstr = InStr; 
    if (elmstr == "") 
    {
       alert("Not a valid integer!" +"(" + StrMsg + ")" );
       return false;
    }   
    
    for (var i = 0; i < elmstr.length; i++) 
    {
        if (elmstr.charAt(i) < "0" ||
            elmstr.charAt(i) > "9") {
          alert("Not a valid integer!" +"(" + StrMsg + ")" );
          return false;
        }
    }   
return true;
}
function isValidEmail(obj)  {
  txt=obj.value;
  if (txt.indexOf("@")<3){
      alert("I'm sorry. This email address seems wrong. Please check the prefix and '@' sign.");
      obj.focus();
      return false;
  }
  return true;
}
function isBlank(obj, StrMsg)  {
  txt=obj.value;
  if (txt==""){
      alert(StrMsg+" cannot be blank!");
      obj.focus();
      return true;
  }
  return false;
}
function isSelectBlank(obj, StrMsg)  {
	var blnSelected=false;
  if (typeof obj == 'undefined')
    return;
	if (obj.length > 0)
	{
    for (i = 0; i < obj.length; i++)
    {
	    if (obj[i].selected)
	    {
        if (obj[i].value=="")
        {
          alert(StrMsg+" must select one!");
          obj.focus();
          return true;
        }
      	return false;
      }
	  }
  }
  return false;
}
function isCheckBlank(obj, StrMsg) {   
	var checked = false;
	if (typeof obj == 'undefined') 
		return false;
  if ( obj.checked ) checked = true;
  for (i = 0; i < obj.length; i++)
    if ( obj[i].checked )
       checked = true;
  if (checked == false)
  {
  	alert(StrMsg+" must check one!");
  	return true;
  }
  return false;
}
function isRadioBlank(obj, StrMsg) {   
	var checked = false;
	if (typeof obj == 'undefined') 
		return false;
  if ( obj.checked ) checked = true;
  for (i = 0; i < obj.length; i++)
    if ( obj[i].checked )
       checked = true;
  if (checked == false)
  {
  	alert(StrMsg+" must check one!");
  	return true;
  }
  return false;
}
