// Main Menu Script
// Created 17/03/08 by GH

/**-------------------------------------------------------------*/
/**-----------------------------Cookies-------------------------*/
/**-------------------------------------------------------------*/


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=/; domain=mywestnet.com.au";
}

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);
}

function SetCookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function GetCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i=0;
	while (i < clen) {	
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
		}
	return null;
}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function hloadweathertop() {

    var cWeather = readCookie('weather');

    DisplayForcastTopHeader(cWeather);

}

function hafterload() {
	
	var cWeather = GetCookie("weather");
	
	if (cWeather != null && cWeather != '')
	{
		DisplayForcast(cWeather);
		DisplayForcastTop(cWeather);
	}
	
	var cFuel = GetCookie("fuelwatch");
	
	if (cFuel != null && cFuel != '')
	{	
		displayFuelInfo(cFuel);
	}
	
	var cstatenews = GetCookie("vstatenews");
	
	if (cstatenews != null && cstatenews != '')
	{	
		GetStateNewsInfo(cstatenews)
	}
	else {
		GetStateNewsInfo('AUS')
	}
	
	var cworldTimeTxt = GetCookie("vworldTimeTxt");
	var vworldTimeLoc = GetCookie("vworldTimeLoc");
	
	if (cworldTimeTxt != null && cworldTimeTxt != '' && vworldTimeLoc != null & vworldTimeLoc != '')
	{	
		document.getElementById('worldTimeLocTxt').innerHTML = cworldTimeTxt;
		change_location(vworldTimeLoc,'cookie')
	}
	else {
			hhideShowWorldTime('hide');
	}
	
}

/**-------------------------------------------------------------*/
/**-----------------------------Menu Sections-------------------*/
/**-------------------------------------------------------------*/

var vtabnames = new Array("home","news","sport","weather","entertainment","gaming","lifestyle","members","services","freezone");
var vstayon;

function hFindPath(vdefault){
	vstayon = "";
	
	if (vdefault = 'undefined')  // We can manually set the location if we want by setting vdefault
	{
		var vPath = location.pathname.split("/"); // Split the URL up into sections so we can find where they are
		vPath = vPath[1];
		
		for (i=0; i<10; i++)
		{
			vLCtabnames = vtabnames[i].toLowerCase(); // Ensure path names are lowercase
			
			if (vPath == vLCtabnames) // If we find where they are then set the correct tab
			{
				
				vstayon = vtabnames[i];
				break;
			}
		}
		
		if (vstayon == '' || vstayon == null)       //24/10/2006 CLi: home page won't have any path name, this is for homepage menu
		{
			vstayon = "home";
			
		}
		
	}
	else {
		vstayon = vdefault;
	}
	
//	if(vstayon != "home"){
//		document.getElementById('changeLocation').innerHTML = '&nbsp;';
//	}
	
	hSMShow(vstayon);
}

function hSMShow(j){
	
	for (i=0; i<10; i++) // Go through and Hide all the menus
	{
		document.getElementById('SM-' + vtabnames[i]).style.display = 'none';
		document.getElementById('MM-' + vtabnames[i]).style.background = "url(http://images.mywestnet.com.au/images01/header/topnav_separator.gif) repeat-y top left";
		if (j == vtabnames[i]){ // Check to see if this menu needs to be displayed and if it does then unhide it
			document.getElementById('SM-' + j).style.display = 'block';
			document.getElementById('MM-' + vtabnames[i]).style.background = "url(http://images.mywestnet.com.au/images01/header/topnav_separator.gif) repeat-y top left #FFFFFF";
		}
	}
	document.getElementById('MM-' + vtabnames[0]).style.backgroundImage = "none";
	
}

function hSMHide(element, evt) { // The function works out if the user leaves the menu
   if (typeof evt.toElement != 'undefined' && evt.toElement && typeof element.contains != 'undefined') { 
	return !contains(element, evt.toElement);
	 
   }
   else if (typeof evt.relatedTarget != 'undefined' && evt.relatedTarget) {

	   return !contains(element, evt.relatedTarget);
	 
   }

}

function contains (container, containee) { // The function works out if the user leaves the menu
	
	if(vstayon == null || vstayon == '' || vstayon == 'undefined'){
		vstayon = 'home';
	}


   while (containee) {
     if (container == containee) {
       return true;
     }
     containee = containee.parentNode;
   }
   for (i=0; i<10; i++)
	{
		document.getElementById('SM-' + vtabnames[i]).style.display = 'none';
	}
	hSMShow(vstayon);
}

/**-------------------------------------------------------------*/
/**---------------------Show Hide Login Boxes-------------------*/
/**-------------------------------------------------------------*/

function populateLoginFromCookie(name){

    cValue = GetCookie(name + "Username");
    
    if (cValue != null && cValue != ''){
        document.getElementById('username').value = cValue;
        document.getElementById('cbSaveLoginUsername').checked = true;
    }
    else{
        document.getElementById('username').value = '';
        document.getElementById('cbSaveLoginUsername').checked = false;
    }
    
    document.getElementById('Password').value = '';

}

function hmemberLogin(x, name) {

    var tabs = new Array("photo","myaccount","myemail","homepage");

    if(name != null)
	    populateLoginFromCookie(name);

    document.getElementById('login').style.display = (x == 'login') ? 'block' : 'none';
    document.getElementById('homepage').style.display = (name == 'homepage') ? 'block' : 'none';
	document.getElementById('photo').style.display = (name == 'photo') ? 'block' : 'none';

    if(x = 'login')
    {
        document.getElementById('loginType').innerHTML = (name == 'myemail') ? '<strong>Email Address:</strong>' : '<strong>Username:</strong>';
    }
	
    for (i=0; i<tabs.length; i++)
	{
		document.getElementById('btn' + tabs[i] + "On").style.display = (tabs[i] == name || tabs[i] == x) ? 'block' : 'none';
		document.getElementById('btn' + tabs[i] + "Off").style.display = (tabs[i] == name || tabs[i] == x) ? 'none' : 'block';
	}

	document.getElementById("hdnLoginType").value = (name == null) ? '' : name;
		
}



function addWestnetSearch(){
        try {
            window.external.AddSearchProvider("<%=MyWestnet.WNHelper.SiteURL%>/opensearch.xml");
            
            //05/06/2007 CLi: call usage tracking using AJAX
            var default_search_ajax = new sack();
            default_search_ajax.method = "HEAD";
            default_search_ajax.requestFile = 'usagecheck.aspx?Service=DefaultSearch';
            default_search_ajax.runAJAX();	
        } catch (e) {
            alert("You need to be using IE7 or Firefox2 to add a search engine\r\nYou can also install search plugins by using the drop down menu to the right of the search box.");
        }
        return;
    }