/* AJAX Call to get current country */
var CURRENT_COUNTRY = "USA";

//*****Generic geojax call, everyone should use this!*****
function makeRequest(url, eventToFire, parameters) {
    http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    if (!http_request) {
    
       // alert('Giving up :( Cannot create an XMLHTTP instance');//
        return false;
    }
   
    http_request.onreadystatechange = eventToFire;
    
    if (parameters == null)
    {
		http_request.open('GET', url, true);
		http_request.send(null);
	}
	else
	{
	    http_request.open('POST', url, true);
		http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http_request.setRequestHeader("Content-length", parameters.length);
		http_request.setRequestHeader("Connection", "close");
		http_request.send(parameters);
	}    
}


function makeCountryRequest()
{
    //makeRequest("/bahamas/getcountrybyip.aspx?ip=24.87.66.148", setCountryFromPost)
    makeRequest("/bahamas/getcountrybyip.aspx", setCountryFromPost)
}

function makeCountryRequestFromHome()
{
    //makeRequest("/bahamas/getcountrybyip.aspx?ip=24.87.66.148", setCountryFromPostHome)
    makeRequest("/bahamas/getcountrybyip.aspx", setCountryFromPost)
}

function getQueryStringValue(variable)
{
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
                return pair[1];
            }
        }
        return "";
}


function setCountryFromPost()
{	
     if (http_request.readyState == 4) {
        if (http_request.status == 200) {
            CURRENT_COUNTRY  = http_request.responseText;
            
            if (CURRENT_COUNTRY == "canada") {
	            toggleUsCan(true);
            }
        }
    }
}

function setCountryFromPostHome()
{	
     if (http_request.readyState == 4) {
        if (http_request.status == 200) {
            CURRENT_COUNTRY  = http_request.responseText;
             
	        if (CURRENT_COUNTRY == "canada") {
		        window.location.href = "/bahamas/country.aspx?country=canada"
	        } else {
		        window.location.href = "/bahamas/index.aspx"
	        } 
        }
    }
}

function toggleUsCan(isCan) {
    doc = window.frames["bookItNow"];
    //cnLabel = document.getElementById("divCnDealsSwitch");
    //usLabel = document.getElementById("divUsDealsSwitch");
    cnDeals = document.getElementById("divCnDeals");
    usDeals = document.getElementById("divUsDeals");
    bookitSwitch = document.getElementById("bookitLangSwitch");

    if (typeof doc != "undefined" && typeof cnDeals != "undefined" && typeof usDeals != "undefined" && typeof bookitSwitch != "undefined") 
    {
        if (isCan) 
        {
            setCookie("bahamas_country", "canada");
            doc.location.href = "/bahamas/getbookitnow.aspx?country=canada";
            
            //cnLabel.style.visibility = "hidden";
            //cnLabel.style.display = "none";   
            //usLabel.style.visibility = "visible";
            //usLabel.style.display = "block";
            
            usDeals.style.visibility = "hidden";
            usDeals.style.display = "none";   
            cnDeals.style.visibility = "visible";
            cnDeals.style.display = "block";
            
            bookitSwitch.style.display = "block";
            bookitSwitch.style.visibility = "visible";
        }
        else
        {

            setCookie("bahamas_country", "usa");
            doc.location.href = "/bahamas/getbookitnow.aspx";
            
            //usLabel.style.visibility = "hidden";
            //usLabel.style.display = "none";
            //cnLabel.style.visibility = "visible";
            //cnLabel.style.display = "block";
            
            cnDeals.style.visibility = "hidden";
            cnDeals.style.display = "none";
            usDeals.style.visibility = "visible";
            usDeals.style.display = "block";
            
            bookitSwitch.style.display = "none";
            bookitSwitch.style.visibility = "hidden";
        }
    }
}

function toggleBookIt(isFr) {
    fr = document.getElementById("divFrBookItToggle");
    ca = document.getElementById("divCnBookItToggle");
    doc = window.frames["bookItNow"];
    
    if (isFr)
    {
        doc.location.href = "/bahamas/getbookitnow.aspx?country=fcanada";
        fr.style.visibility = "hidden";
        fr.style.display = "none";   
        ca.style.visibility = "visible";
        ca.style.display = "block";
    } 
    else
    {
        doc.location.href = "/bahamas/getbookitnow.aspx?country=canada";
        ca.style.visibility = "hidden";
        ca.style.display = "none";   
        fr.style.visibility = "visible";
        fr.style.display = "block";
    }
}

function queryStringCountryOverride()
{
    var country = getQueryStringValue("country");
    
    if (country == "canada")
        setCookie("bahamas_country", "canada");
    
    if (country == "usa")
        setCookie("bahamas_country", "usa");
}	    
       
