function validateForm(){
	var isGoogle = document.getElementById('tripPlannerSelectGoogle').checked;
	var origin = document.getElementById('origin').value;
	var destination = document.getElementById('destination').value;
	var validationErrors = [];
	var errorStyle = "thin inset red";
	
	if (origin == 'intersection'){
		pFromIntersection2 = document.getElementById('pFromIntersection2');
		pFromIntersection1 = document.getElementById('pFromIntersection1');
	
		if ( pFromIntersection1.value == null || pFromIntersection1.value == "" ){
			validationErrors[validationErrors.length]="Origin <b>'From'</b> intersection needs value";
			pFromIntersection1.style.border=errorStyle;
		}
		if ( pFromIntersection2.value == null || pFromIntersection2.value == "" ){
			validationErrors[validationErrors.length]="Origin <b>'To'</b> intersection needs value";
			pFromIntersection2.style.border=errorStyle;
		}
	}else if (origin == 'address'){
		pFromStreet = document.getElementById('pFromStreet');
		if ( pFromStreet.value == null || pFromStreet.value == "" ){
			validationErrors[validationErrors.length]="Origin address needs value";
			pFromStreet.style.border=errorStyle;
		}
	}
	
	if (destination == 'intersection'){
		pToIntersection2 = document.getElementById('pToIntersection2');
		pToIntersection1 = document.getElementById('pToIntersection1');
	
		if ( pToIntersection1.value == null || pToIntersection1.value == "" ){
			validationErrors[validationErrors.length]="Destination <b>'From'</b> intersection needs value";
			pToIntersection1.style.border=errorStyle;
		}
		if ( pToIntersection2.value == null || pToIntersection2.value == "" ){
			validationErrors[validationErrors.length]="Destination <b>'To'</b> intersection needs value";
			pToIntersection2.style.border=errorStyle;
		}
	}else if (destination == 'address'){
		pToStreet = document.getElementById('pToStreet');
		if ( pToStreet.value == null || pToStreet.value == "" ){
			validationErrors[validationErrors.length]="Destination address needs value";
			pToStreet.style.border=errorStyle;
		}
	}
		
	//validate date
	var validDate = false;
	var date = document.getElementById('googleDay');
	try{
		var splitResult = date.value.split("/");
		var mm = splitResult[0];
		var dd = splitResult[1];
		var yyyy = splitResult[2];

		var jDate = new Date();
		
		jDate.setYear(yyyy);
		jDate.setUTCMonth(mm-1);
		jDate.setUTCDate(dd);
		
		if ( jDate != "Invalid Date"){
			validDate = true;
		}
	}catch(e){

	}
	if (!validDate){validationErrors[validationErrors.length]='Date must be in mm/dd/yyyy format, i.e: 1/11/2009'; date.style.border=errorStyle;}

	if (validationErrors.length > 0){
		YAHOO.namespace("example.container");
		
		if (YAHOO.example.container.panel2){
			YAHOO.example.container.panel2.hide();
		}
		
		YAHOO.example.container.panel2 = new YAHOO.widget.Panel("panel2", { width:"360px", visible:false, draggable:false, close:true } );
		//YAHOO.example.container.panel2.setHeader("Validation Errors");
		YAHOO.example.container.panel2.setBody(prettyPrint(validationErrors));
		YAHOO.example.container.panel2.render("container");		
		YAHOO.example.container.panel2.show();
		return false;
	}
	else{
		return true;
	}
}

function prettyPrint(array){
	var string = '';
	for (i=0;i<array.length;i++){
		string += array[i] + '<br>';
	}
	return string;	
}

function formtripPlannerSelect(id)
{
	if(id == "google")
	{
		//hide walking distance, route include preferences, change form
		document.getElementById('max1').style.display = "none";
		document.getElementById('max2').style.display = "none";
		document.getElementById('includeRoutes').style.display = "none";
		document.getElementById('preferences').style.display = "none";
		document.getElementById('googleDay').style.display = "block";
		document.getElementById('rtdDay').style.display = "none";
		document.getElementById('tripPlannerSelectGoogle').checked=true;
		document.getElementById('googleDateLabel').style.display = "block";
		document.getElementById('rtdDateLabel').style.display = "none";
	}
	else //rtd
	{
		document.getElementById('max1').style.display = "block";
		document.getElementById('max2').style.display = "block";
		document.getElementById('includeRoutes').style.display = "block";
		document.getElementById('preferences').style.display = "block";
		document.getElementById('googleDay').style.display = "none";
		document.getElementById('rtdDay').style.display = "block";
		document.getElementById('tripPlannerSelectRtd').checked=true;
		document.getElementById('googleDateLabel').style.display = "none";
		document.getElementById('rtdDateLabel').style.display = "block";
	}
}

showOriginPlannerChoices = function(id){
	if(id == 'intersection'){
		document.getElementById('intersectionOriginBtn').className = 'tripPlannerTabOn';
		document.getElementById('addressOriginBtn').className = 'tripPlannerTabOff';
		document.getElementById('landmarkOriginBtn').className = 'tripPlannerTabOff';
		document.getElementById('intersectionOriginContent').style.display = 'block';
		document.getElementById('addressOriginContent').style.display = 'none';
		document.getElementById('landmarkOriginContent').style.display = 'none';
	}
	if(id == 'address'){
		document.getElementById('intersectionOriginBtn').className = 'tripPlannerTabOff';
		document.getElementById('addressOriginBtn').className = 'tripPlannerTabOn';
		document.getElementById('landmarkOriginBtn').className = 'tripPlannerTabOff';
		document.getElementById('intersectionOriginContent').style.display = 'none';
		document.getElementById('addressOriginContent').style.display = 'block';
		document.getElementById('landmarkOriginContent').style.display = 'none';
	}
	if(id == 'landmark'){
		document.getElementById('intersectionOriginBtn').className = 'tripPlannerTabOff';
		document.getElementById('addressOriginBtn').className = 'tripPlannerTabOff';
		document.getElementById('landmarkOriginBtn').className = 'tripPlannerTabOn';
		document.getElementById('intersectionOriginContent').style.display = 'none';
		document.getElementById('addressOriginContent').style.display = 'none';
		document.getElementById('landmarkOriginContent').style.display = 'block';		
		if (this.originInit == null) {
			this.landmarkId = id + 'Origin';
			fillLandmarkCategories();
			originInit=true;
		}
	}
	document.getElementById('origin').value=id;
}

showDestinPlannerChoices = function(id){
	if(id == 'intersection'){
		document.getElementById('intersectionDestinBtn').className = 'tripPlannerTabOn';
		document.getElementById('addressDestinBtn').className = 'tripPlannerTabOff';
		document.getElementById('landmarkDestinBtn').className = 'tripPlannerTabOff';
		document.getElementById('intersectionDestinContent').style.display = 'block';
		document.getElementById('addressDestinContent').style.display = 'none';
		document.getElementById('landmarkDestinContent').style.display = 'none';
	}
	if(id == 'address'){
		document.getElementById('intersectionDestinBtn').className = 'tripPlannerTabOff';
		document.getElementById('addressDestinBtn').className = 'tripPlannerTabOn';
		document.getElementById('landmarkDestinBtn').className = 'tripPlannerTabOff';
		document.getElementById('intersectionDestinContent').style.display = 'none';
		document.getElementById('addressDestinContent').style.display = 'block';
		document.getElementById('landmarkDestinContent').style.display = 'none';
	}
	if(id == 'landmark'){
		document.getElementById('intersectionDestinBtn').className = 'tripPlannerTabOff';
		document.getElementById('addressDestinBtn').className = 'tripPlannerTabOff';
		document.getElementById('landmarkDestinBtn').className = 'tripPlannerTabOn';
		document.getElementById('intersectionDestinContent').style.display = 'none';
		document.getElementById('addressDestinContent').style.display = 'none';
		document.getElementById('landmarkDestinContent').style.display = 'block';
		//see if it has been used (check for null too...)
		if (this.destinInit == null ) {
			this.landmarkId = id + 'Destin';
			fillLandmarkCategories();
			destinInit=true;
		}
	}
	document.getElementById('destination').value=id;	
}

//dwr functions
function fillLandmarkCategories(){
	dwr.util.useLoadingMessage();
	DWRFacade.getLandmarkCategories(updateLandmarkCategories);
}

function updateLandmarkCategories(data){
	dwr.util.removeAllOptions(landmarkId + 'Categories');
	dwr.util.addOptions(landmarkId + 'Categories', data, "key", "value");
	fillLandmarks(document.getElementById(landmarkId + 'Categories').value, landmarkId+'Landmarks');
}

function fillLandmarks(categoryId,selectId){
	this.selectId=selectId;
	DWRFacade.getLandmarks(categoryId, updateLandmarks);
}

function updateLandmarks(data){
	dwr.util.removeAllOptions(selectId);
	dwr.util.addOptions(selectId, data, "key", "value");
}

function loadDate() {
   current_date = new Date();
   var month = (current_date.getMonth()+1)
   if (month<10)
   month="0"+month;
   var day = current_date.getDate()
   if (day<10)
   day="0"+day;
   year = current_date.getFullYear();
   return month + '/' + day + '/' + year ;
}

function loadHourAndSuffix() {
   current_time = new Date();
   hour = current_time.getHours();
   minutes = current_time.getMinutes();
   if (hour > 12) {
        dwr.util.setValue('ampm', 'p');
        dwr.util.setValue('hour', hour-12);
   }else if (hour == 12){
        dwr.util.setValue('ampm', 'p');
        dwr.util.setValue('hour', hour);
   }else{
        dwr.util.setValue('ampm', 'a');
        dwr.util.setValue('hour', hour);
   } 
}


function loadMinutes() {
   current_time = new Date();
   minutes = current_time.getMinutes();

   var minuteArray=new Array(60);
   
   for(var i=0;i<60;i++){
    	if ( i < 10 ){
    		minuteArray[i] = '0' + i;
    	}else{
    		minuteArray[i] = i;
    	}
   }
   dwr.util.addOptions('minute', minuteArray);
   dwr.util.setValue('minute', minutes);
}

exampleObjects = ['pFromIntersection1', 'pFromIntersection2', 'pToIntersection1','pToIntersection2','pFromStreet', 'pToStreet','pIncludeDataField','pExcludeDataField'];
exampleText = ['Ex: Broadway','Ex: Colfax','Ex: Broadway','Ex: Colfax','Ex: 123 Elm st','Ex: 456 Main st','Include Routes Ex: 15, 40','Exclude Routes Ex: 7, 21'];

function clearExampleText(){
	for (var i=0;i<exampleText.length;i++){
		formObject = document.getElementById(exampleObjects[i]);
		if (formObject.value == exampleText[i]){formObject.value='';}	
	}
}

function loadExampleText(){
	for (var i=0;i<exampleText.length;i++){
		formObject = document.getElementById(exampleObjects[i]);
		if (formObject.value == null || formObject.value == ''){
			formObject.value=exampleText[i];
		}
		formObject.onfocus=function(){this.value='';};
	}
}
