var detailsID = 'details'; //Used To Save Current Area
var browserType=decideBrowserVersion();
var rootUrl;
function setRootURL(url)
{
    rootUrl=url;
}

function getRootURL()
{
    return rootUrl;
}

function decideBrowserVersion()
{
    var brwsVer="undefined";
    if (document.layers)
    {
        brwsVer="ns4";
    }  
    if (document.getElementById)
    {
        brwsVer="ns6";
    }  
    if (document.all)
    {
        brwsVer="ie4";
    }  
    if (navigator.userAgent.indexOf('Opera') > 0)
    {
        brwsVer="opera";
    }  

// Microsoft Stupidity Check(tm).
    if (brwsVer=="ie4") 
    {
        if ((navigator.userAgent.indexOf('MSIE 5') > 0)|| (navigator.userAgent.indexOf('MSIE 6') > 0)) 
        {
            brwsVer="ie5";
        }
    }
    return brwsVer;
}

function getWaitHTML()
{
    return "<div class=\"wait\"><p class=\"wait\"><img src=\"" 
    + getRootURL() + "/images/spinner.gif\" alt=\"\""
    + " style=\"vertical-align: middle;\" /> Please Wait.</p></div>";
}



//*********

function openAnonPopWindow(module,menu,url,pagename)
{
     var popWindow = 
     window.open(url, "_blank", "width=800,height=500,left=50,top=50, toolbar=yes,location=yes,"
                 + "resizable=yes,directories=yes,menubar=yes,status=yes,scrollbars=yes");
     popWindow.focus();
}
                     
function openPopWindow(module,menu,url,name,pagename)
{
 var popWindow = 
 window.open(url, name, "width=800,height=500,left=50,top=50, toolbar=yes,location=yes,"
             + "resizable=yes,directories=yes,menubar=yes,status=yes,scrollbars=yes");
 popWindow.focus();
}

function openInfoWindow(url,name,anchorname)
{
 var pos=getAnchorPosition(anchorname);
 
 var popWindow = 
 window.open(url, name, "width=200,height=200,left=" + pos.x + ",top=" + pos.y + ",location=no,"
             + "directories=no,status=no");
 //popWindow.focus();
}


function tabClicked(tab, url)
{
    var wrapperDiv = $(tab).parentNode.parentNode.parentNode;
    var tabList = $(tab).parentNode;
    var bodyDiv;
    var oldTab;
    var i;

    if (wrapperDiv == null)
    { return false; }

    for (i = 0; i < tabList.childNodes.length; i++)
    {
            if (tabList.childNodes[i].nodeName == "LI" && 
                    (Element.hasClassName(tabList.childNodes[i],"selected_tab") ||
                     Element.hasClassName(tabList.childNodes[i],"selected_tab_with_filter")))
            {
                    oldTab = tabList.childNodes[i];
                    i = tabList.childNodes.length;
            }
    }

    if (oldTab == tab)
    {
            //We're Trying To Open The New Tab Again, So Exit
            return false;
    }

    for (i = 0; i < wrapperDiv.childNodes.length; i++)
    {
            if (wrapperDiv.childNodes[i].nodeName == "DIV" && 
                    (wrapperDiv.childNodes[i].className.indexOf("tab_set_body") != -1))
            {
                                    bodyDiv = wrapperDiv.childNodes[i];
                                    i = wrapperDiv.childNodes.length;
            }   
    }

    if((url!=null)&&(url!="")){
        var pleaseWait = '<div id="please_wait">&nbsp;</div><div id="please_wait_inner"><p class="wait" style="border: 1px solid #ededed; background: white; padding: 5px;"><img src="/images/spinner.gif" alt="" style="vertical-align: middle;" /> Please Wait.</p></div>'
        $(bodyDiv).innerHTML += pleaseWait;
        new Ajax.Updater(bodyDiv, url, {asynchronous:true});
    }
    else 
    {
        $(bodyDiv).innerHTML = "<h2><center>No content specified</center></h2>";
    }

    Element.removeClassName(oldTab, "selected_tab");
    Element.removeClassName(oldTab, "selected_tab_with_filter");
    Element.addClassName(tab, "selected_tab");
    return false;
}

function insertPageTemplate (templateUrl, areaId)
{
  var slash="/";
  var postBody="";
  if (templateUrl.charAt(0)=='/')
  {
    slash="";
  }
  var URL = slash+unescape(templateUrl);
  var URLPath=URL;
  var questIdx=URL.indexOf('?');
  if (questIdx>-1){
      URLPath=URL.substring(0,questIdx);
      postBody=URL.substring(questIdx+1);
  }
  $(areaId).innerHTML = getWaitHTML();
  new Ajax.Updater(
      {success: areaId},
      URLPath,
      {
          method: 'post',
          postBody: postBody,
          onFailure: reportDisplayError
      });
}

function getPage(page_name, form, replace) 
{
    var URL = unescape(location.href);	// get current URL in plain ASCII
        
    var postBody = Form.serialize(form);
    postBody += "&page=" + page_name;
    
    $(replace).innerHTML = getWaitHTML();

    new Ajax.Updater(
        {success: replace},
        URL,
        {
            method: 'post',
            postBody: postBody,
            onFailure: reportDisplayError
        });
}

function reportDisplayError(request) 
{
    var html = "<p class=\"error_text center\">There was an error getting the requested ";
    html += "details.</p>";
}

/* Get Edit Sections */
function toggleEdit(areaId, srcUrl, controlId, expSmb, cllpsSmb, method)
{
    var postBody="";
    var slash="/";
    if (srcUrl.charAt(0)=='/'){slash="";}
    var URL = slash+unescape(srcUrl);
    var URLPath=URL;
    var questIdx=URL.indexOf('?');
    var params="";
    while((expSmb.indexOf("~")>-1)||(expSmb.indexOf("|")>-1)){
        expSmb=expSmb.replace("~","\"");
        expSmb=expSmb.replace("|","'");
    }
    while((cllpsSmb.indexOf("~")>-1)||(cllpsSmb.indexOf("|")>-1)){
        cllpsSmb=cllpsSmb.replace("~","\"");
        cllpsSmb=cllpsSmb.replace("|","'");
    }
    if (questIdx>-1){
        var URLPath=URL.substring(0,questIdx);
        params=URL.substring(questIdx+1);
    }
    postBody = params+postBody;
    if($(areaId).style.display == 'none'){
        $(controlId).innerHTML=cllpsSmb;
    }else{
        $(controlId).innerHTML=expSmb;
    }
    Element.toggle(areaId);
    if ($(areaId).style.display != 'none')
    {
        $(areaId).innerHTML = getWaitHTML();
        detailsID = areaId;

        if (method == "post")
        {
            new Ajax.Updater(
                {success: areaId},
                URLPath,
                {
                    method: "post",
                    evalScripts: true,
                    postBody: postBody,
                    onFailure: reportDisplayError
                });
        } 
        else
        {
            new Ajax.Updater(
                {success: areaId},
                URLPath,
                {
                    method: "get",
                    evalScripts: true,
                    parameters: postBody,
                    onFailure: reportDisplayError
                });
        }
    }
}

/* toggles area(s) by id or string of comma-separated id's. Changes control element to [+]/[-] appearance*/
function toggleRows(rowId,anchId)
{
    if($(anchId).innerHTML=="[+]")
    {
        $(anchId).innerHTML="[-]";
    }else
    {
        $(anchId).innerHTML="[+]";
    }
    if(rowId.indexOf(',')>-1)
    {
        idx=0;
        while (rowId.indexOf(',',idx)>-1)
        {
            strId=rowId.substring(idx,rowId.indexOf(',',idx));
            Element.toggle(strId);
            idx=rowId.indexOf(',',idx)+1;
        }
        rowId=rowId.substring(idx);
    }
    Element.toggle(rowId);
}

/* toggles area(s) by id or string of comma-separated id's. Changes control element to expand/collapse appearance*/
function toggleArea(areaId, anchId, signOn, signOff)
{
    while((signOn.indexOf("~")>-1)||(signOn.indexOf("|")>-1))
    {
        signOn=signOn.replace("~","\"");
        signOn=signOn.replace("|","'");
    }
    while((signOff.indexOf("~")>-1)||(signOff.indexOf("|")>-1))
    {
        signOff=signOff.replace("~","\"");
        signOff=signOff.replace("|","'");
    }
    idx=0;
    if(areaId.indexOf(',')>-1)
    {
        while (areaId.indexOf(',',idx)>-1)
        {
            strId=areaId.substring(idx,areaId.indexOf(',',idx));
            Element.toggle(strId);
            idx=areaId.indexOf(',',idx)+1;
        }
        areaId=areaId.substring(idx);
    }
    if($(areaId).style.display == 'none')
    {
        $(anchId).innerHTML=signOff;
    }else
    {
        $(anchId).innerHTML=signOn;
    }
    Element.toggle(areaId);
}

function submitLoginForm(formId,detailsArea,topMenuUri,sideMenuUri,sessionId)
{
    var actionURL = $(formId).action;
    Form.disable(formId);
    var params = Form.serialize(formId);
    $(detailsArea).innerHTML += getWaitHTML();
    new Ajax.Request(
                actionURL,
                {
                    method: 'post',
                    postBody: params,
                    onSuccess: function (t) 
                    {
                        if (t.responseText == "success") 
                        { 
                            $(detailsArea).innerHTML = "";
                            $(detailsArea).display = 'none';
                            Try.these(
                                function() 
                                { 
                                    afterEditSuccess(); 
                                }
                            ); 
                        }
                        else 
                        { 
                            $(detailsArea).innerHTML = t.responseText; 
                        }
                    },
                    onFailure: reportDisplayError
                });
    insertPageTemplate(getRootURL()+topMenuUri+";jsessionid="
                    +sessionId+"?"+params,'top_menu_zone');
    insertPageTemplate(getRootURL()+sideMenuUri+";jsessionid="
                    +sessionId+"?"+params,'side_menu_zone');
}

function numbersonly(myfield, e, dec)
{
    var keychar;
    var key=(window.event)?window.event.keyCode:e.which;
    keychar = String.fromCharCode(key);

    // control keys
    if ((key==null) || (key==0) || (key==8) ||
        (key==9) || (key==13) || (key==27) )
       return true;

    // numbers
    else if ((("0123456789").indexOf(keychar) > -1))
       return true;

    // decimal point jump
    else if (dec && (keychar == "."))
    {
       myfield.form.elements[dec].focus();
       return false;
    }
    else
       return false;
}

function setThumbEltActive(element)
{
    var iconIdx=1;
    while($("thumb_"+iconIdx)!= null)
    {
        iconElt=$("thumb_"+iconIdx);
        iconElt.className=(iconElt==element)?"thmb_act":"thmb";
        iconIdx++;
    }
}

function roundNumber(num, dec) 
{
    var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
    return result;
}

function openWin(internalURL,w,h) 
{
    openWinByName('win',internalURL,w,h);
}

function openWinByName(internalName,internalURL,w,h) 
{
    var internalW = 640;
    var internalH = 480;
    if (w != undefined) {
            internalW = w;
    }
    if (h != undefined) {
            internalH = h;
    }
    var internalArgs = 'scrollbars,resizable,toolbar,status,width='+internalW+
        ',height=' + internalH + ',left=50,top=50';
    var popupWin = window.open(getRootURL()+internalURL,internalName,internalArgs);
    popupWin.focus();
}


//functions extending String object
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function windowDimensions()
{
    if (document.documentElement && document.documentElement.clientHeight)
    {
        return [document.documentElement.clientWidth,
                document.documentElement.clientHeight];
    }
    else if (document.body)
    {
        return [document.body.clientWidth, document.body.clientHeight];
    }
    
    return [window.innerWidth, window.innerHeight];
}

function windowOffset()
{
    if (document.documentElement && document.documentElement.scrollTop)
    {
    	return [document.documentElement.scrollLeft,
    	        document.documentElement.scrollTop];
    }
    else if (document.body)
    {
    	return [document.body.scrollLeft, document.body.scrollTop];
    }
    
    return [window.pageXOffset, window.pageYOffset];
}

