function regform()
{
		if (document.getElementById("password")) {
			if (isEmpty(document.getElementById('password')))
			{  	alert("Please supply a password.");
				document.getElementById('password').focus()
				return false;
			}
		}
		if (document.getElementById("rpassword")) {
			if (isEmpty(document.getElementById('rpassword')))
			{  	alert("Please verify your password.");
				document.getElementById('rpassword').focus()
				return false;
			}
		}
		if (isEmpty(document.getElementById('firstname')))
		{  	alert("Please supply a first name.");
			document.getElementById('firstname').focus()
			return false;
		}
		
		if (isEmpty(document.getElementById('lastname')))
		{  	alert("Please supply a last name.");
			document.getElementById('lastname').focus()
			return false;
		}
		
		if (isEmpty(document.getElementById('address1')))
		{  	alert("Please supply an address.");
			document.getElementById('address1').focus()
			return false;
		}
		
		if (isEmpty(document.getElementById('city')))
		{  	alert("Please supply a city.");
			document.getElementById('city').focus()
			return false;
		}
		
		if (isEmpty(document.getElementById('p_country')))
		{  	alert("Please supply a country.");
			document.getElementById('p_country').focus()
			return false;
		}
		
		if (document.getElementById('p_state_list').value == 'ZZ') {
			alert("Please select a state.")
			document.getElementById('p_state_list').focus();
			return false;
		}

		if (isEmpty(document.getElementById('zipcode')))
		{  	alert("Please supply a zip code.");
			document.getElementById('zipcode').focus()
			return false;
		}
		if (document.getElementById("phone")) {
			if (isEmpty(document.getElementById('phone')))
			{  	alert("Please supply a phone number.");
				document.getElementById('phone').focus()
				return false;
			}
		}
		if (document.getElementById("email")) {
			if (isEmpty(document.getElementById('email')))
			{  	alert("Please supply an e-mail address.");
				document.getElementById('email').focus()
				return false;
			} else
			{	if (isEmail(document.getElementById('email').value)  != true)
				{	alert("Please enter a valid e-mail address.");
					document.getElementById('email').focus()
					return false;
				}
			}
		}

}

function validateNotEmpty(fld, msg, vresult){
	var ok = false;
	if (!fld){
		vresult.message.push( msg );
		
	}else if (fld instanceof Array){
		for (var i=0; i<fld.length; i++){
			if (!isEmpty(fld[i])){ok = true; break;}
		}
		if (!ok){
			vresult.message.push( msg );
		}
		
	}else if (isEmpty(fld)){
		vresult.fields_invalid.push( fld );
		vresult.message.push( msg );
		
	}else{
		ok = true;
		
	}
	return ok;
}

function chkform(){
	var form = document.forms["checkout"];
	var fld, msg, vresult = {"message":[],"fields_invalid":[]};
	
	validateNotEmpty(form.bfirstname, "Please supply a billing First Name.", vresult);
	validateNotEmpty(form.blastname, "Please supply a billing Last Name.", vresult);
	validateNotEmpty(form.baddress1, "Please supply a billing Address.", vresult);
	validateNotEmpty(form.bcity, "Please supply a billing address City.", vresult);
	if (form.bcountry) validateNotEmpty(form.bcountry, "Please supply a billing address Country.", vresult);
	
	if ((fld = document.getElementById('p_bstate_list')) && fld.value == 'ZZ'){
		msg = "Please select a billing State."
		vresult.fields_invalid.push( fld );
		vresult.message.push( msg );
	}

	validateNotEmpty(form.bzipcode, "Please supply a billing Zip/Postal Code.", vresult);
	validateNotEmpty(form.bphone, "Please supply a billing Phone Number.", vresult);
	
	var fld_A = document.getElementById('bemail'), fld_B = document.getElementById('bemail_verify');
	if (fld_A && isEmpty(fld_A)){
	  	msg = "Please enter a billing E-Mail Address.";
		vresult.fields_invalid.push( fld_A );
		vresult.message.push( msg );
	} else if (fld_A && ! isEmail(fld_A.value)){
		msg = "Please enter a valid billing E-Mail Address.";
		vresult.fields_invalid.push( fld_A );
		vresult.message.push( msg );
	} else if (fld_B && isEmpty(fld_B)){
		msg = "Please verify the billing E-Mail Address.";
		vresult.fields_invalid.push( fld_B );
		vresult.message.push( msg );
	} else if (fld_B && ! isEmail(fld_B.value)){
		msg = "Please enter a valid billing E-Mail Address.";
		vresult.fields_invalid.push( fld_B );
		vresult.message.push( msg );
	} else if (fld_A && fld_B && fld_A.value != fld_B.value){
    	msg = "Please verify the billing E-Mail Address.";
		vresult.fields_invalid.push( fld_B );
		vresult.message.push( msg );
	}
	
	if ( (fld = document.getElementById("same")) && ( (fld.value!="no" && ! fld.checked) || (fld.value=="no" && fld.checked) )){
		
		validateNotEmpty(form.firstname, "Please supply a shipping First Name.", vresult);
		validateNotEmpty(form.lastname, "Please supply a shipping Last Name.", vresult);
		validateNotEmpty(form.address1, "Please supply a shipping Address.", vresult);
		validateNotEmpty(form.city, "Please supply a shipping address City.", vresult);
		if (form.country) validateNotEmpty(form.country, "Please supply a shipping address Country.", vresult);
		
		if ((fld = document.getElementById('p_state_list')) && fld.value == 'ZZ'){
			msg = "Please select a shipping State."
			vresult.fields_invalid.push( fld );
			vresult.message.push( msg );
		}
		validateNotEmpty(form.zipcode, "Please supply a shipping Zip/Postal Code.", vresult);
		//validateNotEmpty(form.phone, "Please supply a shipping Phone Number.", vresult);
		
	}
	
	if (vresult.message.length){
		alert(vresult.message.join("\n"));
		if (vresult.fields_invalid.length) vresult.fields_invalid[0].focus();
		return false;
	} else {
		return true;
	}
}

function radio_button_checker(){
	ok = false;
	//alert("No shipping method chosen");	
	for (var i=0,iE=document.getElementsByName("p_shipzone").length; i<iE; i++){
		if (document.getElementsByName("p_shipzone")[i].checked){
			ok = true;
		}
	}
	return ok;
}

var statesOfUS=new Array()
var statesOfCA=new Array()
var statesOfPR=new Array()
var statesOfVI=new Array()
var statesOfAU=new Array()

//SPECIFY MENU SETS AND THEIR LINKS. FOLLOW SYNTAX LAID OUT

statesOfUS[0] = 'ZZ-Choose State...'
statesOfUS[1] = 'AL-AL (Alabama)'
statesOfUS[2] = 'AK-AK (Alaska)'
statesOfUS[3] = 'AZ-AZ (Arizona)'
statesOfUS[4] = 'AR-AR (Arkansas)'
statesOfUS[5] = 'CA-CA (California)'
statesOfUS[6] = 'CO-CO (Colorado)'
statesOfUS[7] = 'CT-CT (Connecticut)'
statesOfUS[8] = 'DE-DE (Delaware)'
statesOfUS[9] = 'DC-DC (District of Columbia)'
statesOfUS[10] = 'FL-FL (Florida)'
statesOfUS[11] = 'GA-GA (Georgia)'
statesOfUS[12] = 'GU-GU (Guam)'
statesOfUS[13] = 'HI-HI (Hawaii)'
statesOfUS[14] = 'ID-ID (Idaho)'
statesOfUS[15] = 'IL-IL (Illinois)'
statesOfUS[16] = 'IN-IN (Indiana)'
statesOfUS[17] = 'IA-IA (Iowa)'
statesOfUS[18] = 'KS-KS (Kansas)'
statesOfUS[19] = 'KY-KY (Kentucky)'
statesOfUS[20] = 'LA-LA (Louisiana)'
statesOfUS[21] = 'ME-ME (Maine)'
statesOfUS[22] = 'MD-MD (Maryland)'
statesOfUS[23] = 'MA-MA (Massachusetts)'
statesOfUS[24] = 'MI-MI (Michigan)'
statesOfUS[25] = 'MN-MN (Minnesota)'
statesOfUS[26] = 'MS-MS (Mississippi)'
statesOfUS[27] = 'MO-MO (Missouri)'
statesOfUS[28] = 'MT-MT (Montana)'
statesOfUS[29] = 'NE-NE (Nebraska)'
statesOfUS[30] = 'NV-NV (Nevada)'
statesOfUS[31] = 'NH-NH (New Hampshire)'
statesOfUS[32] = 'NJ-NJ (New Jersey)'
statesOfUS[33] = 'NM-NM (New Mexico)'
statesOfUS[34] = 'NY-NY (New York)'
statesOfUS[35] = 'NC-NC (North Carolina)'
statesOfUS[36] = 'ND-ND (North Dakota)'
statesOfUS[37] = 'OH-OH (Ohio)'
statesOfUS[38] = 'OK-OK (Oklahoma)'
statesOfUS[39] = 'OR-OR (Oregon)'
statesOfUS[40] = 'PA-PA (Pennsylvania)'
statesOfUS[41] = 'PR-PR (Puerto Rico)'
statesOfUS[42] = 'RI-RI (Rhode Island)'
statesOfUS[43] = 'SC-SC (South Carolina)'
statesOfUS[44] = 'SD-SD (South Dakota)'
statesOfUS[45] = 'TN-TN (Tennessee)'
statesOfUS[46] = 'TX-TX (Texas)'
statesOfUS[47] = 'UT-UT (Utah)'
statesOfUS[48] = 'VT-VT (Vermont)'
statesOfUS[49] = 'VI-VI (Virgin Islands)'
statesOfUS[50] = 'VA-VA (Virginia)'
statesOfUS[51] = 'WA-WA (Washington)'
statesOfUS[52] = 'WV-WV (West Virginia)'
statesOfUS[53] = 'WI-WI (Wisconsin)'
statesOfUS[54] = 'WY-WY (Wyoming)'
statesOfUS[55] = 'AE-AE (U.S. Military)'
statesOfUS[56] = 'AP-AP (U.S. Military)'

statesOfCA[0] = 'ZZ-Canadian Provinces...'
statesOfCA[1] = 'SK-SK (Saskatchewan)'
statesOfCA[2] = 'YT-YT (Yukon Terr)'
statesOfCA[3] = 'AB-AB (Alberta)'
statesOfCA[4] = 'BC-BC (British Columbia)'
statesOfCA[5] = 'MB-MB (Manitoba)'
statesOfCA[6] = 'NB-NB (New Brunswick)'
statesOfCA[7] = 'NF-NF (Newfoundland)'
statesOfCA[8] = 'NT-NT (Northwest Territory)'
statesOfCA[9] = 'NS-NS (Nova Scotia)'
statesOfCA[10] = 'ON-ON (Ontario)'
statesOfCA[11] = 'PE-PE (Prince Edward Island)'
statesOfCA[12] = 'PQ-PQ (Quebec)'

statesOfPR[0] = '  -N/A'

statesOfVI[0] = '  -N/A'

statesOfAU[0] = '  -N/A'

function updStateList(selBox, selValue)
{
	var i=0
	var state, stateName, stateradio
	if (selBox.name == "p_bcountry")
	{
		StateList = document.getElementById("p_bstate_list");
		StateList.options.length=0;
	}
	if (selBox.name == "p_country")
	{
		StateList = document.getElementById("p_state_list");
		StateList.options.length=0;
	}
	if (selValue== "USA")
	{
		// for (i=0;i<statesOfUS.length;i++)
		for (i=0;i<statesOfUS.length;i++)
		{
			state = statesOfUS[i].substring(0,2)
			stateName = statesOfUS[i].substring(3,statesOfUS[i].length)
			StateList.options[i]=new Option(stateName, state, false)
		}
	}
	if (selValue== "Canada")
	{
	
		for (i=0;i<statesOfCA.length;i++)
		{
			state = statesOfCA[i].substring(0,2)
			stateName = statesOfCA[i].substring(3,statesOfCA[i].length)
			StateList.options[i]=new Option(stateName, state, false)
		}
	}
	if (selValue== "Australia")
	{
	
		for (i=0;i<statesOfAU.length;i++)
		{
			state = statesOfAU[i].substring(0,2)
			stateName = statesOfAU[i].substring(3,statesOfAU[i].length)
			StateList.options[i]=new Option(stateName, state, false)
		}
	}
	if (selValue== "Puerto Rico")
	{
	
		for (i=0;i<statesOfPR.length;i++)
		{
			state = statesOfPR[i].substring(0,2)
			stateName = statesOfPR[i].substring(3,statesOfPR[i].length)
			StateList.options[i]=new Option(stateName, state, false)
		}
	}
	if (selValue== "Virgin Islands")
	{
	
		for (i=0;i<statesOfVI.length;i++)
		{
			state = statesOfVI[i].substring(0,2)
			stateName = statesOfVI[i].substring(3,statesOfVI[i].length)
			StateList.options[i]=new Option(stateName, state, false)
		}
	}
}

function copyBillInfo()
{
	var frm = document.checkout;
	if (frm.p_same.checked)
	{
		frm.p_firstname.value = frm.p_bfirstname.value;
		frm.p_lastname.value = frm.p_blastname.value;
		frm.p_address1.value = frm.p_baddress1.value;
		frm.p_company.value = frm.p_bcompany.value;
		frm.p_attention.value = frm.p_battention.value;
		frm.p_city.value = frm.p_bcity.value;
		var val = frm.p_bcountry.value;
		for(i=0;i<frm.p_country.length;i++)
		{
			if(frm.p_country.options[i].value==val)
			{
				frm.p_country.selectedIndex=i;
			}
		}
		var selBox = frm.p_country;
		updStateList(selBox, val);
		var val2 = frm.p_bstate_list.value;
		for(i=0;i<frm.p_state_list.length;i++)
		{
			if(frm.p_state_list.options[i].value==val2)
			{
				frm.p_state_list.selectedIndex=i;
			}
		}
		frm.p_zipcode.value = frm.p_bzipcode.value;
		frm.p_phone.value = frm.p_bphone.value;
		frm.p_firstname.disabled = true;
		frm.p_lastname.disabled = true;
		frm.p_address1.disabled = true;
		frm.p_company.disabled = true;
		frm.p_attention.disabled = true;
		frm.p_city.disabled = true;
		frm.p_country.disabled = true;
		frm.p_state_list.disabled = true;
		frm.p_zipcode.disabled = true;
		frm.p_phone.disabled = true;
		if (document.getElementById('addressbook'))
		{
			document.getElementById('addressbook').selectedIndex = 0;
			document.getElementById('addressbook').disabled = true;
		}
	} else
	{
		frm.p_firstname.disabled = false;
		frm.p_lastname.disabled = false;
		frm.p_address1.disabled = false;
		frm.p_company.disabled = false;
		frm.p_attention.disabled = false;
		frm.p_city.disabled = false;
		frm.p_country.disabled = false;
		frm.p_state_list.disabled = false;
		frm.p_zipcode.disabled = false;
		frm.p_phone.disabled = false;
		if (document.getElementById('addressbook'))
		{
			document.getElementById('addressbook').selectedIndex = 0;
			document.getElementById('addressbook').disabled = false;
		}
	}
}

function copyData(inField,copyField) {
	if (document.checkout.p_same.checked) {
		document.getElementById(copyField).value = document.getElementById(inField).value
	}
}

function limitText(textArea, length) {
    if (textArea.value.length > length) {
        textArea.value = textArea.value.substr(0,length);
    }
}



function chkcc(){
	var ok = true;
	var form = document.forms["checkout"];
	var PayPalPayment = false;
	if (document.getElementById("txtPPType"))
	{
		PayPalPayment = document.getElementById("txtPPType").checked;
	}
	if (PayPalPayment != true)
	{
		if (document.getElementById('cctype'))
		{
			if (document.getElementById('cctype').value == '') {
				alert('Please suply a payment method');
				ok = false;
			}
			if (document.getElementById('cctype').value.substr(0,2).toUpperCase() == 'AM') {
				if (document.getElementById('cc_num').value.substr(0,1) != "3") {
					alert("American Express must begin with a 3 digit");
					ok = false;
				}
				if (form.cc_num.value.length != 15) {
					alert("American Express must be 15 numbers");
					ok = false;
				}
				if (form.cvv.value.length != 4) {
					alert("American Express CVV must be 4 numbers");
					ok = false;
				}
			}
			if (document.getElementById('cctype').value.substr(0,2).toUpperCase() == "VI") {
				if (form.cc_num.value.substr(0,1) != "4") {
					alert("Visa must begin with a 4 digit");	
					ok = false;
				}
				if (form.cc_num.value.length != 16) {
					alert("Visa must be 16 numbers");
					ok = false;
				}
				if (form.cvv.value.length != 3) {
					alert("Visa CVV must be 3 numbers");
					ok = false;
				}
			}
			if (document.getElementById('cctype').value.substr(0,2).toUpperCase() == "DI") {
				if (form.cc_num.value.substr(0,1) != "6") {
					alert("Discover must begin with a 6 digit");
					ok = false;
				}
				if (form.cc_num.value.length != 16) {
					alert("Discover must be 16 numbers");
					ok = false;
				}
				if (form.cvv.value.length != 3) {
					alert("Discover CVV must be 3 numbers");
					ok = false;
				}
			}
			if (document.getElementById('cctype').value.substr(0,2).toUpperCase() == "MA") {
				if (form.cc_num.value.substr(0,1) != "5") {
					alert("Mastercard must begin with a 5 digit");
					ok = false;
				}	
				if (form.cc_num.value.length != 16) {
					alert("MasterCard must be 16 numbers");
					ok = false;
				}
				if (form.cvv.value.length != 3) {
					alert("MasterCard CVV must be 3 numbers");
					ok = false;
				}
			 }
			if (document.getElementById('cctype').value.toUpperCase() == "OA") {
				form.cc_num.value = "";
				form.cc_num.value = "";
				form.cvv.value = "";
			 }
			if (document.getElementById('cctype').value.toUpperCase() != "OA") {
				var ccMonth = form.cardmo.value;
				var ccYear = form.cardyr.value;
				var today = new Date();
				ccMonth = ccMonth * 1;
				ccYear = ccYear * 1;
				if (ccYear < today.getFullYear() || ccYear == 0) {
					alert('Card expiration year invalid');
					ok = false;
				}
				if ((ccYear == today.getFullYear()) && (ccMonth < (today.getMonth() + 1)) || ccMonth == 0) {
					alert('Card expiration month invalid');
					ok = false;
				}
			}
			form.cardmo.disabled = false;
			form.cardyr.disabled = false;
			form.cc_num.disabled = false;
			form.cvv.disabled = false;
		}
	}
	if (ok == true)
	{
		var form = document.forms["checkout"];
		form.action = "checkout-process.asp?action=verify";
		form.submit();
	}
	//alert(ok);
}

function oktoMove() {
	var ok = true;
	return ok;
}

function chkPaymentform2(){
	var form = document.forms["checkout"];
	var email = document.getElementById('sessionEmail').value
	var groupID = document.getElementById('ccgroup').options[document.getElementById('ccgroup').selectedIndex].value
	var shipState = document.getElementById('p_state_list').options[document.getElementById('p_state_list').selectedIndex].value
	var zip = document.getElementById('zipcode').value
	var costItems = document.getElementById('costItems').value
	for (var i=0,iE=document.getElementsByName("p_shipzone").length; i<iE; i++){
		if (document.getElementsByName("p_shipzone")[i].checked){
			shipMethod = document.getElementsByName("p_shipzone")[i].value;
		}
	}
	var url = "/ajax/check-points.asp?groupid="+ groupID +"&email="+ email +"&state="+ shipState +"&zip="+ zip +"&shipmethod="+ shipMethod +"&items="+ costItems;
	xmlhttp = new GetXmlHttpObject();
	xmlhttp.open('GET',url,true);
	xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			needCC = xmlhttp.responseText;
			if (needCC == 'Y')
			{
				chkcc();
			} else {
				var form = document.forms["checkout"];
				form.action = "checkout-process.asp?action=verify";
				form.submit();
			}
		}
	}
	xmlhttp.send(null);
}


function chkPaymentform(){

	var form = document.forms["checkout"];
	form.action = "checkout-process.asp?action=verify";
	form.submit();
}



function OAChoice() {
	var form = document.forms["checkout"];
	/*if (document.getElementById('cctype').options[document.getElementById('cctype').selectedIndex].value.toUpperCase() != "OA") {
		form.cc_num.value = "";
		form.cvv.value = "";
		form.cardmo.disabled = false;
		form.cardyr.disabled = false;
		form.cc_num.disabled=false;
		form.cvv.disabled=false;
		document.getElementById('polabel').style.display = 'none';
		document.getElementById('pofield').style.display = 'none';
		document.getElementById('pomessage').style.display = 'none';
		document.getElementById('ponum').value = '';
	} else {
		form.cardmo.disabled = true;
		form.cardyr.disabled = true;
		form.cc_num.disabled = true;
		form.cvv.disabled = true;
		document.getElementById('polabel').style.display = 'block';
		document.getElementById('pofield').style.display = 'block';
		document.getElementById('pomessage').style.display = 'block';
	}*/
}

function chkLogin() {
	if (isEmpty(document.getElementById('email')))
	{  	alert("Please supply an email address.");
		document.getElementById('email').focus();
		return false;
	} else
	{	if (isEmail(document.getElementById('email').value)  != true)
		{	alert("Please enter a valid email address.");
			document.getElementById('email').focus();
			return false;
		}
	}
	if (isEmpty(document.getElementById('password')))
	{  	alert("Please supply a password or zip code.");
		document.getElementById('password').focus();
		return false;
	}
}

function chkForgotPass() {
	if (isEmpty(document.getElementById('p_bemail')))
	{  	alert("Please supply an email address.");
		document.getElementById('p_bemail').focus();
		return false;
	} else
	{	if (isEmail(document.getElementById('p_bemail').value)  != true)
		{	alert("Please enter a valid email address.");
			document.getElementById('p_bemail').focus();
			return false;
		}
	}
}
function checkCoupon() {
	var gc1 = document.giftcert.giftcert1.value;
	var gc2 = document.giftcert.giftcert2.value;
	if (gc1 == "" && gc2 == "") {
		alert("Please enter a gift certificate number.");
		document.giftcert.giftcert1.focus();
		return false;
	}
	else { return true; }
}
function selectAddress()
{
	var bookID = document.getElementById('addressbook').options[document.getElementById('addressbook').selectedIndex].value;
	var url = "/ajax/select-address.asp?bookid="+ bookID;
	xmlhttp = new GetXmlHttpObject();
	xmlhttp.open('GET',url,true);
	xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			optionArray = eval(xmlhttp.responseText);
			var fname = optionArray[0];
			var lname = optionArray[1];
			var company = optionArray[2];
			var address = optionArray[3];
			var city = optionArray[5];
			var country = optionArray[8];
			var state = optionArray[7];
			var zip = optionArray[6];
			var phone = optionArray[9];
			document.getElementById('firstname').value = fname;
			document.getElementById('lastname').value = lname;
			document.getElementById('company').value = company;
			document.getElementById('address1').value = address;
			document.getElementById('city').value = city;
			document.getElementById('zipcode').value = zip;
			document.getElementById('phone').value = phone;
			document.getElementById('country').value = country;
			updStateList('country',country);
			document.getElementById('p_state_list').value = state;
		}
	}
	xmlhttp.send(null);
}
