﻿
function popup(mylink, windowname)
{
	if (! window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
   		href=mylink;
	else
   		href=mylink.href;
	window.open(href, windowname, 'width=400,height=200,scrollbars=yes');
	return false;
}

function alphanumeric(alphane)
{
	var numaric = alphane;
	for(var j=0; j<numaric.length; j++)
		{
		  var alphaa = numaric.charAt(j);
		  var hh = alphaa.charCodeAt(0);
		  if((hh > 47 && hh<58) || (hh > 64 && hh<91) || (hh > 96 && hh<123))
		  {
		  }
		else	{
			 return false;
		  }
		}
 return true;
}

function checklogin()
{
  if(document.mainOrder.txtuseremail.value=="")
	{
	     alert("Please Enter Your Email Address.");
		 document.mainOrder.txtuseremail.focus();
		 return false;
	}
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(mainOrder.txtuseremail.value)==false)
       {
        alert("Invalid E-mail Address! Please re-enter.")
            document.mainOrder.txtuseremail.focus();
			return false;
       }
	if(document.mainOrder.txtpassword.value=="")
	{
	     alert("Please Enter Your Password.");
		 document.mainOrder.txtpassword.focus();
		 return false;
	}
	if(!alphanumeric(document.mainOrder.txtpassword.value))
	{
	     alert("Please Enter Your Password.");
		 document.mainOrder.txtpassword.focus();
		 return false;
	}
	
	return true;
}

function PageLoad()
{
		if(document.getElementById("RadioGroup2").checked==true)
		{
			 document.getElementById("cmbddlMonth").style.display = "";  
		}else if(document.getElementById("RadioGroup1").checked==true)
		{
			 document.getElementById("cmbddlMonth").style.display = "none";
		}		
} 

function ShowHideState(obj,mode)
{
        if(mode=='bill')
        {
          if(document.getElementById(obj.id).value=="US")
           {
                document.getElementById('txtBillState').style.display='none'
                document.getElementById('cmbbillstate').style.display=''
           }
           else
           {
            document.getElementById('txtBillState').style.display=''
             document.getElementById('cmbbillstate').style.display='none'
           }
       }
       else
       {
           if(document.getElementById(obj.id).value=="US")
           {
                document.getElementById('txtShipState').style.display='none'
                document.getElementById('cmbshipstate').style.display=''
           }
           else
           {
            document.getElementById('txtShipState').style.display=''
             document.getElementById('cmbshipstate').style.display='none'
           }
       }
}
function checkvalidation()
{
    /* var	cardMonth=document.mainOrder.cmbddlMonth.value;
    if(cardMonth==0)
	{
	     alert("Desired day of shipping must be selected!.");
		 document.mainOrder.cmbddlMonth.focus();
		  return false;
	}*/
	if(document.mainOrder.txtquantity.value=="")
	{
	     alert("Please Enter Quanity.");
		 document.mainOrder.txtquantity.focus();
		 return false;
	} 
	if(document.mainOrder.txtshipname.value=="")
	{
	     alert("Please Enter Customer Ship Name.");
		 document.mainOrder.txtshipname.focus();
		 return false;
	} 
	if(document.mainOrder.txtshipaddress.value=="")
	{
	     alert("Please Enter Your Address");
		 document.mainOrder.txtshipaddress.focus();
		 return false;
	}
	if(document.mainOrder.txtshipcity.value=="")
	{
	     alert("Please Enter Your City Name.");
		 document.mainOrder.txtshipcity.focus();
		 return false;
	}
	
	if(document.mainOrder.txtshipzip.value=="")
	{
	     alert("Please Enter Your Zip Code.");
		 document.mainOrder.txtshipzip.focus();
		 return false;
	}
	
	var	shipstate=document.mainOrder.cmbshipstate.value;
    if(shipstate==0)
	{
	     alert("Shipping state must be selected!.");
		 document.mainOrder.cmbshipstate.focus();
		  return false;
	}
	if(document.mainOrder.txtshipphone.value=="")
	{
	     alert("Please Enter Your Phone Number.");
		 document.mainOrder.txtshipphone.focus();
		 return false;
	}
	if(document.mainOrder.txtshipmail.value=="")
	{
	     alert("Please Enter Your Email Address.");
		 document.mainOrder.txtshipmail.focus();
		 return false;
	}
	if (echeck(document.mainOrder.txtshipmail.value)==false){
		
		 document.mainOrder.txtshipmail.focus();
			return (false)
	}
	if(document.mainOrder.txtbillcardname.value=="")
	{
	     alert("Please Enter Your  Card Name.");
		 document.mainOrder.txtbillcardname.focus();
		 return false;
	}
	if(document.mainOrder.txtbillcardnumber.value=="")
	{
	     alert("Please Enter Your  Card Number.");
		 document.mainOrder.txtbillcardnumber.focus();
		 return false;
	}
	
	if(document.mainOrder.txtbillverinumber.value=="")
	{
	     alert("Please Enter Card Verification Number.");
		 document.mainOrder.txtbillverinumber.focus();
		 return false;
	} 
	if(document.mainOrder.txtbilladdress.value=="")
	{
	     alert("Please Enter Your Billing Address.");
		 document.mainOrder.txtbilladdress.focus();
		 return false;
	} 
	if(document.mainOrder.txtbillcity.value=="")
	{
	     alert("Please Enter Your Billing City Name.");
		 document.mainOrder.txtbillcity.focus();
		 return false;
	} 
	if(document.mainOrder.txtbillzip.value=="")
	{
	     alert("Please Enter Your Billing Zip Code");
		 document.mainOrder.txtbillzip.focus();
		 return false;
	}
	
	 var	billing=document.mainOrder.cmbbillstate.value;
   	if(billing==0)
	{
	     alert("billing state must be selected!.");
		 document.mainOrder.cmbbillstate.focus();
		 return false;
	}
	var	cardMonth=document.mainOrder.cmbbillexpddlMonth.value;
	var	cardYear=document.mainOrder.cmbbillexpddlYear.value;
	if(cardMonth==0)
	{
	     alert("Expiry Date Month must be selected!.");
		 document.mainOrder.cmbbillexpddlMonth.focus();
		  return false;
	}
	if(cardYear==0)
	{
	     alert("Expiry Date Year must be selected!.");
		 document.mainOrder.cmbbillexpddlYear.focus();
		  return false;
	}
	cardNumber=document.mainOrder.txtbillcardnumber.value;
            cardType=document.mainOrder.cmbbillcardtype.value;
            cardMonth=cardMonth;
            cardYear=cardYear;            	
	if (validate(cardNumber,cardType,cardMonth,cardYear)==false) 
	{
           return false;
	}	
	if(document.mainOrder.chkagreeCheck.checked==false)
	{
	     alert("Please check the checkbox if you are agree with our terms and conditions.");
		 document.mainOrder.chkagreeCheck.focus();
		 return false;
	}
	return true;
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true;					
}

function validate(cardNumber,cardType,cardMonth,cardYear) 
{   
        var length = cardNumber.length;			//perform card specific length and prefix tests

		var status=true;
		switch( cardType )
		{
			case 'American Express':
				if(length != 15 ) 
				{
				    status = false;
                }
                else
                {
                    var prefix = parseInt( cardNumber.substring(0,2));
                    if( prefix != 34 && prefix != 37 )
                    {
					  status = false;
                    }
                }
                break;
                
            
            case 'Discover':
				if( length != 16 ) 
				{
                     status = false;
                }
                else
                {
                    var prefix = parseInt( cardNumber.substring(0,4));
                    if( prefix != 6011 )
                    {
					   status = false;
                    }
                 }
                break;
                
            case 'MasterCard':
				if( length != 16 ) 
				{
					status = false;
                }
                else
                {
                    var prefix = parseInt( cardNumber.substring(0,2));
                    if( prefix < 51 || prefix > 55)
                    {
					    status = false;
                    }
                }
                break;
            
           case 'VISA':
				 if( length != 16 && length != 13 )
				 {
					status = false;
                 }
                else
                {
                    var prefix = parseInt(cardNumber.substring(0,1));
                    if( prefix != 4 ) 
                    {
					  status = false;
                     }
                }
                break;
         }
            
         if(status==false)
         {
            alert("Sorry! The Card Number you have entered is Invalid.");
         }  
         else
         {  
           if( !mod10( cardNumber ) ) 
            { // run the check digit algorithm
             alert("Sorry! The Card Number you have entered is Invalid.");
             status = false;
            }
        }
        if (expired(cardMonth,cardYear)==false) 
            status = false;
   	    
   	    return status;
    }

	
	function mod10(cardNumber) { // LUHN Formula for validation of credit card numbers.
		return true; //?????
		var ar = new Array( cardNumber.length );
		var i = 0,sum = 0;
        for( i = 0; i < cardNumber.length; ++i ) {
    		ar[i] = parseInt(cardNumber.charAt(i));
    	}
    	for( i = ar.length -2; i >= 0; i-=2 ) { // you have to start from the right, and work back.
			ar[i] *= 2;							 // every second digit starting with the right most (check digit)
    		if( ar[i] > 9 ) ar[i]-=9;			 // will be doubled, and summed with the skipped digits.
    	}										 // if the double digit is > 9, ADD those individual digits together 
        for( i = 0; i < ar.length; ++i ) {
			sum += ar[i];						 // if the sum is divisible by 10 mod10 succeeds
        }
        return (((sum%10)==0)?true:false);	 	
	}
        
	function expired(month, year) 
 	{
  		var currentTime = new Date();
  		var curr_month='0' + currentTime.getMonth();
  		var curr_year=currentTime.getFullYear(); 
  		if (year < curr_year)
  		{
   			alert("Sorry! The Expiration Year You Have Entered Would Make This Card Invalid.");
   			return false;
  		}
  		else if((month <= curr_month + 1) && (year == curr_year))
  		{
    		alert("Sorry! The Expiration Month You Have Entered Would Make This Card Invalid.");
    		return false;
  		}
 	}
	function CheckClearanceValue()
	{
	    document.getElementById("cmbddlMonth").style.display = "";  
	}
	function CheckClearanceValue1()
	{
		    document.getElementById("cmbddlMonth").style.display = "none";
    }