/*--------------------------------------------------
	Project:	The November Group - Lakeview Hotels
	Script:		TNG_Main.js
	Includes:
		TNGMain
		TNGCookie
		Hotel
--------------------------------------------------*/


var TNGMain={

	//create XMLHttpRequest object
	createRequest:function()
	{
		//declare variables
		var returnValue=null;
		
		try
		{
			returnValue = new XMLHttpRequest();
		}
		catch(microsoftOption1)
		{
			try
			{
				returnValue = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(microsoftOption2)
			{
				try
				{
					returnValue = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(failed)
				{
					returnValue=null;
				}
			}
		}
		
		return returnValue;
	}
}

//cookie handling functions
var TNGCookie={

	//set a cookie value
	set:function(strName, strValue, dtmExpires, strPath, strDomain, blnSecure)
	{
		document.cookie= strName + "=" + encodeURIComponent(strValue) +
			((dtmExpires) ? "; expires=" + dtmExpires.toGMTString() : "") +
			((strPath) ? "; path=" + strPath : "") +
			((strDomain) ? "; domain=" + strDomain : "") +
			((blnSecure) ? "; secure" : "");
	},
	
	//get a cookie value
	get:function(strName)
	{
		//declare variables
		var strSearch = strName + "=";
		var strCookie="";
		var intOffset=0;
		var intEnd=0;
		
		if (document.cookie.length > 0)
		{ // if there are any cookies
			intOffset = document.cookie.indexOf(strSearch);
			if (intOffset != -1) // if cookie name exists
			{
				intOffset += strSearch.length // set index of beginning of value
				intEnd = document.cookie.indexOf(";", intOffset); // set index of end of cookie value
				if (intEnd == -1)
				{
					intEnd = document.cookie.length;
				}
				strCookie=decodeURIComponent(document.cookie.substring(intOffset, intEnd));
			}           
		}
		
		//for browsers that return "undefined" when a cookie is not found, return an empty string instead, for consistent return values across browsers
		if(strCookie=="undefined")
		{
			strCookie="";
		}
		return strCookie;
	},
	
	//delete a cookie
	del:function(strName)
	{
		this.set(strName,"",-1);
	}
}

function Hotel(strId,eleProvince,eleHotel)
{
	this.strId=strId;
	this.eleProvince=eleProvince;
	this.eleHotel=eleHotel;
	this.eleOptionHotelDefault=null;
	this.strRequestUrl="/scripts/php/hotels.php";
	this.arrReservationLinks=new Array();
	
	this.initialize=function()
	{
		if (this.eleProvince && this.eleHotel)
		{
			//store default hotel option
			if(this.eleHotel.options.length == 1) 
				this.eleOptionHotelDefault = this.eleHotel.options[0];
			
			//retrieve cookie values
			var strProvinceId=TNGCookie.get(this.getId()+"strProvinceId");
			var strHotelName=TNGCookie.get(this.getId()+"strHotelName");
			
			//select province from cookie
			if(strProvinceId.length>0)
			{
				for(var i=0;i<this.eleProvince.options.length;i++)
				{
					if(this.eleProvince.options[i].text==strProvinceId)
					{
						this.eleProvince.options[i].selected=true;
						break;
					}
				}
			}
			
			//load hotels
			this.provinceStateChange();
		}
	};

	this.getId=function()
	{
		return this.strId;
	};
	this.getReservationLink=function(intEntryId)
	{
		return this.arrReservationLinks[intEntryId];
	};
	this.setId=function(strId)
	{
		this.strId=strId;
	};
		
	this.provinceStateChange=function()
	{
		if(this.eleProvince && this.eleHotel)
		{
			//clear hotels
			while(this.eleHotel.options.length)
				this.eleHotel.remove(0);
				
			//add loading message
			var eleOption=document.createElement("option");
			eleOption.value="";
			eleOption.text="Loading...";
			try{
				this.eleHotel.add(eleOption,null);
			}catch(e){
				this.eleHotel.add(eleOption); //IE
			}
			
			//store cookie values
			TNGCookie.set(this.getId()+"strProvinceId",this.eleProvince.options[this.eleProvince.selectedIndex].text.toString(),null,"/")
			
			//retrieve cookie values
			var strHotelName=TNGCookie.get(this.getId()+"strHotelName");
			
			var objHotel=this;
			
			if(this.eleProvince.value.length>0)
			{
				var objRequest=TNGMain.createRequest();
				objRequest.open("GET",this.strRequestUrl+"?fProvince_state="+encodeURIComponent(this.eleProvince.options[this.eleProvince.selectedIndex].text.toString()),true);
				objRequest.onreadystatechange=function()
				{
					if(objRequest.readyState==4 && objRequest.status==200)
					{
						var objRequestData=eval("("+objRequest.responseText+")");
						
						//clear hotels
						while(objHotel.eleHotel.options.length)
							objHotel.eleHotel.remove(0);
						
						//clear reservation links
						objHotel.arrReservationLinks=new Array();
						
						for(var i=0;i<objRequestData.arrHotels.length;i++)
						{
							//add hotel
							var eleOptionHotel=document.createElement("option");
							eleOptionHotel.value=objRequestData.arrHotels[i].intEntryId;
							eleOptionHotel.text=decodeURIComponent(objRequestData.arrHotels[i].strHotelName);
							try{
								objHotel.eleHotel.add(eleOptionHotel,null);
							}catch(e){
								objHotel.eleHotel.add(eleOptionHotel); //IE
							}
							
							if(eleOptionHotel.text==strHotelName)
								eleOptionHotel.selected=true;
								
							//add reservation link
							objHotel.arrReservationLinks[objRequestData.arrHotels[i].intEntryId]=objRequestData.arrHotels[i].strReservationLink;
						}
					}
				};
				objRequest.send(null);
			}
			else
			{
				//clear hotels
				while(this.eleHotel.options.length)
					this.eleHotel.remove(0);
					
				//add hotel default message
				if(this.eleOptionHotelDefault!=null)
				{
					try{
						this.eleHotel.add(this.eleOptionHotelDefault,null);					
					}catch(e){
						this.eleHotel.add(this.eleOptionHotelDefault); //IE					
					}
				}
			}
		}
	};
	
	this.hotelChange=function()
	{
		//store cookie values
		TNGCookie.set(this.getId()+"strHotelName",this.eleHotel.options[this.eleHotel.selectedIndex].text.toString(),null,"/")			
	};
	
	this.setProvince=function(strProvince)
	{
		if(this.eleProvince)
		{
			var blnSelected=false;
			
			for(var i=0;i<eleProvince.options.length;i++)
			{
				if(eleProvince.options[i].text==strProvince)
				{
					eleProvince.options[i].selected=true;
					blnSelected=true;
					break;
				}
			}
			
			if(blnSelected)
				this.provinceStateChange();
			else
				eleProvince.options[0].selected=true;			
		}
	};
	
	this.initialize();
}
