function GetXmlHttpObject_ms()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if(window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr)
{
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)

	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy.")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month.")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day.")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter valid 4 digit year between "+minYear+" and "+maxYear+".")
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date.")
		return false
	}
	return true
}
///For TRIM White Spaces////////////////////////
  function trim(str) 
       { 
	    return str.replace(/^\s*|\s*$/g,"");
      }
	  
///////////Email
function isAValidEmail( emailField )
{ 
   // var emailregex=/^[\w]+\+?\w*@[\w]+\.[\w.]+\w$/; 
   var emailregex=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    var match=emailField.match( emailregex ); 
    if ( !match ) 
    { 
       return false; 
    }
    else
    {
       return true; 
    }
}

function isPhoneNumber(s) 
{

     // Check for correct phone number
	 var validPhoneDigits = '0123456789()-+';
	 var phoneLength = s.length;
	 
	for(var i=0; i < phoneLength; i++)
	 {
	   if(validPhoneDigits.indexOf(s.substr(i,1)) == '-1')
		{
		  var returnvar = false;
		  break;
		}
		else
		{
		  var returnvar = true;
		} 
		
	 }
	 return returnvar;
}
	  
 
function isValidproductprice(strTmp)
	{
		var valid = ".,0123456789 ";
		for (var i=0; i < strTmp.length; i++) 
		{
			temp = strTmp.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") 
			{
				alert("Please enter valid product price.");
				return false;
			}	
    	}
		return true;
	}
 



function login_validation(){
    
    var fa= document.theform;
    
	if(trim(fa.username.value)==""){
        alert("Please enter username.");
        fa.username.focus();
        return false;
    }
    
    if(trim(fa.password.value)==""){
        alert("Please enter password.");
        fa.password.focus();
        return false;
        
    }
	   
    return true;
    
}

function Forget_validation()
{
		var fa= document.theform;
    
	if(trim(fa.username.value)==""){
        alert("Please enter username.");
        fa.username.focus();
        return false;
    }	
	  return true;
}

function ForgotpassValidation()
{
	var fa= document.theform;
    if(trim(fa.username.value)=="")
	  {
        alert("Please enter username.");
        fa.username.focus();
        return false;
      }
	
    if(trim(fa.email.value)=="")
	  {
        alert("Please enter email address.");
        fa.email.focus();
        return false;
      }
	
	if(!isAValidEmail(fa.email.value))
      {
	    alert("Please enter valid email address.");
        fa.email.focus();
        return false;
	  }	
 }
	
///Change Password Validation

  function valchgpassfrm() 
   {
		var fa= document.theform;
		
		if(trim(fa.opassword.value)==""){
			alert("Please enter old password.");
			fa.opassword.focus();
			return false;
		}
		
		if(trim(fa.npassword.value)==""){
			alert("Please enter new password.");
			fa.npassword.focus();
			return false;
		}
	var pass_len = document.theform.npassword.value.length;
	if((pass_len < 5) || (pass_len > 15))
	     {
			alert("Password length should be between 5 to 15 characters.");
			document.theform.npassword.focus();
			return false;
		 }	
		
		
		if(trim(fa.cpassword.value)==""){
			alert("Please enter confirm password.");
			fa.cpassword.focus();
			return false;
			
		}
	var cpass_len = document.theform.cpassword.value.length;
	if((cpass_len < 5) || (cpass_len > 15))
	     {
			alert("Password length should be between 5 to 15 characters.");
			document.theform.cpassword.focus();
			return false;
		 }
		if(trim(fa.cpassword.value)!=trim(fa.npassword.value)) {
			 alert("New and confirm password do not match.");
			 fa.npassword.focus();
			 return false;	 
		}
		return true;
  }




	  
function showcounter()
	{
	   if(document.theform.description.value.length > 2042)
		{
			document.theform.description.value=document.theform.description.value.substr(0,2042);
		}
		else
		{
		    document.getElementById('showcharacter').innerHTML="Number of characters : "+document.theform.description.value.length;
		}
	}	

	

function RegisterValidation()
{
   if(trim(document.theform.first_name.value)=="")
		 {
			alert("Please enter first name.");
			document.theform.first_name.focus();
			return false;
		 }
   if(trim(document.theform.last_name.value)=="")
		 {
			alert("Please enter last name.");
			document.theform.last_name.focus();
			return false;
		 }		 
   if(trim(document.theform.company_name.value)=="")
		 {
			alert("Please enter company name.");
			document.theform.company_name.focus();
			return false;
		 }	
   if(trim(document.theform.address1.value)=="")
		{
			alert("Please enter address1.");
			document.theform.address1.focus();
			return false;
		}
     if(trim(document.theform.city.value)=="")
		 {
			alert("Please enter city/town.");
			document.theform.city.focus();
			return false;
		 }	
	 if(trim(document.theform.state.value)=="")
		 {
			alert("Please enter state.");
			document.theform.state.focus();
			return false;
		 }	
	if(trim(document.theform.zipcode.value)=="")
		 {
			alert("Please enter zip code.");
			document.theform.zipcode.focus();
			return false;
		 }
	if(isNaN(document.theform.zipcode.value))
	   {
	     alert("Please enter valid zip code.");
		 document.theform.zipcode.focus();
		 return false;	   
	   }	 	
	if(trim(document.theform.country.value)=="")
		 {
			alert("Please select country.");
			document.theform.country.focus();
			return false;
		 }	
	if(trim(document.theform.phone.value)=="")
		 {
			alert("Please enter phone.");
			document.theform.phone.focus();
			return false;
		 }		 	 	 	 	
	
	if(!isPhoneNumber(document.theform.phone.value))
		 {
			alert("Please enter valid phone.");
			document.theform.phone.focus();
			return false;
		 }	
	 
		 
	if(trim(document.theform.username.value)=="")
		 {
			alert("Please enter username.");
			document.theform.username.focus();
			return false;
		 }
		 
	var user_len = document.theform.username.value.length;
	if((user_len < 5) || (user_len > 15))
	     {
			alert("Username length should be between 5 to 15 characters.");
			document.theform.username.focus();
			return false;
		 }		  
	    	 
	if(trim(document.theform.password.value)=="")
		 {
			alert("Please enter password.");
			document.theform.password.focus();
			return false;
		 }
		 
	var pass_len = document.theform.password.value.length;
	if((pass_len < 5) || (pass_len > 15))
	     {
			alert("Password length should be between 5 to 15 characters.");
			document.theform.password.focus();
			return false;
		 }		 
	if(trim(document.theform.conf_password.value)=="")
		 {
			alert("Please enter confirm password.");
			document.theform.conf_password.focus();
			return false;
		 }
		 
	var cpass_len = document.theform.conf_password.value.length;
	if((cpass_len < 5) || (cpass_len > 15))
	     {
			alert("Confirm password length should be between 5 to 15 characters.");
			document.theform.conf_password.focus();
			return false;
		 }	 
	if(trim(document.theform.conf_password.value) != trim(document.theform.password.value))
	   {
	     	alert("Password and confirm password do not match.");
			document.theform.password.focus();
			return false;	   
	   }	 
		 
		 	 		 
	if(trim(document.theform.email.value)=="")
		 {
			alert("Please enter email address.");
			document.theform.email.focus();
			return false;
		 }
		 
	var emal = document.theform.email.value;	 
	if(!isAValidEmail( emal ))
	    {
		  alert("Please enter valid email address.");
		  document.theform.email.focus();
		  return false;				
		
		}	
	     return true;	
 }


function EditUserValidate()
{
	
   if(trim(document.theform.first_name.value)=="")
		 {
			alert("Please enter first name.");
			document.theform.first_name.focus();
			return false;
		 }
   if(trim(document.theform.last_name.value)=="")
		 {
			alert("Please enter last name.");
			document.theform.last_name.focus();
			return false;
		 }		 
   if(trim(document.theform.company_name.value)=="")
		 {
			alert("Please enter company name.");
			document.theform.company_name.focus();
			return false;
		 }	
   if(trim(document.theform.address1.value)=="")
		{
			alert("Please enter address1.");
			document.theform.address1.focus();
			return false;
		}
			
		   
     if(trim(document.theform.city.value)=="")
		 {
			alert("Please enter city/town.");
			document.theform.city.focus();
			return false;
		 }	
	 if(trim(document.theform.state.value)=="")
		 {
			alert("Please enter state.");
			document.theform.state.focus();
			return false;
		 }	
	if(trim(document.theform.zipcode.value)=="")
		 {
			alert("Please enter zip code.");
			document.theform.zipcode.focus();
			return false;
		 }
	if(isNaN(document.theform.zipcode.value))
	   {
	     alert("Please enter valid zip code.");
		 document.theform.zipcode.focus();
		 return false;	   
	   }	 	
	if(trim(document.theform.country.value)=="")
		 {
			alert("Please select country.");
			document.theform.country.focus();
			return false;
		 }	
	if(trim(document.theform.phone.value)=="")
		 {
			alert("Please enter phone.");
			document.theform.phone.focus();
			return false;
		 }		 	 	 	 	
	
	if(!isPhoneNumber(document.theform.phone.value))
		 {
			alert("Please enter valid phone.");
			document.theform.phone.focus();
			return false;
		 }	
		 	 		 
	if(trim(document.theform.email.value)=="")
		 {
			alert("Please enter email address.");
			document.theform.email.focus();
			return false;
		 }
		 
	var emal = document.theform.email.value;	 
	if(!isAValidEmail( emal ))
	    {
		  alert("Please enter valid client email.");
		  document.theform.email.focus();
		  return false;				
		
		}	
	     return true;	
 
}
function BillingValidation()
{
   if(trim(document.theform.first_name.value)=="")
		 {
			alert("Please enter first name.");
			document.theform.first_name.focus();
			return false;
		 }
   if(trim(document.theform.last_name.value)=="")
		 {
			alert("Please enter last name.");
			document.theform.last_name.focus();
			return false;
		 }		 
   /*if(trim(document.theform.company_name.value)=="")
		 {
			alert("Please enter company name.");
			document.theform.company_name.focus();
			return false;
		 }*/
   if(trim(document.theform.address1.value)=="")
		{
			alert("Please enter address1.");
			document.theform.address1.focus();
			return false;
		}
     if(trim(document.theform.city.value)=="")
		 {
			alert("Please enter city/town.");
			document.theform.city.focus();
			return false;
		 }	
	 if(trim(document.theform.state.value)=="")
		 {
			alert("Please enter state.");
			document.theform.state.focus();
			return false;
		 }	
	if(trim(document.theform.zipcode.value)=="")
		 {
			alert("Please enter zip code.");
			document.theform.zipcode.focus();
			return false;
		 }
	if(isNaN(document.theform.zipcode.value))
	   {
	     alert("Please enter valid zip code.");
		 document.theform.zipcode.focus();
		 return false;	   
	   }	 	
	if(trim(document.theform.country.value)=="")
		 {
			alert("Please select country.");
			document.theform.country.focus();
			return false;
		 }	
	if(trim(document.theform.phone.value)=="")
		 {
			alert("Please enter phone.");
			document.theform.phone.focus();
			return false;
		 }		 	 	 	 	
	
	if(!isPhoneNumber(document.theform.phone.value))
		 {
			alert("Please enter valid phone.");
			document.theform.phone.focus();
			return false;
		 }	
	if(trim(document.theform.email.value)=="")
		 {
			alert("Please enter email address.");
			document.theform.email.focus();
			return false;
		 }
		 
	var emal = document.theform.email.value;	 
	if(!isAValidEmail( emal ))
	    {
		  alert("Please enter valid client email.");
		  document.theform.email.focus();
		  return false;				
		
		}	
		
		if(trim(document.theform.sfirst_name.value)=="")
		 {
			alert("Please enter first name.");
			document.theform.sfirst_name.focus();
			return false;
		 }
   if(trim(document.theform.slast_name.value)=="")
		 {
			alert("Please enter last name.");
			document.theform.slast_name.focus();
			return false;
		 }		 
   /*if(trim(document.theform.scompany_name.value)=="")
		 {
			alert("Please enter company name.");
			document.theform.scompany_name.focus();
			return false;
		 }*/
   if(trim(document.theform.saddress1.value)=="")
		{
			alert("Please enter address1.");
			document.theform.saddress1.focus();
			return false;
		}
     if(trim(document.theform.scity.value)=="")
		 {
			alert("Please enter city/town.");
			document.theform.scity.focus();
			return false;
		 }	
	 if(trim(document.theform.sstate.value)=="")
		 {
			alert("Please enter state.");
			document.theform.sstate.focus();
			return false;
		 }	
	if(trim(document.theform.szipcode.value)=="")
		 {
			alert("Please enter zip code.");
			document.theform.szipcode.focus();
			return false;
		 }
	if(isNaN(document.theform.szipcode.value))
	   {
	     alert("Please enter valid zip code.");
		 document.theform.szipcode.focus();
		 return false;	   
	   }	 	
	if(trim(document.theform.scountry.value)=="")
		 {
			alert("Please select country.");
			document.theform.scountry.focus();
			return false;
		 }	
	if(trim(document.theform.sphone.value)=="")
		 {
			alert("Please enter phone.");
			document.theform.sphone.focus();
			return false;
		 }		 	 	 	 	
	
	if(!isPhoneNumber(document.theform.sphone.value))
		 {
			alert("Please enter valid phone.");
			document.theform.sphone.focus();
			return false;
		 }	
	if(trim(document.theform.semail.value)=="")
		 {
			alert("Please enter email address.");
			document.theform.semail.focus();
			return false;
		 }
		 
	var emal = document.theform.semail.value;	 
	if(!isAValidEmail( emal ))
	    {
		  alert("Please enter valid client email.");
		  document.theform.semail.focus();
		  return false;				
		
		}	
	/*if(trim(document.theform.instructions.value)=="")
		 {
			alert("Please enter special instructions.");
			document.theform.instructions.focus();
			return false;
		 }*/
	     return true;	
}
 
 
function ChageUrl(val,id,pagenm)
  {
     var valu    = val;
	 var orderby = id;
	 var page    = pagenm;
	 location.href=page+"?pg="+valu+"&order="+orderby;
  }	
  
function CursorField()
 {
   document.theform.username.focus();
 
 }
 

function hideshow(id)
{
  document.getElementById(id).value ="";
} 

 

function updateModelImage()
{
	if(xmlHttp.readyState == 4)
	{
		var retString = xmlHttp.responseText;
		document.getElementById("trip_charge_value").value=retString;
	}
}
function ShowTripcharge(val)
{
       	xmlHttp = GetXmlHttpObject_ms();
		var url = "show_fields.php";
		xmlHttp.open("POST", url, true);
		xmlHttp.onreadystatechange = updateModelImage
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xmlHttp.send("val="+escape(val));
}


function cc_check(input_value){
    var iChars = "0123456789";
    var ster12=input_value.length;
    for (var i = 0; i < ster12;  i++) {
      	if (iChars.indexOf(input_value.charAt(i)) == -1) {
     return false;
  	}
  }
  
}


function showPopupnew(w,h)
{   
 
var popUp    = document.getElementById("popupcontent1");    

popUp.style.display="";
popUp.style.top = "471px";   
popUp.style.left = "500px";   
popUp.style.width = w + "px";   
popUp.style.height = h + "px";    
popUp.style.visibility = "visible";
}

function hidePopup()
{   
    var popUp = document.getElementById("popupcontent1");   
	popUp.style.display = "none";
}

function ContactValid()
{
 if(trim(document.theform.name.value) == "")
    {
	   alert("Please enter contact name.");
	   document.theform.name.focus();
	   return false;
	}
	
	if(document.theform.phone.value!="")
    {
	   if(!isPhoneNumber(document.theform.phone.value))
		 {
			alert("Please enter valid phone.");
			document.theform.phone.focus();
			return false;
		 }	
	}	
	
  if(trim(document.theform.email.value) == "")
    {
	   alert("Please enter email address.");
	   document.theform.email.focus();
	   return false;
	}	
  if(!isAValidEmail(document.theform.email.value))	
    {
	   alert("Please enter valid email address.");
	   document.theform.email.focus();
	   return false;
	}
	
  if(trim(document.theform.private_key.value) == "")	
    {
	   alert("Please enter verification code.");
	   document.theform.private_key.focus();
	   return false;
	}	
	
   return true;
} 

function Referer(page_referer)
{
    location.href=page_referer;
}

 
function ShowDivImage()
{
 document.getElementById("other_images_id").style.display = "";
}

function SendToCart(id)
{
   location.href = "process.php?prid="+id+"&command=cart";
}

function SendPage(pagenm)
{
	location.href = pagenm;	
}

function ConfirmDelete()
{
	if(confirm("Are you sure you want to delete  this product from your cart?"))
	   {
	     return true;	   
	   }
	else
	   {
	     return false;	   
	   }  	
}


function ContinuePage()
{
	location.href = "store.php";
}

function CartValidate()
{
	var counter = document.theform.counter.value;
		for(k=1;k<=counter;k++)
			{
			   if(trim(document.getElementById("qty_id_"+k).value) == 0 || trim(document.getElementById("qty_id_"+k).value) < 1)
			   	{
					alert("Please enter value.");
					document.getElementById("qty_id_"+k).focus();
					return false;
				}
			   if(isNaN(document.getElementById("qty_id_"+k).value))
			   	{
					alert("Please enter valid quantity.");
					document.getElementById("qty_id_"+k).focus();
					return false;
				}	
								
			}
          return true;   

}
function OpenAskseller()
{
   if(document.getElementById("ask_seller").style.display == "block")
		document.getElementById("ask_seller").style.display = "none";
	else document.getElementById("ask_seller").style.display = "block"
	
}

function Askseller_Validate()
{
	if(trim(document.theform.question.value) == "")
		{
			alert("Please enter question.");
			document.theform.question.focus();
			return false;			
		}
}
function SelectValues()
{
	if(document.theform.confirm.checked == true)
	{
		document.getElementById("ship_select").value = document.getElementById("select").value;
		document.getElementById("ship_first_name").value = document.getElementById("bill_first_name").value;
		document.getElementById("ship_last_name").value = document.getElementById("bill_last_name").value;
		document.getElementById("ship_company").value = document.getElementById("bill_company_name").value;
		document.getElementById("ship_address1").value = document.getElementById("bill_address1").value;
		document.getElementById("ship_address2").value = document.getElementById("bill_address2").value;
		document.getElementById("ship_city").value = document.getElementById("bill_city").value;
		document.getElementById("ship_state").value = document.getElementById("bill_state").value;
		document.getElementById("ship_zipcode").value = document.getElementById("bill_zipcode").value;
		document.getElementById("ship_country").value = document.getElementById("bill_country").value;
		document.getElementById("ship_phone").value = document.getElementById("bill_phone").value;
		document.getElementById("ship_email").value = document.getElementById("bill_email").value;
	}
	else
	{
		document.getElementById("ship_select").value 		= "";
		document.getElementById("ship_first_name").value 	= "";
		document.getElementById("ship_last_name").value 	= "";
		document.getElementById("ship_company").value 		= "";
		document.getElementById("ship_address1").value 		= "";
		document.getElementById("ship_address2").value 		= "";
		document.getElementById("ship_city").value 			= "";
		document.getElementById("ship_state").value 		= "";
		document.getElementById("ship_zipcode").value 		= "";
		document.getElementById("ship_country").value 		= "";
		document.getElementById("ship_phone").value 		= "";
		document.getElementById("ship_email").value 		= "";
	}

}

function ChangeAddress(val)
{
    location.href = val;	
}
function ChangeUrlBill(val)
{
	location.href = val;
}
function SendDescriptionPage(val,pr_id)
{
	location.href = val+'?prid='+pr_id;
}