function Utils() {}

Utils.encodeURL = function(url) {
	return url.replace('&', 'amp;').replace('?', 'ask;');
}

Utils.decodeURL = function(url) {
	return url.replace('amp;', '&').replace('ask;', '?');
}

Utils.formUpdater = function(form, container, url, opts) {
	mtd = (opts == null || opts.method == 'get' ? 'get' : 'post');
	form = document.getElementById(form);
	prms = "{";
	for (i = 0; i < form.elements.length; i++) {
		if (form.elements[i].name != '') {
			if (prms.length > 1)
				prms = prms + "', ";
			prms = prms + form.elements[i].name + ": '" + (form.elements[i].type == 'checkbox' && form.elements[i].checked == false ? '' : form.elements[i].value);
		}
	}
	prms = prms + "'}";
	Utils.execGlobal('params = ' + prms + ';');
	Ajax.Updater(container, url, {method: mtd, parameters: params});
}

// Loads code modules, declaring new objects/functions if any, behaves as executed on page load.
var glbl = this;
Utils.execGlobal = function(code) {
	if (window.execScript) {
		window.execScript(code);
		return null;
	}
	return glbl.eval ? glbl.eval(code) : eval(code);	
}

function Format() {}
Format.isFloat = function(val) {
	if (('' + val).match(/^-{0,1}[0-9]{1,}(\.[0-9]{1,}){0,1}$/))
		return true;
	return false;
}
Format.isInt = function(val) {
	if (('' + val).match(/^-{0,1}[0-9]{1,}$/))
		return true;
	return false;
}
Format.isBool = function(val) {
	if (('' + val).match(/^((true)|(false))$/))
		return true;
	return false;
}
Format.isMail = function(val) {
	if (('' + val).match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/))
		return true;
	return false;
}
Format.isPhoneNum = function(val) {
	if (('' + val).match(/^[0-9 ]{3,}$/))
		return true;
	return false;
}
Format.isFax = function(val) {
	if (('' + val).match(/^[0-9 ]{3,}$/))
		return true;
	return false;
}
Format.isUrl = function(val) {
	if (('' + val).match(/^.{5,}$/))
		return true;
	return false;
}
// dd.mm.yyyy [- :./]
Format.isDate = function(val) {
	if (('' + val).match(/^((0{0,1}[1-9])|((1|2)[0-9])|(3[0-1]))[- :.\/]((0{0,1}[1-9])|(1[0-2]))[- .\/]\d\d\d\d$/))
		return true;
	return false;
}
// hh.mm.ss [- :./]
Format.isTime = function(val) {
	if (('' + val).match(/^(((0|1)[0-9])|(2[0-3]))[- :.\/][0-5][0-9][- :.\/][0-5][0-9]$/))
		return true;
	return false;
}

Format.isDigit = function(val) {
	if (val >= '0' && val <= '9')
		return true;
	return false;
}

// Makes sure that an element value is an int.
Format.checkInt = function(elemId, opts) {
	var elem = document.getElementById(elemId);
	var value = null;
	var positive = false;
	var minvalue = null;
	var maxvalue = null;
	if (elem != null)
		value = elem.value;
	if (opts) {
		positive = opts.positive == true ? true : false;
		minvalue = Format.isInt(opts.minvalue) ? opts.minvalue : null;
		maxvalue = Format.isInt(opts.maxvalue) ? opts.maxvalue : null;
	}
	if (value != null) {
		var st = 0;
		var fn = value.length;
		while(st < fn && value[st] == ' ') st++;
		while(fn > 0 && value[fn-1] == ' ') fn--;
		value = value.substring(st, fn);
		var fz = true;
		for (i = 0; i < value.length; i++) {
			if (value[i] != '0' && value[i] != '-')
				fz = false;
			if (!Format.isDigit(value[i]) || (value[i] == '-' && i == 0) || (fz && value[i] == '0' && i + 1 < value.length)) {
				var vl = value.substring(i+1);
				value = value.substring(0, i) + vl;
				i--;
			}
		}
		if (Format.isInt(value)) {
			if (minvalue != null && parseInt(value) < parseInt(''+minvalue))
				value = minvalue;
			if (maxvalue != null && parseInt(value) > parseInt(''+maxvalue))
				value = maxvalue;
		}
		if (positive && value[0] == '-')
			value = value.substring(1);
		elem.value = value;
	}
}


function Browser() {}
Browser.name = function() {
	if (navigator.userAgent.toLowerCase().indexOf('firefox') != -1)
		return 'firefox';
	else if (navigator.userAgent.toLowerCase().indexOf('chrome') != -1)
		return 'chrome';
	else if (navigator.appName.toLowerCase().indexOf('internet explorer') != -1)
		return 'ie';
	else if (navigator.userAgent.toLowerCase().indexOf('opera') != -1)
		return 'opera';
	else if (navigator.appName.toLowerCase().indexOf('safari') != -1)
		return 'safari';
	else
		return null;
}
Browser.version = function() {
	return parseFloat(navigator.appVersion);
}
//Browser.IE = document.all ? true : false;
//Browser.FF = navigator.userAgent.indexOf("Firefox") != -1;


function Scroll() {}
Scroll.left = function() {
	if (typeof(window.pageXOffset) == 'number')
    	return scrOfY = window.pageXOffset;
	else if (document.body && document.body.scrollLeft)
    	return document.body.scrollLeft;
	else if (document.documentElement && document.documentElement.scrollLeft)
    	return document.documentElement.scrollLeft;
	return 0;
}
Scroll.top = function() {
	if (typeof(window.pageYOffset) == 'number')
    	return scrOfY = window.pageYOffset;
	else if (document.body && document.body.scrollTop)
    	return document.body.scrollTop;
	else if (document.documentElement && document.documentElement.scrollTop)
    	return document.documentElement.scrollTop;
	return 0;
}


function Window() {}
Window.width = function() {
	if (typeof(window.innerWidth) == 'number')
		return window.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth)
		return document.documentElement.clientWidth;
	else if (document.body && document.body.clientWidth)
		return document.body.clientWidth;
	return 0;
}
Window.height = function() {
	if (typeof(window.innerHeight) == 'number')
		return window.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	else if (document.body && document.body.clientHeight)
		return document.body.clientHeight;
	return 0;
}

function Page() {}
Page.width = function() {
	if (window.innerHeight && window.scrollMaxX)// Firefox
		return window.innerWidth + window.scrollMaxX;
	else if (document.body.scrollWidth > document.body.offsetWidth) // all but Explorer Mac
		return document.body.scrollWidth;
	else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		return document.body.offsetWidth;
}
Page.height = function() {
	if (window.innerHeight && window.scrollMaxY)// Firefox
		return window.innerHeight + window.scrollMaxY;
	else if (document.body.scrollHeight > document.body.offsetHeight) // all but Explorer Mac
		return document.body.scrollHeight;
	else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		return document.body.offsetHeight;
}



function Elem() {}
Element.width = function(elem) {
	if (document.layers)
		return elem.clip.width;
	else if (navigator.userAgent.indexOf("Opera") != -1)
		return elem.style.pixelWidth;
	else
		return elem.offsetWidth;
}
Element.height = function(elem) {
	if (document.layers)
		return elem.clip.height;
	else if (navigator.userAgent.indexOf("Opera") != -1)
		return elem.style.pixelHeight;
	else
		return elem.offsetHeight;
}

// Mouse position handling.
function Mouse() {}
Mouse.x = 0;
Mouse.y = 0;

Mouse._mousemoveevent = function(e) {
	try {
		if (document.all ? true : false) { // grab the x-y pos.s if browser is IE
			Mouse.x = event.clientX + Scroll.left(); //document.body.scrollLeft;
			Mouse.y = event.clientY + Scroll.top(); //document.body.scrollTop;
		} else {  // grab the x-y pos.s if browser is NS
			Mouse.x = e.pageX;
			Mouse.y = e.pageY;
		} 
	} catch (ex) {
		Mouse.x = 0;
		Mouse.y = 0;
	}
	
	if (Mouse.x < 0) Mouse.x = 0;
	if (Mouse.y < 0) Mouse.y = 0;
	return true;
}

if (document.addEventListener) {
	document.addEventListener("mousemove", Mouse._mousemoveevent, false);
} else if (document.attachEvent) {
	document.attachEvent("onmousemove", Mouse._mousemoveevent);
}



function execCommand(formName, url) {
    result = null;
	form = document.forms.namedItem(formName);
	
	if (form == null)
	    return false;
	    
	for (element in form.elements)
        if (element.value != null && element.value != "") {
            if (result == null)
                result = element.name + "=" + element.value;
            else
                result += "&" + element.name + "=" + element.value;
        }
        
    result = url + "?" + result;
    
    resCont = document.getElementById("exec_cmd_cont");
    if (resCont == null) {
        resCont = document.createElement('div');
        resCont.id = "exec_cmd_cont";
        resCont.style.display = "none";
    }
    
    Ajax.Updater("exec_cmd_cont", result);
    return false;
}

