﻿

/********************************************************************/

function getobject(obj)
{
    if (document.getElementById)
        return document.getElementById(obj);
    else if (document.all)
        return document.all[obj]; 
}

/********************************************************************/



/********************************************************************/

function clearSelectList(select)
{
	while (select.length > 0)
	    select.remove(0);
}

/********************************************************************/

function populateValidDates()
{
   var target = getobject('ctl00_MainContent_dateOfTravel').value;
   var pickUpTime = getobject('ctl00_MainContent_pickUpTime');
   var pickUpTime2 = getobject('ctl00_MainContent_gdt');

   //alert('test');

/*     if (pickUpTime.options.length > 0) {
        for (var z = 0; z < pickUpTime.options.length; z++)
            pickUpTime.removeChild(pickUpTime.childNodes[z]);
   }


    if (pickUpTime2.options.length > 0) {
        for (var z = 0; z < pickUpTime2.options.length; z++)
            pickUpTime2.removeChild(pickUpTime2.childNodes[z]);
   } */
   clearSelectList(pickUpTime);
   clearSelectList(pickUpTime2);

    if (target != '' && isDate(target))
    {
        var cdate = new Date(target);
        var myDays= ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
        var dayOfWeek = myDays[cdate.getDay()];
        
        var min = new Array('12:00', '12:30', '1:00', '1:30', '2:00', '2:30', '3:00', '3:30', '4:00', '4:30', '5:00', '5:30', '6:00', '6:30', '7:00', '7:30', '8:00', '8:30', '9:00', '9:30', '10:00', '10:30', '11:00', '11:30');
        var wkd = new Array('9:00 A.M.', '9:30 A.M.', '10:00 A.M.', '10:30 A.M.', '11:00 A.M.', '11:30 A.M.', '12:00 P.M.', '12:30 P.M.', '1:00 P.M.', '1:30 P.M.', '2:00 P.M.', '2:30 P.M.', '6:00 P.M.', '6:30 P.M.', '7:00 P.M.', '7:30 P.M.', '8:00 P.M.', '8:30 P.M.', '9:00 P.M.', '9:30 P.M.', '10:00 P.M.','10:30 P.M.','11:00 P.M.');

        var timelist;

        if (dayOfWeek != 'Saturday' && dayOfWeek != 'Sunday')
        {
            var tmp = new Array();

            for (var i = 0; i < wkd.length; i++)
                tmp[i] = wkd[i];

            timelist = tmp;
        }
        else
        {
            timelist = new Array();

            for (var i = 0; i < min.length; i++)
                timelist[i] = min[i] + ' A.M.';

            for (var a = 0; a < min.length; a++)
                timelist[timelist.length] = min[a] + ' P.M.';
        }

        for (var j = 0; j < timelist.length; j++)
        {
            pickUpTime.options[j] = new Option(timelist[j], timelist[j]);
	    	pickUpTime2.options[j] = new Option(timelist[j], timelist[j]);
	    }
    }
}

/********************************************************************/

function openCalendar(frmName, txtName, strDate)
{
	winNew = window.open("/includes/calendar.aspx?formName=" + frmName + "&textBoxName=" + txtName + "&curDate=" + strDate, "winCal", "scrollbars=0,menu=0,height=200,width=156");
	winNew.focus();
}

/********************************************************************/

function isDate(dateStr)
{
    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null)
    {
        alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
        return false;
    }

    month = matchArray[1]; // p@rse date into variables
    day = matchArray[3];
    year = matchArray[5];

    if (month < 1 || month > 12)  // check month range
	{
        alert("Month must be between 1 and 12.");
        return false;
    }

    if (day < 1 || day > 31)
    {
        alert("Day must be between 1 and 31.");
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31)
    {
        alert("Month "+month+" doesn't have 31 days!")
        return false;
    }

    if (month == 2)  // check for february 29th
	{
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        
        if (day > 29 || (day == 29 && !isleap)) {
            alert("February " + year + " doesn't have " + day + " days!");
            return false;
        }
    }

    return true; // date is valid
}

/********************************************************************/

function nextOne(obj)
{
	var count = parseInt(obj.value.length) + 1;
	var p = obj.id.toString().split("_");
	var label = p[2];
	var currID = parseInt(p[3]);
	var num = currID + 1;
	var next = p[0] + "_" + p[1] + "_" + label + "_" + num;

	if (count <= 3)
	{
		getobject(obj.id).focus();	
	}
	else
	{
		getobject(next).focus();
	}
}

/********************************************************************/

window.onload = function() { populateValidDates(); }
