var oWin, oImg;

function init (e) {
	adjustExternalLinks ();
	adjustSearchBox ();
}

function disableFinishButton() {
	document.getElementById("btnFinish").disabled = true;
}

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 (oSearchBox) {
		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);
}
