var ns4 = (document.layers)? true:false;   //NS 4 
var ie4 = (document.all)? true:false;   //IE 4 
var dom = (document.getElementById)? true:false;   //DOM 


function setToAnchor(ID_Anchor,Name_Anchor,ID_Div,dx)
//Fonction permettant de positionner un DIV à une position occupée par une ancre
{
	var pos;
	var DivLeft = 0;   //Position du Div par rapport au côté gauche de la page 
	var DivTop = 0;   //Position du Div par rapport au haut de la page 
	if (dom)
	{
		pos = document.getElementById(ID_Anchor);
		DivLeft = getLeft(pos);
		DivTop = getTop(pos);
		document.getElementById(ID_Div).style.left = DivLeft;
		document.getElementById(ID_Div).style.top = DivTop+dx;
	}
	else if (ie4) 
	{
		pos = document.all[ID_Anchor];
		DivLeft = getLeft(pos);
		DivTop = getTop(pos);
		document.all[ID_Div].style.posLeft = DivLeft;
		document.all[ID_Div].style.posTop = DivTop+dx;
	}
	else if (ns4)
	{
		pos = document.anchors[Name_Anchor];
		DivLeft = pos.x;
		DivTop = pos.y;
		document.layers[ID_Div].pageX = DivLeft;
		document.layers[ID_Div].pageY = DivTop+dx;
	}
}

function getLeft(MyObject)
//Fonction permettant de connaître la position d'un objet
//par rapport au bord gauche de la page.
//Cet objet peut être à l'intérieur d'un autre objet.
{
	if (MyObject.offsetParent)
		return (MyObject.offsetLeft + getLeft(MyObject.offsetParent));
	else 
		return (MyObject.offsetLeft);
} 
function getTop(MyObject)
//Fonction permettant de connaître la position d'un objet
//par rapport au bord haut de la page.
//Cet objet peut être à l'intérieur d'un autre objet.
{
	if (MyObject.offsetParent)
		return (MyObject.offsetTop + getTop(MyObject.offsetParent));
	else
		return (MyObject.offsetTop);
}

function setFocus(keyCode)
{			
	if(keyCode==40 & document.getElementById(SELECT_Destination)!=null & document.getElementById(SELECT_Destination).style.display	!= "none")
		document.getElementById(SELECT_Destination).focus();		
}	

function echap(keyCode)
{			
	if((keyCode==27 || keyCode==13) & document.getElementById(DIV_Destination)!=null)
	{				
		document.getElementById(TEXT_Destination).focus();
		document.getElementById(DIV_Destination).style.display ="none";	
	}
}
		
function getData(e)
{
	var keyCode = e.keyCode;

	if(document.getElementById(TEXT_Destination).value != '' && !(keyCode==27 || keyCode==13))
	{		
		show_data(document.getElementById(TEXT_Destination).value,Language,'gtacities');
		
		setToAnchor(TEXT_Destination,TEXT_Destination,DIV_Destination,20);

		echap(keyCode);
		setFocus(keyCode);
	}
	else
	{
		if(!(keyCode==27 || keyCode==13))
			document.getElementById(HIDDEN_Destination).value= "";	
		
		document.getElementById(SELECT_Destination).innerHTML			= ''; 
		document.getElementById(DIV_Destination).style.display	= "none";
	}
}

function ShowAge(idSource,idRoomCount,index)
{
	if(document.getElementById(idSource).value == "") 
	{
		document.getElementById(idRoomCount).value = "";
		document.getElementById('DropDownList_CotsCount' + index).value = "";			
		document.getElementById("tdAge" + index).style.display = "none";
		
	}
	else
	{
		if(document.getElementById(idRoomCount).value == "") 
		{
			document.getElementById(idRoomCount).value = "1";
		}
		var j=0;
		var obj  = document.getElementById (idSource);
		
		// recovering hidden fields value and comparing tb1 and db1 to conclude about the child number
		for(var i=1;i<=2;i++)
		{
			var obje  = document.getElementById ("h" + i);
			if(obj.value == obje.value)
			{
				j=i;
			}			
		}	
		var RoomCount	= document.getElementById (idRoomCount);
		if(j!=0)
		{
			var NBChild		= document.getElementById ("n" + j);			
			var coef = 1;
			if(RoomCount.value != "") coef = RoomCount.value;				
			var result= NBChild.value * coef;	
			document.getElementById("tdAge" + index).style.display = "";	
									
			for(var h=result;h<4;h++)
			{
				document.getElementById("tdAge" + index + "e" + h).style.display = "none";
			}				
			for(var k=1;k<result;k++)
			{
				document.getElementById("tdAge" + index + "e" + k).style.display = "";
			}
		}
		else
		{
			document.getElementById("tdAge" + index).style.display = "none";	
			for(var l=1;l<=3;l++)
			{
				document.getElementById("tdAge" + index + "e" + l).style.display = "none";		
			}		
		}	
	}
}		

function trim(text) {
	if (text==null || text.length==0)
		return '';
	var end=text.length;
	do {
		end--;
	} while (end>=0 && text.charAt(end)==' ');
	if (end<0)
		return '';
	var start=0;
	while (text.charAt(start)==' ')
		start++;
	return text.substring(start,end+1);
}	

function displaydiv()
{ 
	if(window.event.type=="keypress" & window.event.keyCode==13)
		document.getElementById(DIV_Destination).style.display = "none";		
}

function GetHotelList(user,ilng,curr)
{	
		
	if(Validate())
	{	
		displayDivWaitPageLoad();
		
		var strDestination		= trim(document.getElementById(HIDDEN_Destination).value);
		var sValue = new Array();
		sValue = strDestination.split('_');
		strDestination = sValue[0];
		
		var destinationtype;
		if(sValue[1] == "0")
			destinationtype="city";
		else
			destinationtype="area";
			
		var strRoomCode = "";
		var strRoomCount = "";
		var strCotsCount = "";
		
		for(var i=0;i<=3;i++)
		{
				document.getElementById('DropDownList_RoomType' + i);	
				if(document.getElementById('DropDownList_RoomType' + i)!=null) 
					strRoomCode	+= trim(document.getElementById('DropDownList_RoomType' + i).value)  + ";";					
				else
					strRoomCode	+= ";";
					
				if(document.getElementById('DropDownList_RoomCount' + i)!=null) 
					strRoomCount	+= document.getElementById('DropDownList_RoomCount' + i).value  + ";";					
				else
					strRoomCount += ";";
				
				if(document.getElementById('DropDownList_CotsCount' + i)!=null) 
					strCotsCount	+= document.getElementById('DropDownList_CotsCount' + i).value  + ";";	
				else
					strCotsCount += ";";				
		}
		
		var strChildAge = "";
		for(var k=0;k<=3;k++)
		{
			var count	= document.getElementById('DropDownList_RoomCount' + k).value;
			for(var l=0;l<=3;l++)
			{
				if(document.getElementById('Dropdownlist_Age' + k + l)!=null && l<count) 
					strChildAge	+= document.getElementById('Dropdownlist_Age' + k + l).value  + ";";	
				else
					strChildAge += ";";	
			}
		}	
		var strFacility = "";
		for(k=0;k<10;k++)
		{
			if(document.getElementById('fc' + k)!=null) 
				if(document.getElementById('fc' + k).checked == true)
					strFacility	+= document.getElementById('fc' + k).value  + ";";		
		}
		
		var strHotelName		= document.getElementById('TextBox_HotelName').value;
		
		var strCategorie		= "";
		if (document.getElementById('selectCat')!=null)
			strCategorie= document.getElementById('selectCat').value;
		
		var strLocation			= "";
		if (document.getElementById('selectCat')!=null)
			strLocation = document.getElementById('selectLocation').value;
		
		var strCheckindate		= document.getElementById('TextBox_ArrivalDate').value;
		var strDuration			= document.getElementById('Textbox_OvernightPeriods').value;
		
		var strUrl = "Hotel_List.aspx?user="+user+"&curr="+curr+"&ilng="+ilng+"&destinationtype="+destinationtype
			+ "&destinationcode=" + strDestination + "&checkindate=" + strCheckindate + "&duration=" + strDuration + "&roomcode=" + strRoomCode
			+ "&roomscount=" + strRoomCount + "&cotscount=" + strCotsCount + "&age=" + strChildAge + "&facility=" + strFacility + "&hotelname=" + strHotelName
			+ "&categrie=" + strCategorie + "&location=" + strLocation;

		this.location = strUrl;
	}
}		

function Validate()
{
	var blnError = false;
	
	/// hide notifications	
	DisplayNotification(0);
	
	var objTextBox_Destination		= document.getElementById(HIDDEN_Destination);
	var objTextBox_ArrivalDate		= document.getElementById("TextBox_ArrivalDate");
	var objTextBox_DepartureDate	= document.getElementById("TextBox_DepartureDate");
		
	if(objTextBox_Destination.value == "")
	{
	 	DisplayNotification(1);
	 	document.getElementById("divMsg").style.display = "";
		blnError = true;
	}
	else
		document.getElementById("divMsg").style.display = "none";
		
	
	if(objTextBox_ArrivalDate.value == "")
	{
	 	DisplayNotification(1);
		blnError = true;
	}
	else if (!isDate(objTextBox_ArrivalDate.value))
		{
			DisplayNotification(2);
			blnError = true;
		}
		else
		{
			var currentTime =  new Date();
			currentTime = currentTime.getDate()+'/'+(currentTime.getMonth()+1) + '/'+currentTime.getFullYear();
			if (!GetDays(currentTime,objTextBox_ArrivalDate.value))
			{
				DisplayNotification(3);
				blnError = true;
			}
		
			if(objTextBox_DepartureDate.value != "" && isDate(objTextBox_DepartureDate.value) && !GetDays(objTextBox_ArrivalDate.value,objTextBox_DepartureDate.value))
			{			
				DisplayNotification(2);
				blnError = true;				
			}
			
		}
	
	if(objTextBox_DepartureDate.value == "")
	{
	 	DisplayNotification(1);
		blnError = true;
	}
	else if (!isDate(objTextBox_DepartureDate.value))
		{
			DisplayNotification(2);
			blnError = true;
		}
		else
		{
			var currentTime =  new Date();
			currentTime = currentTime.getDate()+'/'+(currentTime.getMonth()+1) + '/'+currentTime.getFullYear();
			if (!GetDays(currentTime,objTextBox_DepartureDate.value))
			{
				DisplayNotification(4);
				blnError = true;
			}
			
		}
	
	
	SwitchValidatorsStatus(true);
	
	return (!blnError);
		
}	

function DisplayNotification(iError)
{
	var oDiv;
	
	oDiv = document.getElementById("divnotification");
	if (iError == 0)
		oDiv.style.display = 'none';
	else
		oDiv.style.display = '';
	switch (iError)
	{
		case 0 :
			oDiv = document.getElementById("Label_PleaseFillData");
			oDiv.style.display = 'none';
			oDiv = document.getElementById("Label_Bad_Date");
			oDiv.style.display = 'none';
			oDiv = document.getElementById("Label_Compare_DepartDate");
			oDiv.style.display = 'none';
			oDiv = document.getElementById("Label_Compare_ArriveDate");
			oDiv.style.display = 'none';
		break;
		case 1 :
			oDiv = document.getElementById("Label_PleaseFillData");
			oDiv.style.display = '';
		break;
		case 2 :
			oDiv = document.getElementById("Label_Bad_Date");
			oDiv.style.display = '';
		break;		
		case 3 :
			oDiv = document.getElementById("Label_Compare_ArriveDate");
			oDiv.style.display = '';
		break;		
		case 4 :
			oDiv = document.getElementById("Label_Compare_DepartDate");
			oDiv.style.display = '';
		break;		
		case 5 :
			oDiv = document.getElementById("Label_Compare_ArriveDate");
			oDiv.style.display = '';
			oDiv = document.getElementById("Label_Compare_DepartDate");
			oDiv.style.display = '';
		break;		
	}
	location.href = "#";
}		

function SwitchValidatorsStatus(bStatus)
{
	var objValidator = document.getElementById('RequiredFieldValidator_ArrivalDate');
	if (objValidator)
		ValidatorEnable(objValidator, bStatus);
		
	objValidator = document.getElementById('ArrivalDateValidator');
	if (objValidator)
		ValidatorEnable(objValidator, bStatus);
		
	objValidator = document.getElementById('CustomValidator_ArrivalDate');
	if (objValidator)
		ValidatorEnable(objValidator, bStatus);
		
	objValidator = document.getElementById('RequiredFieldValidator_DepartureDate');
	if (objValidator)
		ValidatorEnable(objValidator, bStatus);
		
	objValidator = document.getElementById('DepartureDateValidator');
	if (objValidator)
		ValidatorEnable(objValidator, bStatus);
		
	objValidator = document.getElementById('CustomValidator_DepartureDate');
	if (objValidator)
		ValidatorEnable(objValidator, bStatus);
		
	objValidator = document.getElementById('CompareValidator_Dates');
	if (objValidator)
		ValidatorEnable(objValidator, bStatus);
		
}