var storedStartPoint = "";
var storedStartCity = "";
var storedStartState = "";
var storedStartZip = "";
var storedEndPoint = "";
var storedEndCity = "";
var storedEndState = "";
var storedEndZip = "";

function initTimeout(geocodeTimeout){
    //initialize geocode timeout, value in ini file
    //set as global value
    geoTimeout = geocodeTimeout;
}

function storeFromTo(startpoint, startcity, startstate, startzip, endpoint, endcity, endstate, endzip){
//variables stored if they were passed from the results screen. then compare to 
//what user submits if they revised their trip and re-geocode if necessary. If values
//have changed, user revised start or end and we need to clear lat/longs

    storedStartPoint = startpoint;
	storedStartCity = startcity;
	storedStartState = startstate;
	storedStartZip = startzip;
	storedEndPoint = endpoint;
    storedEndCity = endcity;
	storedEndState = endstate;
	storedEndZip = endzip;

}

function updateFieldsFromAmbigResults(dropdownValue, pointType){
    //ambiguous results returned. user has selected from the list. now populate fields.
    var ambigArray = dropdownValue.split(':@@:');
    
    document.main.elements[pointType+'point'].value = ambigArray[0];
    document.main.elements[pointType+'city'].value = ambigArray[1];
     	      
    //now check zip to make sure it's ok
    //4-3-07 - remove > from zip string if exists
    if (ambigArray[2].indexOf('>') != -1) {
        ambigArray[2].replace('>','');
    }
    
    if (ambigArray[2].length == 4) {
        //apend leading zero if it was removed
        ambigArray[2] = '0'+ambigArray[2];
    }
    if (ambigArray[2].length == 5 && ambigArray[2] != '0') {
        //only update with zips of the correct length
        document.main.elements[pointType+'zip'].value = ambigArray[2];
    }
    
    //4-3-07 - added lats and longs to ambiguous results
    //plot point on map
    //clear existing point, if one exists
    changingInputBox(pointType, storedPoint);
    
    var tmpPoint = new GLatLng(ambigArray[3],ambigArray[4]);
    var storedPoint = storedEndPoint;
    var mapMarker = null;
      
    if (pointType == 'start') {
        var tmpPointType = "from";
        baseIcon.image = "/images/trip_planner/startflag.png";
        mapMarker = new PdMarker(tmpPoint, {icon: baseIcon, draggable: true});
        origMarker = mapMarker;
        storedPoint = storedStartPoint;
        origMarker.setId("orig");
        
        //marker dragging functionality		
		GEvent.addListener(origMarker, "dragend", function() {
			document.main.fromLongCoord.value = origMarker.getPoint().lng();
			document.main.fromLatCoord.value = origMarker.getPoint().lat();
			positionAndShowMapChoice('mapRename');
			markerDragged = "orig";
			clearVariables('start');
		});
    }
    else {
        var tmpPointType = "to";  
		baseIcon.image = "/images/trip_planner/endflag.png";
	    mapMarker = new PdMarker(tmpPoint, {icon: baseIcon, draggable: true});
	    destMarker = mapMarker;
        storedPoint = storedStartPoint;
        destMarker.setId("dest");
        
        //marker dragging functionality	
        GEvent.addListener(destMarker, "dragend", function() {
			document.main.toLongCoord.value = destMarker.getPoint().lng();
			document.main.toLatCoord.value = destMarker.getPoint().lat();
			positionAndShowMapChoice('mapRename');
			markerDragged = "dest"
			clearVariables('end');
		});
    }
		
    googleMap.addOverlay(mapMarker);
	mapMarker = null;

	document.main.elements[tmpPointType+'LatCoord'].value = ambigArray[3];
	document.main.elements[tmpPointType+'LongCoord'].value = ambigArray[4];
	
}

function changingInputBox(pointType, storedPoint) {
    //detect a change in start or end point - if point has changed, need to clear 
    //the variables. called when user changes origin or destination
    
    var pointArticle = 'to';
    var mapMarker = destMarker;
    
    if (pointType == 'start') {
       pointArticle = 'from';
       mapMarker = origMarker;
    }
              
    if (document.main.elements[pointType + 'point'].value != storedPoint) {
        //clear lat/longs
        document.main.elements[pointArticle+'LatCoord'].value = "";
	    document.main.elements[pointArticle+'LongCoord'].value = "";
	    
	    //remove the markers from the map
        if (mapMarker != null) {
            googleMap.removeOverlay(mapMarker);
        }
    }
}

function formCheck(form) {
    //var f = document.theform
    var f = form;
    var start_length = 34;
    var end_length = 34;
    var city_length = 20;
    var hr_value;
    var min_value;
    var days;

    if ((f.startpoint.value == "") || (f.startpoint.value == "Or enter a new Origin")) {
        alert("Please enter a STARTING LOCATION to process your itinerary request.");
        f.startpoint.focus();
        return false;
    }
    if (f.startpoint.value.length > start_length) {
        alert("Please enter a STARTING LOCATION less than 35 characters.");
        f.startpoint.focus();
        return false;
    }
    if ((f.endpoint.value == "") || (f.endpoint.value == "Or enter a new Destination")) {
        alert("Please enter a DESTINATION to process your itinerary request.");
        f.endpoint.focus();
        return false;
    }
    if (f.endpoint.value.length > end_length) {
        alert("Please enter an END LOCATION less than 35 characters.");
        f.endpoint.focus();
        return false;
    }
    if (f.startcity.value.length > city_length) {
        alert("Please enter a START CITY less than 21 characters.");
        f.startcity.focus();
        return false;
    }
    if (f.endcity.value.length > city_length) {
        alert("Please enter an END CITY less than 21 characters.");
        f.endcity.focus();
        return false;
    }
    
    //  set time of trip from user input
    if (f.time1_hr.value == "" || f.time1_min.value == "") {
        alert ("You must enter a value in the \"hour\" and \"min\" field");
        f.time1_hr.focus();
        return false;
    }

	if (!checkDate(f)) {
	    //check format of entered date
	    return false;
	}
	
    var chkval = f.time1_hr.value;
    var nval = parseInt(chkval,10);
    
    if (chkval != "" && !(nval <= 12 && nval >= 0)) {
        alert("Please enter a value between 0 and 12 in the \"Hour\" field." );
        document.main.time1_hr.focus();
        return false;
    }
    
    if (chkval != "" && (nval <= 12 && nval >= 0)) {
        hr_value = nval;
    }

    chkval = f.time1_min.value;
    nval = parseInt(chkval,10);

    if (chkval != "" && !(nval <= 60 && nval >= 0)){
        alert("Please enter a value between 0 and 60 in the \"Minute\" field.");
        document.main.time1_min.focus();
        return false;
    }
    
    if (chkval != "" && (nval <= 60 && nval >= 0)) {
        min_value = nval;
    }

    if (f.time1_ampm[0].checked) {
        // am
		if (hr_value == 12) {
            // time is midnight
            hr_value = 0;
        }
    }
    else {
        // pm
		if (hr_value != 12) {
            // time is after 1:00 pm
            hr_value = hr_value + 12;
        }
     }
	
	var timet = (hr_value * 60) + min_value;   

 
	 //arrive at is selected			
    //if (f.when[0].checked) {
    if (f.when.value == "A") {  
        f.time2.value = timet;
    }
     
     else {
     //does this when leave by is clicked.
    //if (f.when[1].checked) {
        f.time1.value = timet;
    }
   
		//now convert date into days past today
		var d1 = new Date(document.main.dayField.value); //entered by user with calendar  
    var d2 = new Date(); // creates a new Date representing today 
		
		//convert to millisecond representation to get values in milliseconds
		//value is number of milliseconds past january first of this year
    var milli_d1 = d1.getTime(); 
    var milli_d2 = d2.getTime(); 
    
    //24 hour time
    var hours_d2 = d2.getHours();
    
    if (d1 > d2) {
        //millisecond difference between date entered at midnight and right now
        var diff = (d1 - d2);
        if (diff > 86400000) {
            //meaning the number of ms past right now is more than 1 days worth
            //find number of days using ms.
            
            //round up to next day since more than 1 day
            days = Math.ceil(diff*(1/86400000));
            days = parseInt(days);
        }
        else {
            //tomorrow, seconds past right now is less than 1 day
            //or less than 86400000 ms for one day
            days = 1;
        }    
    
    }
    else {
        //d2 is now date, now date is after selected date, so means today
        days = 0;
    }
		
		document.main.day.value = days;
		
	return true;
				
}//end function formCheck
