function showhideattr(i) {
	with (document) {
		var x = getElementById("attr." + i).style;
		getElementById("showhide." + i).src = (x.display == "none") ? "/template/default/images/cart/min.jpg" : "/template/default/images/cart/pls.jpg";
		x.display = (x.display == "none") ? "block" : "none";
	}
}

function formatcurr(x) {
	s = "" + x;
	i = s.indexOf(".");  if (i == -1) s = s + ".00"; else if (i == s.length - 2) s = s + "0"; else if (i < s.length - 3) s = s.substr(0, i + 3);
	for (i = s.indexOf(".") - 3; i > 0; i -= 3) s = s.substring(0, i) + ',' + s.substr(i);
	return "$" + s;
}

var taxsel, tax, subtotal, ewrf, fuel;
function calctax() {
	try {
		with (document) {
			var sc = getElementById("shipcountry"), ss = getElementById("shipstate"), ssc = getElementById("shipstateCAN"), ec = getElementById("ewrfcell");
			if (sc.value == "USA" && ss.value == "CA") { taxsel = tax; ec.innerHTML = formatcurr(ewrf); } else { taxsel = 0; ec.innerHTML = "N/A"; }
			if (sc.value == "USA") { ss.style.display = "inline"; ssc.style.display = "none"; } else { ss.style.display = "none"; ssc.style.display = "inline"; } 
		}
	} catch(e) {}
	document.getElementById("taxcell").innerHTML = formatcurr(taxsel);
	calctotal();
}

var shipchargesel, shipcharge = new Array();
function calcshipcharge() {
	with (document) {
		var sel = getElementById("shipmethod").selectedIndex;
		shipchargesel = shipcharge[sel];
		getElementById("shipchargecell").innerHTML = formatcurr(shipchargesel);
		if (fuel != -1) {
			getElementById("fuelcell").innerHTML = (sel >= 1 && sel <= 5) ? formatcurr(fuel) : formatcurr(0);
		}
		calctotal();
	}
}

function calctotal() {
	total = subtotal + taxsel + shipchargesel;
	with (document) {
		if (getElementById("shipcountry").value == "USA" && getElementById("shipstate").value == "CA") total += ewrf;
		if (fuel != -1) {
			var sel = getElementById("shipmethod").selectedIndex;
			if (sel >= 1 && sel <= 5) total += fuel;
		}
		getElementById("totalcell").innerHTML = formatcurr(total);
	}
}

function delitem(i) {
	with (document) {
		getElementById("qty." + i).value = 0; 
		f.submit();
	}
}

function init() {
	with (document.checkout) {
		if (ft.value == "sb") { chkshipmethod(); chkamex(); shipname.select(); shipname.focus(); }
		else if (ft.value == "ai") { email.select(); email.focus(); }
		//else if (ft.value == "oc") { placeorder.focus(); }
	}
}

function copyship() {
	with (document.checkout) {
		if (!shipeqbill.checked) {
			billname.select(); billname.focus();
		} else {
			billname.value = shipname.value;
			billaddr1.value = shipaddr1.value;
			billaddr2.value = shipaddr2.value;
			billcity.value = shipcity.value;
			billstate.value = shipstate.value;
			billzip.value = shipzip.value;
			billphone_code.value = shipphone_code.value;
			billphone_prefix.value = shipphone_prefix.value;
			billphone_suffix.value = shipphone_suffix.value;
			billphone_ext.value = shipphone_ext.value;
		}
	}
}

function chkshipmethod() {
	with (document.checkout) {
		if (shipstate.value == "AP" || shipstate.value == "AE" || shipstate.value == "AA") {
			shipmethod1.disabled = true; shipmethod2.disabled = true; shipmethod3.disabled = true; shipmethod4.disabled = true; shipmethod5.checked = true; shipmethod5.disabled = false; shipmethod6.disabled = true;
		} else if (shipstate.value == "AK" || shipstate.value == "HI") {
			shipmethod1.disabled = true; shipmethod2.disabled = true; shipmethod3.checked = true; shipmethod3.disabled = false; shipmethod4.disabled = false; shipmethod5.disabled = true; shipmethod6.disabled = true;
		} else {
			/*shipmethod1.checked = true;*/ shipmethod1.disabled = false; shipmethod2.disabled = false; shipmethod3.disabled = false; shipmethod4.disabled = false; shipmethod5.disabled = true; shipmethod6.disabled = (shipstate.value != "CA");
		}
		document.getElementById("upsground").style.visibility = (shipstate.value == "AK" || shipstate.value == "HI") ? "visible" : "hidden";
		document.getElementById("ups3day").style.visibility = (shipstate.value == "AK" || shipstate.value == "HI") ? "visible" : "hidden";
		document.getElementById("tax").style.visibility = (shipstate.value == "CA") ? "visible" : "hidden";
	}
}

function chkamex() {
	with (document.checkout) {
		var isamex = (ccname.value == 'AMEX');
		ccnum2.maxLength = ccnum2.size = (isamex ? 6 : 4); 
		ccnum3.maxLength = ccnum3.size = (isamex ? 5 : 4); 
		document.getElementById('cclastdigits').style.display = (isamex ? 'none' : 'inline'); 
	}
}

function trim(s) {
	return s.replace(/^\s*|\s*$/g, "");
}

function checkshippingbillinginfo() {
	with (document.checkout) {
		if (trim(shipname.value) == "") { alert("SHIPPING INFORMATION:\nPlease enter the full name."); shipname.focus(); return false; }
		if (trim(shipaddr1.value) == "") { alert("SHIPPING INFORMATION:\nPlease enter the address."); shipaddr1.focus(); return false; }
		if (trim(shipcity.value) == "") { alert("SHIPPING INFORMATION:\nPlease enter the city."); shipcity.focus(); return false; }
		if (shipstate.value == "") { alert("SHIPPING INFORMATION:\nPlease select the state."); shipstate.focus(); return false; }
		if (trim(shipzip.value) == "") { alert("SHIPPING INFORMATION:\nPlease enter the ZIP code."); shipzip.focus(); return false; }
		//if (shipphone.value == "") { alert("SHIPPING INFORMATION:\nPlease enter the phone."); shipphone.focus(); return false; }
		if (trim(shipphone_code.value + shipphone_prefix.value + shipphone_suffix.value + shipphone_ext.value) != "") {
			if (trim(shipphone_code.value).length != 3 || trim(shipphone_prefix.value).length != 3 || trim(shipphone_suffix.value).length != 4) { alert("SHIPPING INFORMATION:\nPlease check the phone."); shipphone_code.focus(); return false; }
		}
		if (!(shipmethod1.checked || shipmethod2.checked || shipmethod3.checked || shipmethod4.checked || shipmethod5.checked || shipmethod6.checked) ) {
			if (shipstate.value == "AP" || shipstate.value == "AE" || shipstate.value == "AA") {
			 alert("SHIPPING INFORMATION:\nShipping to APO/FPO is only available with the purchase of at least 1 system."); shipstate.focus(); return false;
			} else {
				alert("SHIPPING INFORMATION:\nPlease select a shipping method."); shipmethod1.focus(); return false; 
			}
		}
		if (!(complyaddr.checked)) { alert("SHIPPING INFORMATION:\nPlease check the box underneath our shipping policy in order to continue."); complyaddr.focus(); return false; }

		if (trim(billname.value) == "") { alert("BILLING INFORMATION:\nPlease enter the full name."); billname.focus(); return false; }
		if (trim(billaddr1.value) == "") { alert("BILLING INFORMATION:\nPlease enter the address."); billaddr1.focus(); return false; }
		if (trim(billcity.value) == "") { alert("BILLING INFORMATION:\nPlease enter the city."); billcity.focus(); return false; }
		if (billstate.value == "") { alert("BILLING INFORMATION:\nPlease select the state."); billstate.focus(); return false; }
		if (trim(billzip.value) == "") { alert("BILLING INFORMATION:\nPlease enter the ZIP code."); billzip.focus(); return false; }
		//if (billphone.value == "") { alert("BILLING INFORMATION:\nPlease enter the phone."); billphone.focus(); return false; }
		if (trim(billphone_code.value + billphone_prefix.value + billphone_suffix.value + billphone_ext.value) == "") { alert("BILLING INFORMATION:\nPlease enter the phone."); billphone_code.focus(); return false; }
		else if (trim(billphone_code.value).length != 3 || trim(billphone_prefix.value).length != 3 || trim(billphone_suffix.value).length != 4) { alert("BILLING INFORMATION:\nPlease check the phone."); billphone_code.focus(); return false; }
		
		// 5/18/2006 10:41:26 AM -- fax is no longer required
		//if (!shipeqbill.checked && (fax_code.value + fax_prefix.value + fax_suffix.value == "")) { alert("BILLING INFORMATION:\nPlease enter the fax number."); fax_code.focus(); return false; }
		
		if (trim(fax_code.value + fax_prefix.value + fax_suffix.value) != "") {
			if (trim(fax_code.value).length != 3 || trim(fax_prefix.value).length != 3 || trim(fax_suffix.value).length != 4) { alert("BILLING INFORMATION:\nPlease check the fax number."); fax_code.focus(); return false; }
		}
		
		if (ccname.value == "") { alert("PAY WITH:\nPlease select the credit card."); ccname.focus(); return false; }
		if (trim(ccnum1.value) == "" || trim(ccnum2.value) == "" || trim(ccnum3.value) == "" /*|| trim(ccnum4.value) == ""*/) { alert("PAY WITH:\nPlease enter the credit card number."); ccnum1.focus(); return false; }
		n = trim(ccnum1.value) + trim(ccnum2.value) + trim(ccnum3.value) + trim(ccnum4.value);
		if (ccname.value == "AMEX" && !(n.length == 15 && n.charAt(0) == "3")) { alert("PAY WITH:\nPlease check the credit card number."); ccnum1.focus(); return false; } 
		if (ccname.value == "DISCOVER" && !(n.length == 16 && n.charAt(0) == "6")) { alert("PAY WITH:\nPlease check the credit card number."); ccnum1.focus(); return false; } 
		if (ccname.value == "MASTERCARD" && !(n.length == 16 && n.charAt(0) == "5")) { alert("PAY WITH:\nPlease check the credit card number."); ccnum1.focus(); return false; } 
		if (ccname.value == "VISA" && !(n.length == 16 && n.charAt(0) == "4")) { alert("PAY WITH:\nPlease check the credit card number."); ccnum1.focus(); return false; } 
		if (ccmonth.value == "") { alert("PAY WITH:\nPlease select the expiration month."); ccmonth.focus(); return false; }
		if (ccyear.value == "") { alert("PAY WITH:\nPlease select the expiration year."); ccyear.focus(); return false; }
		//...check expiration...
		n = vcode.value
		if (!(n.length == 3 || n.length == 4)) { alert("PAY WITH:\nPlease enter/check the VCode."); vcode.focus(); return false; }
	}
	return true;
}

function checkadditionalinfo() {
	with (document.checkout) {
		e = email.value
		if (e == "") { alert("E-MAIL:\nPlease enter the e-mail address."); email.focus(); return false; }
		if (e.indexOf(" ") > -1 || e.indexOf("@") == -1 || e.indexOf(".") == -1 || e.length < 7) { alert("E-MAIL:\nPlease check the e-mail address."); email.focus(); return false; }

		if (!(referral1.checked || referral2.checked || referral3.checked || referral4.checked || referral5.checked || 
			referral6.checked || referral7.checked || referral8.checked || 
			referral11.checked || referral12.checked || referral13.checked || referral14.checked || 
			referral21.checked || referral22.checked || referral23.checked ||  
			referral100.checked)) { alert("REFERENCE:\nPlease select the source."); referral1.focus(); return false; }
		if (referral100.checked && referral_other.value == "") { alert("REFERENCE:\nPlease enter the source."); referral_other.focus(); return false; }

		if (!gift.checked && giftmsg.value.length > 0) { alert("GIFT WRAPPING:\nGift message is only available with gift wrapping option."); gift.focus(); return false; }
		if (giftmsg.value.length > 500) { alert("GIFT WRAPPING:\nPlease enter max. 500 characters for the message."); giftmsg.focus(); return false; }

		if (comment.value.length > 500) { alert("COMMENTS:\nPlease enter max. 500 characters."); comment.focus(); return false; }
	}
	return true;
}

function confirmorder() {
	with (document.checkout) {
		if (!(acceptwarranty.checked)) { alert("WARRANTY INFORMATION:\nPlease check the box underneath our warranty information order to continue."); acceptwarranty.focus(); return false; }
		cancel.disabled = true;
		prev.disabled = true;
		placeorder.disabled = true; placeorder.value = "Please wait...";
	}
}
