

if (window.attachEvent) window.attachEvent("onload", init);
	else window.onload = function() { init(); }

function $(strElement) {
	return document.getElementById(strElement);
}

function init() {
	if (document.getElementsByTagName) {
		var a = document.getElementsByTagName("a");
		for (var i=0; i<a.length; i++) {
			if (a[i].getAttribute("href") && a[i].getAttribute("rel") == "external") {
				a[i].target = "_blank";
			}
			if (a[i].getAttribute("href") && !a[i].getAttribute("title")) { 
				a[i].title = a[i].innerHTML;
			}
			if (a[i].getAttribute("href")) {
				if (a[i].getAttribute("href") == "#") {
					a[i].onclick = function() { return false; };
				}
			}
		}
		var button = document.getElementsByTagName("input");
		for (var j=0; j<button.length; j++) {
			if (button[j].getAttribute("type") == "submit" || button[j].getAttribute("type") == "reset" || button[j].getAttribute("type") == "button") {
				button[j].className = "button";
				button[j].title = button[j].getAttribute("value");
			}
		}
	}
}

function hideElement(strElement) {
	if ($(strElement)) {
		$(strElement).style.display = "none";
	}
}

function showElement(strElement) {
	if ($(strElement)) {
		$(strElement).style.display = "";
	}
}

// Formats an input field
function formatPhone(field, event) {
	//-- field loses focus if field tabbed to
	event = event || window.event;
	code = event.which || event.keyCode;

	if ((code != 9) && (code != 16)) {
		var temp = field.value.replace(/[^0-9_]/g, "");
		var cursor = temp.length;
		temp = temp.replace(/^([0-9_]{0,3})([0-9_]{0,3})([0-9_]{0,4})([0-9_]*)/, "($1) $2-$3 x $4");
		if (cursor < 11)
			temp = temp.replace(/\s*x\s*$/, "");
		if (cursor < 7)
			temp = temp.replace(/\-\s*$/, "");
		if (cursor < 3)
			temp = temp.replace(/\)\s*$/, "");
		if (! cursor)
			temp = "";
		field.value = temp;
	}
	return true;
}

function selectPrevShipping(objCheckbox, intAddressID) {
	if (objCheckbox.checked && $("previousShippingAddress")) {
		var foundMatch = false;
		var parent = $("previousShippingAddress");
		var children = parent.getElementsByTagName("INPUT");
		for (var i=0; i<children.length; i++) {
			if (children[i].name == "ship_AddressID") {
				children[i].checked = false;
				if (children[i].value == intAddressID.toString()) {
					children[i].checked = true;
					foundMatch = true;
				}
			}
		}
		if (foundMatch) {
			hideElement("newShippingAddress");
			$("previousShippingNo").checked = false;
			$("previousShippingYes").checked = true;
			showElement("previousShippingAddress");
		}
	} else {
		clearShipping(objCheckbox);
	}
}

function copyNewShipping(objCheckbox, firstName, middleName, lastName, companyName, address1, address2, city, state, zip, phone1, phone2) {
	if (objCheckbox.checked) {
		theForm = objCheckbox.form;
		theForm.ship_FirstName.value = firstName;
		theForm.ship_MiddleName.value = middleName;
		theForm.ship_LastName.value = lastName;
		theForm.ship_CompanyName.value = companyName;
		theForm.ship_Address1.value = address1;
		theForm.ship_Address2.value = address2;
		theForm.ship_City.value = city;
		theForm.ship_State.value = state;
		theForm.ship_Zip.value = zip;
		theForm.ship_Phone1.value = phone1;
		theForm.ship_Phone2.value = phone2;

		hideElement("previousShippingAddress");
		$("previousShippingNo").checked = true;
		$("previousShippingYes").checked = false;
		showElement("newShippingAddress");
	} else {
		clearShipping(objCheckbox);
	}
}

function clearShipping(objCheckbox) {
	if (!objCheckbox.checked) {
		if ($("previousShippingAddress")) {
			var parent = $("previousShippingAddress");
			var children = parent.getElementsByTagName("INPUT");
			for (var i=0; i<children.length; i++) {
				if (children[i].name == "ship_AddressID") {
					children[i].checked = false;
				}
			}
		}

		theForm = objCheckbox.form;
		theForm.ship_FirstName.value = "";
		theForm.ship_MiddleName.value = "";
		theForm.ship_LastName.value = "";
		theForm.ship_CompanyName.value = "";
		theForm.ship_Address1.value = "";
		theForm.ship_Address2.value = "";
		theForm.ship_City.value = "";
		theForm.ship_State.value = "";
		theForm.ship_Zip.value = "";
		theForm.ship_Phone1.value = "";
		theForm.ship_Phone2.value = "";

		hideElement("previousShippingAddress");
		$("previousShippingNo").checked = true;
		$("previousShippingYes").checked = false;
		showElement("newShippingAddress");
	}
}

function checkAddresses(objElement) {
	var foundChecked = false;
	var parent, inputName;
	if (objElement.getAttribute("name") == "previousBilling") {
		parent = $("previousBillingAddress");
		inputName = "bill_AddressID";
	} else if (objElement.getAttribute("name") == "previousShipping") {
		parent = $("previousShippingAddress");
		inputName = "ship_AddressID";
	}
	
	var children = parent.getElementsByTagName("INPUT");
	if (children.length > 0) {
		//-- check if any addresses are already selected
		for (var i=0; i<children.length; i++) {
			if (children[i].name == inputName) {
				if (!foundChecked && children[i].checked) {
					foundChecked = true;
				}
			}
		}
		if (!foundChecked) {
			//-- find the first address and mark it checked
			for (var j=0; j<children.length; j++) {
				if (children[j].name == inputName) {
					if (!foundChecked) {
						children[j].checked = true;
						foundChecked = true;
					}
				}
			}
		}
	}
}

function checkRequiresDetail(objSelect) {
	var req = false;
	var sel = objSelect.selectedIndex;
	var val = $("label_ShippingMethodDetail").getAttribute("rel");

	if (objSelect.options[sel].getAttribute("rel")) {
		req = true;
		val = objSelect.options[sel].getAttribute("title");
	}

	$("label_ShippingMethodDetail").innerHTML = val;
	if (req) {
		showElement("shippingMethodDetail");
	} else {
		hideElement("shippingMethodDetail");
		$("other_ShippingMethodDetail").value = "";
	}
}
