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,'hbcities');
		
		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 displaydiv()
{ 
	if(window.event.type=="keypress" & window.event.keyCode==13)
		document.getElementById(DIV_Destination).style.display = "none";		
}

function displayDivWaitPageLoad()
{
	var objDiv = document.getElementById('divWaitPageLoad');
	if (objDiv) objDiv.style.display = '';
}
function ShowRooms()
{
	var obj = document.getElementById ("DropDownList_RoomCount").value;
	for(var i= 0;i<obj;i++)
	{
		var trObj = document.getElementById ("trRoom"+i);
		trObj.style.display = "";
	}
	
	for(var i= obj;i<4;i++)
	{
		var trObj = document.getElementById ("trRoom"+i);
		trObj.style.display = "none";
	}
}
function DisplayChildOld(index)
{
	var obj = document.getElementById ("DropDownList_Child"+index).value;
	if(obj>0)
	{
		var tdObj = document.getElementById ("tdChildOld"+index);
		tdObj.style.display = "";
		
		document.getElementById("divMsgChild"+index).style.display = "none";
		
		for(var i= 0;i<obj;i++)
		{
			var selectObj = document.getElementById ("DropDownList_ChildOld"+index+i);
			selectObj.style.display = "";
		}
		
		for(var i= obj;i<4;i++)
		{
			var selectObj = document.getElementById ("DropDownList_ChildOld"+index+i);
			selectObj.style.display = "none";
		}
	}
	else
	{
		document.getElementById ("tdChildOld"+index).style.display = "none";
		for(var i= 0;i<4;i++)
		{
			var selectObj = document.getElementById("DropDownList_ChildOld"+index+i);
			selectObj.style.display = "none";
		}
	}
}

function GetHotelList(user,curr,ilng)
{			

	if(Validate())
	{	
		var blnDestination =false;			

		if(ObjDestinations!=null && ObjDestinations!='')
				blnDestination = true;			
		
		if(document.getElementById("DropDownList_Countries").value == "" && !blnDestination 
			&& document.getElementById(HIDDEN_Destination).value == "")
			document.getElementById("divMsgCountry").style.display = "";
		else
		{
			document.getElementById('divMsgCountry').style.display = "none";
			
			if(document.getElementById("DropDownList_Cities").value == "" && !blnDestination 
				&& document.getElementById(HIDDEN_Destination).value == "")
				document.getElementById("divMsgCity").style.display = "";
			else
			{
				document.getElementById('divMsgCity').style.display = "none";
				
				var strCountryCode		= document.getElementById("DropDownList_Countries").value; 
				
				var strDestination;					
							
				if(blnDestination)
					strDestination	= trim(ObjDestinations);
				else if(document.getElementById(HIDDEN_Destination).value != "")
				{
					var sValue = new Array();
					sValue = document.getElementById(HIDDEN_Destination).value.split('_');
					strDestination	= trim(sValue[1]);
					strCountryCode	= trim(sValue[0]);
				}
				else if(document.getElementById("DropDownList_Cities").value != "")
					strDestination	= trim(document.getElementById('DropDownList_Cities').value);
				
				var strRoomCount = "", strAdultCount = "", strChildCount = "", strChildOld = "";
				
				strRoomCount = document.getElementById('DropDownList_RoomCount').value
				for(var i=0;i<=3;i++)
				{
					if(document.getElementById('DropDownList_Adult' + i)!=null 
						&& document.getElementById('trRoom' + i).style.display != "none") 
						strAdultCount	+= trim(document.getElementById('DropDownList_Adult' + i).value)  + ";";					
					else
						strAdultCount	+= "0;";
						
					if(document.getElementById('DropDownList_Child' + i)!=null
						&& document.getElementById('trRoom' + i).style.display != "none") 
						strChildCount	+= document.getElementById('DropDownList_Child' + i).value  + ";";					
					else
						strChildCount += "0;";			
				}
			
							
								
				for(var k=0;k<=3;k++)
				{
					var count = 0;
					if(document.getElementById('DropDownList_Child' + k)!=null) 
						count = document.getElementById('DropDownList_Child' + k).value;
							
					for(var l=0;l<4;l++)
					{
						if(document.getElementById('DropDownList_ChildOld' + k + l)!=null && l<count) 
							strChildOld	+= document.getElementById('DropDownList_ChildOld' + k + l).value  + ";";	

					}
				}	
				
				
				var strCheckindate		= document.getElementById("TextBox_ArrivalDate").value;
				var strDuration			= document.getElementById("Textbox_OvernightPeriods").value;
				var strZone				= document.getElementById("DropDownList_Locations").value;
				var strHotelName		= document.getElementById('TextBox_HotelName').value;

				var strCategorie		= "";
				if (document.getElementById('DropDownList_category')!=null)
					strCategorie= document.getElementById('DropDownList_category').value;
				
				var strUrl = "Hotel_List.aspx?user="+user+"&curr="+curr+"&ilng="+ilng+"&page=1"
					+ "&countrycode="+strCountryCode+"&destinationcode=" + strDestination + "&checkindate=" + strCheckindate + "&duration=" + strDuration + "&zone=" + strZone 
					+ "&roomscount=" + strRoomCount + "&adultcount=" + strAdultCount + "&childcount=" + strChildCount + "&childold=" + strChildOld + "&hotelname=" + strHotelName
					+ "&categrie=" + strCategorie;		

				this.location = strUrl;
			}
		}
	}
}

function Validate()
{
	var blnError = false;
	
	/// hide notifications	
	DisplayNotification(0);
	
	var objTextBox_ArrivalDate		= document.getElementById("TextBox_ArrivalDate");
	var objTextBox_DepartureDate	= document.getElementById("TextBox_DepartureDate");
			
	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;				
	}
	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);
		
}
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);
}	
