// JavaScript Document
/* PopUps */

function popUp(URL,ancho,alto) {
window.open(URL, '_blank', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=' + ancho + ',height=' + alto);
}

function popUpscroll(URL,ancho,alto) {
window.open(URL, '_blank', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=' + ancho + ',height=' + alto);
}


// This script has been created by Rojadirecta (if you want to use this script check Rojadirecta's Creative Common license)

var mi_array=null;
var globalhuso=null;
var globalformato=null;
function guardaHorario(){
	mi_array=new Array();

	/**
	Se optienen todas la horas y se guardan en un array para su posterior uso.
	*/
	var ie = (document.all) ? true : false;
	var elements = (ie) ? document.all : document.getElementsByTagName('span');
	for (i=0; i<elements.length;i++)
	{
		if(elements[i].className=='t'){
			var a=elements[i].innerHTML;
			var b = toDate(a,"h:m");
			mi_array[i]=b;
		}
	}
	var hoy = new Date();
	globalhuso=hoy.getTimezoneOffset()*(-1);//Se guarda el huso local en una variable global.
		
	/**
	* Se define el formato de la hora segun la region.
	*/
	var formato=formatoRegion(globalhuso);

	/**
	* Se actualiza la hora segun la region.
	*/
	horaHuso(hoy.getTimezoneOffset()*(-1),formato);

	/**
	*Se selecciona el radiobutton correspondiente al formato de la hora.
	*/
	actualizaRadioFormato(formato);
}

/**
*Se selecciona el radiobutton correspondiente al formato de la hora.
*/
function actualizaRadioFormato(formato){
	var ie = (document.all) ? true : false;
	var radios = (ie) ? document.all : document.getElementsByTagName('input');

	for (i=0; i<radios.length;i++)
	{
		if(radios[i].name=='formato')
		{
			if(radios[i].value==formato)
			{
				radios[i].checked=true;
			}
		}
	}
}

/**
* Se define el formato de la hora segun la region.
*/
function formatoRegion(huso){
	var formato="24H";
	if ( (huso == 600) || (huso == 570) || (huso == -300) || (huso
		== -420) || (huso == -480) || (huso == -540) || (huso ==-600) 
		|| (huso ==-660) )
	{
		formato="AMPM";
	}
	return formato;
}
/**
* Funcion que cambia las horas y le da formato segun el huso horario.
*/
function horaHuso(huso,formato){
	/**
	* En caso de que el huso haya cambio, se guarda el nuevo huso
	* y se pone el formato del huso horario. Sino se pone el nuevo formato
	* que viene como parametro.
	*/
	if(globalhuso!=huso){
		globalhuso=huso;
		formato=formatoRegion(huso);
		actualizaRadioFormato(formato);
	}
	globalformato=formato;

	var ie = (document.all) ? true : false;
	var elements = (ie) ? document.all : document.getElementsByTagName('span');
	for (i=0; i<elements.length;i++)
	{
		if(elements[i].className=='t'){
			var b = new Date(mi_array[i]);

			b.setMinutes(b.getMinutes()-(120-huso));
			if(formato=="AMPM"){
				elements[i].innerHTML=formatoAMPM(b);
			}else{
				elements[i].innerHTML=formato24h(b);			
			}
		}
		else if (elements[i].className=='husohorario')
		{
			var mistring = huso/60;
			mistring = '' + huso/60;
			var mas = '\+';
			if (mistring.match(/\-/)) { mas = '' }
			elements[i].innerHTML='UTC/GMT ' + mas + mistring;
		}
	}
}
//Transforma un String con formato Hora a Date.
function toDate(dStr,format) {
	var now = new Date();
	if (format == "h:m") {
 		now.setHours(dStr.substr(0,dStr.indexOf(":")));
 		now.setMinutes(dStr.substr(dStr.indexOf(":")+1));
 		now.setSeconds(0);
 		return now;
	}
		return "Invalid Format";
}

//Transforma Date a String con formato hora AM/PM.
function formatoAMPM(dia)
{
   var now    = dia;
   var hour   = now.getHours();
   var minute = now.getMinutes();
   var ap = "am";
   if (hour   > 11) { ap = "pm";             }
   if (hour   > 12) { hour = hour - 12;      }
   if (hour   == 0) { hour = 12;             }
   if (hour   < 10) { hour   = "" + hour;   }
   if (minute < 10) { minute = "0" + minute; }
   var timeString = hour +
                    ':' +
                    minute +
                    "" +
                    ap;
   return timeString;
} // function formatoAMPM()

//Transforma Date a String con formato hora 24h.
function formato24h(dia){
	var hours = dia.getHours()
	var minutes = dia.getMinutes()
	if (hours  < 10) { hours   = "0" + hours; }
   	if (minutes < 10) {minutes = "0" + minutes; }
	timeString=hours + ":" + minutes;

	return timeString;
}

// Clock Script
var fecha = new Date();
var husodos = fecha.getTimezoneOffset();
var husotres = husodos*(-1000)*60;
function tS(){ x=new Date(tN().getUTCFullYear(),tN().getUTCMonth(),tN().getUTCDate(),tN().getUTCHours(),tN().getUTCMinutes(),tN().getUTCSeconds()); x.setTime(x.getTime()+husotres); return x; } 
function tN(){ return new Date(); } 
function lZ(x){ return (x>9)?x:'0'+x; } 
function tH(x){ if(x==0){ x=12; } return (x>12)?x-=12:x; } 
function dT(){ document.getElementById('tP').innerHTML=eval(oT); setTimeout('dT()',1000); } 
function aP(x){ return (x>11)?'pm':'am'; } 

if ( (husodos == '-600') || (husodos == '-570') || (husodos == 300) || (husodos == 420) || (husodos == 480) || (husodos == 540) || (husodos == 600) || (husodos == 660) )
{
		var oT="tH(tS().getHours())+':'+lZ(tS().getMinutes())+':'+lZ(tS().getSeconds())+aP(tS().getHours())";
}
else {
		var oT="lZ(tS().getHours())+':'+lZ(tS().getMinutes())+':'+lZ(tS().getSeconds())";
}
//if(!document.all){ window.onload=dT(); }else{ dT(); }
// Fin Clock Script
