﻿// 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") == "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();
};

Website.Base.InitValidators = function () {
	var validators = $$(".errorValidator");

	for (var v = 0; v < validators.length; v++) {
		var validator = validators[v];
		var validationElement = $(validator.controltovalidate);
		if (validationElement === null) {
			//no controltovalidate, therefore is non .net validator
			//guess control to validate
			var deepSiblings = validator.getParent().getElements('*');

			var indexOfValidator = deepSiblings.indexOf(validator);

			for (var i = indexOfValidator - 1; i >= 0; i--) {
				if (/^(input)|(select)$/i.test(deepSiblings[i].tagName)) {
					validationElement = deepSiblings[i];
					break;
				}
			}

		}
		if (validationElement !== null && validationElement.getProperty("type") !== 'checkbox') {
			var parentSize = validationElement.getParent().getSize();
			var vars = validationElement.getCoordinates(validator.getParent());
			var right = parentSize.x - (vars.left + vars.width) - 26;
			if (right < 0) {
				right = 0;
			}
			var top = -2;

			validator.setStyle('top', top);
			validator.setStyle('right', right);
		}

		if (!validator.hasAttribute('onmouseover')) {
			validator.removeEvents('mouseover,mouseout');
			validator.addEvent('mouseover', function (val) {
				return function () {
					JSLibrary.ValidatorControlAdapter.Error.OnMouseOver(val);
				};
			} (validator));
			validator.addEvent('mouseout', function (val) {
				return function () {
					JSLibrary.ValidatorControlAdapter.Error.OnMouseOut(val);
				};
			} (validator));
		}
	}
};



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) { }


