// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //
// BEWARE - this javascript also appears inline on search paths.        //
// templates/blocks/search/formlayout and formlayout-villa              //
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //


var rooms

function AddRooms(){
	
	if( document.getElementById('rooms') ){
		rooms = document.getElementById('rooms').value;
	}

	for (i=1; i<=3; i++) {
		
		if (i <= rooms) {	
			ShowContent( 'room' + i );
			AddChildAges( i );
		}else{
			HideContent( 'room'+i );
			NullContent( i );
			AddChildAges( i );
		}

	}

}

function AddChildAges( d ){
	
	children = 0;
	if( document.getElementById('children'+d) ){
		children = document.getElementById('children'+d).value;
	}
	
	for (x=1; x<=4; x++) {
		HideContent( 'room_'+d+'_childage' );
		HideContent( 'room_'+d+'_childage_'+x );
	}
		   
	for (x=1; x<=children; x++) {
		ShowContent( 'room_'+d+'_childage' )
		ShowContent( 'room_'+d+'_childage_'+x )
		
		if( document.getElementById('room_'+d+'_childages_'+x).value == ''){
			document.getElementById('room_'+d+'_childages_'+x).value = 7;
		}
	}
	
}


function AddChildAge(){
	
	children = 0;
	if( document.getElementById('children') ){
		children = document.getElementById('children').value;
	}
	
	for (x=1; x<=4; x++) {
		HideContent( 'room_1_childage' );
		HideContent( 'room_1_childage_'+x );
	}
		   
	for (x=1; x<=children; x++) {
		ShowContent( 'room_1_childage' )
		ShowContent( 'room_1_childage_'+x )
	}
	
}

function LoodRooms(){
	
	rooms = document.getElementById('rooms').value;

	for (i=1; i<=3; i++) {
		
		if (i <= rooms) {	
			ShowContent( 'room' + i );
			AddChildAges( i );
		}else{
			HideContent( 'room'+i );
			NullContent( i );
			AddChildAges( i );
		}

	}
		
}

function NullContent(d){
		
	if( document.getElementById('adults'+d) != null ){
		document.getElementById('adults'+d).value = 2;
	}

	if( document.getElementById('children'+d) != null ){
		document.getElementById('children'+d).value = 0;
	}

	if( document.getElementById('infants'+d) != null ){
		document.getElementById('infants'+d).value = 0;
	}

}

function HideContent(d){

	if(d.length < 1){
		return;
	}
	
	if( document.getElementById(d) ){
		document.getElementById(d).style.display = "none";
		document.getElementById(d).style.visibility = "hidden";
	}

}

function ShowContent(d){

	if(d.length < 1){
		
		return;
	}
	
	document.getElementById(d).style.display = "block";
	document.getElementById(d).style.visibility = "visible";
	
}

function ReverseContentDisplay(d) {

	if(d.length < 1){
		return;
	}

	if(document.getElementById(d).style.display == "none"){
		document.getElementById(d).style.display = "block";
		document.getElementById(d).style.visibility = "visible";
	}else{
		document.getElementById(d).style.display = "none";
		document.getElementById(d).style.visibility = "hidden";
	}
}


var xmlHttp

function GetResort(){ 

	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null){
 		alert ("Browser does not support HTTP Request")
 		return
 	}
 	
	var url="/geoLevel"
	url=url+"/"+document.getElementById("destination_name").value
	url=url+"/"+document.getElementById("region_name").value

	
	
	xmlHttp.onreadystatechange=stateChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
	
}


function GetRegion(){ 
	
	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null){
 		alert ("Browser does not support HTTP Request")
 		return
 	}
 	
	var url="/geoLevel"
	url=url+"/"+document.getElementById("destination_name").value
 	
	xmlHttp.onreadystatechange=stateChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
	
}
	
function stateChanged(){

	
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 	
		//alert( xmlHttp.responseText );
		document.getElementById("geoLevel").style.display = "block";
		document.getElementById("geoLevel").innerHTML = xmlHttp.responseText;
		
		document.getElementById("geoLevelDefault").style.visibility = "hidden";	
		document.getElementById("geoLevelDefault").display = "none";
		document.getElementById("geoLevelDefault").style.height = "1px";
		document.getElementById("geoLevelDefault").innerHTML="";

 	}
 	
}
	
function GetXmlHttpObject(){
	
	var xmlHttp=null;
	
	try{	// Firefox, Opera 8.0+, Safari
 		xmlHttp=new XMLHttpRequest();
 	}catch (e){
 	
	 	//Internet Explorer
	 	try{
	  		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  	}catch (e){
	  		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  	}
  	}

  	return xmlHttp;
  	
}

function searchPanel(){
	
	if( document.getElementById('rooms') ){
		rooms 		= document.getElementById('rooms').value;
	}
	
	destination = document.getElementById('destination_name').value;
	region 		= document.getElementById('region_name').value;
	
	if( destination == 'any'){
		alert("Please select a destination");
		return false;
	}
	
	if( region == 'any' ){
		alert("Please select a region");
		return false;
	}
	
	for (i=1; i<=3; i++) {

		if (i <= rooms) {
			adult = document.getElementById('adults'+i).value;
			
			if( adult == 0){

				alert("There must be an adult in room " + i);
				
				return false;
							
			}
			
		}
		
		children = 0;
		if( document.getElementById('children'+i) ){
			
			children = document.getElementById('children'+i).value;

			for (y=1; y<=children; y++) {
				
				childAge = document.getElementById('room_'+i+'_childages_'+y).value;

				if( childAge == ''){
					
					alert("Child "+y+" in room "+i+" has no age");
					
					return false;
					
				}

				if( childAge > 15){
					
					alert("Child "+y+" in room "+i+" cannot be over 15");
					
					return false;
				}
							
			}
			
			
		}

	}
	
		children = 0;
		if( document.getElementById('children') ){
			
			children = document.getElementById('children').value;

			for (y=1; y<=children; y++) {
				
				childAge = document.getElementById('room_1_childages_'+y).value;

				if( childAge == ''){
					
					alert("Child "+y+" has no age");
					
					return false;
					
				}
				
			}
			
			
		}
	
	
	return true;
}