﻿// JScript File
// call flow -- jsPopulateCombo >> 
//Global XMLHTTP Request object
var XmlHttp;
var fnIdentifier;
var blnResReturn;
//Creating and setting the instance of appropriate XMLHTTP Request object to a “XmlHttp” variable  
function CreateXmlHttp()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttp = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttp = new XMLHttpRequest();
	}
}


//Called when response comes back from server
function HandleResponse()
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{	
            if (XmlHttp.responseXML.documentElement != null)
            {
			    ClearAndSetListItems(XmlHttp.responseXML.documentElement);
			}
			else
			{
			        if (fnIdentifier == 4) {alert('Login Failed.');document.getElementById('guepwd').value='';document.getElementById('gueuname').value='';}
			        if (fnIdentifier == 5) {alert('Incorrect Reservation Number. No data retrieved.');document.getElementById('txtResNo').value='';blnResReturn=false;}
			}
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
}



function populateCategory() 
{
	var requestUrl = "clsRetrieveValues.aspx?CatLoad=1";
	fnIdentifier = 1;
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
	
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl,  true);

		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleResponse;
		
		//Sends the request to server
		XmlHttp.send(null);		
	}
}

function clearDropDown(objDDL) {   
    if (document.getElementById("ddlCategory").value.length != 0) {   
    return  
    }   
    var loopCount = objDDL.options.length   
    for (i=0; i<loopCount; i++) {   
    //Note: Always remove(0) and NOT remove(i)   
    objDDL.remove(0)
    }   
} 

function populateTypeOnChange() 
{
    
	var categoryVal = document.getElementById("ddlCategory");
	// reset the Room Allotted value
	var oRmAlloc = document.getElementById("txtRoomAllotted");
	oRmAlloc = 0
	//ddownList.selectedIndex = 0
	
	if (categoryVal.value.length == 0)
	{
	
	    var rooTypVal = document.getElementById("ddlType");
	    var basisVal = document.getElementById("ddlBasis");
	    var txtRoomDesc = document.getElementById('txtRoomDesc');
	    var imgSrc = document.getElementById('imgUploadImg');
	    var txtRmAllotted = document.getElementById('txtRoomAllotted');
	    
	    clearDropDown(rooTypVal);
	    clearDropDown(basisVal);
	    
	    rooTypVal.value = ''
	    basisVal.value = ''
	    
	    txtRmAllotted.value = ''
	    txtRoomDesc.value = ''
        imgSrc.src = ''
                
    }
    fnIdentifier = 2;
	//Getting the selected category from category combo box.
	var selectedCategory = categoryVal.options[categoryVal.selectedIndex].value;
	//var selectedCategory = selVal
	
	// URL to get states for a given country
	
	var requestUrl = "clsRetrieveValues.aspx?Category=" + encodeURIComponent(selectedCategory);
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl,  true);
		
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleResponse;

		
		//Sends the request to server
		XmlHttp.send(null);		
	}
}

function populateBasisOnChange()
{   
    var categoryVal = document.getElementById("ddlCategory");
    var typeVal = document.getElementById("ddlType");
    var selectedPropertyId  = document.getElementById("hidPropId").value;
    fnIdentifier = 3;
	//Getting the selected category from category combo box.
	var selectedCategory = categoryVal.options[categoryVal.selectedIndex].value;
	//alert(selectedCategory);
	if (selectedCategory == '') {alert('Please choose a category');}
	var selectedType = typeVal.options[typeVal.selectedIndex].value;
	//alert(selectedType);
	
	// URL to get states for a given country
	
	var requestUrl = "clsRetrieveValues.aspx?ChngCat=" + encodeURIComponent(selectedCategory) + "&ChngTyp=" + encodeURIComponent(selectedType) + "&PropId=" + encodeURIComponent(selectedPropertyId);
	//alert(requestUrl);
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl,  true);
		
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleResponse;

		//Sends the request to server
		XmlHttp.send(null);		
	}
}



function populateTxtOnBasisChange() 
{
    
	var categoryVal = document.getElementById("ddlCategory");
	var typeVal = document.getElementById("ddlType");
	var BasVal = document.getElementById("ddlBasis");
	
    fnIdentifier = 7;
    
	//Getting the selected category from category combo box.
	var selectedCategory    = categoryVal.options[categoryVal.selectedIndex].value;
	var selectedType        = typeVal.options[typeVal.selectedIndex].value;
	var selectedBasis       = BasVal.options[BasVal.selectedIndex].value;
	var selectedPropertyId  = document.getElementById("hidPropId").value;
	
	// URL to get states for a given country
	
	var requestUrl = "clsRetrieveValues.aspx?Bas=" + encodeURIComponent(selectedBasis) + "&Typ=" + encodeURIComponent(selectedType) + "&Category=" + encodeURIComponent(selectedCategory) + "&PropId=" + encodeURIComponent(selectedPropertyId);
	
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl,  true);
		
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleResponse;

		
		//Sends the request to server
		XmlHttp.send(null);		
	}
}


function ClearAndSetListItems(categoryNode)
{   
   if (fnIdentifier == 4)
   {
		var TypeNodes = categoryNode.getElementsByTagName('option');
	    if (TypeNodes.length > 0)
	    {
			var attrbValue = TypeNodes[0].attributes.getNamedItem("value").text;
			textValue = GetInnerText(TypeNodes[0]);
			//alert(textValue);
			//alert(attrbValue);
			document.location.href("UserHome.aspx?GuestId="+attrbValue+"&GuestName="+textValue);
		}
	    //document.location.href("UserHome.aspx");}
	    else
	    {
	    	alert('Login Failed.');
	    	document.getElementById('guepwd').value='';
	    	document.getElementById('gueuname').value='';
	    }
   }
   else if (fnIdentifier == 5)
   {
        var TypeNodes = categoryNode.getElementsByTagName('option');
        if (TypeNodes.length > 0)
        {
            var attrbValue = TypeNodes[0].attributes.getNamedItem("value").text;
            //textValue = GetInnerText(TypeNodes[0]);
            if (attrbValue == 1)
            {
                location.href('Confirmation.aspx?ResStat='+document.getElementById('txtResNo').value);
            }
            else if (attrbValue == 0)
            {
                alert('No reservations made against this number.');    
                blnResReturn = false;
            }
        }
        else{alert('Please enter a valid reservation number');document.getElementById('txtResNo').value='';blnResReturn = false;}    
   }
   else if (fnIdentifier == 7)
   {
        var TypeNodes = categoryNode.getElementsByTagName('option');
        var txtRoomDesc = document.getElementById('txtRoomDesc');
        var imgSrc = document.getElementById('imgUploadImg');
        //ddownList.selectedIndex = 0
        imgSrc.src = ''
        
        if (TypeNodes.length > 1)
        {
            //var attrbValue = TypeNodes[1].attributes.getNamedItem("value").text;
            txtRoomDesc.value = GetInnerText(TypeNodes[1])
            
            if (TypeNodes.length > 2)
            {
                 imgSrc.src = GetInnerText(TypeNodes[2])
            }            
        }
        else
        {
             txtRoomDesc.value = '';
        }
   }
   else
   {
	   var dropdownList;
	   if (fnIdentifier == 1)
	   {
	        dropdownList = document.getElementById("ddlCategory");
	   }
	   else if (fnIdentifier == 2)
	   {
	        dropdownList = document.getElementById("ddlType");
	   }
	    else if (fnIdentifier == 3) 
	   {
	        dropdownList = document.getElementById("ddlBasis");
	        
	        var oRmAlloc = document.getElementById("txtRoomAllotted");
	        	        
	        var TypeNodes = categoryNode.getElementsByTagName('option');
	        //oRmAlloc = 0
	        
	        var nodeIdx = TypeNodes.length - 1 
            if (TypeNodes[nodeIdx].text == 'RxxmAllo++ed')
            {
                //alert(TypeNodes[nodeIdx].attributes(0).value);
                oRmAlloc.value = TypeNodes[nodeIdx].attributes(0).value
                //alert(oRmAlloc.value );
                   
                // remove the RxxmAllo++ed from the categoryNode          
                y=categoryNode.getElementsByTagName("option")[nodeIdx];
                x=categoryNode.removeChild(y);
                //alert("Removed node: " + x.nodeName)
                
            }
	        
	   }
	    else if (fnIdentifier == 6) 
	   {
	        dropdownList = document.getElementById("ddlState");
	   }
       
		//Clears the state combo box contents.
		for (var count = dropdownList.options.length-1; count >-1; count--)
		{		dropdownList.options[count] = null;	}
	
		var TypeNodes = categoryNode.getElementsByTagName('option');
		var textValue; 
		var attrbValue;
		var optionItem;
	
		//alert(fnIdentifier);
		//alert(TypeNodes.length);
	
		//Add new states list to the state combo box.
		for (var count = 0; count < TypeNodes.length; count++)
		{
	   		textValue = GetInnerText(TypeNodes[count]);
  			//alert(textValue);
	   		attrbValue = TypeNodes[count].attributes.getNamedItem("value").text;
	   		//alert(attrbValue);
	   		if (fnIdentifier == 6)
	   		{
	   		    // Type Node length - 1 provided as 
	   			if ((TypeNodes.length-1) == 1)
	   			{
					document.all.ddlState.style.visibility="hidden";				    
					document.all.dvStateTxtBx.style.visibility="visible";
	   			}
	   			else
	   			{
   					document.all.dvStateTxtBx.style.visibility="hidden";
					document.all.ddlState.style.visibility="visible";
	   			}
	   		}
  			if (attrbValue == 'PropTblStruct'){
  			document.getElementById('series2').innerHTML = textValue;}
  			else
  			{
				optionItem = new Option( textValue, attrbValue,  false, false);
				dropdownList.options[dropdownList.length] = optionItem;
				if (fnIdentifier == 6)
				{
					document.getElementById('series2').innerHTML="";
				}
			}
		}
	
		// if the type values being populated is empty then clear the basis contents
		if (fnIdentifier == 2)
		{
		    dropdownList = document.getElementById("ddlType");
		    if (dropdownList.options.length == 1)
		    { 
		        if (dropdownList.options[0].value == 0) 
		        {
		            //no values present in the types drop down. Hence clear basis drop down
		            dropdownList = document.getElementById("ddlBasis");
		            for (var count = dropdownList.options.length-1; count >-1; count--)
		            {dropdownList.options[count] = null;}
		        }
		    }
		}
	} //end else part of fnIdentifier = 4

    //reset the variable
    fnIdentifier = 0; 
}

//Returns the node text value 
function GetInnerText (node)
{return (node.textContent || node.innerText || node.text) ;}


function changeDepartureDate(noNights,intPg)
{
    //convert to integer so as to enable correct addition of values in setDate function
    var intDays = parseInt(noNights);
    //get the departure control 
    //var departureDate = document.getElementById('dtcheckOutFullDate')
    //get the selected value from the control
    //var dtDepart = new Date(odtcheckOutFullDate.getValue())
    var dtDepart = new Date(odtcheckIn.getValue())
    //increase the number of days by the days passed in
    dtDepart.setDate(dtDepart.getDate() + intDays)
    //assign to the departure date control
    odtcheckOutFullDate.setValue(dtDepart);
}


function changeNoNights()
{
      //get the selected value from the control
    var dtArrival = new Date(dtcheckIn.getValue())
    var dtDepart = new Date(odtcheckOutFullDate.getValue())
    var diff = dtDepart - dtArrival
    //get the control
     var ctrlNights = document.getElementById('number_Nights')
     ctrlNights.value = diff
}

function setHiddenValue(intRoomCase,strValue)
{
    var setCtrl
    if (intRoomCase == 1)
        {
            setCtrl = document.getElementById('hidCate')
        }
    else if (intRoomCase == 2)
        {
            setCtrl = document.getElementById('hidTyp')
        }
   else if (intRoomCase == 3)
        {
            setCtrl = document.getElementById('hidBas')
        }
   setCtrl.value = strValue
}


function CheckLogin()
{
    var strUName = document.getElementById('gueuname').value
    if (strUName == '')
        {   
            alert('Please enter the user name');
            return false;
        }
    var strUPwd = document.getElementById('guepwd').value
    if (strUPwd == '')
       {
            alert('Please enter the password');
            return false;
       }
       
    fnIdentifier = 4;
    var requestUrl = "clsRetrieveValues.aspx?Login=" + encodeURIComponent(strUName) + "&Pwd=" + encodeURIComponent(strUPwd);
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	    {
	
		    //Initializes the request object with GET (METHOD of posting), 
		    //Request URL and sets the request as asynchronous.
		    XmlHttp.open("GET", requestUrl,  true);
		    
   		    //Setting the event handler for the response
		    XmlHttp.onreadystatechange = HandleResponse;

		
		    //Sends the request to server
		    XmlHttp.send(null);		
	    }
}


function ChkResNum()
{
    blnResReturn = true;
    var strResNum  = document.getElementById('txtResNo').value
    //if ((strResNum.trim() == '') |(isNaN(strResNum))) 
    if ((strResNum.trim() == '')) 
        {
            alert('Please enter the reservation number.');
            document.getElementById('txtResNo').value='';
            //return false;
            blnResReturn = false;
        }
    //else 
        //{
            //ajax : chk whether the value is presnt in DB or not and then call location.href
            fnIdentifier = 5;
            var requestUrl = "clsRetrieveValues.aspx?RefNum=" + encodeURIComponent(strResNum);
            //alert(requestUrl);
	        CreateXmlHttp();
	        // If browser supports XMLHTTPRequest object
	        if(XmlHttp)
	        {
	            
		            //Initializes the request object with GET (METHOD of posting), 
		            //Request URL and sets the request as asynchronous.
		            XmlHttp.open("GET", requestUrl,  true);
		            
  		            //Setting the event handler for the response
		            XmlHttp.onreadystatechange = HandleResponse;


		            //Sends the request to server
		            XmlHttp.send(null);		
	         }
	          return blnResReturn;
        //}
}

function populateStates(strval)
{
	//alert(strval);
	var couCode = document.getElementById('ddlCountries');
	if (couCode.selectedIndex > -1){
	var selectedCountry = couCode.options[couCode.selectedIndex].value;}
	//alert(selectedCountry);
	if (strval != '0'){selectedCountry=strval}
	//alert(selectedCountry);
	fnIdentifier = 6;
	var requestUrl = "clsRetrieveValues.aspx?CouCode=" + encodeURIComponent(selectedCountry);
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl,  true);
		
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleResponse;

		//Sends the request to server
		XmlHttp.send(null);		
	}

}

function hideLoadingpage() { 
	if (document.getElementById) { // DOM3 = IE5, NS6 
		document.getElementById('hidepage').style.visibility = 'hidden'; 
	} 
	else { 
			if (document.layers) { // Netscape 4 
				document.hidepage.visibility = 'hidden'; 
			} 
			else { // IE 4 
				document.all.hidepage.style.visibility = 'hidden'; 
			} 
	} 
} 
		
function showLoadingpage() { 
	if (document.getElementById) { // DOM3 = IE5, NS6 
		document.getElementById('hidepage').style.visibility = 'visible'; 
	} 
	else { 
		if (document.layers) { // Netscape 4 
			document.hidepage.visibility = 'show'; 
		} 
		else { // IE 4 
			document.all.hidepage.style.visibility = 'visible'; 
		} 
	} 
} 
			

 function changeTitle() { document.title =  propName + ' ' + document.title   }






