var pageDomain = document.location.host;
var pageImages = "http://"+pageDomain+"/templates/images/";

var marked_row = new Array;
/**
 * Sets/unsets the pointer and marker in browse mode
 *
 * @param   object    the table row
 * @param   integer  the row number
 * @param   string    the action calling this script (over, out or click)
 * @param   string    the default background color
 * @param   string    the color to use for mouseover
 * @param   string    the color to use for marking a row
 *
 * @return  boolean  whether pointer is set or not
 */

function setRowColor(theRowID, cmd, chkboxID) {
   theElement     = document.getElementById(theRowID);
   cssSelect      = document.getElementById(theRowID).getAttribute("jsSelect");
   cssHover       = document.getElementById(theRowID).getAttribute("jsHover");
   cssOriginal    = document.getElementById(theRowID).getAttribute("jsOrig");
   theFormElement = document.getElementById(chkboxID);

   switch(cmd) {
      case "hover":
         theElement.className = cssHover;
         break;
      case "unhover":
         if (theFormElement.checked) {
            theElement.className = cssSelect;
         }else{
            theElement.className = cssOriginal;
         }
         break;
   }
   return true;
}

//verify function to verify all inputs with a set pattern
function verify(frmElem){
    var regex, pattern, errmsg, fid;
    var result;
    //loop through all form elements
    for(var i = 0;i < frmElem.length;i++){
        //try to grab the pattern
        fid = frmElem.elements[i].id;
        pattern = document.getElementById(fid).getAttribute("testcode");
        errmsg  = document.getElementById(fid).getAttribute("testerror");
        //make sure there was a pattern
        if(!(pattern===null)){
            //create a regexp object with the specified pattern
            regex = new RegExp(pattern,"i");
            //test the value with the supplied pattern
            if(!(regex.test(frmElem.elements[i].value))){
                //oops, incorrect entry, get the error message
                //if there wasn't one, make one up
                if(errmsg===undefined){
                    errmsg = "Please double check the selected entry, it is incorrectly formated."
                }
                //do a std error reaction
                alert(errmsg);
                frmElem.elements[i].focus();
                return false;
            }
        }
    }
    return true;
}


/**
* Checks/unchecks all rows
*
* @param   string   the form name
* @param   boolean  whether to check or to uncheck the element
* @param   string   basename of the element
* @param   integer  min element count
* @param   integer  max element count
*
* @return  boolean  always true
*/
function setCheckboxesRange(the_form, do_check, basename, min, max) {
    for (var i = min; i < max; i++) {
        if (typeof(document.forms[the_form].elements[basename + i]) != 'undefined') {
            document.forms[the_form].elements[basename + i].checked = do_check;
        }
        if (typeof(document.forms[the_form].elements[basename + i + 'r']) != 'undefined') {
            document.forms[the_form].elements[basename + i + 'r'].checked = do_check;
        }
    }
    return true;
} 

/***********************************************
* Tabbed Document Viewer script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

/*var selectedtablink=""
var tcischecked=false

function handlelink(aobject){
    selectedtablink=aobject.href
    tcischecked=(document.tabcontrol && document.tabcontrol.tabcheck.checked)? true : false
    if (document.getElementById && !tcischecked){
        var tabobj=document.getElementById("tablist")
        var tabobjlinks=tabobj.getElementsByTagName("A")
        for (i=0; i<tabobjlinks.length; i++)
        tabobjlinks[i].className=""
        aobject.className="current"
        document.getElementById("tabiframe").src=selectedtablink
        return false
    }
    else
    return true
}

function handleview(){
    tcischecked=document.tabcontrol.tabcheck.checked
    if (document.getElementById && tcischecked){
        if (selectedtablink!="")
        window.location=selectedtablink
    }
}*/


/*Estimate Page Grid control*/

/* Tree View Grid Properties & Functions */
   var plusImage = pageImages+"plus1.gif";
   var minusImage = pageImages+"minus1.gif";
   var imageTagPrefix = "img";
   var nodeSeparator = ".";
   var n = 1;

/*
Function to collapse a Node
Function will be called Recursively till the Last Child of the Node
*/
function Collapse(Node){	
    /* Get n'th child of Node <n starts from 1 till the last child>*/
    objChildNode = document.getElementById(Node + nodeSeparator + n);
    /* Get image object of Node */
    objImg	= document.getElementById(imageTagPrefix + nodeSeparator + Node);
    /* Check if Child exists. If does not exist then Reset n = 1 & exit function & */
    if(objChildNode != null)      {	
        /* Toggle the Child Status i.e. if visible then make it invisible & vis-versa */
        if (objChildNode.style.display == "") {	
            /* Close all the Childs, Sub-Childs and so on of the Node*/
            if (objImg != null)objImg.src = plusImage;
            closeAllChilds(Node)
            return;
        } else {	
            objChildNode.style.display = "";
            if (objImg != null)objImg.src = minusImage;
        }
        /* Call Collapse for the Next Child be increamenting n*/
        n = n + 1;
        Collapse(Node);
    } else {
        n = 1;
    }
}

/*if p_direction = 0 then Close else Open*/
function ExplicitCollapse(Node,p_direction)   {	
    objChildNode = document.getElementById(Node + nodeSeparator + n);
    objImg	= document.getElementById(imageTagPrefix + nodeSeparator + Node);
    if(objChildNode != null) {	
        if (p_direction == "0") {	
            objChildNode.style.display = "none";
            if (objImg != null)objImg.src = plusImage;
            if(!closeAllChilds(objChildNode.id)) return false;
        } else {	
            objChildNode.style.display = "";
            if (objImg != null)objImg.src = minusImage;
        }
        n = n + 1;
        ExplicitCollapse(Node,p_direction);
    } else {
        n = 1;
    }
}

/*
Function to close all the childs of a Node
As Each Child Node ID contains its Parent Node ID, Close all the rows having the Node ID as a prefix in their IDs.
*/
function closeAllChilds(nNode) {
    var tempNode,objTempNode,objNode,tempImg,objBody;
    objNode= document.getElementById(nNode);
    /* Get the Parent Element of Node ie. the Table Object */
    objTable = objNode.parentNode; //objNode.parentElement;
    var tagNm = objTable.tagName;
    /* Get the Parent Element is not a Table then exit function */
    if(tagNm.toUpperCase() == "TBODY") {	
        /* For each TR of the Table*/
        for(i=0;i<objTable.rows.length;i++) {	
            /* Get the ID of the TR i.e. Node ID */
            tempNode = objTable.rows[i].id;
            /* If the ID of the TR contains nNode then make the TR invisible*/
            if (tempNode != nNode) {
                if(tempNode.indexOf(nNode) >= 0) {
                    objTempNode = document.getElementById(tempNode);
                    tempImg = document.getElementById(imageTagPrefix + nodeSeparator + tempNode);
                    if (tempImg != null)tempImg.src = plusImage;
                    objTempNode.style.display = "none";
                }
            }
        }
    } else {
       alert("Table Tag not found");
       return false;
    }
    return true;
}
   
/*
Function to keep a Node open onLoad
This function also opens up the corresponding Parent Nodes of that perticular Node
*/
function OpenNode(strNode,p_direction) {
    var objNode;
    var arrNode = strNode.split(nodeSeparator);
    var str = "";
    for(i=0;i<arrNode.length;i++){
        if(str == "") {
           str = arrNode[i];
        } else {
           str = str + nodeSeparator + arrNode[i];
        }
        objNode = document.getElementById(str);
        if (objNode != null) {
            if (p_direction == "1") {
                ExplicitCollapse(str,p_direction);
            } else {
                Collapse(str);
            }
        }
    }
}

