var ua = navigator.userAgent.toLowerCase();
var isSafari=ua.indexOf('safari') >-1;
var isIE = ua.indexOf('msie') != -1 && ua.indexOf('opera') == -1 ;
var isIE6 = isIE && ua.indexOf('msie 6.0') != -1;
var isIE7 = isIE && ua.indexOf('msie 7.0') != -1;

function bid(id)
{
    return window.document.getElementById(id);
}
function ebid(id)
{
    return window.document.getElementById(id);
}

function bt(yes)
{
    var pb=document.getElementById('page-bottom');
    if(yes)
    {
        pb.innerHTML='<img src="images/frame_btm_home.gif" alt="" width="960" height="15" border="0">'
    }
    else
    {
        pb.innerHTML='<img width="960" height="10" border="0" alt="" src="images/page_btm.gif">';
    }
}

function req(loc,sub)
{
    window.location=getCP()+'?pg='+loc;
}

function getFormParams(c)
{
    var params=[];
    var container=document.getElementById(c);
    var selects = container.getElementsByTagName("select");
    for(var i = 0; i < selects.length; i++)
    {
        if(selects[i].name != null)
        {
            params.push(new HP(selects[i].name, selects[i].value));
        }
    }
    var inputs = container.getElementsByTagName("input");
    for(var i = 0; i < inputs.length; i++)
    {
        if(inputs[i].name != null)
        {
            if(inputs[i].type == 'checkbox')
            {
                params.push(new HP(inputs[i].name, inputs[i].checked));
            }
            else if(inputs[i].type == 'radio')
            {
                if(inputs[i].checked)
                {
                    params.push(new HP(inputs[i].name, inputs[i].value));
                }
            }
            else if(inputs[i].type!='button')
            { 
                params.push(new HP(inputs[i].name, inputs[i].value));
            }
        }
    }

    return params;
};


function request(location, params,id,callback)
{
    var xmlhttp;
    if(window.XMLHttpRequest)
    {
        xmlhttp = new XMLHttpRequest();
    }
    else
    {
        try
        {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                return false;
            }
        }
    }

    var hasParams=params != null && params.length > 0;
    if(location.indexOf(getCP())<0)
    {
        location=getCP()+ location
    }
    xmlhttp.open(isIE||hasParams?'POST':'GET',location, true);
    xmlhttp.setRequestHeader ('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.setRequestHeader ('X-Requested-With', 'XMLHttpRequest');

    var func = function()
    {
        if (xmlhttp.readyState == 4)
        {
            if (xmlhttp.status == 200)
            {
                if(id)
                {
                    var el=bid(id);
                    el.innerHTML=xmlhttp.responseText;
                    evalScripts(el);
                }

                if(callback)
                {
                    callback(xmlhttp);
                }

                hideBusyCursor();
            }
        }
    };
    xmlhttp.onreadystatechange = func;
    var queryStr = "";
    if (params != null && params.length > 0)
    {
        for (k=0; k<params.length; k++)
        {
            queryStr += params[k].name + "=" + encodeURIComponent(params[k].value);
            if (k < params.length-1)
            {
                queryStr += "&";
            }
        }
    }

    showBusyCursor();
    xmlhttp.send(queryStr);
    return xmlhttp;
}

function HP(name, value)
{
    this.name = name;
    this.value = value;
}

function evalScripts(element)
{
    var div, divScripts, invalidScripts = [];
    var allDivs = element.getElementsByTagName('div');
    for(var i = 0; i < allDivs.length; i++)
    {
        div = allDivs[i];
        if("noscript" == div.className)
        {
            divScripts = div.getElementsByTagName('script');
            for(var j = 0; j < divScripts.length; j++)
            {
                invalidScripts.push(divScripts[j]);
            }
        }
    }

    // evaluate valid scripts
    var script, isValid, invalidScript;
    var allScripts = element.getElementsByTagName('script');
    for(var k = 0; k < allScripts.length; k++)
    {
        script = allScripts[k];
        isValid = true;
        for(var l = 0; l < invalidScripts.length; l++)
        {
            invalidScript = invalidScripts[l];
            if(script == invalidScript)
            {
                isValid = false;
                break;
            }
        }
        if(isValid)
        { 
            window.eval(script.innerHTML);
        }
    }
}

function showBusyCursor()
{
    var body = window.document.body;
    if(body&&body.className.indexOf('show-progress') == -1)
    {
        body.className += ' show-progress';
    }
}

function hideBusyCursor()
{
    var body = window.document.body;
    if(body)
    {
        body.className = body.className.replace(/ show-progress|show-progress/g, '');
    }
}

function getPageWidth(doc)
{
    var scrollWidth = 0;
    var offsetWidth = 0;

    if(doc.documentElement)
    {
        scrollWidth = doc.documentElement.scrollWidth;
        offsetWidth = doc.documentElement.offsetWidth;
    }
    else if(document.body)
    {
        scrollWidth = doc.body.scrollWidth;
        offsetWidth = doc.body.offsetWidth;
    }

    return scrollWidth > offsetWidth ? scrollWidth : offsetWidth;
}

