var original_body_height;
var original_right_height;
var is_initial = true;

function on_resize()
{
    var client_height = xClientHeight();
    var main_header_height = xHeight('main-header');
    var main_header_map_height = xHeight('main-header-map');
    var main_footer_height = xHeight('main-footer');
    var main_body_height   = client_height-(main_header_height+main_header_map_height+main_footer_height)-2;

    var body = xGetElementById('main-body');
    var body_table = xGetElementById('main-body-table');
    
    if( is_initial )
    {
        original_body_height = xHeight('main-body');
        original_right_height = xHeight('main-right');
    }

    if( xHeight(body_table) > main_body_height )
    {
        main_body_height = xHeight(body_table);
    }
    
    if( main_body_height > xHeight(body) )
    {
        xHeight(body,main_body_height);
        xHeight('main-body-table',main_body_height-20);
    }
}
xAddEventListener(window,'resize',on_resize,false);

function resize_by_div(div)
{
    var main_body_height = original_body_height + div_height;
    var main_right_height = original_right_height + div_height;
    var main_body = xGetElementById('main-body');
    var main_right = xGetElementById('main-right');
    
    if( xHeight(main_body) < main_body_height )
    {
        alert(main_body_height);
        xHeight(main_body,main_body_height);
    }
    if( xHeight(main_right) > main_right_height )
    {
        xHeight(main_right,main_right_height);
    }
}

function resize(div_height)
{
    var main_body_height = original_body_height + div_height;
    var main_right_height = original_right_height + div_height;
    var main_body = xGetElementById('main-body');
    var main_right = xGetElementById('main-right');

    xHeight(main_body,main_body_height);
    xHeight(main_right,main_right_height);
    
    xGetElementById('main-body-table').height = '97%';
}

function set_highlite(elem,highlite)
{
    if( highlite == true )
    {
        xGetElementById(elem).className = 'highlite';
    }
    else
    {
        if( elem == xGetElementById('main-header-tabs-'+context_path[0]) )
        {
            xGetElementById(elem).className = 'current';
        }
        else
        {
            xGetElementById(elem).className = '';
        }
    }
}

var t = null;

function context_initialize()
{
    for( var prop in context_options )
    {
        elem = xGetElementById('main-header-tabs-'+context_options[prop]);
        
        xAddEventListener(elem,'mouseover',display_drop_menu,false);
        xAddEventListener(elem,'mouseout',hide_drop_menu,false);
    }
    
    t = null;

    on_resize();
}

function hide_drop_menu(e)
{
    var event = new xEvent(e);
       
    if( event.target.nodeName == 'A' && event.target.innerHTML != '' )
    {       
        var elem = event.target.parentNode.parentNode.parentNode.parentNode;
        
        tab_id = elem.id;
        tab_id = tab_id.split("-");
        tab_id = tab_id[tab_id.length-1];

        var opts_id = 'main-header-opts-'+tab_id;
        
        t = setTimeout("hide_menu('"+opts_id+"');",500);
    }
}

function hide_submenus(e)
{
    var event = new xEvent(e);
       
    if( event.target.nodeName == 'TD' && event.target.innerHTML != '' && event.relatedTarget.onclick == null)
    {
        var td_id = event.target.id.toLowerCase().split("-");
        
        var opts_id = 'main-header-opts-'+td_id[1];
               
        xRemoveEventListener('main-header-table-'+td_id[1],'mouseout',hide_submenus,false);
        hide_menu(opts_id);
    }
}

function display_drop_menu(e)
{
    var event = new xEvent(e);
    
    if( event.target.nodeName == 'A' && event.target.innerHTML != '' )
    {
        //object_dump(t);
        //clearTimeout(t);
        
        clearMenus();
        
        var elem = event.target.parentNode.parentNode.parentNode.parentNode;
                
        tab_id = elem.id;
        tab_id = tab_id.split("-");
        tab_id = tab_id[tab_id.length-1];

        var opts_id = 'main-header-opts-'+tab_id;

        if(xGetElementById(opts_id))
        {
            xGetElementById('arrow-'+tab_id).src = xGetElementById('server-path').value+"/images/gray-arrow-down.gif";
            
            //var xOffSet = getXOffset('main-header-tabs-'+tab_id);
            var xOffSet = getXOffset('main-header-tabs-'+tab_id)+(xClientWidth()-990)/2;

            xTop(opts_id,73);
            xLeft(opts_id,xOffSet);
            xWidth(opts_id,150);
        
            xAddEventListener(opts_id,'mouseover',function(){clearTimeout(t);t=null;},false);
            xAddEventListener('main-header-table-'+tab_id,'mouseout',hide_submenus,false);
            //xAddEventListener(opts_id,'mouseout',hide_submenus,false);
            
            var subMenus = xGetElementsByClassName('MenuMouseOut',elem);
            
            for( var prop in subMenus )
            {
                xAddEventListener(subMenus[prop].id,'mouseover',highlight_submenu,false);
                xAddEventListener(subMenus[prop].id,'mouseout',unhighlight_submenu,false);
            }
            
            xGetElementById(opts_id).style.display = 'block';
        }
    }
}

function highlight_submenu(e)
{
    var event = new xEvent(e);
    
    event.target.className = 'MenuMouseIn';
}

function unhighlight_submenu(e)
{
    var event = new xEvent(e);
    
    event.target.className = 'MenuMouseOut';
}

function clearMenus()
{
    var elems = xGetElementsByClassName('main-header-opts');
       
    for(var prop in elems)
    {
        if( elems[prop].style )
        {
            tab_id = elems[prop].id;
            tab_id = tab_id.split("-");
            tab_id = tab_id[tab_id.length-1];
            
            xGetElementById('arrow-'+tab_id).src = xGetElementById('server-path').value+"/images/gray-arrow.gif";
            
            //elems[prop].style.display = 'none';
            hide_menu(elems[prop].id);
        }
    }
}

function hide_menu(current_opts_id)
{
    if( xGetElementById(current_opts_id) )
    {
        tab_id = current_opts_id;
        tab_id = tab_id.split("-");
        tab_id = tab_id[tab_id.length-1];
        
        xGetElementById('arrow-'+tab_id).src = xGetElementById('server-path').value+"/images/gray-arrow.gif";
        
        var subMenus = xGetElementsByClassName('MenuMouseOut',xGetElementById(current_opts_id));
        
        for( var prop in subMenus )
        {
            xRemoveEventListener(subMenus[prop].id,'mouseover',highlight_submenu,false);
            xRemoveEventListener(subMenus[prop].id,'mouseout',unhighlight_submenu,false);
        }

        xGetElementById(current_opts_id).style.display = 'none';
        
        if( t != null )
        {
            clearTimeout(t);
            t=null;
        }
    }
}

function getXOffset(tab_id)
{
    var elems = xGetElementsByClassName('main-header-tabs');
    var xOffSet = 155;
       
    for(var prop in elems)
    {
        if( elems[prop].id == tab_id )
        {
            return xOffSet;
        }
        else
        {
            xOffSet += xWidth(elems[prop].parentNode);
        }
    }
}

function div_show(div_index,div_number)
{
    for( var i=0; i<div_number; ++i)
    {
        var div = xGetElementById('div-'+i);
        div.style.display = (div_index == i) ? 'block' : 'none';

        if (div_index == i)
        {
            resize_by_div(div);
        }
    }
}

function div_void(div_index, div_number)
{
    var div_height=0;

    for( var i=0; i<div_number; ++i)
    {
        var div = xGetElementById('div-'+i);

        if( div_index == i )
        {
            if( div.style.display == 'none' )
            {
                div.style.display = 'block';
            }
            else
            {
                div.style.display = 'none';
            }
        }
        div_height = div_height + xHeight(div);
    }
    resize(div_height);
}

function div_show_with_radio(div_index,div_number)
{
    var div_height=0;

    for( var i=0; i<div_number; ++i)
    {
        var div = xGetElementById('div-'+i);
        div.style.display = (div_index == i) ? 'block' : 'none';

        if( xGetElementById('radio-'+i) )
        {
            var radio = xGetElementById('radio-'+i);
            if( div_index == i )
            {
                radio.checked = "true";
            }
            else
            {
                radio.checked = "";
            }
        }

        div_height = div_height + xHeight(div) + 20;
    }
    resize(div_height);
}

function mail_sender()
{
    var sendMailForm = this.document.sendEmailForm;

    if( sendMailForm.emailAddress.value == "" )
    {
        alert("Please enter your email address!");
        sendMailForm.emailAddress.focus();
    }
    else if( !isEmail(sendMailForm.emailAddress.value) )
    {
        alert("Invalid email format.");
        sendMailForm.emailAddress.focus();
    }
    else
    {
        if( sendMailForm.yourName.value == "" )
        {
            alert("Please specify your name!");
            senMailForm.yourName.focus();
        }
        else
        {
            if( sendMailForm.emailMessage.value == "" )
            {
                alert("Please give us any message");
                sendMailForm.emailMessage.focus();
            }
            else
            {
                sendMailForm.submit();
            }
        }
    }
}

function add_cart(product_code,license_key)
{
    var form = xGetElementById('cart-form');

    form.action  = xGetElementById('server-path').value + "/main.php?context=public&path=callingcard-shoppingcart";
    form.method  = "POST";

    xGetElementById('license-key').value = license_key;
    xGetElementById('product-code').value = product_code;
    xGetElementById('cmd').value = "Add";

    form.submit();
}

var auto_process = true;

function process_login(e)
{
    var event = new xEvent(e);

    if( event.keyCode == 9 )
    {
        if( xGetElementById('login-name').value != "" && xGetElementById('login-pass').value != "" )
        {
            auto_process = false;
        }
    }

    if( event.keyCode == 13 && auto_process == true)
    {
        //Force to execute login function if firefox
        //alert('"'+xGetElementById('login-pass').value+'"');
        do_login();
    }
}

var click_to_call_x_pos = 0;
var hover_started = false;

function hover_click_to_call()
{
    if( hover_started == false )
    {
        change_transparency(false);
        hover_started = true;
    }

    xTop('click-to-call-div',xClientHeight()+xScrollTop()-100);
    xLeft('click-to-call-div',xClientWidth()-130);

    xGetElementById('click-to-call-div').style.display = 'block';
}

function change_transparency(mousein)
{
    var obj = xGetElementById('click-to-call-div').style;
    if( mousein )
    {
        obj.opacity = 1;
        obj.MozOpacity = 1;
        obj.KhtmlOpacity = 1;
        obj.filter = "alpha(opacity=100)";
    }
    else
    {
        obj.opacity = 0.7;
        obj.MozOpacity = 0.7;
        obj.KhtmlOpacity = 0.7;
        obj.filter = "alpha(opacity=70)";
    }
}

function display_live_contact()
{
    var bgObj = xGetElementById('livecontact-background');
    var page_width = xClientWidth();
    var page_height = xHeight('main-header')+xHeight('main-body')+xHeight('main-footer');
    
    xTop(bgObj,0);
    xLeft(bgObj,0);
    xWidth(bgObj,page_width);
    xHeight(bgObj,page_height);
    
    bgObj.style.display = 'block';
    
    bgObj.style.opacity = 0.7;
    bgObj.style.MozOpacity = 0.7;
    bgObj.style.KhtmlOpacity = 0.7;
    bgObj.style.filter = "alpha(opacity=70)";
    
    xAddEventListener(bgObj,'click',hide_live_contact,false);  
   
    var obj = xGetElementById('livecontact-div');
    xTop(obj,xScrollTop()+(xClientHeight()-200)/2);
    xLeft(obj,(page_width-400)/2);
    xWidth(obj,400);
    xHeight(obj,200);
    
    obj.style.display = 'block';
}

function hide_live_contact()
{
    var bgObj = xGetElementById('livecontact-background');
    var obj = xGetElementById('livecontact-div');
    
    bgObj.style.display = 'none';
    obj.style.display = 'none';
}

function switch_language(language)
{   
    var form = document.createElement('form');
    form.setAttribute('method','post');

    if( location.href != document.getElementById('server-path').value+"/" )
    {
        form.setAttribute('action',location.href);
    }
    else
    {
        form.setAttribute('action',document.getElementById('server-path').value+"/Home");
    }

    form.setAttribute('id','language-form');
    form.setAttribute('name','language-form');
    
    input = document.createElement('input');
    input.setAttribute('type','hidden');
    input.setAttribute('name','language');
    input.setAttribute('id','language');
    input.setAttribute('value',language);
    form.appendChild(input);
    
    document.getElementsByTagName('body')[0].appendChild(form);
        
    form.submit();
}

/* Create the two div elements needed for the top of the box */
function insertTop(outer)
{
	var outerTop = document.createElement("div");
	outerTop.className = "RBTop"; // The outer div needs a class name
	var innerTop = document.createElement("div");
	outerTop.appendChild(innerTop);
	outer.insertBefore(outerTop, outer.firstChild);
}

/* Create the two div elements needed for the bottom of the box */
function insertBottom(outer)
{
	var outerBottom = document.createElement("div");
	outerBottom.className = "RBBottom"; // The outer div needs a class name
	var innerBottom = document.createElement("div");
	outerBottom.appendChild(innerBottom);
	outer.appendChild(outerBottom);
}

/* Create RoundedBoxes */
function initRB()
{
	// Find all div elements
	var divs = document.getElementsByTagName('div');
	var rbDivs = [];
	for (var i = 0; i < divs.length; i++)
	{
		// Find all div elements with bRoundedBoxDefault in their class attribute while allowing for multiple class names
		if (/\bRoundedBoxDefault\b/.test(divs[i].className))
			rbDivs[rbDivs.length] = divs[i];
	}

	// Loop through the found div elements
	var original, outer, left, right;
	for (var i = 0; i < rbDivs.length; i++)
	{
		// Save the original outer div for later
		original = rbDivs[i];

		// Create a new div, give it the original div's class attribute, and replace default class with DOM/JS class
		outer = document.createElement('div');
		outer.className = original.className;
		outer.className = original.className.replace('RoundedBoxDefault', 'RoundedBox');

		// Change the original div's class name and replace it with the new div
		original.className = 'RBContent';
		original.parentNode.replaceChild(outer, original);

		// Create two new div elements and insert them into the outermost div
		left = document.createElement('div');
		left.className = 'RBLeftBorder';
		outer.appendChild(left);
		right = document.createElement('div');
		right.className = 'RBRightBorder';
		left.appendChild(right);

		// Insert the original div
		right.appendChild(original);

		// Insert the top and bottom divs
		insertTop(outer);
		insertBottom(outer);
	}
}

function init_window()
{
    initRB();

    if( xGetElementById('main-header-login') )
    {
        if(document.layers||(document.getElementById&&!document.all))
        {
            xAddEventListener(xGetElementById('main-header-login'),'keydown',process_login,true);
        }
    }
    
    if( xGetElementById('click-to-call-div') )
    {
        hover_click_to_call();
        xAddEventListener(window,'scroll',hover_click_to_call);
        
        xAddEventListener('click-to-call-div','mouseover',function(){change_transparency(true);});
        xAddEventListener('click-to-call-div','mouseout',function(){change_transparency(false);});
    }
        
}

xAddEventListener(window,'load',init_window,false);