function trimJS(cadena) { if(Left(cadena,7).toLowerCase() == "[object") { cadena.value = cadena.value.replace(/^\s*|\s*$/g,""); } else { return cadena.replace(/^\s*|\s*$/g,""); } }
function ltrimJS(cadena) { if(Left(cadena,7).toLowerCase() == "[object") { cadena.value = cadena.value.replace(/^\s+/,""); } else { return cadena.replace(/^\s+/,""); } }
function rtrimJS(cadena) { if(Left(cadena,7).toLowerCase() == "[object") { cadena.value = cadena.value.replace(/\s+$/,""); } else { return cadena.replace(/\s+$/,""); } }

function Left(str, n) { if(n <= 0) { return ""; } else if(n > String(str).length) { return str; } else { return String(str).substring(0,n); } }
function Right(str, n) { if(n <= 0) { return ""; } else if(n > String(str).length) { return str; } else { var iLen = String(str).length; return String(str).substring(iLen, iLen - n); } }

function getElem(id) {
	if (window.parent.document.getElementById) {
		return document.getElementById(id);
	} else if(window.parent.document.all) {
		return document.all(id);
	} else if(window.parent.document.layers) {
		return document.layers(id);
	}
}

function SClass(obj,classNomb) {
	if(Left(obj,7).toLowerCase() != "[object") { obj = getElem(obj); }
	obj.className = classNomb;
}

function getDIVSize(WH,obj) {
	WH = WH.toUpperCase(); if(trimJS(WH) == "") { WH = "W"; }
	if(Left(obj,7).toLowerCase() != "[object") { obj = getElem(obj); }
	if(navigator.appName.indexOf('Microsoft') != -1) {
		if(WH == "W") { return obj.offsetWidth + 10; } else { return obj.offsetHeight + 29; }
	} else if (navigator.appName.indexOf('Netscape') != -1) {
		if(WH == "W") { return obj.offsetWidth + 6; } else { return obj.offsetHeight + 127; }
	}
}

function getWindowSize(WH,obj) {
	if(obj) {
		WH = WH.toUpperCase(); if(trimJS(WH) == "") { WH = "W"; }
		if(window.parent.document.getElementById) {
			if(WH == "W") { return obj.document.body.scrollWidth; } else { return obj.document.body.scrollHeight; }
		} else if(window.parent.document.all) {
			if(WH == "W") { return obj.contentDocument.body.offsetWidth; } else { return obj.contentDocument.body.offsetHeight; }
		}
	}
}

function setWindowSize(obj,type,W,H) {
	if(obj) {
		type = type.toUpperCase();
		if(type == "WINDOW") {
			obj.resizeTo(W,H);
		} else if(type == "IFRAME") {
			if(obj.width) { obj.width = W; } else { obj.style.width = W + "px"; }
			if(obj.height) { obj.height = H; } else { obj.style.height = H + "px"; }
		}
	}
}

function getObjPosition(LT,obj) {
	if(Left(obj,7).toLowerCase() != "[object") { var obj = getElem(obj); }
	LT = LT.toUpperCase();
	if(LT == "L") {
		xPos = obj.offsetLeft;
		tempEl = obj.offsetParent;
		while (tempEl != null) {
	  		xPos += tempEl.offsetLeft;
	  		tempEl = tempEl.offsetParent;
  		}
		return xPos;
	} else if(LT = "T") {
		yPos = obj.offsetTop;
		tempEl = obj.offsetParent;
		while (tempEl != null) {
			yPos += tempEl.offsetTop;
	  		tempEl = tempEl.offsetParent;
  		}
		return yPos;
	} else {
		return 0;
	}
}

function getStyle(obj, styleProp) {
	/*
	 * Returns the specified computed style on an object.
	 * @param {HTMLObject} obj HTML Object
	 * @param {String} styleProp Property name.
	 * @return {Mixed} Computed style on object.
	 */
	if (obj.currentStyle)
		return obj.currentStyle[styleProp];
	else if (window.getComputedStyle)
		return document.defaultView.getComputedStyle(obj,null).getPropertyValue(styleProp);
}

function popUp(page,name,width,height,doFocus,properties) {
	var posTop = screen.availHeight / 2 - (height / 2) - 30;
	var posLeft = screen.availWidth / 2 - (width / 2);
	properties = "status=no, width=" + width + ", height=" + height + ", top=" + posTop + ", left=" + posLeft + ", " + properties;
	var ventana = window.open(page,name,properties);
	if(doFocus){ ventana.focus(); }
}

function checkEnter(event) {
	NS4 = (document.layers) ? true : false;
	var code = 0;
	if (NS4) { code = event.which; } else { code = event.keyCode; }
	if (code == 13) { return true; } else { return false; }
}

function checkLogin() {
	var d = document.loginForm;
	trimJS(d.login); trimJS(d.password);
	if(d.login.value == "") { alert("El campo Usuario no puede estar vacío"); d.login.focus(); return false; }
	if(d.password.value == "") { alert("El campo Contraseña no puede estar vacío"); d.password.focus(); return false; }
	return true;
}

function CheckContent(form,fields,principal,allowNull,where,otros,doFocus) {
	var d = document.forms[form];
	var fieldsArray = fields.split("*");
	var startDisable = false;
	var IDs = new Array();
	for (i=0; i<fieldsArray.length; i++) {
		IDs[i] = d.elements[fieldsArray[i]].options[d.elements[fieldsArray[i]].selectedIndex].value;
		if(startDisable) { d.elements[fieldsArray[i]].options[0].selected = true; }
		if(fieldsArray[i] == principal) { var IDToUse = i; startDisable = true; }
	}
	if(d.elements[principal].type == "select-one") {
		var selectType = true;
		if(!allowNull && d.elements[principal].value*1 == 0) { var hide = true; } else { var hide = false; }
	} else {
		var selectType = false;
		if(!allowNull && d.elements[principal].value.length == 0) { var hide = true; } else { var hide = false; }
	}
	DisableEnableField(form,fields,IDToUse);
	if(!hide) {
		if(selectType && d.elements[fieldsArray[IDToUse+1]].type == "select-one") {
			d.elements[fieldsArray[IDToUse+1]].disabled = true;
			if(where == "abm") { var add_path = "../"; } else { var add_path = ""; }
			document.getElementById("iframe").src = add_path + "inc/_iframe.asp?form=" + form + "&principal=" + principal + "&fields=" + fields + "&id=" + IDs + "&where=" + where + "&focus=" + doFocus + "&otros=" + otros;
		} else {
			if(doFocus) { d.elements[fieldsArray[IDToUse+1]].focus(); }
		}
	}
}

function DisableEnableField(form,fields,IDToUse) {
	var d = document.forms[form];
	var start = false;
	var fields = fields.split("*");
	for (i=IDToUse; i<fields.length; i++) {
		if(d.elements[fields[i]].type == "select-one") {
			if(d.elements[fields[i]].value*1 == 0) { var valor = 0; } else { var valor = 1; }
		} else {
			if(d.elements[fields[i]].value.length == 0) { var valor = 0; } else { var valor = 1; }
		}
		if(valor == 0) {
			if(fields.length > i+1) {
				if(d.elements[fields[i+1]].type == "select-one") { d.elements[fields[i+1]].options[0].selected = true; }
				d.elements[fields[i+1]].disabled = true;
			}
		} else {
			if(d.elements[fields[i]].disabled) { d.elements[fields[i]].disabled = false; }
		}
	}
}

function CheckUncheckAll(form,txtToRemove) {
	var d = document.forms[form];
	if(d.elements["CheckUncheck" + txtToRemove].checked) { d.elements["selectedChk" + txtToRemove].value = 0; }
	var checkbox_cant = d.elements["checks" + txtToRemove].value.split("*");
	for (i=0; i<checkbox_cant.length; i++) {
		var checkbox = eval("getElem('" + txtToRemove + checkbox_cant[i] + "')");
		if (d.elements["CheckUncheck" + txtToRemove].checked) {
			checkbox.checked = true;
		} else {
			checkbox.checked = false;
		}
		CheckUncheckOne(form,txtToRemove,checkbox_cant[i],"all");
	}
	if (d.elements["CheckUncheck" + txtToRemove].checked) {
		d.elements["selectedChk" + txtToRemove].value = d.elements["totalChk" + txtToRemove].value;
	}
}

function CheckUncheckOne(form,txtToRemove,id,from) {
	var d = document.forms[form];
	var checkbox_cant = d.elements["checks" + txtToRemove].value.split("*");
	if (getElem(txtToRemove + id).checked) {
		d.elements["selectedChk" + txtToRemove].value = (d.elements["selectedChk" + txtToRemove].value*1) + 1;
	} else {
		d.elements["selectedChk" + txtToRemove].value = (d.elements["selectedChk" + txtToRemove].value*1) - 1;
	}
	if(from != "all") {
		if(d.elements["selectedChk" + txtToRemove].value*1 == checkbox_cant.length) {
			d.elements["CheckUncheck" + txtToRemove].checked = true;
		} else {
			d.elements["CheckUncheck" + txtToRemove].checked = false;
		}
	}
}

function checkUncheckAllEasy(obj,thisChecked) {
	if(obj.type == undefined && obj.length != 0) {
		for(i=0; i<obj.length; i++) {
			if(thisChecked) { obj[i].checked = true; } else { obj[i].checked = false; }
		}
	} else {
		if(thisChecked) { obj.checked = true; } else { obj.checked = false; }
	}
}

function checkUncheckOneEasy(objAll,obj) {
	if(obj.type == undefined && obj.length != 0) {
		var checkedCont = 0;
		for(i=0; i<obj.length; i++) { if(obj[i].checked) { checkedCont++ } }
		if(obj.length == checkedCont) { objAll.checked = true; } else { objAll.checked = false; }
	} else {
		if(obj.checked) { objAll.checked = true; } else { objAll.checked = false; }
	}
}

function getCheckboxValues(obj,separator) {
	if(obj.type == undefined && obj.length != 0) {
		var objChecked = ""; var cont = 0;
		for(i=0; i<obj.length; i++) {
			if(obj[i].checked) {
				if(cont > 0) { var addSeparator = separator; } else { var addSeparator = ""; }
				cont++;
				objChecked = objChecked + addSeparator + obj[i].value;
			}
		}
		return objChecked;
	} else {
		if(obj.checked) { return obj.value; } else { return ""; }
	}
}

function formatNumber(strValue,cant) {
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+'.'+
		dblValue.substring(dblValue.length-(4*i+3));
	if(cant != '' && !isNaN(cant)) { var decs = ',' + Left(strCents,cant); } else { var decs = ''; }
	return (((blnSign)?'':'-') + dblValue + decs);
}

function checkAno(year) { return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 1 : 0; }
function isDate(year,month,day,fieldName) {
	if(Left(year,7).toLowerCase() == "[object") { var doFocus = true; year = year.value; month = month.value; day = day.value; } else { var doFocus = false; }
	if(!checkAno(year)) { var dias = 28; } else { var dias = 29; }
	if(((month*1 == 4 || month*1 == 6 || month*1 == 9 || month*1 == 11) && day*1 > 30) || (month*1 == 2 && day*1 > dias)) {
		if(fieldName != "") { var add_alert = " en el campo " + fieldName; } else { var add_alert = ""; }
		alert("La fecha ingresada" + add_alert + " es incorrecta");
		if(doFocus) { day.focus(); }
		return false;
	} else {
		return true;
	}
}

function isEmpty(numeric,field,fieldName,doFocus) {
	if(field.type == "text" || field.type == "textarea" || field.type == "file" || field.type == "password") { var type = 1; trimJS(field); alertMsg = "El campo " + fieldName + " no puede estar vacío"; }
	if(field.type == "radio" || field.type == "checkbox" || field.type == "select-one" || field.type == "select-multiple" || (field.type == undefined && field.length != 0)) { var type = 2; alertMsg = "Debe seleccionar al menos una opción"; if(fieldName != "") { alertMsg = alertMsg + " en " + fieldName; } }
	if(numeric || type == 2) {
		if(field.type == undefined && field.length != 0) {
			var cantChecked = 0;
			for(i=0; i<field.length; i++) { if(field[i].checked) { cantChecked++; } }
			if(cantChecked == 0) { alert(alertMsg); return true; } else { return false; }
		} else {
			if(field.value*1 == 0) { alert(alertMsg); if(doFocus) { field.focus(); } return true; } else { return false; }
		}
	} else {
		if(field.value.length == 0) { alert(alertMsg); if(doFocus) { field.focus(); } return true; } else { return false; }
	}
}

function isNumeric(valor,campo,carMinimo,carMaximo,valMinimo,valMaximo,decimal,delFirstsCeros) {
	trimJS(valor);
	var validos = "0123456789";
	valor.value = valor.value.replace(",",".");
	if(delFirstsCeros) { valor.value = valor.value.replace(/^(0)+/,""); }
	if(decimal) { validos = validos + "."; } else { if(valor.value.indexOf(".",0) != -1) { alert("El campo " + campo + " es numérico pero no acepta decimales.\nElimine cualquier punto o coma que haya ingresado."); valor.focus(); return false; } }
	if(Left(valor.value,1) == "-") { var ii = 1; } else { var ii = 0; }
	for(i=ii; i<valor.value.length; i++){ if (validos.indexOf(valor.value.charAt(i)) == "-1") { alert("El campo " + campo + " debe ser numérico"); valor.focus(); return false; } }

	if(valor.value.indexOf(".",0) != -1) {
		if(carMinimo == 0) { carMinimo = 1; }
		if(valor.value.indexOf(".",0) < carMinimo) {
			alert("El valor entero debe tener " + carMinimo + " o más cifras\nen el campo " + campo); valor.focus(); return false;
		} else {
			var cont = 0; var a = 0;
			for(i=0; i<valor.value.length; i++) {
				if(valor.value.indexOf(".",a) != -1) { cont++; a = valor.value.indexOf(".",a)+1; }
				if(cont > 1) { alert("El valor ingresado en el campo " + campo + " es incorrecto"); valor.focus(); return false; }
			}
		}
	} else {
		if ((carMinimo > 0) && (valor.value.length < carMinimo) || (carMaximo > 0) && (valor.value.length > carMaximo)) {
			if(carMinimo > 0 && carMaximo > 0) {
				if(carMinimo == carMaximo) {
					if(carMinimo > 1) { var s = "s"; } else { var s = ""; }
					var txt = "El campo " + campo + " debe tener " + carMinimo + " cifra" + s + " obligatoriamente"; } else { var txt = "El campo " + campo + " debe tener entre " + carMinimo + " y " + carMaximo + " cifras"; } alert(txt); valor.focus(); return false;  } else { if(carMinimo > 1) { var s = "s"; } else { var s = ""; } alert("El campo " + campo + " debe tener un mínimo de " + carMinimo + " cifra" + s); valor.focus(); return false; } }
	}
	if(valMinimo != "") { if(valor.value*1 < valMinimo) { alert("El valor mínimo permitido es de " + valMinimo + " en el campo " + campo); valor.focus(); return false; } }
	if(valMaximo != "") { if(valor.value*1 > valMaximo) { alert("El valor máximo permitido es de " + valMaximo + " en el campo " + campo); valor.focus(); return false; } }
	return true;
}

function isAlphaNumeric(valor,act,txt) {
	var ok = true; var doFocus = false;
	if(Left(valor,7).toLowerCase() == "[object") { doFocus = true; var checkear = valor.value; } else { var checkear = valor; }
	var alfa = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var nros = "0123456789";
	if(act.toUpperCase() == "A") { var validos = alfa; } else if(act.toUpperCase() == "N") { var validos = nros; } else { var validos = alfa + nros; }
	for (i=0; i<checkear.length; i++){
		if(validos.indexOf(checkear.charAt(i)) == "-1") { ok = false; break; }
	}
	if(!ok) { alert(txt); if(doFocus) { valor.focus(); } return false; } else { return true; }
}

function validMail(field,campo,obligatory,multiple) {
	field.value = field.value.toLowerCase(); trimJS(field);
	var ok = true; var good_mails = ""; var toGetPosition = "";
	if(obligatory || field.value != "") {
		for(z=1; z<=2; z++) {
			if(multiple && field.value.indexOf(",", 0) > 0) {
				emailArr = new Array(); emailArr = field.value.split(",");
			} else {
				emailArr = new Array(); emailArr[0] = field.value;
			}
			if(z == 1) {
				for(a=0; a<emailArr.length; a++) {
					if(a > 0) { var add_coma = ", "; } else { var add_coma = ""; }
					if(trimJS(emailArr[a]) != "") { good_mails = good_mails + add_coma + trimJS(emailArr[a]); }
				}
				field.value = good_mails;
			}
		}

		var validos = "abcdefghijklmnopqrstuvwxyz0123456789-_.@";
		for(a=0; a<emailArr.length; a++) {
			emailArr[a] = trimJS(emailArr[a]);
			var arroba = emailArr[a].indexOf("@");
			if(arroba != -1 && emailArr[a].length >= 9) {
				if(emailArr[a].indexOf(".",arroba) == -1) {
					ok = false; break;
				} else {
					for(i=0; i<emailArr[a].length; i++) {
						if(validos.indexOf(emailArr[a].charAt(i)) == -1) { ok = false; break; }
					}
					if(ok) { toGetPosition = toGetPosition + emailArr[a] + ", "; }
				}
			} else {
				ok = false; break;
			}
		}
		if(!ok) {
			if(emailArr.length > 1) { var add_alert = " nro. " + (a+1); } else { var add_alert = ""; }
			alert("La dirección" + add_alert + " proporcionada en el campo " + campo + " no es válida");
			if(multiple) { var pos1 = toGetPosition.length; var pos2 = ((toGetPosition.length)+emailArr[a].length); } else { var pos1 = 0; var pos2 = emailArr[0].length; }
			setSelectionRange(field, pos1, pos2);
			return false;
		}
	}
	return true;
}

function setSelectionRange(textElem,selectionStart,selectionEnd) {
	if(textElem.setSelectionRange) {
		window.setTimeout(function(x,posL,posR) { return function(){x.setSelectionRange(posL,posR);}; } (textElem,selectionStart,selectionEnd),100);
	} else if(textElem.createTextRange) {
		var range = textElem.createTextRange();
		range.collapse(true);
		range.moveEnd('character', selectionEnd);
		range.moveStart('character', selectionStart);
		range.select();
	}
}

function SH(act,ID) {
	if(getElem(ID).tagName == "TR") { var addDisplay = ""; } else { var addDisplay = "block"; }
	if(act == "") {
		if(getElem(ID).style.visibility == "visible" || getElem(ID).style.visibility == "") {
			getElem(ID).style.visibility = "hidden";
			getElem(ID).style.display = "none";
		} else {
			getElem(ID).style.visibility = "visible";
			getElem(ID).style.display = addDisplay;
		}
	} else if(act.toUpperCase() == "S") {
		getElem(ID).style.visibility = "visible";
		getElem(ID).style.display = addDisplay;
	} else if(act.toUpperCase() == "H") {
		getElem(ID).style.visibility = "hidden";
		getElem(ID).style.display = "none";
	}
}

function goTo(url) { document.location.href = url; return false; }

if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) { window.attachEvent("onload", enableAlphaImages); }
function enableAlphaImages() {
	var spacerPath = '../media/img/nada.gif';
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5 && Number(rslt[1]) < 7);
	if (itsAllGood) {
		for (var i=0; i<document.all.length; i++) {
			var obj = document.all[i];
			var bg = obj.currentStyle.backgroundImage;
			var img = document.images[i];
			if (bg && bg.match(/\.png/i) != null) {
				var img = bg.substring(5,bg.length-2);
				var offset = obj.style["background-position"];
				obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img+"', sizingMethod='scale')";
				obj.style.backgroundImage = "url('"+spacerPath+"')";
				obj.style["background-position"] = offset; // reapply
			} else if (img && img.src.match(/\.png$/i) != null) {
				var src = img.src;
				img.style.width = img.width + "px";
				img.style.height = img.height + "px";
				img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='scale')";
			}
		}
	}
}