// JavaScript Document
//********************************************************************************
// stuff for location-dataset in hidden-fields "loc*"
var posLoc = 0; 
var posZone = 1
// stuff for rating-dataset in hidden-fields "zone*"
// more find in function getBaseRate()
var posZoneName = 0;
var posDir = 1;
var posZoneUp = 2;
var zeroLoc = "Faro Airport";
var baseRateTaxi = 8;
var baseRateMinibus = 12;
var baseRateMediumbus = 18;
var baseRateBus = 24;
var baseRatePers = 2;
// to identify right hidden fields
var regExZone = /^zone/;
var regExLoc = /^loc/;
//*********************************************************************************************
/*
function setFormSubmitted(form,strTest) {
	form.formSent.value = "foo";
	for (var i = 0; i < form.elements.length; i++) {
		if (form.elements[i].name == strTest) {
			form.elements[i].value = "sent";
		}
	}
	showDebug(form.debug,form.formSent.value);
	return;
}*/

function popUpContact() {
	window.open('include/contact_form.php','PopUp','width=480,height=550'); 
}

function isDirectionValid(inFormDepLocation,inFormDestLocation) {
	if (inFormDepLocation == "Other") {
		alert("Please, define a valid departure location.");
		return false;
	}
	if (inFormDestLocation == "Other") {
		alert("Please, define a valid destination location.");
		return false;
	}
	return true;
}

function chkReservFormular (inForm,chBoxObj,numberpers,sname,fname,adr,country,phone,email,confEmail,arflnr,depflnr,chBoxSelect) {
  regExMail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  regExName = /^([A-Za-zÄäÖöÜü\s]+)+([A-Za-zÄäÖöÜü].$)/;
  //regExForb = /^([^\(\)\{\}\[\]\<\>])+([^\(\)\{\}\[\]\<\>\s])+([^\(\)\{\}\[\]\<\>\s]).$/;
  regExForb = /\(|\)|\{|\}|\[|\]|\<|\>/;
  regExPhone = /^(\+?)+([0-9\s])/;
  regExNumPers = /[0-9]/;
  
  
  var formFname = eval("inForm." + fname);
  if (formFname.value == "") {
    alert("Please complete the form with your first name.");
    formFname.focus();
    return false;
  }
  
  var formName = eval("inForm." + sname);
  if (formName.value == "") {
    alert("Please complete the form with your name.");
    formName.focus();
    return false;
  }
  
  /*var formCountry = eval("inForm." + country);
  if (formCountry.value == "") {
  	alert("Please complete the form with your country.");
	formCountry.focus();
	return false;
  }*/
  
  var formEmail = eval("inForm." + email);
  if (formEmail.value == "") {
  	alert("Please complete the form with your email adress.");
	formEmail.focus();
	return false;
  }
  if (!regExMail.exec(formEmail.value)) {
  	alert("Not a valid email adress.");
	formEmail.focus();
	return false;
  }
  
  var formConfirmEmail = eval("inForm." + confEmail);
  if (formConfirmEmail.value == "") {
  	alert("Please confirm your email adress.");
	formConfirmEmail.focus();
	return false;
  }
  if (formEmail.value != formConfirmEmail.value) {
  	alert("Email adresses are different.");
	formConfirmEmail.focus();
	return false;
  }
  
  if (isReturnChecked (chBoxObj)) {
	  var formNumPers = eval("inForm." + numberpers);
	  if (formNumPers.value == "") {
		alert("Please confirm your email adress.");
		formConfirmEmail.focus();
		return false;
	  }
	  if (!regExNumPers.exec(formNumPers.value)) {
		alert("Number of persons must be a numeric value.");
		formNumPers.focus();
		return false;
	  }
  }
  /*
  var formZip = eval("inForm." + zip);
  if (regExForb.exec(formZip.value)) {
  	alert("\"<\",\">\",\"{\",\"}\",\"[\",\"]\",\"(\",\")\" should not be used for postal code.");
	formZip.focus();
	return false;
  }
  
  var formCity = eval("inForm." + city);
  if (regExForb.exec(formCity.value)) {
  	alert("\"<\",\">\",\"{\",\"}\",\"[\",\"]\",\"(\",\")\" should not be used for cities.");
	formCity.focus();
	return false;
  }
  
  var formAdr = eval("inForm." + adress);
  if (regExForb.exec(formAdr.value)) {
  	alert("\"<\",\">\",\"{\",\"}\",\"[\",\"]\",\"(\",\")\" should not be used for adresses.");
	formAdr.focus();
	return false;
  }
  */
  return true;
}


function getActMonth() {
	var actDate = new Date();
	return actDate.getMonth();
}

function getActDay() {
	var actDate = new Date();
	return actDate.getDate();
}

function getActYear() {
	var actDate = new Date();
	return actDate.getYear();
}

function getActMonthLength(year) {
	var monthsLengths = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	if ((getActMonth() == 2) && (year % 4 == 0)) {
		monthsLengths[1] = 29;
	}
	return monthsLengths[getActMonth() - 1];
}

function displayDays(day,month,year,selectName) {
	monthsArray = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	if(!(year % 4) && month == 2) {
		monthsArray[1]=29;
	}
	
	to = monthsArray[parseInt(month)-1];
	o = eval("document.forms['calcrate']." + selectName);
	for(var i = 0; i < o.options.length; i++) {
		o.options[i]=null;
	}
	
	for(i = 1; i <= to; i++) {
		o.options[i-1]=new Option(i,i);
	}
	
	if ((day-1)>to) {
		o.selectedIndex=to-1;
	}
	else {
		o.selectedIndex=day-1;
	}
}

function dateNotPossible(day,month,year) {
	var datePoss = new Date().valueOf() + 172800000;
	var dateReal = new Date();
	dateReal.setMonth(month - 1);
	dateReal.setYear(parseInt(year));
	dateReal.setDate(parseInt(day));
	if (dateReal.valueOf() < datePoss) {
		alert("Earliest possible date of service is 2 days from now.");
		return true;
	}
	return false;
}

function parseTimeString(timeString,type) {
	var t = new String();
	if (type == "minute") {
		t = timeString.substring(3,5);
		if (t == "30") return t;
		if (t == "00") return "0";
	}
	if (type == "hour") {
		t = timeString.substring(0,2);
		if (timeString.substring(0,1) == "0") {
			return timeString.substring(1,2)
		}
		else {
			return t;
		}
	}
	return false;
}

function getDiffMilli(time1,time2,date1,date2) {
	date1.setHours(parseInt(parseTimeString(time1,"hour")));
	date2.setHours(parseInt(parseTimeString(time2,"hour")));
	date1.setMinutes(parseInt(parseTimeString(time1,"minute")));
	date2.setMinutes(parseInt(parseTimeString(time2,"minute")));
	return date2.valueOf() - date1.valueOf();
}

function checkTransferDates(day1,day2,month1,month2,year1,year2,time1,time2,chBox) {
	if (isReturnChecked(chBox)) {
		if (parseInt(year1) > parseInt(year2)) {
			alert("First date later than second date, not possible.");
			return false;
		}
		if (parseInt(year1) == parseInt(year2)) {
			if (parseInt(month1) > parseInt(month2)) {
				alert("First date later than second date, not possible.");
				return false;
			}
			if (parseInt(month1) == parseInt(month2)) {
				if (parseInt(day1) > parseInt(day2)) {
					alert("First date later than second date, not possible.");
					return false;
				}
			}
		}
		var dIn = new Date();
		dIn.setMonth(parseInt(month1)-1);
		dIn.setYear(parseInt(year1));
		dIn.setDate(parseInt(day1));
		
		var dOut = new Date();
		dOut.setMonth(parseInt(month2)-1);
		dOut.setYear(parseInt(year2));
		dOut.setDate(parseInt(day2));
		
		if(getDiffMilli(time1,time2,dIn,dOut) < 0) {
			alert("The return transfer date can not come before the date of the first transfer.<0");
			return false;
		}
		if(getDiffMilli(time1,time2,dIn,dOut) < 14400000) {
			alert("Difference between first and second transfer must be at least 4 hours.<14400000");
			return false;
		}
	}
	return true;
}

function checkFormCalcRate(inForm,inFormChBox,dep1,dest1,dep2,dest2,
						   dep1t,dep2t,transferdate1,transferdate2) {
	var selectObjDep = eval("inForm." + dep1);
	var selectObjDest = eval("inForm." + dest1);
	if (!isCalcPoss(selectObjDep.options[selectObjDep.selectedIndex].text)) {
		alert("Please choose a departure location for the first transfer.");
		return false;
	}
	if (!isCalcPoss(selectObjDest.options[selectObjDest.selectedIndex].text)) {
		alert("Please choose a destination location for the first transfer.");
		return false;
	}
	if (selectObjDep.options[selectObjDep.selectedIndex].text ==
		selectObjDest.options[selectObjDest.selectedIndex].text) {
		alert("Please select different locations for the first transfer.");
		return false;
	}
	var selectObjDep2 = eval("inForm." + dep2);
	var selectObjDest2 = eval("inForm." + dest2);
	// now it's to get the calendars's dates
	var date1Form = eval("inForm." + transferdate1);
	if (!date1Form.value || date1Form.value =="") {
		alert("Please select a date for the first transfer.");return false;
	}
	var date1String = new String(date1Form.value);
	var date2Form = eval("inForm." + transferdate2);
	var date2String = new String(date2Form.value);
	// format of string is yyyy/mm/dd
	var year1 = date1String.substring(0,4);
	var year2 = date2String.substring(0,4);
	var month1 = parseTimeString(date1String.substring(5,7),"hour");
	var month2= parseTimeString(date2String.substring(5,7),"hour");
	var day1 = parseTimeString(date1String.substring(8,10),"hour");
	var day2= parseTimeString(date2String.substring(8,10),"hour");
	if (isReturnChecked(inFormChBox)) {
		if (!isCalcPoss(selectObjDep2.options[selectObjDep2.selectedIndex].text)) {
			alert("Please choose a departure location for the second transfer.");
			return false;
		}
		if (!isCalcPoss(selectObjDest2.options[selectObjDest2.selectedIndex].text)) {
			alert("Please choose a destination location for the second transfer.");
			return false;
		}
		if (selectObjDep2.options[selectObjDep2.selectedIndex].text ==
			selectObjDest2.options[selectObjDest2.selectedIndex].text) {
			alert("Please select different locations for the second transfer.");
			return false;
		}
		if (!date2Form.value || date2Form.value =="") {
		alert("Please select a date for the second transfer.");return false;
		}
		var selectedtime1 = eval("inForm." + dep1t);
		var selectedtime2 = eval("inForm." + dep2t);
		var time1 = selectedtime1.options[selectedtime1.selectedIndex].text;
		var time2= selectedtime2.options[selectedtime2.selectedIndex].text;
		if (dateNotPossible(day1,month1,year1)) return false;
		if (!checkTransferDates(day1,day2,month1,month2,year1,year2,time1,time2,inFormChBox)) return false;
	}
	if (dateNotPossible(day1,month1,year1)) return false;
	return;
}

function defineLocation(inForm,selected) {
	var regArray =  new Array("('Select','',true,true)",
		"('Lisboa')",
		"('Fátima')",
		"('Porto')",
		"('Sevilha')",
		"('Madrid')",
		"('Málaga')",
		"('Other')"
		);
	/*
	var spainArray =  new Array("('Select','',true,true)",
		"('Sevilha')",
		"('Other')"
		);
	
	var westernArray =  new Array("('Select','',true,true)",
		"('Açoteias')",
		"('Albufeira')",
		"('Aljezur')",
		"('Alvor')",
		"('Arm. de Pêra')",
		"('Burgau')",
		"('Carvoeiro')",
		"('Ferragudo')",
		"('Galé')",
		"('Lagoa')",
		"('Lagos')",
		"('Monchique')",
		"('P. da Luz')",
		"('P. da Rocha')",
		"('P. do Vau')",
		"('Portimão')",
		"('Quarteira')",
		"('Q. do Lago')",
		"('Sagres')",
		"('Salema')",
		"('V. do Lobo')",
		"('Vilamoura')",
		"('Other')"
		);
	
	var easternArray =  new Array("('Select','',true,true)",
		"('Monte Gordo')",
		"('Tavira')",
		"('Other')"
		);
	*/
	var algarveArray =  new Array("('Select','',true,true)",
		"('Faro Airport')",
		"('Monte Gordo')",
		"('Tavira')",
		"('Açoteias')",
		"('Albufeira')",
		"('Aljezur')",
		"('Alvor')",
		"('Arm. de Pêra')",
		"('Burgau')",
		"('Cabanas de Tavira')",
		"('Carvoeiro')",
		"('Ferragudo')",
		"('Galé')",
		"('Lagoa')",
		"('Lagos')",
		"('Monchique')",
		"('Praia da Luz')",
		"('Praia da Rocha')",
		"('Praia do Vau')",
		"('Portimão')",
		"('Quarteira')",
		"('Quinta do Lago')",
		"('Sagres')",
		"('Oura')",
		"('Salema')",
		"('Vale do Lobo')",
		"('Vilamoura')",
		"('Islantilla Spain')",
		"('Isla Canela Spain')",
		"('Other')"
		);
	
	var airportArray =  new Array("('Select','',true,true)",
		"('Faro Airport')"
		);
	
	var destArray =  new Array("('Destination','',true,true)",
		"('Airport')",
		"('Algarve')",
		"('Others (P/ES)')"
		);
	
	var depArray =  new Array("('Departure','',true,true)",
		"('Airport')",
		"('Algarve')",
		"('Portugal')",
		"('Spain')"
		);
	
	var emptyArray =  new Array("('---------------','',true,true)",
		"('')"
		);
	
	//if (selected == "Eastern Algarve") selectedArray = easternArray;
	if (selected == "Algarve") selectedArray = algarveArray;	
	//if (selected == "Spain") selectedArray = spainArray;
	//if (selected == "Portugal") selectedArray = portRegArray;
	if (selected == "Others") selectedArray = regArray;
	if (selected == "Airport") selectedArray = airportArray;
	if (selected == "Departure") selectedArray = emptyArray;
	if (selected == "Destination") selectedArray = emptyArray;
	if (selected == "empty") selectedArray = emptyArray;
	
	while (selectedArray.length < inForm.options.length) {
		inForm.options[(inForm.options.length - 1)] = null;
	}
	
	for (var i=0; i < selectedArray.length; i++) {
			eval("inForm.options[i]=" + "new Option" + selectedArray[i]);
	}
}

function setRegionSelectBox() {
	
}
// inForm is the form to set
function setSelectionInSelectBox(selectedValue,inForm) {
	var count = inForm.options.length;
	for (var i = 0; i < count; i++) {
		if ( selectedValue == inForm.options[i].text) {
			inForm.options[i].selected = inForm.options[i];
		}
	}
}

function setRadioSelection(value,radioObj) {
	//if (value == radioObj[0].value) radioObj.checked = radioObj[0];
	//if (value == radioObj[1].value) radioObj.checked = radioObj[1];
	if (value == radioObj[0].value) radioObj[0].checked = 1;
	if (value == radioObj[1].value) radioObj[1].checked = 1;
}

function getRadioValue(radioObj) {
	if (radioObj[0].checked) return radioObj[0].value;
	if (radioObj[1].checked) return radioObj[1].value;
}

function handleReturnLocation(inForm,chBoxSelect,inFormDepRegion,inFormDepLocation,
							  			  inFormDestRegion,inFormDestLocation,
										  inFormDep2Region,inFormDep2Location,
							  			  inFormDest2Region,inFormDest2Location) {
	// See if event was checking or uncheking box
	if (isReturnChecked (chBoxSelect)) {
		// see if dep or dest or valid (not "Other")
		if (isDirectionValid(inFormDepLocation.options[inFormDepLocation.selectedIndex].text,
							 inFormDestLocation.options[inFormDestLocation.selectedIndex].text)) {
			// the first departure location is valid.....
			if (isCalcPoss(inFormDepLocation.options[inFormDepLocation.selectedIndex].text)) {
				// set the selection of the right selection in region, needed for calling defineLocation
				//setSelectionInSelectBox(inFormDepRegion.options[inFormDepRegion.selectedIndex].text,
										//inFormDest2Region);
				setRadioSelection(getRadioValue(inFormDepRegion),inFormDest2Region);
				// then go to fill array into the second destination-location selectbox
				//defineLocation(inFormDest2Location,inFormDest2Region.options[inFormDest2Region.selectedIndex].text);
				defineLocation(inFormDest2Location,getRadioValue(inFormDest2Region));
				// set the selection of the right selection in region
				setSelectionInSelectBox(inFormDepLocation.options[inFormDepLocation.selectedIndex].text,
										inFormDest2Location);
			}
			if (isCalcPoss(inFormDestLocation.options[inFormDestLocation.selectedIndex].text)) {
				// set the selection of the right selection in region, needed for calling defineLocation
				//setSelectionInSelectBox(inFormDestRegion.options[inFormDestRegion.selectedIndex].text,
										//inFormDep2Region);
				setRadioSelection(getRadioValue(inFormDestRegion),inFormDep2Region)
				// then go to fill array into the second departure-location selectbox
				//defineLocation(inFormDep2Location,inFormDep2Region.options[inFormDep2Region.selectedIndex].text);
				defineLocation(inFormDep2Location,getRadioValue(inFormDep2Region));
				// set the selection of the right selection in region
				setSelectionInSelectBox(inFormDestLocation.options[inFormDestLocation.selectedIndex].text,
										inFormDep2Location);
			}
		}
		else {
			chBoxSelect.checked = false;
			return false;
		}
	}
	else {
		if (isCalcPoss(inFormDepLocation.options[inFormDepLocation.selectedIndex].text) ||
			isCalcPoss(inFormDestLocation.options[inFormDestLocation.selectedIndex].text)) {
			//setSelectionInSelectBox("Destination",inFormDest2Region);
			setRadioSelection("Algarve",inFormDest2Region);
			//defineLocation(inFormDest2Location,inFormDest2Region.options[inFormDest2Region.selectedIndex].text);
			defineLocation(inFormDest2Location,getRadioValue(inFormDest2Region));
			//setSelectionInSelectBox("Departure",inFormDep2Region);
			setRadioSelection("Algarve",inFormDep2Region);
			//defineLocation(inFormDep2Location,inFormDep2Region.options[inFormDep2Region.selectedIndex].text);
			defineLocation(inFormDep2Location,getRadioValue(inFormDep2Region));
		}
	}
	if (isDirectionValid(inFormDepLocation.options[inFormDepLocation.selectedIndex].text,
							 inFormDestLocation.options[inFormDestLocation.selectedIndex].text)) {
		calculateRate(inForm,chBoxSelect,
					  inFormDepLocation.options[inFormDepLocation.selectedIndex].text,
					  inFormDestLocation.options[inFormDestLocation.selectedIndex].text,
					  inFormDep2Location.options[inFormDep2Location.selectedIndex].text,
					  inFormDest2Location.options[inFormDest2Location.selectedIndex].text);
	}
	else {
		return false;
	}
}

function isReturnChecked (chBoxObj) {
	if (chBoxObj.checked) return true;
	return false;
}

function radioObjValue(obj) {
	for (var i = 0; i < obj.length; i++) {
		if (obj[i].checked) return obj[i].value;
	}
	return false;
}

function isSelectionNeeded(inFormObject,chBox,inForm) {
	//var numWays = radioObjValue(radioObj);
	if (!isReturnChecked(chBox)) {
		if (inFormObject == "depart2" ||
			inFormObject == "chdep2" ||
			inFormObject == "dest2" ||
			inFormObject == "chdest2") {
			alert("This part of the form is for the return trasnfer!\nCheck the RETURN box to book a return transfer.");
			inForm.elements[2].focus();
			return;
		}		
	}
	return false;
}

function getBaseRate(num) {
	if (num == 3) return baseRateTaxi;
	if (num == 4) return baseRateMinibus;
	if (num == 6) return baseRateMediumbus;
	if (num == 7) return baseRateBus;
	if (num == 5) return baseRatePers;
	return 0;
}

function calulateDiffRate(depRate,destRate,depDir,destDir,numPers) {
	baseRate = getBaseRate(numPers);
	if (depDir == destDir) {
		if (parseFloat(depRate) > parseFloat(destRate)) {
			return parseFloat(depRate) - parseFloat(destRate) + parseFloat(baseRate);
		}
		if (parseFloat(destRate) > parseFloat(depRate)) {
			return parseFloat(destRate) - parseFloat(depRate) + parseFloat(baseRate);
		}
	}
	else {
		return parseFloat(destRate) + parseFloat(depRate) - parseFloat(baseRate);
	}
	return 0;
}

function getDesiredValueFromDataString(inForm,identString,posIdent,posDesiredValue,regEx) {
	for (i = 0; i < inForm.elements.length; i++) {
		if (regEx.exec(inForm.elements[i].name)) {
			// get the dataset from hidden field value
			name = inForm.elements[i].value;
			// separate the unique values
			valStrArr = name.split('-');
			// try if the identification is the right one and catch the desired value
			if (valStrArr[posIdent] == identString) return valStrArr[posDesiredValue];
		}
	}
	return 0;
}
/*
// stuff for location-dataset in hidden-fields "loc*"
var posLoc = 0; 
var posZone = 1
// stuff for rating-dataset in hidden-fields "zone*"
// more find in function getBaseRate()
var posZoneName = 0;
var posDir = 1;
var posZoneUp = 2;
var zeroLoc = "Faro Airport";
var baseRateTaxi = 8;
var baseRateMinibus = 12;
var baseRatePers = 2;
// to identify right hidden fields
var regExZone = /^zone/;
var regExLoc = /^loc/;
*/
function calculateRate(inForm,chBox,dep1,dest1,dep2,dest2) {
	if (dep1 == "Other" || dest1 == "Other") {
		popUpContact();
	}
	var rate = 0;
	var numPers = inForm.elements[0].options[inForm.elements[0].selectedIndex].value;
	// get zones from first departure and destination
	var zoneDep1 = getDesiredValueFromDataString(inForm,dep1,posLoc,posZone,regExLoc);
	var zoneDest1 = getDesiredValueFromDataString(inForm,dest1,posLoc,posZone,regExLoc);
	// Transfers in the same zone shouldn't be possible
	if (zoneDest1 != 0 && zoneDest1 == zoneDep1) {
		alert("Sorry, we do not provide service from and to the same location.");
		// set selectboxes to default
		//setSelectionInSelectBox("Destination",calcrate.chooserdest1);
		setRadioSelection('Algarve',chdest1);
		//defineLocation(calcrate.dest1,calcrate.chdest1.options[calcrate.chooserdest1.selectedIndex].text);
		defineLocation(calcrate.dest1,getRadioValue(calcrate.chdest1));
		return false;
	}
	
	// get directions of first departure and destination from zero-location(Faro Airport) on
	var dirDep1 = getDesiredValueFromDataString(inForm,zoneDep1,posZoneName,posDir,regExZone);
	var dirDest1 = getDesiredValueFromDataString(inForm,zoneDest1,posZoneName,posDir,regExZone);
	// try if it isn't Faro Airport
	var rateDep1 = getDesiredValueFromDataString(inForm,zoneDep1,posZoneName,numPers,regExZone);
	var rateDest1 = getDesiredValueFromDataString(inForm,zoneDest1,posZoneName,numPers,regExZone);
	if (!isReturnChecked(chBox)) {
		if (isCalcPoss(dep1) && isCalcPoss(dest1)) {
			if (dep1 != zeroLoc && dest1 != zeroLoc) {
				rate = calulateDiffRate(rateDep1,rateDest1,dirDep1,dirDest1,numPers);
			}
			if (dest1 == zeroLoc) rate = rateDep1;
			if (dep1 == zeroLoc) rate = rateDest1;
		}
	}
	else {
		if (dep2 == "Other" || dest2 == "Other") {
			popUpContact();
		}
		var zoneDep2 = getDesiredValueFromDataString(inForm,dep2,posLoc,posZone,regExLoc);
		var zoneDest2 = getDesiredValueFromDataString(inForm,dest2,posLoc,posZone,regExLoc);
		// get directions of first departure and destination from zero-location(Faro Airport) on
		var dirDep2 = getDesiredValueFromDataString(inForm,zoneDep2,posZoneName,posDir,regExZone);
		var dirDest2 = getDesiredValueFromDataString(inForm,zoneDest2,posZoneName,posDir,regExZone);
		// try if it isn't Faro Airport
		var rateDep2 = getDesiredValueFromDataString(inForm,zoneDep2,posZoneName,numPers,regExZone);
		var rateDest2 = getDesiredValueFromDataString(inForm,zoneDest2,posZoneName,numPers,regExZone);
		
		if (isCalcPoss(dep1) && isCalcPoss(dest1) &&
			isCalcPoss(dep2) && isCalcPoss(dest2)) {
			if (dep1 != zeroLoc && dest1 != zeroLoc) {
				rate1 = calulateDiffRate(rateDep1,rateDest1,dirDep1,dirDest1,numPers);
			}
			if (dest1 == zeroLoc) rate1 = rateDep1;
			if (dep1 == zeroLoc) rate1 = rateDest1;
			//rate = eval(getRate(dep1,dest1,numPers) + getRate(dep2,dest2,numPers));
			if (dep2 != zeroLoc && dest2 != zeroLoc) {
				rate2 = calulateDiffRate(rateDep2,rateDest2,dirDep2,dirDest2,numPers);
			}
			if (dest2 == zeroLoc) rate2 = rateDep2;
			if (dep2 == zeroLoc) rate2 = rateDest2;
			rate = parseFloat(rate1) + parseFloat(rate2);
		}
	}
	inForm.result.value = rate;
}

function isCalcPoss(destValue) {
	var validDestArray = new Array(
		'Faro Airport',
		'Monte Gordo',
		'Tavira',
		'Albufeira',
		'Açoteias',
		'Vale do Lobo',
		'Quinta do Lago',
		'Vilamoura',
		'Quarteira',
		'Galé',
		'Arm. de Pêra',
		'Carvoeiro',
	   	'Lagoa',
		'Ferragudo',
		'Portimão',
		'Praia da Rocha',
		'Praia do Vau',
		'Alvor',
		'Lagos',
		'Monchique',
		'Aljezur',
		'Praia da Luz',
		'Burgau',
		'Cabanas de Tavira',
		'Salema',
		'Sagres',
		'Oura',
		'Islantilla Spain',
		'Isla Canela Spain',
		'Lisboa',
		'Fátima',
		'Porto',
		'Sevilha',
		'Málaga',
		'Madrid'
		);
		
	for (var i = 0; i < validDestArray.length; i++) {
		if (validDestArray[i] == destValue) return true;
	}
	return false;
}

function showDebug(debugForm,value) {
//function showDebug(value) {
	//document.calcrate.debug.value = value;
	debugForm.value = value;
}
