/*  Author Adam Ford
 * ---------------------------------------------------------------------------
 * Calendar Functions
 * 
 */

function getMonthName(num,dispType)
{
   var months = new Array(13);
   months[0]  = "January";
   months[1]  = "February";
   months[2]  = "March";
   months[3]  = "April";
   months[4]  = "May";
   months[5]  = "June";
   months[6]  = "July";
   months[7]  = "August";
   months[8]  = "September";
   months[9]  = "October";
   months[10] = "November";
   months[11] = "December";
   if (dispType=="Short") {
        return months[num].substring(0,3);
   } else {
        return months[num];
   }
} // function getMonthName()

// note that this functions contents is TIGHTLY COUPLED with form, and its controls ID names
/*function func_Choose_MOT() {
    var docForm=document.forms['tripRequest'];
    var MOT=document.forms['tripRequest'].incMOD_select.options[document.forms['tripRequest'].incMOD_select.selectedIndex].value;
    if (MOT=="incMOD_Type") {
        docForm.inclMOT_1.checked=false; //Metro Train
        docForm.inclMOT_4.checked=false; //Metro Tram
        docForm.inclMOT_5.checked=false; //Metro Bus
        docForm.inclMOT_0.checked=true; //V/line Train
        docForm.inclMOT_6.checked=true; //Regional Coach/Bus
        docForm.inclMOT_3.checked=false; //Melbourne Airport Bus (Skybus)
    } else {
        docForm.inclMOT_1.checked=true; //Metro Train
        docForm.inclMOT_4.checked=true; //Metro Tram
        docForm.inclMOT_5.checked=true; //Metro Bus
        docForm.inclMOT_0.checked=true; //V/line Train
        docForm.inclMOT_6.checked=true; //Regional Coach/Bus
        docForm.inclMOT_3.checked=true; //Melbourne Airport Bus (Skybus)
    }
}*/

// call at bottom of form
function func_init () {
    //var theForm = document.forms['tripRequest'];
    var thisdate = new Date();
    var yr = thisdate.getFullYear();
    var mth = thisdate.getMonth();
    mth_pad = ((mth < 10) ? '0' + mth : mth) // pad month with zero where necessary
    var date = thisdate.getDate();
    date_pad = ((date < 10) ? '0' + date : date)// pad date 
    var dt=date;
    var hr = thisdate.getHours();
    var min = thisdate.getMinutes();
    var sec = thisdate.getSeconds();

    var yrmth=''+yr+mth_pad;
    /*document.writeln('Year: '   + yr )
    document.writeln('Month: '+ mth )
    document.writeln('Date: '+ date )
    document.writeln('Hours: '+ hr )
    document.writeln('Minutes: '+ min )
    document.writeln('Seconds: '+ sec )
    document.writeln('Yrmth: '+ yrmth )*/
    var ap = "AM";
    var hr_disp=hr;
    if (hr   > 12) { hr_disp = hr - 12; }
    if (hr   == 0) { hr_disp = 12;        }
    if (hr   > 11) { ap = "PM";        }
    ////
    itdDateDay=document.getElementById('itdDateDay');
    itdDateDay.options[date - 1].selected = true;
    ////
    // Time section
    //set am or pm
    var itdTimeAMPM = document.getElementById("itdTimeAMPM");
    if (ap=="AM") {
        itdTimeAMPM.options[0].selected=true;
    } else {
       itdTimeAMPM.options[1].selected=true; 
    }
    // Set Hours
    var itdTimeHour = document.getElementById("itdTimeHour");
    for (i=0;i<itdTimeHour.options.length;i++)
    {
        var current = itdTimeHour.options[i];
        if (current.value==hr_disp) {
            current.selected=true;
        }
    }
    // Set Minutes
    var itdTimeMinute = document.getElementById("itdTimeMinute");
    for (i=0;i<itdTimeMinute.options.length;i++)
    {
        var current = itdTimeMinute.options[i];
        if (current.value>min) {
            if (current.value<=min+5) {
                current.selected=true;
            }
        }
    }
}

function func_validate_from() {
    // if origin 
    var name_origin=document.getElementById("name_origin");
    var name_destination=document.getElementById("name_destination");
    var err_message="";
    var name_origin_val=name_origin.value.replace(/^\s*|\s*$/g,'');;
    var name_destination_val=name_destination.value.replace(/^\s*|\s*$/g,'');;
    //name_destination=name_destination.replace(/^\s*|\s*$/g,'');
    //.replace(/^\s*|\s*$/g,'');
    if (name_origin_val=="Enter Origin" || name_origin_val=="") {
        err_message=err_message+'Enter value for "Origin" field\n';
        name_origin.focus();
    }
    if (name_destination_val=="Enter Destination" || name_destination_val=="") {
        err_message=err_message+'Enter value for "Destination" field\n';
        name_destination.focus();
    }
    var itdDateDay=document.getElementById("itdDateDay");
    var itdDateYearMonth=document.getElementById("itdDateYearMonth");
    var itdTimeHour=document.getElementById("itdTimeHour");
    var itdTimeMinute=document.getElementById("itdTimeMinute");
    var itdTimeAMPM=document.getElementById("itdTimeAMPM");
    //alert (thisdate.toString()); 
    //thisdate.
    //var myDate=new Date()
    //var itdYear=itdDateYearMonth.value.substring(0,4);
    //var itdMonth=itdDateYearMonth.value.substring(5);
    var itdYear=itdDateYearMonth.value.substring(0,4);
	
	// Check Month - if Oct, Nov,Dec use 10,11,12
	// Check Month - if before Oct use 1,2,3,4,5,6,7,8,9	
    var itdMonth=itdDateYearMonth.value.substring(4);		
    
    
    var itd24Hour=itdTimeHour.value
   
    if (itdTimeAMPM.value=="pm") {
        itd24Hour=parseInt(itd24Hour)+12;
    }
     //myDate.setHours(itd24Hour);
    //myDate.setMinutes(itdTimeMinute.value);
    //myDate.setFullYear(2006,4,itdDateDay.value)
    //new Date ( year, month, date, hour, minute, second )
    myDate = new Date(itdYear,itdMonth-1,itdDateDay.value,itd24Hour,itdTimeMinute.value);
    myDate.setMinutes(myDate.getMinutes()+10);
    //alert (" year:"+itdYear+' month:'+(itdMonth-1)+' date:'+itdDateDay.value+' 24Hour:'+itd24Hour+' min:'+itdTimeMinute.value);
    //alert (itdDateYearMonth.value+' year:'+itdYear+' month:'+itdMonth+' day:'+itdDateDay.value+' hour:'+itd24Hour+' ampm:'+itdTimeAMPM.value+' min:'+itdTimeMinute.value+' mydate:'+myDate.toDateString()+ ' thisdate:'+thisdate.toString());
    if (myDate<thisdate) {
        var newDate = new Date();
        var newHours = newDate.getHours();
        var newAMPM = "AM";
        if (newHours   > 12) { newHours = newHours - 12; newAMPM="PM" }
        err_message=err_message+'Choose a date-time that is not in the past\n(It is now:'+newDate.toDateString()+' '+newHours+':'+newDate.getMinutes()+' '+newAMPM+')';
        itdDateDay.focus();
    }
    
    // check that combination of month/year, date and time is no earlier than the present
    
    /*if (itdDateDay.value>date) {
        err_message=err_message+'Day field must be today or later<br />';
    }
    if (itdDateDay.value>date) {
        err_message=err_message+'Month/Year field must be this month or later<br />';
    }
    if (itdDateDay.value>date) {
        err_message=err_message+'Month/Year field must be this month or later<br />';
    }*/
    
    if (err_message!="") {
        alert("Warning:\n"+err_message);
        return false
    } else {
        //alert("test - true");
        // redirect form output to a new window
        var w = window.open('calendar_resources/PleaseWait.html','Metlink_Journey_Planner','');//width=500,height=300,toolbar=0
        //document.forms['tripRequest'].target = 'Metlink_Journey_Planner';
        
        // NOT DONE! - get new window in focus no matter what!
        
        return true;
    }
}
/*
// No need to have direction
function jp_change_direction() {
	var span_direction_1_to=document.getElementById('span_direction_1_to');
	var span_direction_1_from=document.getElementById('span_direction_1_from');
	var span_direction_2_to=document.getElementById('span_direction_2_to');
	var span_direction_2_from=document.getElementById('span_direction_2_from');
	var radio_to=document.getElementById('radio_to');
	var radio_from=document.getElementById('radio_from');

	if (radio_to.checked==true) {
		span_direction_1_to.style.display='none';
		span_direction_1_from.style.display='block';
		span_direction_2_to.style.display='block';
		span_direction_2_from.style.display='none';		
	} else {
		span_direction_1_to.style.display='block';
		span_direction_1_from.style.display='none';
		span_direction_2_to.style.display='none';
		span_direction_2_from.style.display='block';
	}
	return 1;
}*/


function disallowDate(date) {
  // date is a JS Date object
  if (  (date.getFullYear() <= year && date.getMonth() < month) ||
        (date.getFullYear() >=  endYear && date.getMonth() >= month) )
  {
    return true;
  }
  return false; // enable other dates
};

/*
//probably don't need now as I am building a string to do all this wonderfull hacky stuff
function changeDate(){
  
  var DaySelectElement = document.getElementById(itdDateDay);
  var YearMonthSelectElement = document.getElementById(itdDateYearMonth);
  var calDate = document.getElementById(calDate);
  calDate.value = YearMonthSelectElement.value.substr(0,4) +'-' + YearMonthSelectElement.value.substr(4,2) + '-' + DaySelectElement.value;
}*/

function onSelect(cal) {

  var selDay = cal.date.getDate();
  var selYearMonth = cal.date.print('%Y%m');


  // Set the day Select
  var DaySelectElement = document.getElementById("itdDateDay");
  DaySelectElement.selectedIndex = selDay - 1;

  // Set the month Select
  var YearMonthSelectElement = document.getElementById("itdDateYearMonth");

  for (i=0;i<YearMonthSelectElement.options.length;i++)
  {
    if (YearMonthSelectElement.options[i].value == selYearMonth)
    {
      YearMonthSelectElement.selectedIndex = i;
      break;
    }
  }

  var p = cal.params;
  var update = (cal.dateClicked || p.electric);
  if (update && p.inputField) {
    p.inputField.value = cal.date.print(p.ifFormat);
    if (typeof p.inputField.onchange == "function")
      p.inputField.onchange();
  }
  if (update && p.displayArea)
    p.displayArea.innerHTML = cal.date.print(p.daFormat);
  if (update && typeof p.onUpdate == "function")
    p.onUpdate(cal);
  if (update && p.flat) {
    if (typeof p.flatCallback == "function")
      p.flatCallback(cal);
  }
  if (update && p.singleClick && cal.dateClicked)
    cal.callCloseHandler();
};


function sendme(myForm) 
{ 
    window.open("","myNewWin","width=500,height=300,toolbar=0"); 
    var a = window.setTimeout("document.froms[0].submit();",500); 
    
} 