var menuOpen;
var menuOpenTitle;
var selectedItem;

function onMenuItemOver(ev)
{
    var Dom = YAHOO.util.Dom;
    var Event = YAHOO.util.Event;
    var target = Event.getTarget(ev);
    var cs = target.getAttribute('class') ? target.getAttribute('class') :  target.getAttribute('className');
    if (cs.indexOf("menuItemSelected")>=0)
        return;
    replaceClass(target, "subItem", "menuItemOver");
}

function onMenuItemOut(ev)
{
    var Dom = YAHOO.util.Dom;
    var Event = YAHOO.util.Event;
    var target = Event.getTarget(ev);
    replaceClass(target, "menuItemOver", "subItem");
    var cs = target.getAttribute('class') ? target.getAttribute('class') : target.getAttribute('className');
    if (cs.indexOf("menuItemSelected")>=0)
    {
        replaceClass(target, "menuItemSelected", "menuItemSelected");
    }
}

function onMenuTitleOver(ev)
{
    var Dom = YAHOO.util.Dom;
    var Event = YAHOO.util.Event;
    var target = Event.getTarget(ev);
    var cs = target.getAttribute('class') ? target.getAttribute('class') : target.getAttribute('className');
    if (cs.indexOf("menuTitleSelected")>=0)
        return;
    replaceClass(target, "menuTitle", "menuTitleOver");
}

function onMenuTitleOut(ev)
{
    var Dom = YAHOO.util.Dom;
    var Event = YAHOO.util.Event;
    var target = Event.getTarget(ev);
    replaceClass(target, "menuTitleOver", "menuTitle");
    var cs = target.getAttribute('class') ? target.getAttribute('class') : target.getAttribute('className');
    if (cs.indexOf("menuTitleSelected")>=0)
    {
        replaceClass(target, "menuTitleSelected", "menuTitleSelected");
    }
}

function init_menu()
{
    var Dom = YAHOO.util.Dom;
    var Event = YAHOO.util.Event;

    var nav = document.getElementById("masterdiv");
    if (nav!=undefined)
    {
        var nodes = Dom.getElementsByClassName("subItem", "div", nav);
        Event.addListener(nodes, "mouseover", onMenuItemOver);
        Event.addListener(nodes, "mouseout", onMenuItemOut);
        
        var nodes2 = Dom.getElementsByClassName("menuTitle", "div", nav);
        Event.addListener(nodes2, "mouseover", onMenuTitleOver);
        Event.addListener(nodes2, "mouseout", onMenuTitleOut);   
    }
}

function SwitchMenuWrap(ev)
{
    try{
    var Dom = YAHOO.util.Dom;
    var Event = YAHOO.util.Event;
      

    if (menuOpenTitle!=undefined)
    {
        Dom.removeClass(menuOpenTitle, "menuTitleSelected");
    }

    var target = Event.getTarget(ev);
    if (target!=undefined)
    {
        Dom.addClass(target, "menuTitleSelected");
    }
    menuOpenTitle = target;
    SwitchMenu(target.getAttribute('subid'));
} catch (e) {errorReport(e);};
}

function loadIframeWrap(ev)
{
    var Dom = YAHOO.util.Dom;
    var Event = YAHOO.util.Event;

    var target = Event.getTarget(ev);
    loadIframe(target.getAttribute('targetSrc'));
    if (selectedItem!=undefined)
    {
        Dom.removeClass(selectedItem, "menuItemSelected");
    }
    Dom.addClass(target, "menuItemSelected");
    selectedItem = target;
}

function createMenuTitle(root, subid, name)
{
    var Dom = YAHOO.util.Dom;
    var Event = YAHOO.util.Event;

    var root = Dom.get(root);
    if (root!=undefined)
    {
        var menu = document.createElement('div');
        Dom.addClass(menu, 'menuTitle');
        Dom.addClass(menu, 'trans');
        menu.setAttribute('subid', 'sub'+subid);
        menu.setAttribute('title', name);
        menu.innerHTML = name;
        root.appendChild(menu);
        
        var submenu = document.createElement('span');
        Dom.addClass(submenu, 'submenu');
        submenu.setAttribute('id', 'sub'+subid);        
        root.appendChild(submenu);
        
        Event.addListener(menu, 'click', SwitchMenuWrap);
        Event.addListener(menu, "mouseover", onMenuTitleOver);
        Event.addListener(menu, "mouseout", onMenuTitleOut);
    }
}

function createMenuItem(subid, name, targetSrc, subsubid)
{
    var Dom = YAHOO.util.Dom;
    var Event = YAHOO.util.Event;

    var root = Dom.get('sub'+subid);
    if (root!=undefined)
    {
        var item = document.createElement('div');
        item.setAttribute('title', name);
        item.setAttribute('targetSrc', targetSrc);
        item.setAttribute('subsubid', subsubid);
        item.innerHTML = name;
        Dom.addClass(item, 'subItem');
        Dom.addClass(item, 'trans');
        root.appendChild(item);
        Event.addListener(item, 'click', loadIframeWrap);
        Event.addListener(item, "mouseover", onMenuItemOver);
        Event.addListener(item, "mouseout", onMenuItemOut);
    }
}

function initializeMenuStatus(target_idx, target_subidx)
{
    var Dom = YAHOO.util.Dom;

    SwitchMenu('sub'+target_idx);

    var y = Dom.get('sub'+target_idx);
    if (y==undefined)
        return;

    var x = Dom.getElementsByClassName('subItem', 'div', y);
    if (x==undefined || x.length<1)
        return;

    var target = undefined;
    for (var i=0; i<x.length; i++)
    {
        var subsubid = x[i].getAttribute('subsubid');
        if (subsubid!=undefined && subsubid == target_subidx)
        {
            target = x[i];
            break;
        }
    }
    if (target==undefined)
        return;

    if (selectedItem!=undefined)
    {
        Dom.removeClass(selectedItem, "menuItemSelected");
    }
    Dom.addClass(target, "menuItemSelected");
    selectedItem = target; 
}

function SwitchMenu(item)
{
    try{
    if (menuOpen != undefined)
    {
        menuOpen.style.visibility = "hidden";
        menuOpen.style.display = "none";
    }

    var target;
    if (item != undefined)
    {
        target = document.getElementById(item);
        if (target != undefined)
        {
            menuOpen = target;
            target.style.visibility = "visible";
            target.style.display = "block";
        }
    }
    menuOpen = target;
} catch (e) {errorReport(e);}
}

function loadIframe(theURL)
{
    var target = document.getElementById("mainContent");
    if (target != undefined)
    {
        target.src=theURL;
    }
}

function resizeMe()
{
    try
    {
        var docHeight = 0;
        if (mainContent != undefined && mainContent.document != undefined && mainContent.document.body != undefined)
            docHeight = mainContent.document.body.scrollHeight;
        if (docHeight<400 || docHeight == undefined)
            docHeight = 400;
        var target = document.getElementById("mainContent");
        if (target != undefined)
        { 
            docHeight += 100;
            target.style.height = docHeight + 'px';
        }
    }
    catch (e)
    {
    }
}

/* Usage: el = replaceHtml(el, newHtml) instead of el.innerHTML = newHtml */
function replaceHtml(el, html) {
    var oldEl = (typeof el === "string" ? document.getElementById(el) : el);
    /*@cc_on // Pure innerHTML is slightly faster in IE
        oldEl.innerHTML = html;
        return oldEl;
    @*/
    var newEl = oldEl.cloneNode(false);
    newEl.innerHTML = html;
    oldEl.parentNode.replaceChild(newEl, oldEl);
    /* Since we just removed the old element from the DOM, return a reference
    to the new element, which can be used to restore variable references. */
    return newEl;
}

function errorReport(e)
{
    return;
    if (e == undefined || typeof(e) != 'object')
    {
        //alert("errorReport with bad obj!");
        return;
    }
    var msg = "Error:\n";
    for (var i in e)
    {
        msg += i + ": " + e[i] + "\n";
    }
    //alert(msg);
}

function validate_email(email)
{
    apos = email.indexOf("@");
    if (apos<1)
        return false;

    name = email.substr(0, apos);
    domain = email.substr(apos+1);

    if (name.length<1 || domain<3)
        return false;

    if (!name.match(/^[A-Za-z_\-0-9.]+$/))
        return false;
    if (!domain.match(/^[A-Za-z_\-0-9]+\.[A-Za-z_\-0-9]+/))
        return false;

    return true;
}


/*************************************************************/
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
/*************************************************************/

function replaceClass(target, oldClass, newClass)
{
    try
    {
        var Dom = YAHOO.util.Dom;
        var Event = YAHOO.util.Event;

        if (target == undefined)
            return;

        if (oldClass!=undefined)
            Dom.removeClass(target, oldClass);

        if (newClass!=undefined)
            Dom.addClass(target, newClass);
    } catch (e) {errorReport(e);}
}

function handleSessionTimeout()
{
    alert("Your session has been timeout!\n");
    //parent.location.replace('/index.php');
    top.location.assign('/index.php');
}

function kickActive()
{
    _kickActive();
}

function handlekickActive(http_request)
{
    if (http_request.readyState == 4)
    {
        if (http_request.status == 401)
        {
            return;
        }        
        else if (http_request.status == 200)
        {
            var mesg = http_request.responseText;
            if (mesg.match(/OK/i))
            {
                setTimeout("_kickActive();", 60000);
            }
        }
    }
}

function _kickActive()
{
    var http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
    } 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) {
        return false;
    }

    http_request.onreadystatechange = function() { handlekickActive(http_request); };

    http_request.open('GET', '/kickactive.php?rand=' + Math.floor(Math.random()*100000) , true);
    http_request.send(null);
}

function onFocusSelect(obj)
{
    if (obj==undefined)
        return;
    if (obj.type == undefined)
        return;
    if (obj.type != 'text' && obj.type != 'password')
        return;

    if (obj.select != undefined)
        obj.select();
}

function handleResizeMe()
{
    try {
        if (window != parent)
        {
            if (   parent.frames != undefined && parent.frames[0] != undefined
                && parent.frames[0].parent != undefined 
                && typeof parent.frames[0].parent.resizeMe == 'function')
                parent.frames[0].parent.resizeMe();
        }
    } catch (e) {}
}

function myEscape(str)
{
    var eStr = str;

    eStr = eStr.replace(/&/g, "&amp;");
    eStr = eStr.replace(/"/g, "&quot;");
    eStr = eStr.replace(/'/g, "&apos;");
    eStr = eStr.replace(/</g, "&lt;");
    eStr = eStr.replace(/>/g, "&gt;");

    return eStr;
}

// validation helper functions
function myIsNumber(o)
{
    if (typeof o == 'string')
    {
        return (null == o.match(/^\d+$/) ? false: true);
    }
    else if (typeof o == 'number')
    {
        return true;
    }

    return false;
}
/////////////////////////////////////////////
