function SH(act,id) {var obj = getElem(id);if(obj) {if(act=="") {if(obj.style.visibility=="hidden" || obj.style.visibility=="") {obj.style.visibility="visible";if(obj.tagName!="TR") { obj.style.display = "block"; } else { obj.style.display = ""; }} else {obj.style.visibility="hidden";obj.style.display="none";}} else if(act.toUpperCase()=="S") {obj.style.visibility = "visible";	if(obj.tagName!="TR") {obj.style.display="block";} else { obj.style.display=""; }} else if(act.toUpperCase()=="H") {obj.style.visibility="hidden"; obj.style.display="none";}}}
//----------------------------------------------------------------
function getElem(id) { if (window.parent.document.getElementById) { return window.parent.document.getElementById(id); } else if (window.parent.document.all) { return window.parent.document.all(id); } else if (window.parent.document.layers) { return window.parent.document.layers(id); } }
//----------------------------------------------------------------
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 changeSource(s) {
if(s.indexOf("*")>1) {var c=s.split("*"); top.cc.location.href=c[0]; document.getElementById("cpro").innerHTML="Provincia > " + c[1]; return true;}
top.location.href=s; return true;}
//----------------------------------------------------------------
function validaEmail(emails) {
trimJS(emails);emails.value = emails.value.toLowerCase();email = new String(emails.value);
if(email == "" || email.indexOf("@", 0) == -1 || email.indexOf(".", 0) == -1 || email.length <= 9) {
alert("La dirección de eMail proporcionada no es válida");
emails.focus();return false;
} else {var ok = 1;var validos = "abcdefghijklmnopqrstuvwxyz0123456789-_@.";
for (i=0;i<email.length;i++) {if (validos.indexOf(email.charAt(i)) == "-1") { ok = 0; }}
if (ok == 0) {alert("La dirección de eMail proporcionada no es válida");emails.focus();return false;}}
return true;
}
//----------------------------------------------------------------
function estaVacio(s,texto){
var len = s.value.length;
for (i=0;i<len;++i){if (s.value.charAt(i)!=" ") {return false;}}
alert(texto); s.focus();return true;
}
//----------------------------------------------------------------
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 loadXML(xmlFile){if (window.ActiveXObject){xmlDoc = new ActiveXObject("Microsoft.XMLDOM");xmlDoc.async=false;xmlDoc.load(xmlFile);return xmlDoc;}
else if (document.implementation && document.implementation.createDocument){
var xmlhttp = new window.XMLHttpRequest();xmlhttp.open("GET",xmlFile,false);xmlhttp.send(null);var xmlDoc=xmlhttp.responseXML.documentElement;return xmlDoc;}else{alert('Su navegador no puede manejar la codificación de los Favoritos.');return false;}}
function verifyXML(){if (xmlDoc.readyState!=4){return false;}}
function favIsValid(){if(document.favs.city.value.length<4){alert('Ingrese por lo menos 4 caracteres');return false;}else{xmlhttpPost('/inc/fav_ajax.asp');SH('S','fav_res');return false;}}
function xmlhttpPost(strURL){var xmlHttpReq=false;var self=this; if(window.XMLHttpRequest){self.xmlHttpReq=new XMLHttpRequest();}
else if (window.ActiveXObject) {self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");}
self.xmlHttpReq.open('POST', strURL, true);self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange=function() {if (self.xmlHttpReq.readyState==4) {updatepage(self.xmlHttpReq.responseText);}}
self.xmlHttpReq.send("a=s&n="+fav_num+"&c="+ escape(document.favs.city.value));}
function updatepage(str){document.getElementById("fav_res").innerHTML = str;}
function setCookie(id,no,ac) {value=(readCookie("ICFavoritos")!=null)?readCookie("ICFavoritos"):"";
if(ac=="a"){if(fav_num>4){alert("Primero elimine alguna ciudad."); return;} if(value.indexOf("*"+id)==-1){value=value+"*"+id+"#"+no;fav_num++;}}
if(ac=="e"){value=value.replace("*"+id+"#"+no,"");fav_num--;}
SH('H','fav_res');var date = new Date();date.setTime(date.getTime()+(365*24*60*60*1000));var expires ="; expires="+date.toGMTString();document.cookie = "ICFavoritos="+value+expires+"; path=/";writeEditFavs();}
function readCookie(name) {var nameEQ = name + "=";var ca = document.cookie.split(';');for(var i=0;i < ca.length;i++) {var c = ca[i];		while (c.charAt(0)==' ') c=c.substring(1,c.length);if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);}return null;}
function writeEditFavs(){cfavs="";if(readCookie("ICFavoritos")!=null){makeFavs();cfavs="<div class=bl><b>Ciudades seleccionadas</b></div>";for(i=1;i<=fav_num;i++){cfavs+="<div class=lo>"+favos[i][1]+"</div><div class=im><img src=\"/media/fav_delete.gif\" width=\"16\" height=\"16\" title=\"Eliminar\" border=\"0\" onclick=\"setCookie('"+favos[i][0]+"','"+favos[i][1]+"','e')\"></div><div class=cl1></div>"}}getElem("fav_cha").innerHTML=cfavs;}
function displayFavs(){var xmlDoc; 
cfavs="";if(readCookie("ICFavoritos")!=null){makeFavs();for(i=1;i<=fav_num;i++){xmlDoc=loadXML('/inc/responses/xmls/'+favos[i][0]+'.xml');temp=xmlDoc.getElementsByTagName('temp').item(0).firstChild.data;hum=xmlDoc.getElementsByTagName('hum').item(0).firstChild.data;idest=xmlDoc.getElementsByTagName('idest').item(0).firstChild.data;cfavs+="<div class=im><img src='http://servicios.nuestroclima.com/m/ic/e1/"+idest+".gif' width=24></div><div class=da><div>"+temp+"&deg;C</div>"+hum+"%</div><div class=lo><a href=/pronosticos/?l="+(favos[i][0]*1)+">"+favos[i][1]+"</a></div><div class=cl1></div>"}}getElem("fav_dat").innerHTML=cfavs;}
function makeFavs(){favos=readCookie("ICFavoritos");if(favos!=null){favos=favos.split("*");for(i=1;i<=fav_num;i++){favos[i]=favos[i].split("#");}} return favos;}
//----------------------------------------------------------------
function blink(c){if (c!=''){eval(c+".innerHTML='<img src=/media/img/orange_arrow_blink.gif width=6 height=9 border=0 vspace=2>'")}} function blinkOff(c){if (c!=''){eval(c+".innerHTML='<img src=/media/img/nada.gif width=6 height=9 border=0 vspace=2>'")}}
//----------------------------------------------------------------
function validaNumeros(valor,texto,minimo, minimotexto){
numero = new String(valor.value)
var ok = 1;var validos = "0123456789";for (i=0;i<numero.length;i++){if (validos.indexOf(numero.charAt(i)) == "-1") ok = 0;}
if (ok == 0) {alert(texto);valor.focus(); return false}
if (numero.length<minimo){alert(minimotexto);valor.focus(); return false}
return true;
}
//----------------------------------------------------------------
function validaNumerosGuion(valor,texto,minimo, minimotexto){
numero = new String(valor.value);var ok = 1;var validos = "0123456789-";
for (i=0;i<numero.length;i++){if (validos.indexOf(numero.charAt(i)) == "-1") ok = 0;}
if (ok == 0) {alert(texto);valor.focus(); return false}
if (numero.length<minimo){alert(minimotexto);valor.focus(); return false}
return true;
}
//----------------------------------------------------------------
var activeDialog = 0;
function OpenDialog(strURL, properties) {
window.onfocus = WindowFocus;
activeDialog = window.open(strURL, "dialog", properties);
return true;
}
function WindowFocus(event) {
if (activeDialog) { try {activeDialog.focus(); }
catch (e) { activeDialog = 0; }
}
}
//----------------------------------------------------------------
function trimJS(cadena) { cadena.value = cadena.value.replace(/^\s*|\s*$/g,""); }
//----------------------------------------------------------------
function isDate(p_Expression){
return !isNaN(new Date(p_Expression));}
//----------------------------------------------------------------
function dateAdd(p_Interval, p_Number, p_Date){
if(!isDate(p_Date)){return "fecha inválida: '" + p_Date + "'";}
if(isNaN(p_Number)){return "número inválido: '" + p_Number + "'";}

p_Number = new Number(p_Number);
var dt = new Date(p_Date);
switch(p_Interval.toLowerCase()){
case "yyyy": {// year
dt.setFullYear(dt.getFullYear() + p_Number);break;}
case "q": {// quarter
dt.setMonth(dt.getMonth() + (p_Number*3));break;}
case "m": {// month
dt.setMonth(dt.getMonth() + p_Number);break;}
case "y":// day of year
case "d":// day
case "w": {// weekday
dt.setDate(dt.getDate() + p_Number);break;}
case "ww": {// week of year
dt.setDate(dt.getDate() + (p_Number*7));break;}
case "h": {// hour
dt.setHours(dt.getHours() + p_Number);break;}
case "n": {// minute
dt.setMinutes(dt.getMinutes() + p_Number);break;}
case "s": {// second
dt.setSeconds(dt.getSeconds() + p_Number);break;}
case "ms": {// second
dt.setMilliseconds(dt.getMilliseconds() + p_Number);break;}
default: {
return "intervalo inválido: '" + p_Interval + "'";}
}
return dt;
}
//----------------------------------------------------------------
function weekdayName(p_Date, p_abbreviate){
if(!isDate(p_Date)){return "fecha inválida: '" + p_Date + "'";}
var dt = new Date(p_Date);
var retVal = dt.toString().split(' ')[0];
var retVal = Array('Domingo','Lunes','Martes','Miércoles','Jueves','Viernes','Sábado')[dt.getDay()];
if(p_abbreviate==true){retVal = retVal.substring(0, 3)}// abbr to 1st 3 chars
return retVal;
}
//----------------------------------------------------------------
function monthName(p_Date, p_abbreviate){
if(!isDate(p_Date)){return "invalid date: '" + p_Date + "'";}
var dt = new Date(p_Date);
var retVal = Array('Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agost','Septiembre','Octubre','Noviembre','Diciembre')[dt.getMonth()];
if(p_abbreviate==true){retVal = retVal.substring(0, 3)}// abbr to 1st 3 chars
return retVal;
}
//----------------------------------------------------------------
function datePart(p_Interval, p_Date, p_firstdayofweek, p_firstweekofyear){
if(!isDate(p_Date)){return "fecha inválida: '" + p_Date + "'";}

var dtPart = new Date(p_Date);
switch(p_Interval.toLowerCase()){
case "yyyy": return dtPart.getFullYear();
case "q": return parseInt(dtPart.getMonth()/3)+1;
case "m": return dtPart.getMonth()+1;
case "y": return dateDiff("y", "1/1/" + dtPart.getFullYear(), dtPart);// day of year
case "d": return dtPart.getDate();
case "w": return dtPart.getDay();// weekday
case "ww":return dateDiff("ww", "1/1/" + dtPart.getFullYear(), dtPart);// week of year
case "h": return dtPart.getHours();
case "n": return dtPart.getMinutes();
case "s": return dtPart.getSeconds();
case "ms":return dtPart.getMilliseconds();// millisecond// 
default: return "intervalo inválido: '" + p_Interval + "'";
}
}
//----------------------------------------------------------------
function dateDiff(p_Interval, p_Date1, p_Date2, p_firstdayofweek, p_firstweekofyear){
if(!isDate(p_Date1)){return "fecha inválida: '" + p_Date1 + "'";}
if(!isDate(p_Date2)){return "fecha inválida: '" + p_Date2 + "'";}
var dt1 = new Date(p_Date1);
var dt2 = new Date(p_Date2);

var iDiffMS = dt2.valueOf() - dt1.valueOf();
var dtDiff = new Date(iDiffMS);
var nYears  = dt2.getUTCFullYear() - dt1.getUTCFullYear();
var nMonths = dt2.getUTCMonth() - dt1.getUTCMonth() + (nYears!=0 ? nYears*12 : 0);
var nQuarters = parseInt(nMonths/3);
var nMilliseconds = iDiffMS;
var nSeconds = parseInt(iDiffMS/1000);
var nMinutes = parseInt(nSeconds/60);
var nHours = parseInt(nMinutes/60);
var nDays  = parseInt(nHours/24);
var nWeeks = parseInt(nDays/7);

// return requested difference
var iDiff = 0;
switch(p_Interval.toLowerCase()){
case "yyyy": return nYears;
case "q": return nQuarters;
case "m": return nMonths;
case "y": // day of year
case "d": return nDays;
case "w": return nDays;
case "ww":return nWeeks;// week of year// 
case "h": return nHours;
case "n": return nMinutes;
case "s": return nSeconds;
case "ms":return nMilliseconds;// millisecond
default: return "intervalo inválido: '" + p_Interval + "'";
}
}

