﻿// JScript File

var xhttp = false;

function getXHTTPObject() {     
	if (window.ActiveXObject) {  
        try {  
			// IE 6 and higher 
			xhttp = new ActiveXObject("MSXML2.XMLHTTP"); 			
        } catch (e) { 
            try { 
                // IE 5 
                xhttp = new ActiveXObject("Microsoft.XMLHTTP"); 				
            } catch (e) { 
                xhttp=false; 
            } 
        } 
	} 
    else if (window.XMLHttpRequest) { 
        try { 
            // Mozilla, Opera, Safari ... 
            xhttp = new XMLHttpRequest();
        } catch (e) { 
            xhttp=false;
        } 
    } 
}

function RecountUnitPrice(paragraphID,unitID) {

	/* we need a xhttp-object */
	getXHTTPObject();
	
	/* is xhttp-object ok? */
	if (!xhttp) { 
        return; // exit 
    }
	/* lets get data */
	var supplements = GetSelectedSupplements(paragraphID,unitID);
	var selectedBoardType = GetSelectedBoardType(paragraphID,unitID);
	var returnUrl = "/Public/HotelBookingAJAX.aspx?ParagraphID=" + paragraphID + "&SelectedSupplements=" + supplements + "&UnitID=" + unitID + "&SelectedBoardType=" + selectedBoardType + "&Action=recount";

//  Original onreadystatechange position
//   xhttp.onreadystatechange = function () {
//		RecountUnitPrice_callback(paragraphID,unitID);
//	};
    xhttp.open("GET",returnUrl,true);
	xhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
    xhttp.send(null);

//  Start if FF fix
    xhttp.onreadystatechange = function () {
		RecountUnitPrice_callback(paragraphID,unitID);
	};
//   End of FF fix

}		

function RecountUnitPrice_callback(paragraphID,unitID) {    
    if (xhttp.readyState == 4) {        
		var priceHolder = document.getElementById("Unit" + paragraphID + unitID + "GrandUnitTotal");		
		if(xhttp.status==200) {
		    try {
		        if (xhttp.responseText.length > 0 & xhttp.responseText.substring(0,1) == 'D'){
					//alert('redirect : ' + xhttp.responseText.substring(0,xhttp.responseText.indexOf('|')));
		            window.location = xhttp.responseText.substring(0,xhttp.responseText.indexOf('|'));
		        }else{
		            priceHolder.innerHTML = xhttp.responseText;
                            adjustPricePerNight(xhttp.responseText, unitID);
					}
			}
			catch(e) {				
			}
		}
		else {
			priceHolder.innerHTML = "Error in AJAX [" + xhttp.status + "]";				
		}
    }
}	


function GetSelectedSupplements(paragraphID,unitID)
{   
    var allAvailableSupplements = document.getElementById("Unit" + paragraphID + unitID + "AllAvailableSupplements");
    var supplements = allAvailableSupplements.value.split(",");         
    var ret = "";
    if (supplements[0] != "") {
      for(var i=0; i< supplements.length; i++)
      {
          var supp = document.getElementById(supplements[i]);
          if (supp.checked) {
            ret += supplements[i] + ",";
          }
      }
    }
    return ret;    
}

function GetSelectedBoardType(paragraphID,unitID)
{
    var ret = 0;   
    var renderedUnits = document.getElementById("RenderedUnits" + paragraphID).value.split(","); 
	var renderedUnitsBoardTypes = document.getElementById("UnitsBoardTypeInputKind" + paragraphID).value.split(","); 

    for(var i=0; i< renderedUnits.length; i++)
    {
        if (renderedUnits[i] === unitID)
        {
            var boardType = renderedUnitsBoardTypes[i];
				var boardTypeHolderName = "Unit" + paragraphID + unitID +"BoardType";
				var boardTypeHolder = document.getElementsByName(boardTypeHolderName);
				for (i = 0; i < boardTypeHolder.length; i++)
				{
					if (boardTypeHolder[i].checked)
					{
						if (boardTypeHolder[i].value == "Breakfast")
							ret = 1;
						else if (boardTypeHolder[i].value == "HalfBoard")                            
							ret = 2;
						else if (boardTypeHolder[i].value == "FullBoard")                            
							ret = 3;
						else
							ret = 0;
						break;
					}
				}                    
            return ret;
        }        
    }
    return ret;    
}

function AddToCart(unitType, paragraphID, unitID)
{
	/* we need a xhttp-object */
	getXHTTPObject();
	
	/* is xhttp-object ok? */
	if (!xhttp) { 
        return; // exit 
    } 
    
	/* lets get data */
    var supplements = GetSelectedSupplements(paragraphID,unitID);
	var selectedBoardType = GetSelectedBoardType(paragraphID,unitID);
	var returnUrl = "/Public/HotelBookingAJAX.aspx?ParagraphID=" + paragraphID + "&SelectedSupplements=" + supplements + "&UnitID=" + unitID + "&SelectedBoardType=" + selectedBoardType + "&UnitType=" + unitType  + "&Action=addtocart";
//  original onreadystatechange position
//    xhttp.onreadystatechange = function () {
//		AddToCart_callback(paragraphID,unitID);
//	};

//  original xhttp.open
//    xhttp.open("GET",returnUrl,false);
    xhttp.open("GET",returnUrl,true);
	xhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
    xhttp.send(null);

//  Start FF fix
    xhttp.onreadystatechange = function () {
		AddToCart_callback(paragraphID,unitID);
	};
//  End FF fix
}

function AddToCart_callback(paragraphID,unitID) {    
    if (xhttp.readyState == 4) {        
        var ret = "";		
		if(xhttp.status==200) {
			try {			    
			
				if (xhttp.responseText.length > 0 & xhttp.responseText.substring(0,1) == 'D'){
					//alert('redirect : ' + xhttp.responseText.substring(0,xhttp.responseText.indexOf('|')));
		            window.location = xhttp.responseText.substring(0,xhttp.responseText.indexOf('|'));
		        }else{
					ret = xhttp.responseText;
				}
			}
			catch(e) {				
			}
		}
		else {
			ret = "Error in AJAX [" + xhttp.status + "]";						
		}
		OnCartAddCallBack(ret,paragraphID,unitID);
    }    
}	

function OnCartAddCallBack(newcount,paragraphID, unitID)
{    
//    document.getElementById("Unit" + paragraphID + unitID + "QuantityHolder").innerHTML = newcount;    
    if(newcount == "0")
    {
        document.getElementById("Unit" + paragraphID + unitID + "AddToCartLink").style.display="none";
        document.getElementById("Unit" + paragraphID + unitID + "UnitsAreReservedSpan").style.display="";
    }
    window.location = window.location;
}



function GetRegions(parentID, languageID, selectControlName, callback)
{
    callback = callback || function() {};
     document.getElementById(selectControlName).style.display = 'none';       
     
	/* we need a xhttp-object */
	getXHTTPObject();
	
	/* is xhttp-object ok? */
	if (!xhttp) { 
        return; // exit 
    } 
    
	/* lets get data */    	
	var returnUrl = "/Public/HotelBookingAJAX.aspx?ParentID=" + parentID + "&LanguageID=" + languageID + "&Action=getregions";
//  Original onreadystatechange position
//   xhttp.onreadystatechange = function () {
//		GetRegions_callback(selectControlName);
//	};
    xhttp.open("GET",returnUrl,true);
	xhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
    xhttp.send(null);

//  Start if FF fix
    xhttp.onreadystatechange = function () {
		GetRegions_callback(selectControlName, callback);
	};
//   End of FF fix
}

function GetRegions_callback(selectControlName, callback) {  
    callback = callback || function() {}
    document.getElementById(selectControlName).style.display = '';
    if (xhttp.readyState == 4) {        
        var ret = "";		
		if(xhttp.status==200) {
			try {
		        if (xhttp.responseText.length > 0 & xhttp.responseText.substring(0,1) == 'D'){
					//alert('redirect : ' + xhttp.responseText.substring(0,xhttp.responseText.indexOf('|')));
		            window.location = xhttp.responseText.substring(0,xhttp.responseText.indexOf('|'));
		        }else{
		            ret = xhttp.responseXML;
					OnGetRegionsCallBack(ret,selectControlName);							
					callback();
				}
			}
			catch(e) {
			    alert(e);                
			}
		}
		else {		    
			ret = "Error in AJAX [" + xhttp.status + "]";
			alert(ret);			
		}
    }    
}

function OnGetRegionsCallBack(regionsXml, selectControlName)
{
    var regionSelect = document.getElementById(selectControlName);

  if (typeof(regionSelect) == 'object') {
    var options = ParseRegions(regionsXml);
    for (i = 0; i < options.length; i++)
    {
        regionSelect.options[regionSelect.options.length] = options[i];
    }
  }
}

function ParseRegions(regionsXml)
{    
    var pairsNotParsed = regionsXml.getElementsByTagName('region');    
    var options = new Array();
    for (i = 0; i < pairsNotParsed.length; i++)
    {
        var regionXml = pairsNotParsed.item(i);
        var name = regionXml.attributes.getNamedItem("name").value;
        var id = regionXml.attributes.getNamedItem("id").value;      
        var optionObj = new Option(name,id);
        options[options.length] = optionObj;
    }
    return options;
}

function CleareSelect(selectName)
{
    var currentSelect = document.getElementById(selectName);     
    while (currentSelect.options.length > 1) 
    {
        currentSelect.options[1] = null;
    }
}

function OnCountryChange(sender,languageID, callback)
{
    callback = callback || function() {};
    CleareSelect("RegionSelect");
    CleareSelect("LocationSelect");
    if (sender.value > 0)
        GetRegions(sender.value,languageID,"RegionSelect", callback);     
}

function OnRegionChange(sender,languageID, callback)
{    
    callback = callback || function() {};
    CleareSelect("LocationSelect");
    if (sender.value > 0) {
        GetRegions(sender.value,languageID,"LocationSelect",callback);     
}
}
