/********************************************************************
 *
 * Manages Map and Validation Tools 
 * Author: TTI, Gabriel Torres
 * Date:   January 25, 2008
 *
 *******************************************************************/

//gt; display wait animated gif
function showProgressUpd(name)
{
    //path to the divProgress element
    try {
        document.parentWindow.parent.document.getElementById(name).style.display = "";
    }
    catch(e) {
    }
}

//gt: hide wait animated gif
function hideProgressUpd(name)
{
    //path to the divProgress element
    try {
        window.parent.document.getElementById(name).style.display="none";
        document.parentWindow.parent.document.getElementById(name).style.display = "none";
        
    }
    catch(e) {
    }
}

//gt: open a html page in a new browser window
function OpenWindow(sPage, sName)
{
    window.open(sPage, sName,
        'height=430,width=500,resizable=1,scrollbars=1,titlebar=0,menubar=0,toolbar=0,location=0,directories=0,status=0',false);
}

//gt: verify date format
function ValidateDate(sDate)
{   
    var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
    var bResult = false;
    if ((sDate.match(RegExPattern)) && (sDate!='')) 
        bResult = true;
    return bResult;
}

//gt: verify date format
function ValidateTXTDate(sender, args)
{   
    args.IsValid = false;    
    if (ValidateDate(args.Value)) {
        var dtInput = new Date(args.Value);
        var dtToday = new Date();        
        if (dtInput<=dtToday)  //verify date is not in the future
            args.IsValid = true;
    }        
}

//gt: verify date format and start date is before than end date (only if both dates have been provided)
function ValidateStartEndDate(sender, args)
{   
    args.IsValid = false;    
    if (ValidateDate(args.Value)) {
        var dtInput = new Date(args.Value);
        var dtToday = new Date();        
        if (dtInput<=dtToday) { //verify date is not in the future
            if (ValidateDate(document.getElementById('txtStartDate').value) && 
                ValidateDate(document.getElementById('txtEndDate').value)) { //check both date fields have been entered
                var dtStartDate = new Date(document.getElementById('txtStartDate').value);
                var dtEndDate = new Date(document.getElementById('txtEndDate').value);
                if (dtStartDate<=dtEndDate) //verify start date is before end date
                    args.IsValid = true;
            }
            else 
                args.IsValid = true;
        }
    }        
}

//gt: verify start hour is less than end hour
function ValidateStartEndHour(sender, args)
{   
    if (parseInt(document.getElementById('ddlStartHour').value) <= 
        parseInt(document.getElementById('ddlEndHour').value))
        args.IsValid = true;
    else
        args.IsValid = false;
}

//gt: return a random number
function GetRan()
{
    var sRes = "";
    var dtNow = new Date();    
    sRes = dtNow.getYear() + "_" + dtNow.getMonth() + "_" + dtNow.getDate() + "_";
    sRes += dtNow.getHours() + "_" + dtNow.getMinutes() + "_" + dtNow.getSeconds() + "_";
    sRes += Math.floor(Math.random()*1000);
    return sRes;
}

//gt: change z-index value of the report div to display calendar appropriately
function ShowCalendar()
{
    //alert(document.getElementById('divReportSection').style.zIndex);
    document.getElementById('divReportSection').style.zIndex=-1;
}

//gt: change z-index value of the report div to display reports appropriately
function HideCalendar()
{
    //alert('Hide');
    document.getElementById('divReportSection').style.zIndex=0;
}

//gt: 
function ShowTravelTime(sId, sMsg)
{
    alert(sMsg);
    document.getElementById(sId).value = sMsg;
}