// JavaScript Document for live clock
var curTime="timeZone(timeHour(timeSet().getHours()))+':'+timeZone(timeSet().getMinutes())+' '+afterNoon(timeSet().getHours())";

function timeNow()
{ 
  return new Date(); 
} 

function fullDate(day,minute,hour,dayLight)
{ 
    var week;
    var numMin;
    var date;
    var dOff=0;
    
    if(dayLight < 0)
    {
    	numMin=minute+1;
    	week=7*(dayLight+1);
    }
    else
    {
    	numMin=minute;
    	week=7*(dayLight-1);
    } 
    date=new Date(timeNow().getUTCFullYear(),numMin,1,hour,0,0);
    if(dayLight < 0)
    { 
      date.setTime(date.getTime()-86400000); 
    } 
    if(date.getDay() != day)
    { 
        if(date.getDay() < day)
        {
        	dOff=(day-date.getDay());
        }
        else
        {
        	dOff=0-(date.getDay()-day);
        } 
        
        if(dayLight < 0 && dOff > 0)
        { 
          week-=7; 
        } 
        if(dayLight > 0 && dOff < 0)
        { 
          week+=7; 
        } 
        date.setTime(date.getTime()+((dOff+week)*86400000)); 
    } 
    return date; 
} 

function dayLightSaving()
{ 
//  Daylight savings started 2nd Sunday in March instead of 1st Sunday in April. - Ben T	
    //Passing new parameters to fullDate() for 2nd Sunday in March and 1st Sunday in Nov solves the problem
    //if(((timeNow().getTime() > fullDate(0,3,3,1).getTime()) && (timeNow().getTime() < fullDate(0,9,3,-1).getTime())))
    if(( (timeNow().getTime() > fullDate(0,2,3,2).getTime()) && (timeNow().getTime() < fullDate(0,10,3,1).getTime()) ))
  	{
  		return 3600000;
  	}
  	else
  	{
  		return 0;
  	}
} 

function timeSet()
{ 
    var newDate=new Date(timeNow().getUTCFullYear(), timeNow().getUTCMonth(),timeNow().getUTCDate(),timeNow().getUTCHours(),timeNow().getUTCMinutes(),timeNow().getUTCSeconds()); 
    newDate.setTime(newDate.getTime()+dayLightSaving()-25200000); 
    return newDate; 
} 

function timeZone(time)
{ 
  if(time > 9)
  {
  	return time;
  }
  else
  {
  	return '0' + time;
  }
} 

function timeHour(hours)
{ 
    if(hours == 0)
    { 
      	hours=12; 
    }
    
    if(hours > 12)
    {
    	return hours-=12;
    }
    else
    {
    	return hours;	
    } 
} 

function writeDate()
{ 
  document.getElementById('currentTime').innerHTML="Hora actual de Monta&ntilde;a U.S. "+eval(curTime);//+" MT"; 
  setTimeout('writeDate()',1000); 
} 

function afterNoon(hours)
{ 
  if(hours > 11)
  {
	return 'pm';
  }
  else
  {
  	return 'am';
  }
} 
