var oWin, oImg;

var cc_scripts = function() {
	return {
		init: function() {
			cc_scripts.addTextBoxFeatures();
			cc_scripts.suggestToFriendLink();
		},
		addTextBoxFeatures: function() {
			// Add calendar popup feature
			var textboxes = cc_scripts.getElementsByClassName('cal-textbox');
			
			for (var n = 0, el; el = textboxes[n++];) {
				// Instantiate calendar
				cc_cal = new CalendarPopup('cal-div');
				
				// Create calendar link
				var link_el = document.createElement('a');
				var div_el  = document.createElement('div');
				
				link_el.id               = link_el.name = 'cal-' + n;
				link_el.href             = '#';
				link_el.style.marginLeft = "3px";
				link_el.innerHTML        = '<img src="/images/calendar.gif" align="top" border="0" alt="Select a date">';
				
				cc_scripts.addListener(link_el, "click", (function(el, link_id) {return function(e) {cc_cal.select(el, link_id, 'MM/dd/yyyy'); var evt = e || event; if (evt.preventDefault) evt.preventDefault(); else evt.returnValue = false;}})(el, link_el.id));
				
				div_el.id = 'cal-div';
				div_el.style.position = 'absolute';
				div_el.style.backgroundColor = '#fff';
				
				el.parentNode.insertBefore(link_el, el.nextSibling);
				
				el.parentNode.appendChild(div_el);
			}
		},
		suggestToFriendLink: function() {
			var el = document.getElementById('suggest-friend-wrap');
			
			if (el) {
				var el_h2 = document.getElementById('suggest-friend-header');
				
				// Create new anchor element
				var el_a       = document.createElement('a');
				el_a.id        = 'suggest-friend-anchor';
				el_a.href      = 'javascript:void(0);';
				el_a.innerHTML = el_h2.innerHTML;
				
				// Append new anchor and delete old h2
				el.insertBefore(el_a, el_h2);
				el.removeChild(el_h2);
			}
		},
		getElementsByClassName: function(class_name, root) {
			root = typeof(root) != "undefined" ? root : document;
			
			var els       = root.all || root.getElementsByTagName('*');
			var found_els = new Array();
			
			for (var n = 0, el; el = els[n++];) {
				if (el.className == class_name) found_els.push(el);
			}
			
			return found_els;
		},
		addListener: function(el, type, func) {
			if (window.addEventListener) {
				el.addEventListener(type, func, false);
			} else if (window.attachEvent) {
				el.attachEvent("on" + type, func);
			}
		},
		addDOMLoadedListener: function(func) {
			if (document.addEventListener) {
				document.addEventListener('DOMContentLoaded', func, false);
			} else if (document.all && !window.opera) {
				// Use a deferred JavaScript hack
				document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>');
				
				var contentloadtag = document.getElementById("contentloadtag");
				
				contentloadtag.onreadystatechange = function() {
					if (this.readyState == 'complete') setTimeout(func, 0);
				}
			} else {
				// DOM ready isn't possible. Defer to window.onload.
				cc_scripts.addListener(window, 'load', func);
			}
		}
	};
}();

cc_scripts.addDOMLoadedListener(cc_scripts.init);

function init (e) {
	adjustExternalLinks ();
	adjustSearchBox ();
}

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);
}

