﻿// Initialise JavaScript namespaces.

Website = {};
Website.Base = {};

Website.Products = {};

Website.isMSIE = (navigator.userAgent.indexOf("MSIE") > -1) ? true : false;
Website.isMSIE6 = (navigator.userAgent.indexOf("MSIE 6") > -1) ? true : false;

Website.Base.Init = function () {
	if (window.name == "TB_iframeContent") {
		if (window.frameElement && $(window.frameElement).get("class") == "tb-transparent") {
			$(window.document.body).addClass("tb-transparent");
		}
		if (window.frameElement && $(window.frameElement).get("class") == "popupDark") {
			$(window.document.body).addClass("popupDark");
		} else {
			$(window.document.body).addClass("popup");
		}
	}

	// Hide the DIV wrapper around generated hidden fields.
	var viewstate = $("__VIEWSTATE");

	if (viewstate) {

		// For some reason, IE6 doesn't parse the DOM correctly.
		if (Website.isMSIE6) {
			var element = viewstate.getParent();

			if (element) {
				element = element.getElement("DIV DIV");
			}

			if (element) {
				element.set("id", "aspHiddenFields");
			}

		} else {
			var parent = viewstate.getParent();

			if (parent.tagName == "DIV") {
				viewstate.getParent().set("id", "aspHiddenFields");
			}
		}
	}
};

Website.Base.InitValidators = function () {
	
};

Website.Base.GetEventKeyCode = function(e) {
	if (window.event) {
		return e.keyCode;
	} else if (e.which) {
		return e.which;
	}
};

Website.Base.GetEventCaller = function(e) {
	if (window.event) {
		return e.srcElement;
	} else if (e.which) {
		return e.target;
	}
};

Website.Base.GetInputs = function(list, names) {
	var nameList = names.split(",");
	var result = [];

	for (var n = 0; n < nameList.length; n++) {
		result[nameList[n]] = null;
	}

	for (var i = 0; i < list.length; i++) {
		var input = list[i];

		if (input && input.id) {
			for (var ii = 0; ii < nameList.length; ii++) {
				var name = nameList[ii];

				if (name && input.id.indexOf(name) > -1) {
					result[name] = input;
				}
			}
		}
	}

	return result;
};

Website.Base.Logout = function() {
	var link = "http://" + window.location.host + window.location.pathname + window.location.search;

	if (link.indexOf("?") < 0) {
		link += "?";
	} else if (link.lastIndexOf("&") != link.length - 1) {
		link += "&";
	}

	link += "logout=true";
	window.location = link;

	return false;
};

window.addEvent("load", Website.Base.Init);

window.addEvent("load", function() {
	ValidatorUpdateDisplay = function(val) {
		if (typeof (val.display) == "string") {
			if (val.display == "None") {
				return;
			}

			if (val.display == "Dynamic") {
				val.style.display = val.isvalid ? "none" : "block";
				return;
			}
		}

		if ((navigator.userAgent.indexOf("Mac") > -1) && (navigator.userAgent.indexOf("MSIE") > -1)) {
			val.style.display = "block";
		}

		val.style.visibility = val.isvalid ? "hidden" : "visible";
	};
});

// Prevent webservice errors from showing up.
window.baseAlert = window.alert;

window.alert = function(obj) {
	var show = true;

	try {
		if (obj.indexOf("The server method") >= 0) { show = false; }
	} catch (e) { }

	if (show) {
		window.baseAlert(obj);
	}
};

// IE6 background image flicker fix.
try { document.execCommand("BackgroundImageCache", false, true); } catch (err) { }
