var oWin, oImg;

function init (e) {
	adjustExternalLinks();
	addSameWholesaleShipping();
	//adjustSearchBox();
}

function addSameWholesaleShipping() {
	var shipFields = document.getElementById("wholesale-ship-fields");
	
	if (shipFields) {
		// Add check box
		var cb = document.createElement("input");
		var cb_label = document.createElement("label");
		
		cb.id = "wholesale-same-checkbox";
		cb.type = "checkbox";
		cb.onclick = setWholesaleShipFields;
		
		cb_label.setAttribute("for", "wholesale-same-checkbox");
		cb_label.innerHTML = "My shipping and billing addresses are the same";
		
		// Add to page
		shipFields.parentNode.insertBefore(cb, shipFields);
		shipFields.parentNode.insertBefore(cb_label, shipFields);
	}
}

function setWholesaleShipFields() {
	var cb = document.getElementById("wholesale-same-checkbox");
	
	var fname = document.getElementById("fnameTextBox");
	var lname = document.getElementById("lnameTextBox");
	var address = document.getElementById("addressTextBox");
	var city = document.getElementById("cityTextBox");
	var state = document.getElementById("stateDropDownList");
	var other_state = document.getElementById("otherStateTextBox");
	var country = document.getElementById("countryDropDownList");
	var zip = document.getElementById("zipTextBox");
	
	var ship_fname = document.getElementById("shipFnameTextBox");
	var ship_lname = document.getElementById("shipLnameTextBox");
	var ship_address = document.getElementById("shipAddressTextBox");
	var ship_city = document.getElementById("shipCityTextBox");
	var ship_state = document.getElementById("shipStateDropDownList");
	var ship_other_state = document.getElementById("shipOtherStateTextBox");
	var ship_country = document.getElementById("shipCountryDropDownList");
	var ship_zip = document.getElementById("shipZipTextBox");
	
	if (cb.checked) {
		ship_fname.value = fname.value;
		ship_lname.value = lname.value;
		ship_address.value = address.value;
		ship_city.value = city.value;
		ship_state.value = state.value;
		ship_other_state.value = other_state.value;
		ship_country.value = country.value;
		ship_zip.value = zip.value;
	} else {
		ship_fname.value = ship_lname.value = ship_address.value = ship_city.value = ship_other_state.value = ship_zip.value = "";
		ship_state.selectedIndex = ship_country.selectedIndex = 0;
	}
}

function disableFinishButton() {
	var finish_btn = document.getElementById("btnFinish");
	var finish_div = finish_btn.parentNode;
	var finish_p = document.createElement("p");

	finish_btn.disabled = true;
	
	finish_p.className = "error";
	finish_p.innerHTML = "Please wait while your order is being processed. Refreshing this page may result in duplicate payments.";
	
	finish_div.insertBefore(finish_p, finish_btn);
}

function fillShipping() {
	var oFirstName = document.getElementById("txtFirstName");
	var oLastName = document.getElementById("txtLastName");
	var oAddress = document.getElementById("txtAddress");
	var oCity = document.getElementById("txtCity");
	var oState = document.getElementById("ddlState");
	var oCountry = document.getElementById("ddlCountry");
	var oZip = document.getElementById("txtZip");

	var oShipFirstName = document.getElementById("txtShipFirstName");
	var oShipLastName = document.getElementById("txtShipLastName");
	var oShipAddress = document.getElementById("txtShipAddress");
	var oShipCity = document.getElementById("txtShipCity");
	var oShipState = document.getElementById("ddlShipState");
	var oShipCountry = document.getElementById("ddlShipCountry");
	var oShipZip = document.getElementById("txtShipZip");
	
	if (document.getElementById("sameShipping").checked) {
		oShipFirstName.value = oFirstName.value;
		oShipLastName.value = oLastName.value;
		oShipAddress.value = oAddress.value;
		oShipCity.value = oCity.value;
		oShipState.selectedIndex = oState.selectedIndex;
		oShipCountry.selectedIndex = oCountry.selectedIndex;
		oShipZip.value = oZip.value;
	} else {
		oShipFirstName.value = "";
		oShipLastName.value = "";
		oShipAddress.value = "";
		oShipCity.value = "";
		oShipState.value = "";
		oShipCountry.value = "";
		oShipZip.value = "";
	}
}

function adjustSearchBox () {
	var oSearchBox = document.getElementById("keywords");

	if (window.addEventListener) {
		oSearchBox.addEventListener("focus", clearInput, false);
		oSearchBox.addEventListener("blur", setInput, false);
	} else {
		oSearchBox.attachEvent("onfocus", clearInput);
		oSearchBox.attachEvent("onblur", setInput);
	}
}

function clearInput (e) {
	var oTarget = (e.target) ? e.target : event.srcElement;
	
	if (oTarget.value == "Search our shop") oTarget.value = "";
}

function setInput (e) {
	var oTarget = (e.target) ? e.target : event.srcElement;
	
	if (oTarget.value == "") oTarget.value = "Search our shop";
}

function checkTax(sState, nAmount, nTotal) {
	var oDdl = document.getElementById("ddlState");
	var oLabel = document.getElementById("taxLabel");
	var nTaxAmount = nAmount * nTotal;
	
	if (oDdl.options[oDdl.selectedIndex].value == sState) {
		oLabel.innerHTML = "$" + nTaxAmount.toFixed(2) + " sales tax will be added.";
	} else {
		oLabel.innerHTML = "";	
	}
}

function disableImageClick(e) {
	var sMsg, dDate, nYear;
	var oSource;
	var aAllow = new Array("/links/");

	for (var n = 0; n < aAllow.length; n++) {
		if (window.location.href.indexOf(aAllow[n]) > -1) return true;
	}

	dDate = new Date();
	nYear = dDate.getFullYear();
	sMsg = "This image is copyright " + nYear;
	
	if (document.all)
		oSource = event.srcElement;
	else {
		oSource = e.target;
	}
	
	if (oSource.nodeName == "IMG") {
		alert(sMsg);
		e.cancelBubble = true;
		
		if (e.stopPropagation) {
			e.stopPropagation();
			e.preventDefault();
		}
		return false;
	}
	
	return true;
}

function adjustExternalLinks() {
	if (document.getElementsByTagName) {
		var n, hyperlink;
		var linkArray = document.getElementsByTagName("a");

		for (n = 0; n < linkArray.length; n++) {
			hyperlink = linkArray[n];

			if (hyperlink.className == "external-link") {
				hyperlink.onclick = function () {window.open(this.href, "_blank", "width=800, height=600, menubar=yes, toolbar=yes, location=yes, scrollbars=yes, resizable=yes"); return false;};
			}
		}
	}
}

function popUp(sURL) {
	var sFeatures, sHTML;
	oImage = new Image();
	
	oImage.src = sURL;
	
	if (oImage.complete) {
		resizePopUp();
	} else {
		oImage.onload = resizePopUp;
	}
}

function resizePopUp() {
	var nWidth, nHeight;
	
	nWidth = (parseInt(oImage.width) + 50 > screen.width) ? screen.width - 50 : parseInt(oImage.width) + 25;
	nHeight = (parseInt(oImage.height) + 200 > screen.height) ? screen.height - 200 : parseInt(oImage.height) + 110;
	
	sFeatures = "width=" + nWidth + ", height=" + nHeight + ", status=no, scrollbars=yes";
	
	sHTML = "<html><head><title>Image Viewer</title>";
	sHTML += "<meta http-equiv='imagetoolbar' content='no'>";
	sHTML += "<script type='text/javascript' src='/scripts/scripts.js'></script>";
	sHTML += "</head><body style='margin:5px 0 0;padding:0;text-align:center;font:12px sans-serif'>";
	sHTML += "<img src='" + oImage.src + "' alt='Product Image' onclick='window.close();'>";
	sHTML += "<p><a href='javascript:window.close();'>Close Window</a></p>";
	sHTML += "</body></html>";

	oWin = window.open("", "", sFeatures);
	oWin.document.open();
	oWin.document.write(sHTML);
	oWin.document.close();
}

function openWindow(sUrl, nWidth, nHeight) {
	var sFeatures = "width=" + nWidth + ", height=" + nHeight + "menubar=yes, toolbar=yes, location=yes, resizable=yes scrollbars=yes";
	
	window.open(sUrl, "_blank", sFeatures);
}

if (window.addEventListener) {
	window.addEventListener("load", init, false);
	document.addEventListener("contextmenu", disableImageClick, false);
} else if (window.attachEvent) {
	window.attachEvent("onload", init);
	document.attachEvent("oncontextmenu", disableImageClick);
}
