﻿function initialise()
{
    var startTab = $("menu").getElement('img[id$=tabLogin]');
    if (!startTab) return;
    
    changeTab(startTab);
}

function changeTab(tab)
{
    resetTabs();
    
    if (!tab) return;
    
    var newSrc = tab.src.replace("off", "on");
    tab.src = newSrc;
       
    var url = tab.id.replace("tab","") + ".aspx";
    var position = url.lastIndexOf("_");
    
    if (position > -1) url = url.substring(position+1, url.length);
    
    //ajaxRequest(url);
    loadPage(url);
}

function resetTabs()
{
    var tabs = $ES('img', 'menu'); // Get all images from div "menu"
    if (!tabs) return;
    
    for (i = 0; i < tabs.length; i++)
    {
        var tab = tabs[i];
        
        if (tab) tab.src = tab.src.replace("_on.", "_off.");
        tab = null;
    }
    //var loginTab = document.getElementById("tabLogin");
    //loginTab.src = loginTab.src.replace("_on.", "_off.");
    
    tabs = null;
    
    tabs = $ES('img', 'loginMenu'); // Get all images from div "loginmenu"
    if (!tabs) return;
    
    for (i = 0; i < tabs.length; i++)
    {
        var tab = tabs[i];
        
        if (tab) tab.src = tab.src.replace("_on.", "_off.");
        tab = null;
    }
}

function loadPage(url) 
{
    var iContent = $("ctl00_iContent");
    if (!iContent) return;
    
    iContent.src = url;
}

function ajax_setpage(page)
{
    ajaxRequest("./" + page + ".aspx");
}

function ajax_init(page)
{  
    ajaxRequest("./newsletters/" + page + ".aspx");
}

function ajaxRequest(url)
{        
    //loader
    //document.getElementById('ajax_content').innerHTML = "<table style='width: 100%; height: 500px; margin-left: auto; margin-right: auto;' cellpad='0' cellspacing='0'><tr><td valign='middle' align='center'><center><img src='./images/loaders/loader1.gif' /></center></td></tr></table>";
      
    //document.getElementById('ajax_content').innerHTML = "<img src='./images/loaders/loader1.gif' /><br />Loading, please wait...";
    document.getElementById('ajax_content').innerHTML = "Loading, please wait...";    
          
    var ajaxRequest = new Ajax(url,
    {
        method: 'get',
        update: $('ajax_content'),
        evalScripts: true
    }
    ).request();  
}

/*

function ajaxRequest(url)
{        
    //loader
    document.getElementById('ajax_content').innerHTML = "<table style='width: 100%; height: 500px; margin-left: auto; margin-right: auto;' cellpad='0' cellspacing='0'><tr><td valign='middle' align='center'><center><img src='./graphics/loaders/loader1.gif' /></center></td></tr></table>";
            
    var ajaxRequest = new Ajax(url,
    {
        method: 'get',
        update: $('ajax_content'),
        evalScripts: true
    }
    ).request();  
}

*/

function sideTab(obj)
{
    if (!obj) return;
    
    var section = obj.replace("sideTab", "");
    //section = section.toLowerCase();
    
//    switch(obj)
//    {
//        case "sideTabAll":
//            alert("Here 2...");
//        //ajaxRequest("MyProducts.aspx?mode=all");
//        document.location.href = "MyProducts.aspx?mode=all";
//        break;
//        
//        case "sideTabMortgage":
//        //ajaxRequest("MyProducts.aspx?mode=all");
//        document.location.href = "MyProducts.aspx?mode=mortgage";
//        break;
//        
//        default:
//        break;
//    }
    
    indentPic(obj);
    document.location.href = "MyProducts.aspx?mode=" + section;
}


function indentPic(menuItem)
{
    resetSideTabs();
    
    var obj = document.getElementById(menuItem);
    if (!obj) return;
    
    var location = findPos(obj);
    
    //obj.style.left = location[0] + 10 + "px";
    obj.style.marginLeft = "10px";
    obj = null;

    if (menuItem == "sideTabAccount")
    {
        document.location.href = "./myaccount.aspx";
    }
}

function resetSideTabs()
{
    var tabs = $ES('img', 'divSideTabs'); // Get all images from div "divSideTabs"
    if (!tabs) return;
    
    for (i = 0; i < tabs.length; i++)
    {
        var tab = tabs[i];
        
        if (tab) tab.style.marginLeft = "0px";
        tab = null;
    }
}

function findPos(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) 
	{
		do 
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} 
		while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

function focusObject(obj)
{
    resetAlphas(6);
    setAlpha(obj,10);
}

function setAlpha(obj, value) 
{ 
    //Remember - filter won't work in IE if the div's width is not set
    
	document.getElementById(obj).style.opacity = value/10; //Moz + Safari
	document.getElementById(obj).style.filter = 'alpha(opacity=' + value*10 + ')'; //IE
}

function resetAlphas(baseValue)
{
    setAlpha('ctl00_ContentPlaceHolder1_PersonalControl1_divPersonalDetails',baseValue);
    setAlpha('ctl00_ContentPlaceHolder1_PersonalControl2_divPersonalDetails2',baseValue);
    setAlpha('divAddressDetails',baseValue);
    setAlpha('divContactDetails',baseValue);
}

function ShowDiv(divId)
{
//alert(divId);
    var div = document.getElementById(divId);

    if(div.style.display == 'none')
    {
        div.style.display = "block";
    }
    else
    {
        div.style.display = "none";
    }
}
