var dDate=new Date()
var rDate=new Date()

function jump_country(SelOBJ) {
	
	
}

// --- GENERIC POP-UP WINDOW SCRIPT --- //

function displayWindow(url, width, height) {
	var Win = window.open(url,"displayWindow",'width=' + width + ',height=' + height + ',resizable=no,scrollbars=yes,menubar=no,status=no,left=0,top=0');
}

// --- VALIDATE THE USER INFORMATION --- //
function User_Validator(theForm) {

	if (theForm.password.value.length < 1)
	{
		alert("Please enter at least 1 characters in the \"Password\" field.");
		theForm.password.focus();
		return (false);
	}
	
	// check if both password fields are the same
	if (theForm.password.value != theForm.confirm_password.value)
	{
		alert("The two passwords are not the same.");
		theForm.confirm_password.focus();
		return (false);
	}
	
	return (true);
}

function update_return_location()
{
	//	alert(document.forms.resform1.pu_location.options[document.forms.resform1.pu_location.selectedIndex].value);
	document.forms.resform1.rt_location.selectedIndex = document.forms.resform1.pu_location.selectedIndex;
	//	if (destination) location.href = destination;
}

function toEpoch(mObj,dObj,yObj,hrObj,minObj, rt_mObj,rt_dObj,rt_yObj,rt_hrObj,rt_minObj, dyn_layer1, dyn_layer2){
  //	var durObj = document.getElementById('rental_duration');
  var month = (mObj.options[mObj.selectedIndex].value )
  var day = dObj.options[dObj.selectedIndex].value
  var year = yObj.value
  var hour = hrObj.options[hrObj.selectedIndex].value
  var minute = minObj.options[minObj.selectedIndex].value
  var second = 0
  
  var rt_month = (rt_mObj.options[rt_mObj.selectedIndex].value )
  var rt_day = rt_dObj.options[rt_dObj.selectedIndex].value
  var rt_year = rt_yObj.value
  var rt_hour = rt_hrObj.options[rt_hrObj.selectedIndex].value
  var rt_minute = rt_minObj.options[rt_minObj.selectedIndex].value
  var rt_second = 0
  
  rDate.setMonth(rt_month,rt_day)
  rDate.setFullYear(rt_year)
  rDate.setHours(rt_hour,rt_minute,rt_second)
  
  
  //alert(month+']['+day+']['+year)
  dDate.setMonth(month,day)
  dDate.setFullYear(year)
  dDate.setHours(hour,minute,second)
  
  number_of_days =  Math.ceil ( (((rDate.getTime()-dDate.getMilliseconds())/1000) - ( (dDate.getTime()-dDate.getMilliseconds())/1000 ) )/ 86400 )
  if ( hour > rt_hour ) {
  	alert ('These dates will result in a charge of ' + number_of_days + ' Days');
  } 
  
  //	durObj.innerHTML = "Rental Duration " + number_of_days + " days";
  toDate(mObj, dObj, yObj, hrObj, minObj, dyn_layer1)
  toDate(rt_mObj, rt_dObj, rt_yObj, rt_hrObj, rt_minObj, dyn_layer2)
}

function getDays(mObj,dObj,yObj,hrObj, minObj, dyn_layer){
  // build array of days in a select container based on selected month and year

  if(!dDate) dDate=new Date()
	iMonth=parseInt(mObj.options[mObj.selectedIndex].value)
  iYear=(yObj.value?yObj.value:1900);
	old_day = dObj.options[dObj.selectedIndex].value;
	
	var iDays=31;
	switch(iMonth){
		case 4: case 6: case 9: case 11:	--iDays; break;
		case 2: iDays=29;	if ((iYear%4)!=0) --iDays;
	}

  dObj.options.length = 0;
  for(var i=0; i<iDays; i++){
    dObj.options[i] = new Option(i+1,i+1)
  }
  if(old_day > iDays) old_day = iDays;
  dObj.selectedIndex = old_day - 1;
  
  toDate(mObj, dObj, yObj, hrObj, minObj, dyn_layer);
}

function toDate(mObj, dObj, yObj, hrObj, minObj, dyn_layer){
  var mEpoch = toEpoch2(mObj,dObj,yObj,hrObj,minObj); 
  //	alert (mEpoch);
  if(mEpoch<10000000000) mEpoch *= 1000; // convert to milliseconds (Epoch is usually expressed in seconds, but Javascript uses Milliseconds)

  dDate.setTime(mEpoch)
  dyn_layer.innerHTML = dDate
  
  	var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
	var monthname=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
	display_date = weekday[dDate.getDay()] + ", " + monthname[dDate.getMonth()] + " " + dDate.getDate() + ", " + dDate.getFullYear()
  dyn_layer.innerHTML = display_date //	+ mEpoch
 
}

function toEpoch2(mObj,dObj,yObj,hrObj,minObj){
  var month = (mObj.options[mObj.selectedIndex].value ) 	-1
  var day = dObj.options[dObj.selectedIndex].value
  var year = yObj.value
  var hour = hrObj.options[hrObj.selectedIndex].value
 // var minute = minObj.options[minObj.selectedIndex].value
  var second = 0
  
  
  dDate.setMonth(month,day)
  dDate.setFullYear(year)
  dDate.setHours(hour,minute,second)
 
  return ((dDate.getTime()-dDate.getMilliseconds())/1000 );
}

function toEpoch3(month,day,year,hour,minute){
  var second = 0
  //	alert (month);
  dDate.setMonth(month -1,day)
  dDate.setFullYear(year)
  dDate.setHours(hour,minute,second)
 
  return ((dDate.getTime()-dDate.getMilliseconds())/1000 );
}

function compare_dates(form_obj, min_period, min_lead_time) {
	
	var pu_month = form_obj.pu_month.options[form_obj.pu_month.selectedIndex].value;
	var pu_day = form_obj.pu_day.options[form_obj.pu_day.selectedIndex].value;
	var pu_year = form_obj.pu_year.options[form_obj.pu_year.selectedIndex].value;
	var pu_hour = form_obj.pu_hour.options[form_obj.pu_hour.selectedIndex].value;
	var pu_minute = form_obj.pu_minute.options[form_obj.pu_minute.selectedIndex].value;
	
	var rt_month = form_obj.rt_month.options[form_obj.rt_month.selectedIndex].value;
	var rt_day = form_obj.rt_day.options[form_obj.rt_day.selectedIndex].value;
	var rt_year = form_obj.rt_year.options[form_obj.rt_year.selectedIndex].value;
	var rt_hour = form_obj.rt_hour.options[form_obj.rt_hour.selectedIndex].value;
	var rt_minute = form_obj.rt_minute.options[form_obj.rt_minute.selectedIndex].value;
	
	var puEpoch = toEpoch3(pu_month,pu_day,pu_year,pu_hour,pu_minute);
	
	var rtEpoch = toEpoch3(rt_month,rt_day,rt_year,rt_hour,rt_minute);
	
	//	alert (rtEpoch);
	
	var d = new Date();
	var ltEpoch = toEpoch3(d.getMonth() + 1,d.getDate(),d.getFullYear(),d.getHours(),d.getMinutes());
	//	alert (puEpoch);
	//	alert (rtEpoch);
	//	alert (ltEpoch);
	//	alert (puEpoch + ">" + rtEpoch);
	if (puEpoch > rtEpoch) {
		alert ( "Return Date cannot be before pickup date." + puEpoch + " - " + rtEpoch);
		return false;
	}
	
	if (puEpoch < ltEpoch) {
		alert ( "The Pickup Date Cannot be before Today's Date" );
		return false;
	}
	
	if (puEpoch - ((min_lead_time * 86400) - 1) < ltEpoch) {
		alert ( "There is not enough leadtime ("+ min_lead_time +" days) for this quote." );
		return false;
	}
	
	if (rtEpoch - (min_period * 86400) < puEpoch) {
		alert ( "Return Date cannot be within " + min_period + " days of pickup date" );
		return false;
	}
	return true;
}

///////////////						JAVASCRIPT STRING SPLIT FUNCTION						///////////////
function stringSplit ( string, delimiter ) { 
    if ( string == null || string == "" ) { 
        return null; 
    } else if ( string.split != null ) { 
        return string.split ( delimiter ); 
    } else { 
        var ar = new Array(); 
        var i = 0; 
        var start = 0; 
        while( start >= 0 && start < string.length ) { 
             var end = string.indexOf( delimiter, start ) ; 
             if( end >= 0 ) { 
                 ar[i++] = string.substring( start, end ); 
                 start = end+1; 
             } else { 
                 ar[i++] = 
                 string.substring( start, string.length ); 
                 start = -1; 
             } 
        } 
        return ar; 
    } 
} 

///////////////		CLEAR THE STATES IF A COUNTRY THAT DOES NOT HAVE STATES IS SELECTED		///////////////
function clearStates( objectname, message ) {
	objectname.options.length = 0
	var optionName = new Option(message , "ZZ", false, false)
	objectname.options[0] = optionName;
}

///////////////		UPDATE STATES FOR A SPECIFIC COUNTRY									///////////////
function replaceSelValues (objectname, valuearray) {
	var t_state = new Array();
	objectname.options.length = 0
	
	var optionName = new Option("---Select a State---", "ZZ", true, false)
	objectname.options[0] = optionName;
	for (var i=0; i<valuearray.length; i++) {
		t_state[0] = stringSplit ( valuearray[i], ',' );
		var length = objectname.length;
	
    	var optionName = new Option(t_state[0][0], t_state[0][1], false, false)
    	objectname.options[length] = optionName;

		}
    objectname.selected=0;
	objectname.disabled = false;
}

///////////////		Verifies that an email is actually formatted correctly					///////////////
function check_email(e) {
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
	for(i=0; i < e.length ;i++){
		if(ok.indexOf(e.charAt(i))<0){ 
			return (false);
		}	
	} 
	
	if (document.images) {
		re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (!e.match(re) && e.match(re_two)) {
			return (-1);		
		} 
	}
}


