function Check4Required(theForm,theList)
{
var theCount=theForm.elements.length
var theResult=false
var theName=""
var i=0
	// While we're not on the last element, move through one field at a time
	while (i!=theCount)
	{	// If the name matches any of the values in the array called "theList"
		if (theList.indexOf(theForm.elements[i].name)>=0)
		{	// And if the contents of the "value" element are blank
			if (theForm.elements[i].value=="")
			{	// Then remember the name of the field
				theName=theForm.elements[i].name
				// And let the person know what the field name is that they need to complete
				alert("The \"" + theName + "\" field must be completed in order to submit the form.\nPlease complete the \"" + theName + "\" field and then submit your request.")
				// Then bail out of sending the form, because it's not complete yet
				theResult = false
				break
			} // End of checking for null values
			else{theResult=true}
		} // End of "If indexOf"
		i++;
	} // End of While
	return theResult
} // End of Function

function OpenCertDetails() {
	thewindow = window.open('https://www.thawte.com/cgi/server/certdetails.exe?code=USBEAR14', 'anew', config='height=400,width=450,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,directories=no,status=yes');
}

function copyBilling() {
var form=document.Checkout;
	form['Shipping Address'].value = form['Billing Address'].value;
	form['Shipping City'].value = form['Billing City'].value;
	form['Shipping Zipcode'].value = form['Billing Zipcode'].value;
}

function ClearFields() {
var form=document.Checkout;
	form['First Name'].value="";
	form['Last Name'].value="";
	form['Phone'].value="";
	form['EMail'].value="";
	form['Billing Address'].value="";
	form['Shipping Address'].value="";
	form['Billing City'].value="";
	form['Shipping City'].value="";
//	form['Billing State'].value="0";
//	form['Shipping State'].value="0";
	form['Billing Country'].value="0";
	form['Shipping Country'].value="0";
	form['Billing Zipcode'].value="";
	form['Shipping Zipcode'].value="";
	form['Payment Type'].value="0";
	form['Name on Credit Card'].value="";
	form['Credit Card Number'].value="";
	form['Credit Card Expiration Date'].value="";
	form['Additional Comments'].value="";
}
function alertme() {
	dCurrentDate = new Date();
	dCurYear = dCurrentDate.getYear()
	alert(dCurYear + ' in the date ' + dCurrentDate);
}	

function isLeapYear (Year) {
	if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) {
		return true;
	} else {
		return false;
	}
}

function ValidateCheckout(nAge,nYear,nMonth,nDate,bFFL,bHM,bTBD)
{
//check the required fields for completion
	var theResult=false;
	var theForm=document.Checkout;
	var sAgeAlert='The birth date you have entered makes you too young to complete this order.\nIn order to purchase one or more of the items in your shopping cart, you must be ' + nAge + ' years or older.'
	var nDOBMonth = theForm['DOB Month'].options[theForm['DOB Month'].selectedIndex].value;
	var nDOBDay = theForm['DOB Day'].options[theForm['DOB Day'].selectedIndex].value;
	var nDOBYear = theForm['DOB Year'].options[theForm['DOB Year'].selectedIndex].value;

	if (theForm['DOB Month'].selectedIndex==0 
	|| theForm['DOB Year'].selectedIndex==0 
	|| theForm['DOB Day'].selectedIndex==0 
	|| theForm['First Name'].value==''
	|| theForm['Last Name'].value==''
	|| theForm['Phone'].value==''
	|| theForm['EMail'].value==''
	|| theForm['Billing Address'].value==''
	|| theForm['Billing City'].value==''
	|| theForm['Billing State'].selectedIndex==0
	|| theForm['Billing Zipcode'].value==''
	|| theForm['Shipping Address'].value==''
	|| theForm['Shipping City'].value==''
	|| theForm['Shipping Zipcode'].value==''
	|| theForm['Payment Type'].selectedIndex==0
	|| theForm['Credit Card Number'].value==''
	|| theForm['ExpirationMonth'].value==''
	|| theForm['ExpirationYear'].value==''
	|| theForm['Name on Credit Card'].value==''
	|| ((bFFL=='True') && (theForm.FFL.checked!=true))
	|| ((bFFL=='True') && (theForm['FFLDealerDisplay'].value==''))
	|| ((bHM=='True') && (theForm.HM.checked!=true))
	|| ((bTBD=='True') && (theForm.TBD.checked!=true))
	)
	{
		alert('All fields must be completed before you can submit the form. Please make sure that you enter data into every text field, make a selection in every combo box, and check every checkbox before trying to submit the form.');
	}
	else
	{
		if ((nYear - nDOBYear) > nAge)
		{
			theResult = true;
		}
		else
		{
			if ((nYear - nDOBYear) == nAge)
			{
				if ((nMonth - nDOBMonth) > 0)
				{
					theResult = true;
				}
				else
				{
					if ((nMonth - nDOBMonth) == 0)
					{
						if ((nDate - nDOBDay) > 0)
						{
							theResult = true;
						}
						else
						{
							alert(sAgeAlert);
						}
					}
				}
			}
			else
			{
			    alert(sAgeAlert);
			}
		}
	}
return theResult
}	

function isComplete(theForm,nAge,nYear,nMonth,nDate,theList)
{
	var theResult = false;
	var theCheck4RequiredResult = Check4Required(theForm,theList);
	var theCheckAgeResult = CheckAge(theForm,nAge,nYear,nMonth,nDate);
	if ((theCheck4RequiredResult = true) && (theCheckAgeResult = true))
	{
		theResult = true;
	}
	return theResult;
}

	
	function CheckEmail(sValue, sField)
	{
	// checks field for a @ and .
	// fails if both are not found
	
	var sChar;
	var bFindAt = false;
	var bFindPeriod = false;
	
	for(i=0;i < sValue.length && (bFindPeriod == false || bFindAt == false);i++)
	{
		if(sValue.charAt(i)=='@')
		{
			bFindAt = true
		}
		
		if(sValue.charAt(i)=='.')
		{
			bFindPeriod = true;
		}
		
	}

	if(bFindAt!=true || bFindPeriod != true)
	{
		alert('The ' + sField + ' field must contain a valid email address.');
		return false;	
	}
	else
	{
		return true;
	}

}
	
	function SocialSinCheck(sSocial)
	{
		
		/*** check for numbers or - only!**/
		var sTest = "-1234567890";
		var bMatch = false;
		var bFail = false;
		if (sSocial.length != 11 )
		{
			return false;
		
		}
		
		for (i = 0;i <= (sSocial.length - 1) && bFail == false; i++)
		{
			bMatch = false;
			
			for (z = 0; z <= (sTest.length - 1) && bMatch == false; z++)
			{
/**alert(sSocial.charAt(i) + '  ' + sTest.charAt(z) + (sSocial.charAt(i) == sTest.charAt(z)));
**/				if (sSocial.charAt(i) == sTest.charAt(z))
				{
					
					bMatch = true;
				}
							
			}							
		
			if (bMatch == false)
			{
			
				bFail = true;
			}
			else
			{
				bFail = false;
			}
			
		}

		if (bFail == true)
		{
			return false;
		}
		
		/*** check for format nnn-nn-nnnn***/
		
		if (sSocial.charAt(3) != '-' || sSocial.charAt(6) != '-' )
		{
			bFail = true;
		}
		
		/** check for Canadian Format nnn-nnn-nnn**/
		if (bFail == true)
		{
			if (sSocial.charAt(3) == '-' && sSocial.charAt(7) == '-' )
			{
				bFail = false;
			}
		
		}
		
		if (bFail == true)
		{
			return false;
		}
		
		return true;		
				
	}
	
	function VerifyIncluded(sTest,sMatches)
	{
		var bMatch;
		
		for (var i = 0; i < sTest.length ; i++)
		{
			bMatch = false;
		
			for (var z = 0; (z < sMatches.length ) && (bMatch == false); z++)
			{
				
				if(sTest.charAt(i) == sMatches.charAt(z))
				{
					bMatch = true;
				}				
							
			}
			
			if (bMatch == false)
			{
				return false;
			}	
							
		}
	
		return true;
			
	}
	function stripNonDigits(str) {
	var i
	var newstring = ""
	for (i = 0;  i < str.length; i++) {
		mychar = str.charAt(i)
		if (isDigits(mychar)) 
			newstring += mychar
	}
	return newstring
}

function stripChars(str, chars) {
	var i
	var newstring = ""
	for (i = 0;  i < str.length; i++) {
		mychar = str.charAt(i)
		if (chars.indexOf(mychar) == -1)
			newstring += mychar
	}
	return newstring
}

function isDigits(str) {
	var i
	for (i = 0; i < str.length; i++) {
		mychar = str.charAt(i)
		if (mychar < "0" || mychar > "9")
			return false
	}
	return true
}

function isNumber(str) {
	numdecs = 0
	for (i = 0; i < str.length; i++) {
		mychar = str.charAt(i)
		if ((mychar >= "0" && mychar <= "9") || mychar 
			== "." ) {
			if (mychar == ".")
				numdecs++
		}
		else 
			return false
	}
	if (numdecs > 1)
		return false	
return true
}

function isInRange(str, num1, num2) {
	var i = parseInt(str)
	return((i >= num1) && (i <= num2))

}

function trim(myform,field)
{
   value = document.forms[myform].elements[field].value;

  while (value.charAt(value.length-1) == " "){value = value.substring(0,value.length-1);} 
  while(value.substring(0,1) ==" "){value = value.substring(1,value.length);}

  document.forms[myform].elements[field].value = value;
  //Leave the next line only when you want to select the textbox after you have clicked the button.
}
