//set page event handlers
if (window.attachEvent) {
    window.attachEvent("onload", Page_Load);
    //window.attachEvent("onload", getTextSizeStyleSheet);
} else {
    window.addEventListener("DOMContentLoaded", Page_Load, false);
    //window.addEventListener("DOMContentLoaded", getTextSizeStyleSheet, false);
}

  
function Page_Load() {
    //setTimeout("LoadPreviosSelections()",3000);
    if($get("divCountrySearch")!=null){
        LoadPreviosSelections();
    }
}  

function LoadPreviosSelections(){
    //load previous search values from cookies
    switch(readCookie("qs_selectedtab")){
        case "CS":
            ShowTab(0);
            break;
        case "AS":
            ShowTab(1);
            break;
        case "BG":
            ShowTab(2);
            //cannot get cascading drop down values to set
            //$find("cddBG_ActivityTypes").set_SelectedValue(readCookie("qs_activitytype"),"Wellbeing");
            //$n("ddlBG_Countries").value = readCookie("qs_country");
            //$n("ddlBG_ActivityTypes").value = readCookie("qs_activitytype");
            //if(readCookie("qs_datefrom")) $n("txtBG_DateFrom").value = readCookie("qs_datefrom");
            //if(readCookie("qs_dateto")) $n("txtBG_DateTo").value = readCookie("qs_dateto");
            //$n("ddlBG_HowMuch").value = readCookie("qs_budget");
            //$n("ddlBG_HowLong").value = readCookie("qs_duration");          
            break;
    }
}

function ShowTab(index){
    $get("divCountrySearch").className="displaynone";
        
    switch(index){
        case 0:
            $n("hidTabSelected").value = "CS";
            //$n("optCountry").checked=true;
            SetSearchOption($n("optCountry"));
            $get("divCountrySearch").className="";
            break;
        case 1:
            $n("hidTabSelected").value = "AS";
            //$n("optActivity").checked=true;
            SetSearchOption($n("optActivity"));
            $get("divActivitySearch").className="";
            break;
        case 2:
            $n("hidTabSelected").value = "BG";
            //$n("optBudget").checked=true;
            SetSearchOption($n("optBudget"));
            $get("divBudgetSearch").className="";
            break;                        
    }
}

function SetSearchOption(defaultOption)
{
    var overideOption = null;
    var sPath = window.location.pathname;           
    var sPage = sPath.substring(sPath.lastIndexOf("/") + 1).toLowerCase();
        
    switch(sPage)
    {
        case "destinations.aspx":
            overideOption = $n("optCountry");
            break;
        case "holiday-ideas.aspx": 
            overideOption = $n("optActivity");        
            break;
        default:
            if(sPath.indexOf("country/overview") != -1)
            {
                overideOption = $n("optCountry");
            }
            else
            {
                if(sPath.indexOf("activity-category/overview") != -1 || sPath.indexOf("activity/overview") != -1)
                {
                    overideOption = $n("optActivity");        
                }                
            }                              
            break;
    }
    
    if(overideOption == null)    
    {
        defaultOption.checked = true;
        checkRadio(defaultOption);
    }
    else
    {
        overideOption.checked = true;
        checkRadio(overideOption);
    }    
}
  
function showsearchtype(searchtype)
{
    if(searchtype=="AS")
    {
        $get("divCountrySearch").className="displaynone";
        //$get("divBudgetSearch").className="displaynone";
        $get("divActivitySearch").className="";
        //showhideActivitySubTypes();
        $n("hidTabSelected").value = "AS";
    }
    else if(searchtype=="CS")
    {
        $get("divActivitySearch").className="displaynone";
        //$get("divBudgetSearch").className="displaynone";
        $get("divCountrySearch").className="";        
        $n("hidTabSelected").value = "CS";
    }
    else{
        //$get("divActivitySearch").className="displaynone";
        //$get("divCountrySearch").className="displaynone"; 
        //$get("divBudgetSearch").className="";              
        //$n("hidTabSelected").value = "BG";
    }
}

function showhideActivitySubTypes()
{    
//    var ddlActivityTypes = $n("ddlAS_ActivityTypes");                
//    var ddlActivitySubTypes = $n("ddlAS_ActivitySubTypes");
//    if(ddlActivityTypes.options[ddlActivityTypes.selectedIndex].text.indexOf("Sports") == -1 && ddlActivityTypes.options[ddlActivityTypes.selectedIndex].text.indexOf("Languages") == -1)
//    {            
//        ddlActivitySubTypes.className="spacebottom displaynone";
//    }
//    else
//    {
//        ddlActivitySubTypes.className="spacebottom";
//    }        
}

function showhideRefineActivitySubTypes()
{
    
    var ddlActivityTypes = $n("ddlActivityTypes");                
    var ddlActivitySubTypes = $n("ddlActivitySubTypes");
    if(ddlActivityTypes.options[ddlActivityTypes.selectedIndex].text.indexOf("Sports") == -1 && ddlActivityTypes.options[ddlActivityTypes.selectedIndex].text.indexOf("Languages") == -1)
    {            
        ddlActivitySubTypes.className="spacebottom displaynone";
    }
    else
    {
        ddlActivitySubTypes.className="spacebottom";
    }        
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

//set a cookie value, storing the users text size adjustment preference
function adjustTextSize(adjustment,max,min){
    var textadjustValue ;

    if (adjustment == 0){ //reset 
         textadjustValue = 0;
    }
    else{ //adjustment
       textadjustValue = convertToInteger(readCookie('textsize')) + parseInt(adjustment);
       if (textadjustValue > max || textadjustValue  < min) { return; } //return if we have reached the max or min adjustment
    } 
 
    //create or override exsiting cookie
    createCookie('textsize', textadjustValue, 365 * 10);
        
    //update css link
    getTextSizeStyleSheet();
}

//reads the cookie value and set the correct stylesheet
function getTextSizeStyleSheet(){
    
    //get the current adjust value
    var textadjustValue = convertToInteger(readCookie('textsize'));

    //repoint css
    $n('textsizer').href = '/textsizecss/textsize_' + textadjustValue + '.css';
}

function convertToInteger(value)
{
    if (value == null)
    {
        return 0;
    }
    else{ 
        return parseInt(value);
    }           
}

function changecurrency(cur)
{            
    var domain , querystring;
    switch(cur)   
    {
        case 'usa':
            domain = 'usa.golearnto.com';
            break;
        case 'aus':
            domain = 'aust.golearnto.com';
            break;
        case 'uk':
            domain = 'www.golearnto.com';
            break;
    }
    querystring = window.location.href.replace(location.protocol + '//' + location.host + location.pathname,'');                         
    window.location = location.protocol + '//' + domain + location.pathname + querystring;
}
