/**
 * hml_library.js (2008-oct-28)
 * (c) by Alejandro Gudiel
 * All Rights Reserved
 * License does not permit use by third parties
**/

var localLibraryObject_BrowserInformation = false;
function getBrowserInformation() {
	if (localLibraryObject_BrowserInformation) {
		return localLibraryObject_BrowserInformation;
	}

	var strTMP = navigator.appVersion;

	var intTMP = strTMP.indexOf("(", 0);
	if (intTMP >= 1) strTMP = strTMP.substr(intTMP, strTMP.length);
	var strTMP2 = strTMP.substr(1, strTMP.length - 2);
	var arrTMP = strTMP2.split("; ");

	var objInfo = new Object();

	if (arrTMP[2]) {
		objInfo.OS = JavaScriptTextTrim(arrTMP[2]);

		if (arrTMP[1] == "U") {
			intTMP = strTMP.indexOf(")", intTMP);
			var strExtra = strTMP.substr(intTMP + 1, strTMP.length);
			objInfo.browser = JavaScriptTextTrim(strExtra);
		}
		else {
			objInfo.browser = JavaScriptTextTrim(arrTMP[1]);
		}
	}
	else {
		objInfo.OS = arrTMP[0];
		objInfo.browser = "Unknown";
	}

	objInfo.boolIsWindows = (objInfo.OS.substr(0, 3) == "Win");
	objInfo.boolIsMac = (objInfo.OS.indexOf("Mac", 0) >= 0);
	objInfo.boolIsMSIE = (objInfo.browser.substr(0, 4) == "MSIE");
	objInfo.IEVer = 0;
	if (objInfo.boolIsMSIE) {
		arrTMP = objInfo.browser.split(" ");
		objInfo.IEVer = arrTMP[1];
	}
	objInfo.boolIsChrome = (objInfo.browser.indexOf("Chrome", 0) >= 0);
	objInfo.boolIsSafari = (objInfo.browser.indexOf("Safari", 0) >= 0 && !objInfo.boolIsChrome);
	objInfo.boolIsMozilla = (objInfo.browser.indexOf("Gecko", 0) >= 0 && !objInfo.boolIsSafari && !objInfo.boolIsChrome);

	localLibraryObject_BrowserInformation = objInfo;

	return objInfo;
}

String.prototype.htmlEntities = function () {
   return this.replace(/&/g,'&amp;').replace(/"/g,'&quot;').replace(/'/g,'&#039;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
   //"
};

String.prototype.htmlEntities_decode = function () {
   return this.replace(/&amp;/g,'&').replace(/&quot;/g,'"').replace(/&#039;/g,"'").replace(/&lt;/g,'<').replace(/&gt;/g,'>');
};

/*
function htmlspecialcharsUndo(strVariable) {
	strVariable = strVariable.replace(/&amp;/g,"&");
	strVariable = strVariable.replace(/&quot;/g,"\"");
	strVariable = strVariable.replace(/&#039;/g,"'");
	strVariable = strVariable.replace(/&lt;/g,"<");
	strVariable = strVariable.replace(/&gt;/g,">");

	return strVariable;
}
*/

function addLoadListener(objFunction, toObject) {
	if (!toObject) toObject = window;

	if (typeof toObject.addEventListener != "undefined") {
		toObject.addEventListener("load", objFunction, false);
	}
	else if (toObject == window && typeof document.addEventListener != "undefined") {
		document.addEventListener("load", objFunction, false);
	}
	else if (typeof toObject.attachEvent != "undefined") {
		toObject.attachEvent("onload", objFunction);
	}
	else {
		var hmlOnLoad = toObject.onload;
		if (typeof toObject.onload != "function") {
			toObject.onload = objFunction;
		}
		else {
			toObject.onload = function () {
				hmlOnLoad();
				objFunction();
			}
		}
	}
}

function addUnLoadListener(objFunction) {
	if (typeof window.addEventListener != "undefined") {
		window.addEventListener("unload", objFunction, false);
	}
	else if (typeof document.addEventListener != "undefined") {
		document.addEventListener("unload", objFunction, false);
	}
	else if (typeof window.attachEvent != "undefined") {
		window.attachEvent("onunload", objFunction);
	}
	else {
		var hmlOnLoad = window.onunload;
		if (typeof window.onunload != "function") {
			window.onunload = objFunction;
		}
		else {
			window.onunload = function () {
				hmlOnLoad();
				objFunction();
			}
		}
	}
}

function addResizeListener(objFunction) {
    if (typeof window.addEventListener != "undefined") {
        window.addEventListener("resize", objFunction, false);
    }
    else if (typeof document.addEventListener != "undefined") {
        document.addEventListener("resize", objFunction, false);
    }
    else if (typeof window.attachEvent != "undefined") {
        window.attachEvent("onresize", objFunction);
    }
    else {
        var hmlOnResize = window.onresize;
        if (typeof window.onresize != "function") {
            window.onresize = objFunction;
        }
        else {
            window.onresize = function () {
                hmlOnResize();
                objFunction();
            }
        }
    }
}

function getDocumentLayer(strName, objDoc) {
	var p,i,x=false;

	if(!objDoc) objDoc=document;

	if(objDoc[strName]) {
		x=objDoc[strName];
		if (!x.tagName) x = false;
	}

	if (!x && objDoc.all) x=objDoc.all[strName];
	for (i=0;!x && i<objDoc.forms.length; i++) x=objDoc.forms[i][strName];
	if (!x && objDoc.getElementById) x=objDoc.getElementById(strName);
	for (i=0;!x && objDoc.layers && i<objDoc.layers.length; i++) x=getDocumentLayer(strName,objDoc.layers[i].document);
	//for(i=0;!x && i<objDoc.all.length; i++) if (objDoc.all(i).id == strName || objDoc.all(i).name == strName) x = objDoc.all(i);

	return x;
}

function getImageMapAreaInfo(objElement) {
	var arrReturn = new Array();

	if (!objElement.hmlCoord) {
		objElement.hmlCoord = new Array();
		var arrCoords = objElement.coords.split(",");
		var boolIsX = true;
		var intX1 = 1000000;
		var intY1 = 1000000;
		var intX2 = 0;
		var intY2 = 0;
		for (intKey in arrCoords) {
			arrCoords[intKey] = 1*arrCoords[intKey];
			if (boolIsX) {
				if (arrCoords[intKey] < intX1) intX1 = arrCoords[intKey];
				if (arrCoords[intKey] > intX2) intX2 = arrCoords[intKey];
			}
			else {
				if (arrCoords[intKey] < intY1) intY1 = arrCoords[intKey];
				if (arrCoords[intKey] > intY2) intY2 = arrCoords[intKey]
			}
			boolIsX = (!boolIsX);
		}
		objElement.hmlCoord["left"] = intX1;
		objElement.hmlCoord["top"] = intY1;

		objElement.hmlCoord["width"] = intX2 - intX1;
		objElement.hmlCoord["height"] = intY2 - intY1;
	}

	arrReturn = objElement.hmlCoord;

	return arrReturn;
}

function getObjAbsoluteCoordinates(objElement) {
	var arrReturn = new Array();
	var intLeft = 0;
	var intTop = 0;
	var objTMP = objElement;
	var isInternetExplorer = (navigator.appName.indexOf("Microsoft") != -1);

	if ((objElement.tagName == "area" || objElement.tagName == "AREA") && (!isInternetExplorer || true)) {
		var arrTMP = getImageMapAreaInfo(objElement);
		arrReturn["left"] = arrTMP["left"];
    	arrReturn["top"] = arrTMP["top"];
	}
	else {
		if(objTMP.offsetParent) {
	        while (1) {
	            intLeft += objTMP.offsetLeft;
	            intTop += objTMP.offsetTop;

	            if (!objTMP.offsetParent) break;
	            objTMP = objTMP.offsetParent;
	        }
	    }
	    else if(objTMP.x && objTMP.y) {
	    	intLeft += objTMP.x;
	    	intTop += objTMP.y;
	    }

	    arrReturn["left"] = intLeft;
	    arrReturn["top"] = intTop;
	}

    return arrReturn;
}

function getObjDimentions(objElement) {
	var arrReturn = new Array();
	var isInternetExplorer = (navigator.appName.indexOf("Microsoft") != -1);

	if ((objElement.tagName == "area" || objElement.tagName == "AREA") && (!isInternetExplorer || true)) { //20100624 AG: Probando el true
		var arrTMP = getImageMapAreaInfo(objElement);
		arrReturn["width"] = arrTMP["width"];
    	arrReturn["height"] = arrTMP["height"];
	}
	else {
    	arrReturn["width"] = objElement.offsetWidth;
    	arrReturn["height"] = objElement.offsetHeight;
	}

    return arrReturn;
}

// OJO, no devuelve lo mismo para IE que para FF
function getWindowSize(objDoc) {
	if(!objDoc) objDoc=document;

	var intWidth = 0;
	var intHeight = 0;
	if (parseInt(navigator.appVersion) > 3) {
	 	if (navigator.appName.indexOf("Microsoft")!=-1) {
	 		intWidth = objDoc.documentElement.clientWidth;
	 		intHeight = objDoc.documentElement.clientHeight;
	 		if (intWidth == 0 || intHeight == 0) {
	  			intWidth = objDoc.body.offsetWidth;
	  			intHeight = objDoc.body.offsetHeight;
	 		}
	 	}
	 	else {
	 		intWidth = window.innerWidth;
	  		intHeight = window.innerHeight;
	 	}
	}
	var arrReturn = new Array();
	arrReturn["width"] = intWidth;
    arrReturn["height"] = intHeight;

    return arrReturn;
}

function getScrollInformation(objDoc) {
	if(!objDoc) objDoc=document;

	var arrReturn = new Array();
	arrReturn["width"] = objDoc.body.scrollWidth;
    arrReturn["height"] = objDoc.body.scrollHeight;
    arrReturn["left"] = objDoc.body.scrollLeft;
    arrReturn["top"] = objDoc.body.scrollTop;

    return arrReturn;
}

function JavaScriptTextTrim(str) {
	var whitespace = new String(" \t\n\r");
	var s = new String(str);

	if (whitespace.indexOf(s.charAt(0)) != -1) {
		var j=0, i = s.length;
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1) j++;
		s = s.substring(j, i);
	}
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
		var i = s.length - 1;
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1) i--;
		s = s.substring(0, i+1);
	}
	return s;
}

function deleteSelectOptions(objSelect) {
	//for (i=0; i<objSelect.options.length; i = i) objSelect.remove(i);
	//for (i=0; i<objSelect.children.length; i = i) objSelect.removeChild(objSelect.children[i]);
	objSelect.innerHTML = "";
}

function addOptionToSelect(objDocument, objSelect, strValue, strText, boolSelected, boolOptGroup) {
	if (!boolOptGroup) boolOptGroup = false;

	var objBrowserInformation = getBrowserInformation();

	if (boolOptGroup) {
		if (objBrowserInformation.boolIsMSIE && objBrowserInformation.IEVer < 7) {
			// Si es explorer menor a 7, que no agregue el group y regrese el mismo select para que el codigo de afuera funcione igual...
			optTMP = objSelect;
		}
		else {
			var optTMP = objDocument.createElement("OPTGROUP");
			optTMP.label = strText;

			objSelect.appendChild(optTMP);
		}
	}
	else {
		var optTMP = objDocument.createElement("option");
		optTMP.value = strValue;
		optTMP.text = strText;
		optTMP.label = strText;
		optTMP.selected = boolSelected;

		if (objSelect.tagName == "SELECT" || objSelect.tagName == "select") {
			objSelect.options.add(optTMP);
		}
		else {
			objSelect.appendChild(optTMP);
		}
	}

	return optTMP;
}

function preloadImage(strPath) {
	var imgPreloader = new Image();
	imgPreloader.src = strPath;

	return imgPreloader;
}