String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

var chkerr = new Array();
chkerr[0] = lang["js_emptydepcity"];
chkerr[1] = lang["js_shortdepcity"];
chkerr[2] = lang["js_emptyretcity"];
chkerr[3] = lang["js_shortretcity"];
chkerr[4] = lang["js_emptydepdate"];
chkerr[5] = lang["js_baddepdate"];
chkerr[6] = lang["js_emptyretdate"];
chkerr[7] = lang["js_badretdate"];
chkerr[8] = lang["js_retdateantdepdate"];
chkerr[9] = lang["js_depdatebeforetoday"];
chkerr[10] = lang["js_depdateistoday"];
chkerr[11] = lang["js_samefromandto"];
	
function checkAll(Liste){
	for (i = 0; i < Liste.length; i++) Liste[i].checked = true ;
}
function checkfields() {
	var valid = true;
	var err = "";
	var trip_from = document.Search._from.value;
	trip_from = trip_from.trim();
	var trip_to = document.Search.To.value;
	trip_to = trip_to.trim();
	var ar = document.Search.allerRetour[0].checked;
	var date1 = document.Search.date1.value;
	date1 = date1.trim();
	var date2 = document.Search.date2.value;
	date2 = date2.trim();
	
	var now = new Date();
	if (trip_from.length == 0) {
		err += chkerr[0];
		valid = false;
	}
	else if (trip_from.length < 3) {
		err += chkerr[1];
		valid = false;
	}
	if (trip_to.length == 0) {
		err += chkerr[2];
		valid = false;
	}
	else if (trip_to.length < 3) {
		err += chkerr[3];
		valid = false;
	}
	if (date1.length == 0) {
		err += chkerr[4];
		valid = false;
	}
	else if (date1.length == 10) {
		day1 = date1.substring(0,2);
		month1 = date1.substring(3,5);
		year1 = date1.substring(6);
		firstS = date1.substring(2,3);
		lastS = date1.substring(5,6);
		if (firstS != "/" || lastS != "/" || isNaN(day1) || isNaN(month1) || isNaN(year1)) {
			err += chkerr[5];
			valid = false;
		}
		else if (new Date(year1, month1-1, day1) < now) {
			if (now.getDate() == day1 && now.getMonth() == month1-1 && now.getFullYear() == year1) {
				if (!confirm(chkerr[10]))
					valid = false;
			}
			else {
				err += chkerr[9];
				valid = false;
			}
		}
	}
	else {
		err += chkerr[5];
		valid = false;
	}
	if (ar) {
		if (date2.length == 0) {
			err += chkerr[6];
			valid = false;
		}
		else if (date2.length == 10) {
			day2 = date2.substring(0,2);
			month2 = date2.substring(3,5);
			year2 = date2.substring(6);
			firstS = date2.substring(2,3);
			lastS = date2.substring(5,6);
			if (firstS != "/" || lastS != "/" || isNaN(day2) || isNaN(month2) || isNaN(year2)) {
				err += chkerr[7];
				valid = false;
			}
		  else if (new Date(year1, month1-1, day1)>new Date(year2, month2-1, day2)) {
				err += chkerr[8];
				valid = false;
			}
		}
		else {
			err += chkerr[7];
			valid = false;
		}
	}
	var trip_from_match = document.Search.trip_from.value;
	if (trip_from_match == "") trip_from_match = trip_from;
	var trip_to_match = document.Search.tripTo.value;
	if (trip_to_match == "") trip_to_match = trip_to;
	if (trip_from_match != "" && trip_from_match == trip_to_match) {
			err += chkerr[11];
			valid = false;
	}
	
	if (valid) {
		document.Search._from.value = trip_from;
		if (document.Search.trip_from.value == "")
			document.Search.trip_from.value = trip_from;
		document.Search.To.value = trip_to;
		if (document.Search.tripTo.value == "")
			document.Search.tripTo.value = trip_to;
		document.Search.date1.value = date1;
		document.Search.date2.value = date2;
	 	
	 	var poped=0;
		for (i=0, n=document.Search.elements.length; i<n; i++){
			if (document.Search.elements[i].name.indexOf('pop') != -1){
				if(document.Search.elements[i].checked==true){
					poped=poped+1;
					Partname=document.Search.elements[i].id;
					var _AR = "false";
					if (ar) _AR = "true";
					address="/redirect.php?PART="+Partname+"&type=bookmark&tripFrom="+escape(trip_from_match)+"&tripTo="+escape(trip_to_match)+"&NbAdults="+document.Search.NbAdults.value+"&NbEnfants="+document.Search.NbEnfants.value+"&NBBaby="+document.Search.NBBaby.value+"&allerRetour="+_AR+"&date1="+escape(document.Search.date1.value)+"&date2="+escape(document.Search.date2.value)+"&class=0";
					pop(address,Partname, poped);
				}
			}
		}
		document.Search.submit();
	}else{
		document.getElementById("status").innerHTML = err;
	}
}


function pop(address, Partname,poped){
	var width = '640';
	var height = '800';
	var browser=navigator.appName;
	var p = 'scrollbars=yes,resizable=yes,toolbar=yes,' + //these are obvious variables. set "yes" or "no".
	'menubar=yes,status=yes,location=yes,left='+poped*30+',top=0,height=' + //the location on the user's screen
	height + ',width=' + width;
	win=window.open(address,Partname,p);
	if (typeof(win) != undefined  && win!=null){
		win.blur();
	}
	window.focus();
}

if (typeof(doUp)=="undefined") {
	function doUp(keyUp) {
		//enter
		if (keyUp == 13) {
			if (!autocompl || (autocompl && airport.length == 0))
				//
				if (typeof(CountryCode)=="undefined"){
					checkfields();
				}else{
					if (CountryCode=="ES") document.getElementById("status").innerHTML = "<center>Haga clic en 'Buscar' para iniciar su busqueda<br><img src=http://img.jetcost.com/images/website/jetcost/arrow-down.gif></center>";
					else checkfields();
				}
				
		}
	}
}
function changeAllerRetour(){
	var isChecked = document.Search.allerRetour[0].checked;
	if(isChecked){
		document.getElementById("retour").style.visibility='visible';
	}
	else {
		document.getElementById("retour").style.visibility='hidden';
	}
}
function hideReturn(){
	document.getElementById("retour").style.visibility='hidden';
}

if (typeof(checkDate)=="undefined") {
	function checkDate(date) {
		//var dateArr = date.split("/");
		//if (dateArr.length==3 && dateArr[2].length==4 && dateArr[1].length==2 && dateArr[0].length==2 && !isNaN(dateArr[2]) && !isNaN(dateArr[1]) && !isNaN(dateArr[0])) 
		if (date.length == 10) {
			var day = date.substring(0,2);
			var firstS = date.substring(2,3);
			var month = date.substring(3,5);
			var lastS = date.substring(5,6);
			var year = date.substring(6);
			if (firstS == "/" && lastS == "/" && !isNaN(day) && !isNaN(month) && !isNaN(year)) // return new Array(dateArr[0],dateArr[1],dateArr[2]);
			return new Date(Number(year),Number(month)-1,Number(day));
		}
		return false;
	}
}

function showHideOptions() {
	var optionsDiv = document.getElementById("flightOptions");
	var optionsDiv2 = document.getElementById("flightOptions2");
	var optImg = document.getElementById("optImg");
	if (optionsDiv.style.display=='none') {
		optImg.src = optImg.src.replace('plus.gif','moins.gif');
		optionsDiv.style.display='block';
		if (optionsDiv2!=null) optionsDiv2.style.display='block';
	}
	else {
		optImg.src = optImg.src.replace('moins.gif','plus.gif');
		optionsDiv.style.display='none';
		if (optionsDiv2!=null) optionsDiv2.style.display='none';
	}
}
