﻿// JScript File
function formatTextTime(time)
{
    var hora = time;

    var ahora= new Date(hora);
    var year=ahora.getUTCFullYear();
    var month=parseInt(ahora.getMonth()+1);
    if (String(month).length==1)
    {
        month='0'+month;
    }
    var day=ahora.getDate();
    if (String(day).length==1)
    {
        day='0'+day;
    }
    
    var second=ahora.getSeconds();
    if(String(second).length==1)
    {
        second='0'+second;
    }
    var hour=ahora.getHours();
    if(String(hour).length==1)
    {
        hour='0'+hour;
    }
    
    var minute=ahora.getMinutes();
    if (String(minute).length==1)
    {
        minute='0'+minute;
    }

    
    return year+"-"+month+"-"+day + " " + formatHour(time);
  
    
}

function cargoCurrentDate()
{
    var hora = new Date();
    var curHora= new Date();
    curHora=hora.getTime()+1000*60*60;
    hora=hora.getTime();

    hora=hora+1000*60*60;

    var ahora= new Date(hora);
    var year=ahora.getUTCFullYear();
    var month=parseInt(ahora.getMonth()+1);
    if (String(month).length==1)
    {
        month='0'+month;
    }
    var day=ahora.getDate();
    if (String(day).length==1)
    {
        day='0'+day;
    }
    
    var second=ahora.getSeconds();
    if(String(second).length==1)
    {
        second='0'+second;
    }
    var hour=ahora.getHours();
    if(String(hour).length==1)
    {
        hour='0'+hour;
    }
    
    var minute=ahora.getMinutes();
    if (String(minute).length==1)
    {
        minute='0'+minute;
    }
    var el =document.getElementById('txtTimeStart');
    
    el.value=year+"-"+month+"-"+day + " " + formatHour(curHora);
  
    currentChoosenDate=el.value;
    month=parseInt(month,10);
    month=month-1;
    startTime=new Date(year,parseInt(month),day,hour,minute,second);
}


function formatHour(hour)
 {
    var currentTime = new Date(hour);
    var hours = currentTime.getHours();
    var completeHour=hours;
    var minutes = currentTime.getMinutes();
    if (minutes < 10)
    minutes = "0" + minutes;
    if(hours==0)
    {
        hours=12;
    }
    else
    if (hours>12)
    {
        hours=Math.abs(hours-12 );
    }
    var hora=hours + ":" + minutes + " ";
    if(completeHour > 11){
        
         hora+= "PM";
        }
        else
        {
            hora+="AM";
        }
     
    
    
    
    return hora;
 }
 
 function validateDate()
 {
 
    parsearInputDate();
    var ahora= new Date();
    if((startTime.getTime()>ahora.getTime()+7*24*60*60*1000))
    {
        alert("Warning: Reliable weather forecasts are not yet available for that date.");
        HideLoading();
        return true;
    }
    else if (startTime.getTime()<ahora.getTime())
    {
        alert("Warning: you are traveling to the past!");
        //document.getElementById('txtTimeStart').value=currentChoosenDate;
        HideLoading();
        return false;
    }
    else
    {
        currentChoosenDate=document.getElementById('txtTimeStart').value;
    }
    return true;
    
 }
 
 
 function parsearInputDate()
 {
 var inputedTime=String(document.getElementById('txtTimeStart').value);
    var inputedDate=inputedTime.substring(0,10).replace("-",",");
    var year=inputedTime.substring(0,4);
    var mes= inputedTime.substring(5,7)-1;
    var day=inputedTime.substring(8,10);
    if (day.substring(0,1)=='0')
    {
        day=parseInt(day.substring(1,2));
    }
    else
    {
        day=parseInt(day.substring(0,2));
    }
    var hour=inputedTime.substring(11,13);
    if (hour.substring(0,1)=='0')
    {
        hour=hour.substring(1,2);
    }    
    if (inputedTime.substring(inputedTime.length-2,inputedTime.length)=="PM")
    {
        hour=parseInt(hour)+12 ;
    }
    if (hour==24)
    {
    hour=12;
    }else if (hour==12)
    {
        hour=0;
    }
    var indexMinute=inputedTime.indexOf(":");
    var minute=inputedTime.substring(indexMinute+1,indexMinute+3);
    //inputedDate=inputedDate.replace("-",",");
    startTime=new Date(parseInt(year),parseInt(mes),day,parseInt(hour),parseInt(minute));
    
    
 }