// JavaScript Document

// setup global cars
	var cars= new Array();
	
	cars[0]=new Array(2);
	cars[0][0]='Citroen C1';
	cars[0][1]=5.99;
	cars[0][2]=49.99;
	
	cars[1]=new Array(2);
	cars[1][0]='Citroen C3';
	cars[1][1]=6.49;
	cars[1][2]=54.49;
	
	cars[2]=new Array(2);
	cars[2][0]='Citroen C4';
	cars[2][1]=6.99;
	cars[2][2]=58.49;
	
	cars[3]=new Array(2);
	cars[3][0]='Citroen Picasso';
	cars[3][1]=6.99;
	cars[3][2]=58.49;
	
	cars[4]=new Array(2);
	cars[4][0]='Citroen Berlingo';
	cars[4][1]=7.49;
	cars[4][2]=62.49;
	
	cars[5]=new Array(2);
	cars[5][0]='Toyota Prius';
	cars[5][1]=6.99;
	cars[5][2]=58.49;
	
// Global flag to mark trip as eligable for weekend special (9-7-09)
	var weekendSpecial=false,
		weekendSpecialDiscount=0; 
	
// Global variables
	var DOW1, // stores the day of the week a booking starts on
		DOW2; // stores the day of the week a booking ends on
function PrintCars(){
	var x;
	
	for (x=0;x<cars.length;x++)
		document.writeln('<option value="' + x + '">' + cars[x][0] +'</option>');
}

function PrintMonth(){
	var x;
	
	for (x=1;x<=12;x++)
		document.writeln('<option value="' + x + '">' + x +'</option>');
}

function PrintDay(){
	var x;
	
	for (x=1;x<=31;x++)
		document.writeln('<option value="' + x + '">' + x +'</option>');
}

function PrintHour(){
	var x;
	
	for (x=1;x<=12;x++)
		document.writeln('<option value="' + x + '"' + ((x==12)?'selected':'') + '>' + x +'</option>');
}

function PrintMinute(){
	var x;
	
	for (x=0;x<=45;x+=15)
		document.writeln('<option value="' + x + '">' + x +'</option>');
}

function prnerr(errstr){
	document.getElementById('errors').innerHTML+='-&nbsp;' +errstr +'<br>';
}

function calcTrip(lob,cost,cph,miles,total,over,estm,scar){
	var hours = 0,
		tlob = 0,
		tcost = 0,
		tcph = 0,
		tmiles = 0,
		tover = document.getElementById(estm).value,
		dstlob=document.getElementById(lob),
		dstcost=document.getElementById(cost),
		dstcph=document.getElementById(cph),
		dstmiles=document.getElementById(miles),
		dstover=document.getElementById(over),
		dsttotal=document.getElementById(total),
		car=document.getElementById(scar).value;
		
	diff=dateDiff();
	
	// calculate the total hours
	tlob=nextNearest(diff / 60, .5);
	
	hours=tlob;
	
	// CHECK FOR WEEKEND SPECIAL ELIGABLILITY (9-7-09)
	//  if booking starts between 7pm Friday and 7am Sunday and is a 24hr booking or
	//  if booking starts between 7pm Friday and 7am Saturday and is a 48hr booking
	/////////////  BEGIN /////////////////
	if (weekendSpecial) {
		weekendSpecialDiscount=0;
		stHour=parseInt(document.getElementById('h1').value);
		if (document.getElementById('ap1').value.toUpperCase()=='PM' && stHour<12) stHour+=12;
		stMin=parseInt(document.getElementById('m1').value);
	
		if (tlob>=30.5 && (DOW1==5 || (DOW1==6 && (stHour<7 || (stHour==7 && stMin==0))))){	// 48 hour booking, check if it falls within the weekend limits
			if (DOW1==5 && stHour>=19) {
				tcost+=(cars[car][2]*0.7)*2;
				tmiles+=40 * parseInt(hours/24);
				if (hours>48)
					hours-=48;
				else
					hours=0;
			} else if (DOW1==6 && (stHour<7 || (stHour==7 && stMin==0))){
				tcost+=(cars[car][2]*0.7)*2;
				tmiles+=40 * parseInt(hours/24);
				if (hours>48)
					hours-=48;
				else
					hours=0;
			}
		} else if (tlob>=8.5 && (DOW1==5 || DOW1==6 || (DOW1==0 && (stHour<7 || (stHour==7 && stMin==0))))){	// 24 hour booking, check if it falls within the weekend limits
			if (DOW1==5 && stHour>=19){
				tcost+=cars[car][2]*0.7;
				tmiles+=40 * parseInt(hours/24);
				if (hours>24)
					hours-=24;
				else
					hours=0;
			} else if (DOW1==6){ // 24 hours starting anytime on a saturday is ok
				tcost+=cars[car][2]*0.7;
				tmiles+=40 * parseInt(hours/24);
				if (hours>24)
					hours-=24;
				else
					hours=0;
			} else if (DOW1==0 && (stHour<7 || (stHour==7 && stMin==0))){
				tcost+=cars[car][2]*0.7;
				tmiles+=40 * parseInt(hours/24);
				if (hours>24)
					hours-=24;
				else
					hours=0;
			}
		}
	}
	/*if (weekendSpecial){	// reduce the cars 24 hour rate
		weekendSpecialDiscount=0.7;
	}*/
	 
	 /////////////   END  /////////////////
	
	// calculate total cost
	if (hours>=24){
		tcost+=cars[car][2] * parseInt(hours/24);
		tcost-=weekendSpecialDiscount;
		tmiles+=40 * parseInt(hours/24);
		hours-=24 * parseInt(hours/24);
	} 
	
	if(hours>=8.5) {
		tcost+=cars[car][2];
		tcost-=weekendSpecialDiscount;
		tmiles+=40;
		hours=0;
	}
	
	if (hours) {
		tcost+=cars[car][1]*hours;
		tmiles+=40;
	}
	tcost=Math.round(tcost*100)/100;
	
	// calculate cost per hour
	tcph=tcost/tlob;
	tcph=Math.round(tcph*100)/100;
	
	// calculate total free miles
	tmiles=tmiles==0?40:tmiles;
	
	// calculate miles overage
	if (tover>tmiles)
		tover=(tover-tmiles)*0.25;
	else
		tover=0;
		
	dstlob.value=tlob;
	dstcost.value=tcost;
	dstcph.value=tcph;
	dstover.value=tover;
	dstmiles.value=tmiles;
	dsttotal.value=Math.round((tcost+tover)*100)/100;
}

function nextNearest(value, number) {
  var ceil = Math.ceil(value);
  var remainder = value % number;
  if (remainder > 0)
    value = value - remainder + number;
  return value;
}

// JavaScript Document

  <!-- Original:  Ronnie T. Moore                             -->
  <!-- Web Site:  The JavaScript Source                       -->
 
  <!-- This script and many more are available free online at -->
  <!-- The JavaScript Source!! http://javascript.internet.com -->
 
  <!-- Begin
  function isValidDate( dateStr ) {
    // Date validation function courtesty of
    // Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
 
    // Checks for the following valid date formats:
    // MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
 
    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year
 
    var matchArray = dateStr.match( datePat ); // is the format ok?
    if ( matchArray == null ) {
      //alert( dateStr + " Date is not in a valid format." )
      return false;
    }
    month = matchArray[ 3 ]; // parse date into variables
    day   = matchArray[ 1 ];
    year  = matchArray[ 4 ];
	
    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 (month+'/'+day+'/'+year);
  }
 
  function isValidTime( timeStr ) {
    // Time validation function courtesty of
    // Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
 
    // Checks if time is in HH:MM:SS AM/PM format.
    // The seconds and AM/PM are optional.
 
    function d2( val ) {
      return ( val < 10 ) ? '0' + val : val;
    }
 
    var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
 
    var matchArray = timeStr.match( timePat );
    if ( matchArray == null ) {
      //alert( "Time is not in a valid format." );
      return false;
    }
    hour   = matchArray[ 1 ]; 
    minute = matchArray[ 2 ];
    
if ( matchArray[ 3 ] ) {
      second = matchArray[ 3 ]
    } else {
      second = 0
    }
 
    if ( matchArray[ 5 ] ) {
      ampm = matchArray[ 5 ];
    } else {
      ampm = null
    }
 
//  alert( 'time: "' + hour + '" "' + minute + '" "' + second + '" "' + ampm + '"' )
 
    if (((hour < 0  || hour > 12) && ampm !== null) || ((hour < 0  || hour > 23) && ampm == null)) {
      alert( "Hour must be between 1 and 12. (or 0 and 23 for military time)" );
      return false;
    }
 
    if ( hour <= 12 && ( ampm == null ) ) {
      if ( confirm( "Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time" ) ) {
        //alert( "You must specify AM or PM." );
        return false;
      }
    }
 
    if  ( hour > 12 && ( ampm != null ) ) {
      //alert( "You can't specify AM or PM for military time." );
      return false;
    }
 
    //if(ampm != null){
    // if(ampm.toString().toUpperCase()=='PM')
    //       hour = hour + 12;
//	}
 
    if ( minute < 0 || minute > 59 ) {
      //alert( "Minute must be between 0 and 59." );
      return false;
    }
  
    timeVal = d2( hour ) + ':' + d2( minute )
 
//  alert( 'timeVal: ' + timeVal )
 
    if ( second && ( second < 0 || second > 59 ) ) {
      alert( "Second must be between 0 and 59." );
      return false;
    } else
      timeVal += ':' + d2( second )
//  alert( 'timeVal: ' + timeVal )
    
    if(ampm!=null){
        timeVal += ' ' + ampm
    }else{
      if(hour<13)
        timeVal += ' AM'
      else
        timeVal += ' PM'
    }
    //alert( 'timeVal: ' + timeVal )
 
    return true;
  }
 
  function dateDiff() { 
    date1 = new Date();
    date2 = new Date();
    diff  = new Date();
	time1 = document.getElementById('h1').value + ':' + document.getElementById('m1').value + ' ' + document.getElementById('ap1').value;
	time2 = document.getElementById('h2').value + ':' + document.getElementById('m2').value + ' ' + document.getElementById('ap2').value;
 
    /*if(document.getElementById('firstdate').value==''||document.getElementById('firsttime').value==''||document.getElementById('firsttime').value==''||document.getElementById('secondtime').value==''){
      return false;     
	}*/
	
	if(document.getElementById('firstdate').value==''||document.getElementById('seconddate').value==''){
      return false;     
	}
	
	tmp=isValidDate( document.getElementById('firstdate').value );
	
    if ( tmp && isValidTime( time1 ) ) { // Validates first date
     date1temp = new Date(  tmp + " " + timeVal );
      date1.setTime( date1temp.getTime() );
    } else
      return false; // otherwise exits

    tmp=isValidDate( document.getElementById('seconddate').value );
	
	if ( tmp && isValidTime( time2 ) ) { // Validates second date
      date2temp = new Date( tmp + " " + timeVal );
      date2.setTime( date2temp.getTime() );
    } else
      return false; // otherwise exits
	  
	if (date2<date1) return false;
 
    // sets difference date to difference of first date and second date
 
    diff.setTime( Math.abs( date1.getTime() - date2.getTime() ) );
 
    timediff = diff.getTime();
 
    hours = Math.floor( timediff / ( 1000 * 60 * 60 ) );
//  timediff -= hours * ( 1000 * 60 * 60 );
 
	mins = Math.floor( timediff / ( 1000 * 60 ) );
//  timediff -= mins * ( 1000 * 60 );
//
	secs = Math.floor( timediff / 1000 );
//  timediff -= secs * 1000;
 
//   document.getElementById('difference').value = weeks + " weeks, " + days + " days, " + hours + " hours, " + mins + " minutes, and " + secs + " seconds";
   
     //alert('Hours ' + hours);
     //alert('mins ' + mins);
     //alert('secs ' + secs);
 
     //document.getElementById('difference').value =  mins; //.toFixed( 2 )
	 
	 DOW1=date1temp.getDay();
	 DOW2=date2temp.getDay();
     
    return mins; // form should never submit, returns false
  }
//  End -->
