Utils.prototype.constructor = Utils;

function Utils() {
}


Utils.pageLoad=function(obj_id){

    if(obj_id == undefined){
        obj_id='body';
    }
    if(obj_id!='body' && document.getElementById(obj_id)==null){
        obj_id='body';
    }
    var tmpGALL = new Gallbox();
    document.getElementById(obj_id).style.visibility='visible';
    //var teste = new String();
        tmpGALL.animation.fadeIn(obj_id, null);

    //alert('PageLoad');

}

Utils.pageUnload=function(obj_id){
    if(obj_id == undefined){
        obj_id='body';
    }
    if(obj_id!='body' && document.getElementById(obj_id)==null){
        obj_id='body';
    }

    document.getElementById(obj_id).style.visibility='hidden';
}



Utils.popup = function(htmlNode){
    var a =new Gallbox();
    a.container.majorClassName='popupWindow';
    a.container.navigator=0;
    a.container.autosize=0;
    a.container.obj_id='pop';
    a.container.load();
    a.container.setContent(htmlNode);
    a.container.show();
}
Utils.trim = function(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}

Utils.hasWhiteSpace = function(s) {
    reWhiteSpace = new RegExp(/\s/);
    if (reWhiteSpace.test(s)) {
        testresults = true;
    }
    else {
        testresults = false;
    }
    return (testresults);
}

Utils.isEmailAddr = function(email) {
    var str = email
    var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    if (filter.test(str)) {
        testresults = true;
    }
    else {
        testresults = false;
    }
    return (testresults);
}

Utils.getXMLHttpRequest = function() {

    var httpRequest;
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        httpRequest = new XMLHttpRequest();
    }
    else
        if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                try {
                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e) {
                }
            }
        }

    return httpRequest;
}

Utils.submitAJAXForm = function(formID, callback) {

	var httpRequest = Utils.getXMLHttpRequest();

	httpRequest.onreadystatechange = callback;

	var toSubmit = document.getElementById(formID);
	var inputs = toSubmit.getElementsByTagName('input');
	var texts = toSubmit.getElementsByTagName('textarea');
	var selects = toSubmit.getElementsByTagName('select');
	var requestParameters = '';

	for (i = 0; i != inputs.length; i++) {
		if ((inputs[i].attributes['name']) && (inputs[i].value)) {
			if (/file|text|password/.test(inputs[i].type)) {
				if (requestParameters.length != 0) {
					requestParameters += '&';
				}
				requestParameters += escape(inputs[i].attributes['name'].nodeValue) + '=' + escape(inputs[i].value);
			}
			else if (/radio|checkbox/.test(inputs[i].type)) {
				if (inputs[i].checked) {
					if (requestParameters.length != 0) {
						requestParameters += '&';
					}
					requestParameters += escape(inputs[i].attributes['name'].nodeValue) + '=' + escape(inputs[i].value);
				}
			}
			else if (/hidden/.test(inputs[i].type)) {
				if (requestParameters.length != 0) {
					requestParameters += '&';
				}
				requestParameters += escape(inputs[i].attributes['name'].nodeValue) + '=' + escape(inputs[i].value);
			}
		}
	}
	for (i = 0; i != texts.length; i++) {
		if ((texts[i].attributes['name']) && (texts[i].value)) {
			if (requestParameters.length != 0) {
				requestParameters += '&';
			}
			requestParameters += escape(texts[i].attributes['name'].nodeValue) + '=' + escape(Utils.htmlEntitiesDecode(texts[i].value));
		}
	}
	for (i = 0; i != selects.length; i++) {
		if ((selects[i].attributes['name']) && ((selects[i].selectedIndex != -1))) {
			if (requestParameters.length != 0) {
				requestParameters += '&';
			}
			requestParameters += escape(selects[i].attributes['name'].nodeValue) + '=' + escape(selects[i].options[selects[i].selectedIndex].value);
		}
	}

	var method = toSubmit.method.toUpperCase();
	httpRequest.open(method, toSubmit.action + '&noPage=1', true);
	if (method == 'POST') {
		if (httpRequest.overrideMimeType) {
			httpRequest.overrideMimeType(((navigator.appName.indexOf("Microsoft") != -1) ? 'octetstream' : 'octet-stream') + ';');
		}
		httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	}

	httpRequest.send(requestParameters);
}

Utils.showDiv = function(object, width, height) {
	window.scrollTo(0, 0);

    var objects = document.getElementsByTagName('object');

    //centrar o popup na horizontal
    if (window.innerWidth) {
        document.getElementById(object).style.left = ((window.innerWidth / 2) - (width / 2)) + 'px';
    }
    else {
        document.getElementById(object).style.left = ((document.body.offsetWidth / 2) - (width / 2)) + 'px';
    }

    document.getElementById(object).style.top = '150px';
	document.getElementById('overlay_popup').style.display = 'block';
    document.getElementById(object).style.display = 'block';

    openDiv = object;
}

Utils.hideDiv = function(object) {
    document.getElementById(object).style.display = 'none';

    if (document.getElementById('overlay_popup')) {
        document.getElementById('overlay_popup').style.display = 'none';
    }

    openDiv = null;
}

Utils.marker = window.document.getElementById('bottomMarker');
Utils.background = window.document.getElementById('background');
Utils.body = window.document.getElementById('body');
Utils.originalSize = null;
Utils.resizeTimer = null;

Utils.resize = function() {
	Utils.marker = window.document.getElementById('bottomMarker');
	Utils.background = window.document.getElementById('background');
	Utils.body = window.document.getElementById('body');
	Utils.originalSize = Utils.marker.offsetTop;
	Utils.onResize();
	CSS.addStyles();
	window.document.getElementById('footer').style.visibility = 'visible';
}

Utils.onResize = function() {
	var browserHeight = Utils.getBrowserHeight();
	var browserWidth = Utils.getBrowserWidth();

	if (browserHeight > Utils.marker.offsetTop + 130) {
		window.document.body.style.height = browserHeight + 'px';
		Utils.body.style.height = (browserHeight - 130) + 'px';
	}
	else {
		window.document.body.style.height = (Utils.marker.offsetTop + 120) + 'px';
		Utils.body.style.height = (Utils.marker.offsetTop + 20) + 'px';
	}
	Utils.background.style.top = (browserHeight - 276) + 'px';
	Utils.resizeTimer = setTimeout('Utils.onResize()', 100);
}

Utils.getBrowserHeight = function() {
	var height = 0;
	if (window.innerWidth) {
		height = window.innerHeight;
	}
	else if (window.document.documentElement && window.document.documentElement.clientHeight) {
		height = window.document.documentElement.clientHeight;
	}
	else if (document.body) {
		height = document.body.clientHeight;
	}
	return height;
}

Utils.getBrowserWidth = function() {
	var width = 0;
	if (window.innerWidth) {
		width = window.innerWidth;
	}
	else if (window.document.documentElement && window.document.documentElement.clientWidth) {
		width = window.document.documentElement.clientWidth;
	}
	else if (document.body) {
		width = document.body.clientWidth;
	}
	return width;
}

Utils.getAvaliableHeight = function() {
	var height = 0;
	height = Utils.getBrowserHeight() + (window.document.body.scrollTop ? window.document.body.scrollTop : (window.document.documentElement.scrollTop ? window.document.documentElement.scrollTop : 0));
	return height;
}

Utils.getEventTarget = function(event) {
	var toReturn = null;
	event = (event == null ?  window.event : event);

	if (event == null) {
		return null;
	}

	if (event.target) {
		toReturn = event.target;
	}
	else if (event.srcElement) {
		toReturn = event.srcElement;
	}
	return toReturn;
}

Utils.getEvent = function(event) {
	var toReturn = null;
	toReturn = (event == null ?  window.event : event);
	return toReturn;
}

Utils.getObjectClass = function (obj) {
	if (obj && obj.constructor && obj.constructor.toString) {
		var arr = obj.constructor.toString().match(/function\s*(\w+)/);
		if (arr && arr.length == 2) {
			return arr[1];
		}
	}
	return undefined;
}

Utils.getCSSRules = function() {
	var rules = window.document.styleSheets.item(0);
	return rules.cssRules || rules.rules;
}

Utils.getCSSRule = function(ruleName) {
	var rules = WidgetComponent.getCSSRules();
	for (var i = 0; i < rules.length; i++) {
		if (rules.item(i).selectorText.toLowerCase() == '.' + ruleName.toLowerCase()) {
			return rules.item(i);
		}
	}
}

Utils.normalizePixelValue = function(value) {
	return parseInt(value.replace(/px/g, ''));
}

Utils.htmlEntitiesDecode = function(data){
	data = "" + data;
    data = data.replace(/&(\w+);/g, function(wholematch, match){
        switch (match) {
            case 'nbsp':
                return String.fromCharCode(32);

            case 'iexcl':
                return String.fromCharCode(161);

            case 'cent':
                return String.fromCharCode(162);

            case 'pound':
                return String.fromCharCode(163);

            case 'curren':
                return String.fromCharCode(164);

            case 'yen':
                return String.fromCharCode(165);

            case 'brvbar':
                return String.fromCharCode(166);

            case 'sect':
                return String.fromCharCode(167);

            case 'uml':
                return String.fromCharCode(168);

            case 'copy':
                return String.fromCharCode(168);

            case 'ordf':
                return String.fromCharCode(170);

            case 'laquo':
                return String.fromCharCode(171);

            case 'not':
                return String.fromCharCode(172);

            case 'shy':
                return String.fromCharCode(173);

            case 'reg':
                return String.fromCharCode(174);

            case 'macr':
                return String.fromCharCode(175);

            case 'deg':
                return String.fromCharCode(176);

            case 'plusmn':
                return String.fromCharCode(177);

            case 'sup2':
                return String.fromCharCode(178);

            case 'sup3':
                return String.fromCharCode(179);

            case 'acute':
                return String.fromCharCode(180);

            case 'micro':
                return String.fromCharCode(181);

            case 'para':
                return String.fromCharCode(182);

            case 'middot':
                return String.fromCharCode(183);

            case 'cedil':
                return String.fromCharCode(184);

            case 'sup1':
                return String.fromCharCode(185);

            case 'ordm':
                return String.fromCharCode(186);

            case 'raquo':
                return String.fromCharCode(187);

            case 'frac14':
                return String.fromCharCode(188);

            case 'frac12':
                return String.fromCharCode(189);

            case 'frac34':
                return String.fromCharCode(190);

            case 'iquest':
                return String.fromCharCode(191);

            case 'Agrave':
                return String.fromCharCode(192);

            case 'Aacute':
                return String.fromCharCode(193);

            case 'Acirc':
                return String.fromCharCode(194);

            case 'Atilde':
                return String.fromCharCode(195);

            case 'Auml':
                return String.fromCharCode(196);

            case 'Aring':
                return String.fromCharCode(197);

            case 'AElig':
                return String.fromCharCode(198);

            case 'Ccedil':
                return String.fromCharCode(199);

            case 'Egrave':
                return String.fromCharCode(200);

            case 'Eacute':
                return String.fromCharCode(201);

            case 'Ecirc':
                return String.fromCharCode(202);

            case 'Euml':
                return String.fromCharCode(203);

            case 'Igrave':
                return String.fromCharCode(204);

            case 'Iacute':
                return String.fromCharCode(205);

            case 'Icirc':
                return String.fromCharCode(206);

            case 'Iuml':
                return String.fromCharCode(207);

            case 'ETH':
                return String.fromCharCode(208);

            case 'Ntilde':
                return String.fromCharCode(209);

            case 'Ograve':
                return String.fromCharCode(210);

            case 'Oacute':
                return String.fromCharCode(211);

            case 'Ocirc':
                return String.fromCharCode(212);

            case 'Otilde':
                return String.fromCharCode(213);

            case 'Ouml':
                return String.fromCharCode(214);

            case 'times':
                return String.fromCharCode(215);

            case 'Oslash':
                return String.fromCharCode(216);

            case 'Ugrave':
                return String.fromCharCode(217);

            case 'Uacute':
                return String.fromCharCode(218);

            case 'Ucirc':
                return String.fromCharCode(219);

            case 'Uuml':
                return String.fromCharCode(220);

            case 'Yacute':
                return String.fromCharCode(221);

            case 'THORN':
                return String.fromCharCode(222);

            case 'szlig':
                return String.fromCharCode(223);

            case 'agrave':
                return String.fromCharCode(224);

            case 'aacute':
                return String.fromCharCode(225);

            case 'acirc':
                return String.fromCharCode(226);

            case 'atilde':
                return String.fromCharCode(227);

            case 'auml':
                return String.fromCharCode(228);

            case 'aring':
                return String.fromCharCode(229);

            case 'aelig':
                return String.fromCharCode(230);

            case 'ccedil':
                return String.fromCharCode(231);

            case 'egrave':
                return String.fromCharCode(232);

            case 'eacute':
                return String.fromCharCode(233);

            case 'ecirc':
                return String.fromCharCode(234);

            case 'euml':
                return String.fromCharCode(235);

            case 'igrave':
                return String.fromCharCode(236);

            case 'iacute':
                return String.fromCharCode(237);

            case 'icirc':
                return String.fromCharCode(238);

            case 'iuml':
                return String.fromCharCode(239);

            case 'eth':
                return String.fromCharCode(240);

            case 'ntilde':
                return String.fromCharCode(241);

            case 'ograve':
                return String.fromCharCode(242);

            case 'oacute':
                return String.fromCharCode(243);

            case 'ocirc':
                return String.fromCharCode(244);

            case 'otilde':
                return String.fromCharCode(245);

            case 'ouml':
                return String.fromCharCode(246);

            case 'divide':
                return String.fromCharCode(247);

            case 'oslash':
                return String.fromCharCode(248);

            case 'ugrave':
                return String.fromCharCode(249);

            case 'uacute':
                return String.fromCharCode(250);

            case 'ucirc':
                return String.fromCharCode(251);

            case 'uuml':
                return String.fromCharCode(252);

            case 'yacute':
                return String.fromCharCode(253);

            case 'thorn':
                return String.fromCharCode(254);

            case 'yuml':
                return String.fromCharCode(255);

            case 'quot':
                return String.fromCharCode(34);

            case 'lt':
                return String.fromCharCode(60);

            case 'gt':
                return String.fromCharCode(62);

            case 'amp':
                return String.fromCharCode(38);

        }
        return "";
    });
    return data;
}


Config.prototype.constructor = Config;

function Config() {
	this.urlBase = null;
	this.urlContents = null;
}

Config.appConfig = new Config();

