﻿// 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");
            }
        }
    }

    var validators = $$(".errorValidator");

    for (var v = 0; v < validators.length; v++) {
        var validator = validators[v];
        var input = validator.getPrevious("input,textarea");
        var select = validator.getPrevious("select");
        var inputArea = (input != null) ? input : select;
        if (inputArea && inputArea.getProperty("type") != 'checkbox') {
            var parentSize = inputArea.getParent().getSize();
            var vars = inputArea.getCoordinates(inputArea.getParent());
            var right = parentSize.x - (vars.left + vars.width) - 26;

            validator.setProperty("style", validator.getProperty("style") + "; right: " + right + "px;");
        }
    }
};

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;
};

Website.Base.ToggleElement = function (elementId) {
	if ($(elementId).getStyle("display") == "none") {
		$(elementId).setStyle("display", "block");
		return true;
	}
	else {
		$(elementId).setStyle("display", "none");
		return false;
	}
}

Website.Base.ClearTextBox = function (element, defaultText) {
	if ($(element).value == defaultText) {
		$(element).value = "";
	}
}

Website.Base.SubmitFormByCorrectButton = function (e, sender) {

	var caller = Website.Base.GetEventCaller(e);

	if (Website.Base.GetEventKeyCode(e) == 13 && caller.type.toLowerCase() != "textarea") {

		var elements = sender;
		var clicked = false;

		if (!clicked) {
			var callerFound = false;
			for (var i = 0; i < elements.length; i++) {
				var element = elements[i];
				var type = element.tagName;

				if (element.id == caller.id) { callerFound = true; }
				if (type) { type = type.toLowerCase(); }

				if (type == 'input') {
					type = (element.type).toLowerCase();
				}

				if (callerFound && (type == "image" || type == "submit" || type == "a")) {
					if (type == "a") {
						element.onclick();
						clicked = true;
						break;
					}
					else {
						element.click();
						clicked = true;
						break;
					}
				}
			}

			return false;
		}

		return false;
	}

	return true;
}

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;
	}
}
	

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) { }


