﻿
//called usually from local index->search.cfm->search.cfc (homedisplay)


var warnedalready=false;
function changepartywarning(warningtext){
	if(!warnedalready){
		warnedalready=true;
		alert(warningtext);
	}
}
function switchcsform() {
	//alert("hello");
	//if(year == 2009) {switchheader(1, 'on2009');sho('searchform');hide('searchform2008');}
	//else {switchform('searchform2008');switchheader(0, 'on2008');loadregions('searchform2008');sho('searchform2008');hide('searchform');}
}
function setRegionName() {
	document.searchform2008.regionName.value = document.searchform2008.region.options[document.searchform2008.region.selectedIndex].text;
}

function loadregions(type) {
	
	while (document.searchform2008.region.firstChild) {
		document.searchform2008.region.removeChild(document.searchform2008.region.firstChild);
	}
	var arrRegions = new Array();
	if(type == "searchform2008") {
		arrRegions = arrRegion;
		document.searchform2008.holidayType.value = 'camp';
	} else if (type == "searchformApartments") {
		arrRegions = arrApts;
		document.searchform2008.holidayType.value = 'ap';
	} else {
		arrRegions = "";
		return;
	}
	arlen = arrRegions.length;
	//clear it first
	var thisgroup = "";
	var lastgroup = "";
	for(var i=0; i<arlen; i++) {
		
		thisgroup = arrRegions[i].Group;
		if(thisgroup !== lastgroup) {
			optG = document.createElement("optgroup");
			optG.label = thisgroup;
			document.searchform2008.region.appendChild(optG);
		}
		
		if(typeof(arrRegions[i].sale) != "undefined" && arrRegions[i].sale == true) {
			document.searchform2008.region.options[i] = new Option(arrRegions[i].RegionName + " SALE",arrRegions[i].RegionCode);
			document.searchform2008.region.options[i].style.color = "#CC0000";
		} else {
			document.searchform2008.region.options[i] = new Option(arrRegions[i].RegionName,arrRegions[i].RegionCode);
		}
		
		if(arrRegion[i].Selected == true) {
			var selected = i;
		} 
		lastgroup = arrRegions[i].Group;
	}
	//document.searchform2008.region.options[selected].selected = true;
}

function switchheader(which,toclass) {
	var h1s =	document.getElementById('tabs').getElementsByTagName('h1');
	h1s[0].className = "off2008";
	h1s[1].className = "off2009";
	h1s[2].className = "offAPTS";
	h1s[which].className = toclass;
}

function switchheaderecuk(which,toclass) {
	var h1s =	document.getElementById('tabs').getElementsByTagName('h1');
	h1s[0].className = "off2008";
	//h1s[1].className = "off2009";
	h1s[1].className = "offAPTS";
	h1s[which].className = toclass;
}

function switchform(toclass) {
	if(toclass == "searchform2008" || toclass == "searchformApartments") {
		//resetmonths(toclass);
		if(typeof document.getElementById('searchform2008').childNodes[0].className == "undefined") {
			document.getElementById('searchform2008').childNodes[1].className = toclass;
			document.getElementById('searchform').childNodes[1].className = toclass;
		} else {
			document.getElementById('searchform2008').childNodes[0].className = toclass;
			document.getElementById('searchform').childNodes[0].className = toclass;
		}
	}	
}



function resetmonths(from,to,selected) {
	//set colfusion month names
	
	while (document.searchform2008.arrivalMonth.firstChild) {
		document.searchform2008.arrivalMonth.removeChild(document.searchform2008.arrivalMonth.firstChild);
	}
	var arrMonths = new Array();
	arrMonths = arrmonthtxt;
  
	var cnt=0;
	for(var i=from; i<to; i++) {
		document.searchform2008.arrivalMonth.options[cnt] = new Option(arrMonths[i],i+1);
		cnt++;
	} 	
	document.searchform2008.arrivalMonth.selectedIndex = selected;
	
	/*while (document.searchform2008.arrivalYear.firstChild) {
		document.searchform2008.arrivalYear.removeChild(document.searchform2008.arrivalYear.firstChild);
	}
	
	if(document.searchform2008.holidayType.value == 'ap') { // this is apartments
		document.searchform2008.arrivalYear.options[0] = new Option(2008,2008);
	} else {
		document.searchform2008.arrivalYear.options[0] = new Option(2008,2008);
		document.searchform2008.arrivalYear.options[1] = new Option(2009,2009);
	}*/
}

function validatesearch(form)
{  
	if(form == "searchform2008") {
		var numparty = (parseInt(document.searchform2008.adults.value) + parseInt(document.searchform2008.children.value) + parseInt(document.searchform2008.infants.value));
		var numpets = 	(parseInt(document.searchform2008.pets.value));
		var region = document.searchform2008.region.value;
		var aday =  document.searchform2008.arrivalDate.value;
		var amonth =  document.searchform2008.arrivalMonth.value;
		var ayear = document.searchform2008.arrivalYear.value;
		
	} else {
		var numparty = (parseInt(document.searchform.adults.value) + parseInt(document.searchform.children.value) + parseInt(document.searchform.infants.value));
		var numpets = 	(parseInt(document.searchform.pets.value));
		var region = document.searchform.regioncode.value;
		var aday =  document.searchform.day.value;
		var amonth =  document.searchform.month.value;
		if(document.searchform.selyear){var ayear = document.searchform.selyear.value;}
		else{var ayear = document.searchform.year.value;}
	}
	
	//get the server date for validation purposes
	var todaysdate = "{ts '2008-11-26 10:10:33'}";
	var regex = new RegExp('\'[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}\\s','gi');
	todaysdate = todaysdate.match(regex);
	todaysdate = todaysdate[0].substr(1,todaysdate[0].length);
	todaysdatearr = todaysdate.split('-');
	
	var todaysdateobj = new Date();
	todaysdateobj.setFullYear(todaysdatearr[0],todaysdatearr[1]-1,todaysdatearr[2]);
	
	var arrivaldateobj = new Date();
	arrivaldateobj.setFullYear(ayear,amonth-1,aday);
	
	var datediff = (arrivaldateobj - todaysdateobj)/60/60/24/1000;
	
	var error = false;
	//validate for number of days left until travel
	if(datediff <= 3 && datediff >= 0) {
		alert("Ihre Suchanfrage liegt innerhalb der kommenden drei Tage. Bitte wenden Sie sich an unser Beratungsteam unter 01 315 10 10 für die aktuellsten Angebote.");
		error = true;
	}
	
	//party members less than 8
	if(numparty > 8) {
		alert ("Es sind max. 8 Personen pro Stellplatz erlaubt, rufen Sie bitte unser Beratungsteam an unter 052 646 01 01. Hier wird man Ihnen helfen, eine passende Unterkunft f\xFCr Ihre Gruppe zu finden.");
		error = true;
	}
	
	//past date
	if(arrivaldateobj < todaysdateobj) {
		alert ("Das Buchungsdatum liegt in der Vergangenheit, bitte ändern Sie Ihre Angaben.");
		error = true;
	}
	
	if(!error) {
		document[form].submit();
	}

}

function switchsearchformCSAP(year,layer,caller) {
	if(layer == "") { //user has called the year box from the 08 or 09 forms, so set the layer
		if(document.getElementById('searchformblock').className == "searchform2008" || document.getElementById('searchformblock').className == "searchform") {
			layer = "cs";
		} else {
			layer = "ap";
		}
	}
	
	if(year == "") {
		year = document.searchform.year.value;
	}
	
	//alert(year + " - " + layer);
	
	var pagetabs = document.getElementById('tabs');
	
	if(layer == "cs") {
		switchform('searchform2008');
		if(pagetabs !== null) {
			switchheaderecuk(0, 'on2008');
		}
		sho('searchform');
		hide('searchform2008');
		if(document.getElementById("ap_regioncode" ) != null && document.getElementById("cs_regioncode") != null) {
			sho('cs_regioncode');
			hide('ap_regioncode');
			sethiddenfields('searchform',document.searchform.cs_regioncode.options[document.searchform.cs_regioncode.options.selectedIndex].value,'cs');
		}
	} else if (layer == "ap") {
		switchform('searchformApartments');
		if(pagetabs !== null) {
			switchheaderecuk(1, 'onAPTS');
		}
		if(year == 2008) {
			loadregions('searchformApartments');
			sho('searchform2008');
			hide('searchform');
		} else if (year == 2009) {
			sho('searchform');
			hide('searchform2008');
			if(document.getElementById("ap_regioncode") != null && document.getElementById("cs_regioncode") != null) {
				sho('ap_regioncode');
				hide('cs_regioncode');
				sethiddenfields('searchform',document.searchform.ap_regioncode.options[document.searchform.ap_regioncode.options.selectedIndex].value,'ap');
			}
		}
	}
	copyform(year,layer,caller);
	if(typeof dow == "function"){
		dow();
	}
}

function sethiddenfields(form,regioncode,serviceprovider) {
	document[form].regioncode.value = regioncode;
	document[form].serviceprovider.value = serviceprovider;
}

function copyform(year,layer,caller) {
	if(year == 2008 && layer == "ap") {
		document.searchform2008.arrivalDate.selectedIndex = document.searchform.day.selectedIndex;
		document.searchform2008.arrivalMonth.selectedIndex = document.searchform.month.selectedIndex;
		document.searchform2008.arrivalYear.selectedIndex = 0;
		if(document.searchform.selyear && layer == "ap"){document.searchform.selyear.selectedIndex = 1;}
		if(document.searchform.regioncode.selectedIndex !== undefined) {
			document.searchform2008.region.selectedIndex = document.searchform.regioncode.selectedIndex;
		} else {
			if(layer == "ap") {
				document.searchform2008.region.selectedIndex = document.searchform.ap_regioncode.selectedIndex;
			} else {
				document.searchform2008.region.selectedIndex = document.searchform.cs_regioncode.selectedIndex;
			}
		}
		document.searchform2008.nights.selectedIndex = document.searchform.nights.selectedIndex;
		document.searchform2008.adults.selectedIndex = document.searchform.adults.selectedIndex;
		document.searchform2008.children.selectedIndex = document.searchform.children.selectedIndex;
		document.searchform2008.infants.selectedIndex = document.searchform.infants.selectedIndex;
		if(document.searchform2008.pets.selectedIndex !== undefined) {
			document.searchform2008.pets.selectedIndex = document.searchform.pets.selectedIndex;
		}		
		
	} else {
		document.searchform2008.arrivalYear.selectedIndex = 0;
		if(document.searchform.regioncode.selectedIndex !== undefined) {
			document.searchform.regioncode.selectedIndex = document.searchform2008.region.selectedIndex;
		} else {
			if(layer == "ap") {
				if(caller == "tab"){document.searchform.ap_regioncode.selectedIndex = 0;}
				else {document.searchform.ap_regioncode.selectedIndex = document.searchform2008.region.selectedIndex;}
			}		
		}
		if(document.searchform.selyear && layer == "ap"){document.searchform.selyear.selectedIndex = 1;}
		else if(document.searchform.selyear && layer == "cs" && year == 2009){document.searchform.selyear.selectedIndex = 1;}
		else if(document.searchform.selyear && layer == "cs" && year == 2008){document.searchform.selyear.selectedIndex = 0;}
		if((layer == "ap" && caller == "year") || (caller == "tab" && layer == "cs" && year == 2008)) {
			document.searchform.day.selectedIndex = document.searchform2008.arrivalDate.selectedIndex;
			document.searchform.month.selectedIndex = document.searchform2008.arrivalMonth.selectedIndex;
			document.searchform.nights.selectedIndex = document.searchform2008.nights.selectedIndex;
			document.searchform.adults.selectedIndex = document.searchform2008.adults.selectedIndex;
			document.searchform.children.selectedIndex = document.searchform2008.children.selectedIndex;
			document.searchform.infants.selectedIndex = document.searchform2008.infants.selectedIndex;
			if(document.searchform.pets.selectedIndex !== undefined) {
				document.searchform.pets.selectedIndex = document.searchform2008.pets.selectedIndex;
			}		
		}
	}
}

function switchsearchform(year,layer) {
	var pagetabs = document.getElementById('tabs');
	if(year==2008 && layer == "campsites") {
		switchform('searchform2008');
		if(pagetabs !== null) {
			switchheaderecuk(0, 'on2008');
		}
		//loadregions('searchform2008');
		sho('searchform');
		hide('searchform2008');
		
		//document.searchform2008.arrivalDate.selectedIndex = document.searchform.day.selectedIndex;
		//document.searchform2008.arrivalMonth.selectedIndex = document.searchform.month.selectedIndex;
		document.searchform2008.arrivalYear.selectedIndex = 0;
		if(document.searchform.selyear){document.searchform.selyear.selectedIndex = 1;}
		document.searchform2008.region.selectedIndex = document.searchform.regioncode.selectedIndex;
		document.searchform2008.nights.selectedIndex = document.searchform.nights.selectedIndex;
		document.searchform2008.adults.selectedIndex = document.searchform.adults.selectedIndex;
		document.searchform2008.children.selectedIndex = document.searchform.children.selectedIndex;
		document.searchform2008.infants.selectedIndex = document.searchform.infants.selectedIndex;
		if(document.searchform2008.pets.selectedIndex !== undefined) {
			document.searchform2008.pets.selectedIndex = document.searchform.pets.selectedIndex;
		}		
	} else if(year==2008 && layer == "apartments") {
		switchform('searchformApartments');
		if(pagetabs !== null) {
			switchheaderecuk(1, 'onAPTS');
		}
		loadregions('searchformApartments');sho('searchform2008');hide('searchform');
		document.searchform2008.arrivalYear.selectedIndex = 0;
	  if(document.searchform.selyear){document.searchform.selyear.selectedIndex = 1;}
	} else if(year==2009){
		switchform('searchform');
		if(pagetabs !== null) {
			switchheaderecuk(0, 'on2008');
		}
		sho('searchform');hide('searchform2008');
		if(document.searchform.selyear){document.searchform.selyear.selectedIndex = 1;}
			document.searchform2008.arrivalYear.selectedIndex = 0;
			document.searchform.regioncode.selectedIndex = document.searchform2008.region.selectedIndex;
			document.searchform.day.selectedIndex = document.searchform2008.arrivalDate.selectedIndex;
			document.searchform.month.selectedIndex = document.searchform2008.arrivalMonth.selectedIndex;
			document.searchform.nights.selectedIndex = document.searchform2008.nights.selectedIndex;
			document.searchform.adults.selectedIndex = document.searchform2008.adults.selectedIndex;
			document.searchform.children.selectedIndex = document.searchform2008.children.selectedIndex;
			document.searchform.infants.selectedIndex = document.searchform2008.infants.selectedIndex;
			if(document.searchform.pets.selectedIndex !== undefined) {
				document.searchform.pets.selectedIndex = document.searchform2008.pets.selectedIndex;
			}
	}
	if(typeof dow == "function"){
		dow();
	}
}


