/* ************* COMMON FUNCTION  STARTS **************  */
	var email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
	//var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
	var phone=/(^\d{3}-\d{3}-\d{4}$)/;
	//var priceExp=/(^\d$)/;
	//var phone=/^\d(\d|-){7,20}/;
	var dateExp=/(^\d{2}[//]\d{2}[//]\d{4}$)/
	var numberExp = /^([0123456789.])+$/;
		//var email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
//var ext=/^([a-zA-Z0-9_\.\-])+(.+[jpg]|[Jpg]|[JPG]|[gif]|[Gif]|[GIF]|[bmp|[Bmp]|[BMP]$)/;

//date validation
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_new(dtStr){
	
	var dtCh ="/";
	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 a valid date.")
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date.")
		return false
	}
	return true;
}
/////////////

function ltrim(string){
    string=new String(string);
    var string1=new Array();
    var i,j;
    for(i=0,j=0;i<string.length;i++)
    {
      if(j==0)
      {
        if(string.charAt(i)!=" ")
        {
            string1[j++]=string.charAt(i);
        }
      }
      else
      {
        string1[j++]=string.charAt(i);
      }
        
    }
    string="";
    for(i=0;i<string1.length;i++)
    {
      string+=string1[i];
    } 
    return string;
  }

function rtrim(string)
  {
    string=new String(string);
    var string1=new Array();
    var i,j;
    for(i=string.length;i>=0;i--)
    {
      if(string.charAt(i-1)==" ")
      {
        continue;
      }
      else
      {
        for(j=0;j<i;j++)
        {
          string1[j]=string.charAt(j);
        }
        break;
      }
        
    }
    string="";
    for(i=0;i<string1.length;i++)
    {
      string+=string1[i];
    }
    
    
    return string;
  }
  
  function trim(string)
  {
    string=ltrim(string); // // This function is used to trim the left side of a String
    string=rtrim(string);// This function is used to trim the right side of a String
    return string;
  }
////////////////////////////////////////////////////////////////////////////////

/* function is used to validate credicard number */
function validateCreditCardType(cardType,cardNum)
{//alert(cardType+cardNum);
   
   var card = /^\d{16}$/;
	var cardAmex=/^\d{15}$/;

	if(cardType=="Amex" && cardAmex.test(cardNum)==false)// 3400 0000 0000 009 
	{
		return false;
	}
	else if(cardType!="Amex" && card.test(cardNum)==false)
	{
		return false;
	}
	else if(cardType=="Visa" && cardNum.charAt(0)!=4)////Visa  4111 1111 1111 1111 
	{	
		return false;
	}	
	else if(cardType=="MasterCard" && cardNum.charAt(0)!=5)//MasterCard  5500 0000 0000 0004 
	{         
		return false;	   
	}
	else if(cardType=="AMEX" && cardNum.charAt(0)!=3)//American Express  3400 0000 0000 009 
	{
		return false;		
	}
	else 
	{
		return true;
	 }
}
/* function is used to enlarge text- header.php*/
function enlargeText(url)
{
	//alert(url);
	//document.frmenlarge.hid_imgstatus.value="decrease";
	//if(document.frmenlarge.hid_imgstatus.value)
	document.frmenlarge.action=url;
	document.frmenlarge.submit();
}

/* function is used in leftpanel.php*/
function productsLinkList(url,page,index, type,so)
{	
	var url  = url+"?page="+page+"&index="+index+"&type="+type+"&so="+so;
	//alert(url);
	passvalue(url,"div_results","images");
}
/* function is used in products.php*/
function productlistLinkList(url,page,index, type,so)
{	
	var url  = url+"?page1="+page+"&index1="+index+"&type1="+type+"&so1="+so;
	//alert(url);
	passvalue(url,"div_results1","images");
}
/* function used to fill billing address- order_billinginstruction.php */
function fillAddress(billing_name,billing_address,billing_city,billing_state,billing_zip,billing_phone)
{
	if(document.frm_billing_instructions.chkbill.checked)
	{
		document.frm_billing_instructions.billing_name.value=billing_name;
		document.frm_billing_instructions.billing_address.value=billing_address;
		document.frm_billing_instructions.billing_city.value=billing_city;
		document.frm_billing_instructions.billing_state.value=billing_state;
		document.frm_billing_instructions.billing_zip.value=billing_zip;
		document.frm_billing_instructions.billing_phone.value=billing_phone;		
		
	}
	else
	{
		document.frm_billing_instructions.billing_name.value="";
		document.frm_billing_instructions.billing_address.value="";
		document.frm_billing_instructions.billing_city.value="";
		document.frm_billing_instructions.billing_state.value="";
		document.frm_billing_instructions.billing_zip.value="";
		document.frm_billing_instructions.billing_phone.value="";
				
	}
}
/* function used to fill billing address- order_payment.php */
function fillAddress1(stateid,billing_address,billing_city,billing_state,billing_zip)
{
	if(document.frm_payment_info.chkbill.checked)
	{
		document.frm_payment_info.card_address.value=billing_address;
		document.frm_payment_info.card_city.value=billing_city;
		//document.frm_payment_info.card_state.SelectedIndex=billing_state;
		document.frm_payment_info.card_state.selectedIndex=parseInt(stateid);
		document.frm_payment_info.card_zip.value=billing_zip;		
	}
	else
	{
		document.frm_payment_info.card_address.value="";
		document.frm_payment_info.card_city.value="";
		document.frm_payment_info.card_state.value="0";
		document.frm_payment_info.card_zip.value="";				
	}
}
/* function used to fill shipping  address- order_payment.php */
function fillshipAddress(stateid,billing_address,billing_city,billing_state,billing_zip)
{
	if(document.frm_payment_info.chkship.checked)
	{
		/*document.frm_payment_info.ship_firstname.value=firstname;
		document.frm_payment_info.ship_lastname.value=lastname;	*/
		document.frm_payment_info.ship_address.value=billing_address;
		document.frm_payment_info.ship_city.value=billing_city;		
		document.frm_payment_info.ship_state.selectedIndex=parseInt(stateid);
		document.frm_payment_info.ship_zip.value=billing_zip;		
	}
	else
	{
		/*document.frm_payment_info.ship_firstname.value="";
		document.frm_payment_info.ship_lastname.value="";	*/
		document.frm_payment_info.ship_address.value="";
		document.frm_payment_info.ship_city.value="";
		document.frm_payment_info.ship_state.value="0";
		document.frm_payment_info.ship_zip.value="";				
	}
}
/* function is used to validate payment information -order_paymentinfo.php*/
function validatePayment()
{
	if(trim(document.frm_payment_info.card_firstname.value) == ""){
		alert("Please enter first name on credit card.");
		document.frm_payment_info.card_firstname.focus();
		return false;
	}
	else if(trim(document.frm_payment_info.card_lastname.value) == ""){
		alert("Please enter last name on credit card.");
		document.frm_payment_info.card_lastname.focus();
		return false;
	}
	else if(trim(document.frm_payment_info.card_type.value) == "0"){
		alert("Please select  credit card type.");
		document.frm_payment_info.card_type.focus();
		return false;
	}
	else if(trim(document.frm_payment_info.card_number.value) == ""){
		alert("Please enter credit card number.");
		document.frm_payment_info.card_number.focus();
		return false;
	}	
	else if(validateCreditCardType(document.frm_payment_info.card_type.value,trim(document.frm_payment_info.card_number.value))==false){
		alert("Invalid  credit card number.");
		document.frm_payment_info.card_number.focus();
		return false;
	}
	else if(trim(document.frm_payment_info.card_month.value) == "0"){
		alert("Please select expiration month.");
		document.frm_payment_info.card_month.focus();
		return false;
	}
	else if(trim(document.frm_payment_info.card_month.value) == "0"){
		alert("Please select expiration month.");
		document.frm_payment_info.card_number.focus();
		return false;
	}
	else if(trim(document.frm_payment_info.card_year.value) == "0"){
		alert("Please select expiration year.");
		document.frm_payment_info.card_year.focus();
		return false;
	}
	else if(trim(document.frm_payment_info.card_address.value) == ""){
		alert("Please enter billing address.");
		document.frm_payment_info.card_address.focus();
		return false;
	}
	else if(trim(document.frm_payment_info.card_city.value) == ""){
		alert("Please enter billing city.");
		document.frm_payment_info.card_city.focus();
		return false;
	}
	else if(trim(document.frm_payment_info.card_state.value) == "0"){
		alert("Please enter billing state.");
		document.frm_payment_info.card_state.focus();
		return false;
	} 	
	else if(trim(document.frm_payment_info.card_zip.value) == ""){
		alert("Please enter billing zipcode.");
		document.frm_payment_info.card_zip.focus();
		return false;
	}
	if(trim(document.frm_payment_info.ship_firstname.value) == ""){
		alert("Please enter first name.");
		document.frm_payment_info.ship_firstname.focus();
		return false;
	}
	else if(trim(document.frm_payment_info.ship_lastname.value) == ""){
		alert("Please enter last name .");
		document.frm_payment_info.ship_lastname.focus();
		return false;
	}
	else if(trim(document.frm_payment_info.ship_address.value) == ""){
		alert("Please enter shipping address.");
		document.frm_payment_info.ship_address.focus();
		return false;
	}
	else if(trim(document.frm_payment_info.ship_city.value) == ""){
		alert("Please enter shipping  city.");
		document.frm_payment_info.ship_city.focus();
		return false;
	}
	else if(trim(document.frm_payment_info.ship_state.value) == "0"){
		alert("Please enter shipping state.");
		document.frm_payment_info.ship_state.focus();
		return false;
	} 	
	else if(trim(document.frm_payment_info.ship_zip.value) == ""){
		alert("Please enter shipping zipcode.");
		document.frm_payment_info.ship_zip.focus();
		return false;
	}
	else
		return true;
}	

/* function is used to validate order_approval form*/
function approval()
{
	if(trim(document.frm_approval.accp_name.value) == ""){
		alert("Please enter name.");
		document.frm_approval.accp_name.focus();
		return false;
	}
  else if(document.frm_approval.accp_date.value==""){
		alert("Please enter a date.");
		document.frm_approval.accp_date.focus();
		return false;
	}
	 else if(document.frm_approval.accp_number.value==""){
		alert("Please enter a phone number.");
		document.frm_approval.accp_number.focus();
		return false;
	}
   else	if(trim(document.frm_approval.accp_email.value) == ""){
		alert("Please enter an email id.");
		document.frm_approval.accp_email.focus();
		return false;
	}
	else if(!email.test(document.frm_approval.accp_email.value))
	{
	  	alert("please enter a valid email id.");
	  	document.frm_approval.accp_email.focus();
	  	 return false;
	}
    else if(document.frm_approval.accp_referred.value==""){
		alert("Please enter a reference name.");
		document.frm_approval.accp_referred.focus();
		return false;
	}
    else
    {
		 return true;
	 }
}

function customer_info()
{
	if(trim(document.frm_customer_info.cust_name.value) == ""){
		alert("Please enter name.");
		document.frm_customer_info.cust_name.focus();
		return false;
	}
  else if(document.frm_customer_info.cust_address.value==""){
		alert("Please enter an address.");
		document.frm_customer_info.cust_address.focus();
		return false;
	}
	 else if(document.frm_customer_info.cust_city.value==""){
		alert("Please enter a city.");
		document.frm_customer_info.cust_city.focus();
		return false;
	}
	else if(document.frm_customer_info.cust_state.value==0){
		alert("Please select a state.");
		document.frm_customer_info.cust_state.focus();
		return false;
	}
	 else if(document.frm_customer_info.cust_zip.value==""){
		alert("Please enter a zip code.");
		document.frm_customer_info.cust_zip.focus();
		return false;
	}
	/*else if(isInteger(document.frm_customer_info.cust_zip.value)){
		alert("Please enter a valid zip code.");
		document.frm_customer_info.cust_zip.focus();
		return false;
	}*/
	 else if(document.frm_customer_info.cust_hide_key_location.value==""){
		alert("Please enter a location.");
		document.frm_customer_info.cust_hide_key_location.focus();
		return false;
	}
    else
    {
		 return true;
	 }
}
function validateContact()
{
	if(trim(document.frm_contactus.name.value)=="")
	{
		alert("Please enter name.");
		document.frm_contactus.name.focus();
		return false;
	}
     else if(document.frm_contactus.state.value==0){
		alert("Please select a state.");
		document.frm_contactus.state.focus();
		return false;
	}

	else if(trim(document.frm_contactus.email.value)=="")
	{
		alert("Please enter email address.");
		document.frm_contactus.email.focus();
		return false;
	}	
	else if(!email.test(document.frm_contactus.email.value))
	{
	  	alert("Invalid email address.");
	  	document.frm_contactus.email.focus();
	  	 return false;
	}
	else
		return true;
	
}
