// JavaScript Document
/* null checking */
var cancel = 0;

function check_fieldsUser(form_name,f_name,d_name,c_array)
{

	var incre=0;
	for (incre=0; incre<c_array; incre++)
	{
		var frm="document."+form_name+"."+f_name[incre]+".value";
		var foc="document."+form_name+"."+f_name[incre];
		
		if(f_name[incre]=="terms_cond")
		{
			frm="document."+form_name+"."+f_name[incre]+".checked";
			frm=eval(frm);
			//alert(frm+" "+f_name[incre]);
		    if(!frm)
			{
				alert("Whoops, you must agree to terms and conditions to proceed");
				var foc_field=eval(foc);
			  	foc_field.focus();
				return false;
			}
		}
		else if(trimSpacesUser(eval(frm))=="")
		{	
		
			 if(f_name[incre]!="phone" && f_name[incre]!="fax")
			 {
				  alert(d_name[incre]+" is a Required Field !!");
				  var foc_field=eval(foc);
				  foc_field.focus();
				  return false;
			 }
			 
		}
		else if(f_name[incre]=="user_name" || f_name[incre]=="password")
		 {
				if(trimSpacesUser(eval(frm)).length<6)
				{
					 alert(d_name[incre]+" should be minimum 6 characters !!");
					 var foc_field=eval(foc);
					 foc_field.focus();
					 return false;
				}
		 }
		else if(f_name[incre]=="con_password")
		 {
				var frm_p="document."+form_name+"."+f_name[incre-1]+".value";
				if(trimSpacesUser(eval(frm))!=trimSpacesUser(eval(frm_p)))
				{
					 alert(d_name[incre]+" should match with password !!");
					 var foc_field=eval(foc);
					 foc_field.focus();
					 return false;
				}
		 }
		else if(f_name[incre]=="zip")
		{
			if(check_zip(trimSpacesUser(eval(frm)))==false)
			{
				alert(d_name[incre]+" should be valid !!");
				var foc_field=eval(foc);
			  	foc_field.focus();
			  	return false;
			}
		}
		else if(f_name[incre]=="email")
		{
			if(checkEmail(trimSpacesUser(eval(frm)))==false)
			{
				//alert(d_name[incre]+" should be valid !!");
				var foc_field=eval(foc);
			  	foc_field.focus();
				//foc_field.select();
			  	return false;
			}
		}
		else if(f_name[incre]=="phone" && trimSpacesUser(eval(frm))!="")
		{
			if(check_phone(trimSpacesUser(eval(frm)))==false)
			{
				alert(d_name[incre]+" should be valid !!");
				var foc_field=eval(foc);
				foc_field.focus();
				return false;
			}
		}
		else if(f_name[incre]=="fax" && trimSpacesUser(eval(frm))!="")
		{
			if(check_phone(trimSpacesUser(eval(frm)))==false)
			{
				alert(d_name[incre]+" should be valid !!");
				var foc_field=eval(foc);
				foc_field.focus();
				return false;
			}
		}
	}
	
	return true;
}


function trimSpacesUser(stringValue) 
{
	   // Checks the first occurance of spaces and removes them
	   for(i = 0; i < stringValue.length; i++) 
	   {
		    if(stringValue.charAt(i) != " ") 
		     {			break;		}
	   }
	
	   if(i > 0) 
	   {		stringValue = stringValue.substring(i);	}
	
	   // Checks the last occurance of spaces and removes them
	   strLength = stringValue.length - 1;
	   for(i = strLength; i >= 0; i--) 
	   {
		   if(stringValue.charAt(i) != " ") 
		   {		break;		}
	   }
	
	   if(i < strLength) 
	   {		stringValue = stringValue.substring(0, i + 1);	}
	
	   // Returns the string after removing leading and trailing spaces.
	return stringValue;
}


function checkEmail(emailString)
{
	splitVal = emailString.split('@');
	
	if(splitVal.length <= 1) 
	{
		alert("Please enter a valid Email Address !!");
		return false;
	}
	if(splitVal[0].length <= 0 || splitVal[1].length <= 0) 
	{
		alert("Please enter a valid Email Address !!");
		return false;
	}
	
	splitDomain = splitVal[1].split('.');
	if(splitDomain.length <= 1) 
	{
		alert("Please enter a valid Email Address !!");
		return false;
	}
	
	if(splitDomain[0].length <= 0 || splitDomain[1].length <= 1) 
	{
		alert("Please enter a valid Email Address !!");
		return false;
	}
	return true;
}

function checkMail(email)
{
        var str1=email;
        var arr=str1.split('@');
        var eFlag=true;
        if(arr.length != 2)
        {
                eFlag = false;
        }
        else if(arr[0].length <= 0 || arr[0].indexOf(' ') != -1 || arr[0].indexOf("'") != -1 || arr[0].indexOf('"') != -1 || arr[1].indexOf('.') == -1)
        {
                        eFlag = false;
        }
        else
        {
                var dot=arr[1].split('.');
                if(dot.length < 2)
                {
                        eFlag = false;
                }
                else
                {
                        if(dot[0].length <= 0 || dot[0].indexOf(' ') != -1 || dot[0].indexOf('"') != -1 || dot[0].indexOf("'") != -1)
                        {
                                eFlag = false;
                        }

                        for(i=1;i < dot.length;i++)
                        {
                                if(dot[i].length <= 0 || dot[i].indexOf(' ') != -1 || dot[i].indexOf('"') != -1 || dot[i].indexOf("'") != -1 || dot[i].length > 4)
                                {
                                        eFlag = false;
                                }
                        }
                }
        }
                return eFlag;
}


// Function To Check Whether The Value Entered Is A Non Negative Float 

function check_numeric(sText)
{   
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}


// Function To Check Whether The Value Entered Is A Non Negative Integer 

function check_int(sText)
{   
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

// Function To Check Whether The Value Entered Is A Phone Number

function check_phone(sText)
{   
   var ValidChars = "0123456789-()";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function check_zip(sText)
{   
   var ValidChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-() *";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}


function trimSpaces(stringValue) 
{
	   // Checks the first occurance of spaces and removes them
	   for(i = 0; i < stringValue.length; i++) 
	   {
		    if(stringValue.charAt(i) != " ") 
		     {			break;		}
	   }
	
	   if(i > 0) 
	   {		stringValue = stringValue.substring(i);	}
	
	   // Checks the last occurance of spaces and removes them
	   strLength = stringValue.length - 1;
	   for(i = strLength; i >= 0; i--) 
	   {
		   if(stringValue.charAt(i) != " ") 
		   {		break;		}
	   }
	
	   if(i < strLength) 
	   {		stringValue = stringValue.substring(0, i + 1);	}
	
	   // Returns the string after removing leading and trailing spaces.
	return stringValue;
}

function checkChangePassword()
{
	if(cancel==1)
	{
		cancel=0;
		return true;
	}
	
	var oldpassword = trimSpaces(document.getElementById("oldpassword").value);
	var password = trimSpaces(document.getElementById("password").value);
	var confirmpassword = trimSpaces(document.getElementById("confirmpassword").value);
	
	 if(oldpassword==""){
        alert("Please enter your old password");
		document.getElementById("oldpassword").focus();
		document.getElementById("oldpassword").select();
        return false;
    }
	else if(oldpassword.length<6)
	{
		alert("Password should be minimum 6 characters");
		document.getElementById("oldpassword").focus();
		document.getElementById("oldpassword").select();
        return false;
	}
	else if(password==""){
      	alert("Please enter your new password");
		document.getElementById("password").focus();
		document.getElementById("password").select();
        return false;
    }
	else if(password.length<6)
	{
		alert("Password should be minimum 6 characters");
		document.getElementById("password").focus();
		document.getElementById("password").select();
        return false;
	}
	else if(confirmpassword==""){
       	alert("Please enter your confirm password");
		document.getElementById("confirmpassword").focus();
		document.getElementById("confirmpassword").select();
        return false;
    }else if(confirmpassword!=password){
      	alert("New password and confirm password should be same");
		document.getElementById("confirmpassword").focus();
		document.getElementById("confirmpassword").select();
        return false;
    }
	return true;
}

function EmailCheck()
{
var shopper_email = document.shop_pass.email;
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;

if(shopper_email.value == "") 
{
	alert("Please enter the Email Address");
	shopper_email.focus()
	return false;
}

if(!shopper_email.value.match(emailExp))
{
	alert("Please enter the valid Email Address");
	shopper_email.focus()
	return false;
}

return true;	
}



function sameAsAbove(){
	if(document.shopper_billing)
		var frm = document.shopper_billing;
	else
		var frm = document.shopper_account;
	if(frm.sameshipping.checked==true){
		document.getElementById("shipping_address").style.display = "none";
	}else{
		document.getElementById("shipping_address").style.display = "";
	}
}

function checkBillingForm()
{
	if(cancel==1)
	{
		cancel=0;
		return true;
	}
	

	
	var billing_first_name = document.getElementById("billing_first_name");
	var billing_last_name = document.getElementById("billing_last_name");
	var billing_address1 = document.getElementById("billing_address1");
	var billing_address2 = document.getElementById("billing_address2");
	var billing_city = document.getElementById("billing_city");
	var bill_country = document.getElementById("billCountrySelect1");
	var bill_state = document.getElementById("billStateSelect1");
	var billing_zip = document.getElementById("billing_zip");
	var billing_email = document.getElementById("billing_email");
	var billing_phone = document.getElementById("billing_phone");
	
	var sameshipping = document.getElementById("sameshipping");
	
	var shipping_first_name = document.getElementById("shipping_first_name");
	var shipping_last_name = document.getElementById("shipping_last_name");
	var shipping_address1 = document.getElementById("shipping_address1");
	var shipping_address2 = document.getElementById("shipping_address2");
	var shipping_city = document.getElementById("shipping_city");
	var shipping_country = document.getElementById("shipCountrySelect1");
	var shipping_state = document.getElementById("shipStateSelect1");
	var shipping_zip = document.getElementById("shipping_zip");
	var shipping_email = document.getElementById("shipping_email");
	var shipping_phone = document.getElementById("shipping_phone");
	
	
	
	if(trimSpaces(billing_first_name.value)=="")
	{
		alert("Please enter billing first name!!!");
		billing_first_name.focus();
		return false;
	}
	if(trimSpaces(billing_address1.value)=="")
	{
		alert("Please enter billing address1!!!");
		billing_address1.focus();
		return false;
	}
	if(trimSpaces(billing_city.value)=="")
	{
		alert("Please enter billing city!!!");
		billing_city.focus();
		return false;
	}
	if(trimSpaces(bill_country.value)=="")
	{
		alert("Please select billing country!!!");
		bill_country.focus();
		return false;
	}
	if(trimSpaces(bill_state.value)=="")
	{
		alert("Please select billing state!!!");
		bill_state.focus();
		return false;
	}
	if(trimSpaces(billing_zip.value)=="")
	{
		alert("Please enter billing zip!!!");
		billing_zip.focus();
		return false;
	}
	else if(!check_zip(trimSpacesUser(billing_zip.value)))
	{
		alert("Please enter valid billing zip!!!");
		billing_zip.focus();
		return false;
	}

	
	if(trimSpaces(billing_email.value)=="")
	{
		alert("Please enter billing email!!!");
		billing_email.focus();
		return false;
	}
	else if(!checkEmail(trimSpacesUser(billing_email.value)))
	{
		//alert("Please enter valid billing email!!!");
		billing_email.focus();
		return false;
	}
	
	if(trimSpaces(billing_phone.value)!=""&& !check_phone(trimSpaces(billing_phone.value)))
	{
		alert("Please enter valid billing phone!!!");
		billing_phone.focus();
		return false;
	}
	
	if(sameshipping.checked == false)
	{
		if(trimSpaces(shipping_first_name.value)=="")
		{
			alert("Please enter shipping first name!!!");
			shipping_first_name.focus();
			return false;
		}
		if(trimSpaces(shipping_address1.value)=="")
		{
			alert("Please enter shipping address1!!!");
			shipping_address1.focus();
			return false;
		}
		if(trimSpaces(shipping_city.value)=="")
		{
			alert("Please enter shipping city!!!");
			shipping_city.focus();
			return false;
		}
		if(trimSpaces(shipping_country.value)=="")
		{
			alert("Please select shipping country!!!");
			shipping_country.focus();
			return false;
		}
		if(trimSpaces(shipping_state.value)=="")
		{
			alert("Please select/enter shipping state!!!");
			shipping_state.focus();
			return false;
		}
		if(trimSpaces(shipping_zip.value)=="")
		{
			alert("Please enter shipping zip!!!");
			shipping_zip.focus();
			return false;
		}
		else if(!check_zip(trimSpacesUser(shipping_zip.value)))
		{
			alert("Please enter valid shipping zip!!!");
			shipping_zip.focus();
			return false;
		}
			
		if(trimSpaces(shipping_email.value)=="")
		{
			alert("Please enter shipping email!!!");
			shipping_email.focus();
			return false;
		}
		else if(!checkEmail(trimSpacesUser(shipping_email.value)))
		{
			//alert("Please enter valid shipping email!!!");
			shipping_email.focus();
			return false;
		}
		
		if(trimSpaces(shipping_phone.value)!=""&& !check_phone(trimSpaces(shipping_phone.value)))
		{
			alert("Please enter valid shipping phone!!!");
			shipping_phone.focus();
			return false;
		}
			
	}
	
	return true;
}

function checkSubBillingForm(isDigital)
{
	
	isDigital = 'N'; //New code added
	var billing_first_name = document.getElementById("billing_first_name");
	var billing_last_name = document.getElementById("billing_last_name");
	var billing_address1 = document.getElementById("billing_address1");
	var billing_address2 = document.getElementById("billing_address2");
	var billing_city = document.getElementById("billing_city");
	var bill_country = document.getElementById("billCountrySelect");
	var bill_state = document.getElementById("billStateSelect");
	var billing_zip = document.getElementById("billing_zip");
	//var billing_fax = document.getElementById("billing_fax");
	var billing_email = document.getElementById("billing_email");
	var billing_phone = document.getElementById("billing_phone");
	
	if(isDigital!='Y')
	{
		var sameshipping = document.getElementById("sameshipping");
		
		var shipping_first_name = document.getElementById("shipping_first_name");
		var shipping_last_name = document.getElementById("shipping_last_name");
		var shipping_address1 = document.getElementById("shipping_address1");
		var shipping_address2 = document.getElementById("shipping_address2");
		var shipping_city = document.getElementById("shipping_city");
		var shipping_country = document.getElementById("shipCountrySelect");
		var shipping_state = document.getElementById("shipStateSelect");
		var shipping_zip = document.getElementById("shipping_zip");
		//var shipping_fax = document.getElementById("shipping_fax");
		var shipping_email = document.getElementById("shipping_email");
		var shipping_phone = document.getElementById("shipping_phone");
	}
	
	
	if(trimSpaces(billing_first_name.value)=="")
	{
		alert("Please enter billing first name!!!");
		billing_first_name.focus();
		return false;
	}
	if(trimSpaces(billing_address1.value)=="")
	{
		alert("Please enter billing address1!!!");
		billing_address1.focus();
		return false;
	}
	if(trimSpaces(billing_city.value)=="")
	{
		alert("Please enter billing city!!!");
		billing_city.focus();
		return false;
	}
	if(trimSpaces(bill_country.value)=="")
	{
		alert("Please select billing country!!!");
		bill_country.focus();
		return false;
	}
	if(trimSpaces(bill_state.value)=="")
	{
		alert("Please select billing state!!!");
		bill_state.focus();
		return false;
	}
	if(trimSpaces(billing_zip.value)=="")
	{
		alert("Please enter billing zip!!!");
		billing_zip.focus();
		return false;
	}
	else if(!check_zip(trimSpacesUser(billing_zip.value)))
	{
		alert("Please enter valid billing zip!!!");
		billing_zip.focus();
		return false;
	}
	
	
		
	if(trimSpaces(billing_email.value)=="")
	{
		alert("Please enter billing email!!!");
		billing_email.focus();
		return false;
	}
	else if(!checkEmail(trimSpacesUser(billing_email.value)))
	{
		//alert("Please enter valid billing email!!!");
		billing_email.focus();
		return false;
	}
	
	if(trimSpaces(billing_phone.value)!=""&& !check_phone(trimSpaces(billing_phone.value)))
	{
		alert("Please enter valid billing phone!!!");
		billing_phone.focus();
		return false;
	}
	
	
	if(isDigital!='Y')
	{
		if(sameshipping.checked == false)
		{
			if(trimSpaces(shipping_first_name.value)=="")
			{
				alert("Please enter shipping first name!!!");
				shipping_first_name.focus();
				return false;
			}
			if(trimSpaces(shipping_address1.value)=="")
			{
				alert("Please enter shipping address1!!!");
				shipping_address1.focus();
				return false;
			}
			if(trimSpaces(shipping_city.value)=="")
			{
				alert("Please enter shipping city!!!");
				shipping_city.focus();
				return false;
			}
			if(trimSpaces(shipping_country.value)=="")
			{
				alert("Please select shipping country!!!");
				shipping_country.focus();
				return false;
			}
			if(trimSpaces(shipping_state.value)=="")
			{
				alert("Please select/enter shipping state!!!");
				shipping_state.focus();
				return false;
			}
			if(trimSpaces(shipping_zip.value)=="")
			{
				alert("Please enter shipping zip!!!");
				shipping_zip.focus();
				return false;
			}
			else if(!check_zip(trimSpacesUser(shipping_zip.value)))
			{
				alert("Please enter valid shipping zip!!!");
				shipping_zip.focus();
				return false;
			}
			
			if(trimSpaces(shipping_email.value)=="")
			{
				alert("Please enter shipping email!!!");
				shipping_email.focus();
				return false;
			}
			else if(!checkEmail(trimSpacesUser(shipping_email.value)))
			{
				//alert("Please enter valid shipping email!!!");
				shipping_email.focus();
				return false;
			}
			
			if(trimSpaces(shipping_phone.value)!=""&& !check_phone(trimSpaces(shipping_phone.value)))
			{
				alert("Please enter valid shipping phone!!!");
				shipping_phone.focus();
				return false;
			}
		}
	}
	return true;
}


function checkUserRegisterForm()
{
  var form_name = new Array('user_name','email','password','con_password','first_name','last_name','address1','city','billCountrySelect','billStateSelect','zip','phone','terms_cond');		
  var display_name = new Array('User Name','Email Address','Password','Confirm Password','First Name','Last Name','Address Line 1','City','Country','State','ZIP','Phone','Terms and Condition');
  var count = 13;	

  if(check_fieldsUser('shop_reg',form_name,display_name,count))	
  {
	 showListUserRegistration(document.shop_reg,document.shop_reg.user_name,document.shop_reg.email);	
     return false;
  }
  else
  {
  
  		return false;
  
  }
   
}


function checkUserEditForm()
{
  if(cancel==1)
  {
	cancel=0;
	return true;
  }
  var form_name = new Array('email','first_name','last_name','address1','city','billCountrySelect','billStateSelect','zip','phone');		
  var display_name = new Array('Email','First Name','Last Name','Address Line 1','City','Country','State','ZIP','Phone');
  var count = 9;	
  if(check_fieldsUser('shopper_account',form_name,display_name,count))	
  {
	 if(checkBillingForm())
	 {
		showListUserRegistration(document.shopper_account,"",document.shopper_account.email,document.shopper_account.userid.value);	
		return false;
	 }
	 else
		 return false;
  }
  else
  	 return false;
}


function ShopperLogin()
{
	var shopper_name = document.shop_login.user_name;
	var shopper_password = document.shop_login.user_password;
	if(trimSpaces(shopper_name.value) == "")
	{
		alert("Please enter the Username");
		shopper_name.focus();
		return false;
	}
	if(trimSpaces(shopper_password.value) == "")
	{
		alert("Please enter the Password");
		shopper_password.focus();
		return false;
	}
	return true;
}

function checkBuy()
{
	var numericExpression = /^[0-9]+$/;	
	var v_quantity = trimSpacesUser(document.buy.quantity.value);
	
	if(v_quantity == "")
	{
		alert("Quantity is a Required Field");
		document.buy.quantity.focus();
		return false;
	}
	
	if(!v_quantity.match(numericExpression) || v_quantity<=0)
	{
		alert("Quantity should be valid");
		document.buy.quantity.focus();
		return false;
	}
	
}

function cardValidation()
{
	
	var card_type = document.getElementById("card_type");
	var card_number = document.getElementById("card_number");
	var cv_number = document.getElementById("cv_number");
	var exp_month = document.getElementById("exp_month");
	var exp_year = document.getElementById("exp_year");
	
	if(trimSpaces(card_type.value)=="")
	{
		alert("Please select card type!!!");
		card_type.focus();
		return false;
	}
	if(trimSpaces(card_number.value)=="")
	{
		alert("Please enter card number!!!");
		card_number.focus();
		return false;
	}
	else if(!check_int(trimSpaces(card_number.value)))
	{
		alert("Please enter valid card number!!!");
		card_number.focus();
		return false;
	}
	if(trimSpaces(exp_month.value)=="")
	{
		alert("Please select card expiry month!!!");
		exp_month.focus();
		return false;
	}
	if(trimSpaces(exp_year.value)=="")
	{
		alert("Please select card expiry year!!!");
		exp_year.focus();
		return false;
	}
	if(trimSpaces(cv_number.value)=="")
	{
		alert("Please enter verification no!!!");
		cv_number.focus();
		return false;
	}
	else if(!check_int(trimSpaces(cv_number.value)))
	{
		alert("Please enter valid verification no!!!");
		cv_number.focus();
		return false;
	}
}


function openTerms()
{
	window.open("html/shopper_terms.html","terms","width=600,height=400,scrollbars=yes,resizeable=yes");
}


function updateCSS()
{
	
	var browser = BrowserDetect.browser;
	var browser_version = BrowserDetect.version;
	var os = BrowserDetect.OS;
	if((browser.toLowerCase()).indexOf("firefox")>=0 && (os.toLowerCase()).indexOf("mac")>=0)
		document.write('<link type="text/css" rel="stylesheet" href="style/mac.css" />');
}


function validate_contactus()
{
	var contact_name = trimSpaces(document.getElementById("contact_name").value);
	var contact_subject = trimSpaces(document.getElementById("contact_subject").value);
	var contact_email = trimSpaces(document.getElementById("contact_email").value);
	var contact_messages = trimSpaces(document.getElementById("contact_messages").value);

	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;

	if(contact_name == "")
	{
		alert("Please enter contact name");
		document.getElementById("contact_name").focus();
		return;
	}
	else if(contact_subject == "")
	{
		alert("Please enter contact subject");
		document.getElementById("contact_subject").focus();
		return;
	}
	else if(contact_email == "")
	{
		alert("Please enter contact email");
		document.getElementById("contact_email").focus();
		return;
	}
	else if(contact_email == "")
	{
		alert("Please enter contact email address");
		document.getElementById("contact_email").focus();
		return;
	}
	else if(!contact_email.match(emailExp))
	{
		alert("Please enter the valid contact email address");
		document.getElementById("contact_email").focus();
		return;
	}
	else if(contact_messages == "")
	{
		alert("Please enter contact messages");
		document.getElementById("contact_messages").focus();
		return;
	}
	document.getElementById("contact_us").submit();
	return true;
}