// JavaScript Document
// <!--
function shipsame(form){

	if(form.sameasbilling.checked){
	
		 form.Mailing_First.value = form.Billing_First.value;
		 form.Mailing_Last.value = form.Billing_Last.value;
		 form.Mailing_Address1.value = form.Billing_Address1.value;
		 form.Mailing_Address2.value = form.Billing_Address2.value;
		 form.Mailing_City.value = form.Billing_City.value;
		 form.Mailing_State.value = form.Billing_State.value;
		 form.Mailing_Country.selectedIndex = form.Billing_Country.selectedIndex;
		 form.Mailing_Zip.value = form.Billing_Zip.value;
	}
	else{
		 form.Mailing_First.value = "";
		 form.Mailing_Last.value = "";
		 form.Mailing_Address1.value = "";
		 form.Mailing_Address2.value = ""
		 form.Mailing_City.value = "";
		 form.Mailing_Zip.value = "";
	}
}


function formValidator(){
	var Billing_First = document.getElementsByName('Billing_First')[0];
	var Billing_Last = document.getElementsByName('Billing_Last')[0];
	var Billing_Address1 = document.getElementsByName('Billing_Address1')[0];
	var Billing_Address2 = document.getElementsByName('Billing_Address2')[0];
	var Billing_City = document.getElementsByName('Billing_City')[0];
	var Billing_Zip = document.getElementsByName('Billing_Zip')[0];
	
	var Mailing_First = document.getElementsByName('Mailing_First')[0];
	var Mailing_Last = document.getElementsByName('Mailing_Last')[0];
	var Mailing_Address1 = document.getElementsByName('Mailing_Address1')[0];
	var Mailing_Address2 = document.getElementsByName('Mailing_Address2')[0];
	var Mailing_City = document.getElementsByName('Mailing_City')[0];
	var Mailing_Zip = document.getElementsByName('Mailing_Zip')[0];
	
	var cc_name = document.getElementsByName('cc_name')[0];
	var cc_number = document.getElementsByName('cc_number')[0];
	var cc_ccv = document.getElementsByName('cc_ccv')[0];
	
	// Check each input in the order that it appears in the form
	
	if(isAlphabet(Billing_First, "Please enter only letters for your first name")){
		if(isAlphabet(Billing_Last, "Please enter only letters for your last name")){
			if(isEmpty(Billing_Address1, "Please enter your address into the billing Address 1 field.")){
				if(isEmpty(Billing_City, "Please enter a billing city.")){
						if(isAlphabet(Mailing_First, "Please enter only letters for your shipping first name")){
							if(isAlphabet(Mailing_Last, "Please enter only letters for your shipping last name")){
								if(isEmpty(Mailing_Address1, "Please enter your address into the shipping Address 1 field.")){
									if(isEmpty(Mailing_City, "Please enter a shipping city.")){
											if(isEmpty(cc_name, "Please enter a credit card name.")){
												if(isNumeric(cc_number, "Please enter only numbers in the credit card field.")){
													if(isNumeric(cc_ccv, "Please only use numbers for the CCV field.")){
														return true;
													}
												}
											}
										}
							}
						}	
					}
				}
			}
		}
	}
	
	return false;
	
}

function isEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}

function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphabet(elem, helperMsg){
	var alphaExp = /^[a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphanumeric(elem, helperMsg){
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert("Please enter between " +min+ " and " +max+ " characters");
		elem.focus();
		return false;
	}
}

function madeSelection(elem, helperMsg){
	if(elem.value == "Please Choose"){
		alert(helperMsg);
		elem.focus();
		return false;
	}else{
		return true;
	}
}

function emailValidator(elem, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

//-->