﻿// JScript File

function validateaShippingMethoid()
{
	var LshippingMethod;
	for (LIntCtr=0; LIntCtr < document.forms[0].elements.length; LIntCtr++) 
	{
	
		if ((document.forms[0].elements[LIntCtr].name.indexOf('drpShippingMethod') > -1)) 
		{
			LshippingMethod = document.forms[0].elements[LIntCtr].value;
			
			if(LshippingMethod==0)
			{
			alert("Service not available please select another Method/Company")
			document.forms[0].elements[LIntCtr].focus();
			return false;
			}
			/*
			if(LshippingMethod=='')
			{
			alert("Please Select Shipping Method")
			document.forms[0].elements[LIntCtr].focus();
			return false;
			}
			*/
		}
	}
}

function ShoppingCartPanelClick()
{
	var LIntQty;
	for (LIntCtr=0; LIntCtr < document.forms[0].elements.length; LIntCtr++) 
	{
		if ((document.forms[0].elements[LIntCtr].type == 'text') && (document.forms[0].elements[LIntCtr].name.indexOf('txtProductQuantity') > -1)) 
		{
			LIntQty = document.forms[0].elements[LIntCtr].value;
			document.frmpage.hdnProductQuantity.value = LIntQty;
			if(CheckForNull('frmpage','Quantity','hdnProductQuantity')==true)
			{
				if(CheckIsNumber('frmpage','Quantity','hdnProductQuantity')==false)
				{								
					document.forms[0].elements[LIntCtr].focus();
					return false;
				}

				if(MinVal('frmpage','Quantity','hdnProductQuantity',1)==false)
				{								
					document.forms[0].elements[LIntCtr].focus();
					return false;
				}
				if(MinMaxVal('frmpage','Quantity','hdnProductQuantity',9999)==false)
				{								
					document.forms[0].elements[LIntCtr].focus();
					return false;
				}
				if(MinMax('frmpage','Quantity','hdnProductQuantity','1','4')==false)
				{ 								
					document.forms[0].elements[LIntCtr].focus();
					return false;
				}
			}
			else
			{
				document.forms[0].elements[LIntCtr].focus();
				return false;
			}
		}
	}
}


function CheckValidationGrdRowLvl(ObjPassed)
			{
			    objName=ObjPassed.id
				objName=objName.replace("btnUpdate","TxtqtyInner")
		
				if (document.getElementById(objName).value=="")
				{
					alert("Quantity Must Be Entered.")
					document.getElementById(objName).focus();
					return false;
				}
				
				
				return true
			}
			
function CheckValidations(p_stUniqueIdentifier)
{
    if (CheckboxSelection()==false)return false;
    //if (IsAddressSelected(p_stUniqueIdentifier)==false)return false;
    //if (IsShipCompanySelected(p_stUniqueIdentifier)==false)return false;
    
    return true;
}


/*function IsShipCompanySelected(p_stUniqueIdentifier)
{
    if(document.getElementById(p_stUniqueIdentifier + 'drpShippingCompany').value==0)
    {
        alert("Please select a shipping company");
        return false;
    }
    return true;
}*/

function IsAddressSelected(p_stUniqueIdentifier)
{
    if(document.getElementById(p_stUniqueIdentifier + 'drpShipAddresses').value==0)
    {
        alert("Please select an address");
        return false;
    }
    return true;
}


function CheckboxSelection()
{
	var LIntCtr;
	var LIntSelectedCheckBoxes=0;
	for (LIntCtr=0; LIntCtr < document.forms[0].elements.length; LIntCtr++) 
	{
		if ((document.forms[0].elements[LIntCtr].type == 'checkbox') && (document.forms[0].elements[LIntCtr].name.indexOf('grdView') > -1))
		{
			if(document.forms[0].elements[LIntCtr].checked == true)
			{
				LIntSelectedCheckBoxes = parseInt(LIntSelectedCheckBoxes) + 1;
			}
		}
	}
	
	if(parseInt(LIntSelectedCheckBoxes)==0)
	{
		alert('Product(s) Must Be Selected!');
		return false;
	}
	return true;
}
			
	
			
function ChkSelectAll(CheckBoxControl) 
{		
    if (CheckBoxControl.checked == true) 
    {
	    var i;
	    for (i=0; i < document.forms[0].elements.length; i++) 
	    {
		    if ((document.forms[0].elements[i].type == 'checkbox') && (document.forms[0].elements[i].name.indexOf('grdView') > -1)) 
		    {
			    if (document.forms[0].elements[i].disabled==false)
			    {
				    document.forms[0].elements[i].checked = true;
			    }
		    }
	    }
    }
    else 
    {
	    var i;
	    for (i=0; i < document.forms[0].elements.length; i++) 
	    {
		    if ((document.forms[0].elements[i].type == 'checkbox') && (document.forms[0].elements[i].name.indexOf('grdView') > -1)) 
		    {
			    document.forms[0].elements[i].checked = false;
		    }
	    }
    }
}		
    
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 IsExpired( month, year ) 
{    
	var now = new Date();								// this function is designed to be Y2K compliant.
	var expiresIn = new Date(year,month,0,0,0);			// create an expired on date object with valid thru expiration date
	expiresIn.setMonth(expiresIn.getMonth());			// adjust the month, to first day, hour, minute & second of expired month
	
	if(now.getTime() > expiresIn.getTime()) return true; // then we get the miliseconds, and do a long integer comparison
	
	return false;										
}			
		
function GetBillingShippingAddress(p_stUniqueIdentifier,p_stMode,p_stAjaxURL)
{		



	if (document.getElementById(p_stUniqueIdentifier + 'drp' + p_stMode + 'Addresses').value !=0)
	{	  
		var LlocalShippingAddress = document.getElementById(p_stUniqueIdentifier + 'drp' + p_stMode + 'Addresses').value
		var AddressInfo=LlocalShippingAddress.split("^")
		//alert(LlocalShippingAddress);
		document.getElementById(p_stUniqueIdentifier + 'txt' + p_stMode + 'FirstName').value=AddressInfo[1]
		document.getElementById(p_stUniqueIdentifier + 'txt' + p_stMode + 'MiddleName').value=AddressInfo[2]
		document.getElementById(p_stUniqueIdentifier + 'txt' + p_stMode + 'LastName').value=AddressInfo[3]
		document.getElementById(p_stUniqueIdentifier + 'txt' + p_stMode + 'Email').value=AddressInfo[4]
		document.getElementById(p_stUniqueIdentifier + 'txt' + p_stMode + 'Address1').value=AddressInfo[5]
		document.getElementById(p_stUniqueIdentifier + 'txt' + p_stMode + 'Address2').value=AddressInfo[6]
		document.getElementById(p_stUniqueIdentifier + 'txt' + p_stMode + 'City').value=AddressInfo[7]		
		document.getElementById(p_stUniqueIdentifier + 'txt' + p_stMode + 'Zip').value=AddressInfo[9]
	
		document.getElementById(p_stUniqueIdentifier  + 'txt' + p_stMode + 'Phone').value=AddressInfo[11]
		document.getElementById(p_stUniqueIdentifier  + 'txtCompany').value=AddressInfo[12]
		
		lstCountry  = document.getElementById(p_stUniqueIdentifier  + 'drp' + p_stMode + 'Country')
		lstState  = document.getElementById(p_stUniqueIdentifier  + 'drp' + p_stMode + 'State')
		txtState  = document.getElementById(p_stUniqueIdentifier  + 'txt' + p_stMode + 'State')
		txtCountry  = document.getElementById(p_stUniqueIdentifier + 'txt' + p_stMode + 'Country')
		
		lstCountry.selectedIndex = 0
		lstState.selectedIndex = 0
		txtState.value = ""
		txtCountry.value = ""
		
		
		//select country in country drop doen
		for(i=0; i<lstCountry.options.length; i++)
		{	
			if(lstCountry[i].text == AddressInfo[10])
			{	try
				{   
					lstCountry[i].selected = true;
            		lstCountry.selectedIndex = i;
					break;
				}
				catch(e)
				{	//document.frmPage.lstChild[0].selected = true;						
				}
			}					
		} 	
		
		//if country drop down is not selected then display country text in textbox
		if(i >= lstCountry.options.length)
		{
		    txtCountry.value=AddressInfo[10];
		}			
		//call the ajax script to set state values
		fillDropDown(AddressInfo[8],lstCountry,lstState,txtState,"active_state",p_stAjaxURL); 			
	}else
	{
	
	document.getElementById(p_stUniqueIdentifier + 'txt' + p_stMode + 'FirstName').value='';
		document.getElementById(p_stUniqueIdentifier + 'txt' + p_stMode + 'MiddleName').value='';
		document.getElementById(p_stUniqueIdentifier + 'txt' + p_stMode + 'LastName').value='';
		document.getElementById(p_stUniqueIdentifier + 'txt' + p_stMode + 'Email').value='';
		document.getElementById(p_stUniqueIdentifier + 'txt' + p_stMode + 'Address1').value='';
		document.getElementById(p_stUniqueIdentifier + 'txt' + p_stMode + 'Address2').value='';
		document.getElementById(p_stUniqueIdentifier + 'txt' + p_stMode + 'City').value='';
		document.getElementById(p_stUniqueIdentifier + 'txt' + p_stMode + 'Zip').value='';
	
		document.getElementById(p_stUniqueIdentifier  + 'txt' + p_stMode + 'Phone').value='';
		document.getElementById(p_stUniqueIdentifier  + 'txt' + p_stMode + 'Company').value='';
	
	}			
}


function fnValidateCard(args,cardtype) 
{	
    var length = String(args.value).length;  //perform card specific length and prefix tests
    //alert(args);alert(cardtype);
    var cardnumber	= args.value;		
    //alert(cardnumber);
	switch(cardtype) 
	{
	
		case 'Amex':
			if( length != 15 ) 
			{							
				return false;
			}
    
			var prefix = parseInt(cardnumber.substring(0,2));
			if( prefix != 34 && prefix != 37 ) 
			{				
				return false;
			}
		break;

		case 'Discover':
			if( length != 16 ) 
			{
				return false;
			}
    
			var prefix = parseInt( cardnumber.substring(0,4));

			if( prefix != 6011 ) 
			{
				return false;
			}
		break;
    
		case 'MasterCard':
			if( length != 16 ) 
			{
				return false;
			}
			
			var prefix = parseInt( cardnumber.substring(0,2));
			if( prefix < 51 || prefix > 55) 
			{				
				return false;
			}
        break;

		case 'Visa':
		    
			if( length != 16 && length != 13 ) 
			{
			    //alert("length");alert(length);
				return false;
			}
			
			var prefix = parseInt( cardnumber.substring(0,1));
			if( prefix != 4 ) 
			{
			    //alert('prefix');
				return false;
			}
		    break;
	}
        
	if( !mod10( cardnumber ) ) 
	{// run the check digit algorithm		   
		return false;
	}


	return true; // at this point card has not been proven to be invalid
} 

function fnCheckExpiryDate(args,cardmonth,cardyear) 
{  
  
	if(IsExpired( cardmonth, cardyear ) ) 
	{// check if entered date is already expired.		
		return true;		
	}	
	return false;
}
 function Focus(e)
	{	
	    var browserName=navigator.appName; 
        var unicode;
    					 
	    if (browserName=="Microsoft Internet Explorer") 
		    unicode = window.event.keyCode;     //IE
	    else
	 	    unicode = e.which;     //firefox

	    if (unicode == '13')	
	    {
	    document.getElementById("ctl00_ContentPlaceHolder1_btnNext").click();   
        return false;		
        }		
	}
	function FocusBilling(e)
	{	
	
	    var browserName=navigator.appName; 
        var unicode;
    					 
	    if (browserName=="Microsoft Internet Explorer") 
		    unicode = window.event.keyCode;     //IE
	    else
	 	    unicode = e.which;     //firefox

      
	    if (unicode == '13')	
	    {
	     var id=0;
	      document.getElementById("captchaImg").src="../templates/JpegImage.aspx?id="+id;
	     document.getElementById("ctl00_MainContent_btnCheckout").click();   
        return false;		
        }		
	}
	
