/*
|| Prefix Details
|| --------------
|| Br:browser
|| g:global variable
|| s:string
|| n:number
|| b:boolean
|| d:date 
|| o:object
|| WS:White space
|| Elem: Element
*/ 

/*
|| Global variable declaration.
*/

var gbDebug = false;
String.prototype.trim= function()
                       {
                          return this.replace(/^\s*|\s*$/, '');
                       }
String.prototype.isFirstChSpace= function()
                       {
                          return this.replace(/^\s*/, '');
                       }
/*
|| Determine and return browser make.
*/

function getBrMake()
{
   var sMake=null;
   
   try
   {
      if(navigator.appName.indexOf("Netscape")>-1)
         sMake="Netscape";
      else if((navigator.appName.indexOf("Microsoft")>-1) || (navigator.appName.indexOf("MSIE")>-1))
         sMake="Microsoft Internet Explorer";
      else if(navigator.appName.indexOf("Opera")>-1)
         sMake="Opera";
      else
         sMake=navigator.appName;
   }
   catch(ex)
   {
      if(gbDebug) alert("getBrMake:\n"+ex);
   }
   return sMake;
}

/*
|| Determine and return browser type and version.
*/

function getBrTypVer()
{
   var sMake=null;
   var nVer=null;
   var sDtls=null;      
   try
   {
         
     sMake=getBrMake();    
      nVer=parseFloat(navigator.appVersion);
      sDtls=sMake+"-"+nVer;      
   }
   catch(ex)
   {
      if(gbDebug) alert("getBrTypVer:\n"+ex);
   }
   finally
   {
      sMake=null;
      nVer=null;      
   }   
   return sDtls;
}

/*
|| Determine and return browser details and its original version.
*/ 

function getBrDtls()
{
   return navigator.userAgent;
}

/* 
|| Raises user defined exceptions using created error object.
|| @param   sMsg denotes the message to shown.
*/


function raiseEx(sMsg)
{
   var oEx=new Error(sMsg);
   throw oEx;   
}

/* 
|| Add an event handler for the given element.
|| @param  oElem  of type object/node represents the element for e.g. "tr".
|| @param  sEvType of type string represtents evenname e.g. click.
|| @param  bCapture: To capture bubbling event from top to bottom for Ns
||         and bottom to top for IE.
|| @return Boolean value: true if event added successfully else false.
*/

function addEvent(oElem, sEvType, sUdf)
{
  var bRetValue=false;
  var bCapture=true;  
  try
  {
     if(oElem.addEventListener)
     {
        oElem.addEventListener(sEvType, sUdf, bCapture);
        bRetValue=true;
     }
     else if(oElem.attachEvent)
     {
        bRetValue = oElem.attachEvent("on"+sEvType, sUdf);
        if(! bRetValue) throw raiseEx("event handler could not be added in IE for "+oElem.nodeName);
     }
  }
  catch(ex)
  {
     if(gbDebug) alert("addEvent:\n"+ex.message);
  }
  finally
  {      
      bCapture=false;
  }
  /*
  if(gbDebug)   
  {
     if(bRetValue) 
        alert('event handler added successfully for '+oElem.nodeName);
     else    
        alert('event handler could not be added successfully for '+oElem.nodeName);
  }
  */
  return bRetValue; 
}


/*
|| Throughout, whitespace is defined as one of the characters
||  "\t" TAB \u0009
||  "\n" LF  \u000A
||  "\r" CR  \u000D
||  " "  SPC \u0020
||
|| This does not use Javascript's "\s" because that includes non-breaking
|| spaces (and also some other characters).
*/

/*
|| Throughout, node number is defined as following:
|| Node Type Number -> Type
|| 1 -> Element  (<p>...</p>)
|| 2-> Attribute (align="center") 
|| 3-> Text      (this is text)
|| 8-> Comment   (<!-- -->
|| 9-> Document: the root (<html>)
|| 10-> Document tye (<!DOCTYPE ...)
*/

/*
|| Determine whether a node's text content is entirely whitespace.
||
|| @param oNode  A node implementing the |CharacterData| interface (i.e.,
||             a |Text|, |Comment|, or |CDATASection| node
|| @return     True if all of the text content of |oNode| is whitespace,
||             otherwise false.
*/

function isAllWS(oNode)
{
  // Use ECMA-262 Edition 3 String and RegExp features
  
  return !(/[^\t\n\r ]/.test(oNode.data));
}

/*
|| Determine if a node should be ignored by the iterator functions.
||
|| @param oNode  An oect implementing the DOM1 |Node| interface.
|| @return       true if the node is:
||               1) A |Text| node that is all whitespace
||               2) A |Comment| node
||               and otherwise false.
||
*/

function isIgnorable(oNode)
{
  return ( oNode.nodeType==8) || // A comment node
         ( (oNode.nodeType==3) && isAllWS(oNode) ); // a text node, all ws
}

/*
|| Version of |previousSibling| that skips nodes that are entirely
|| whitespace or comments.  (Normally |previousSibling| is a property
|| of all DOM nodes that gives the sibling node, the node that is
|| a child of the same parent, that occurs immediately before the
|| reference node.)
||
|| @param oSib  The reference node.
|| @return      Either:
||              1) The closest previous sibling to |oSib| that is not
||                 ignorable according to |isIgnorable|, or
||              2) null if no such node exists.
*/

function nodeBefore(oSib)
{
  try
  {
     while((oSib=oSib.previousSibling)) 
     {
       if (!isIgnorable(oSib)) return oSib;
     }  
  }
  catch(ex)
  {
     if(gbDebug) alert("nodeBefore:\n"+ex);
  }  
  return null;
}

/*
|| Version of |nextSibling| that skips nodes that are entirely
|| whitespace or comments.
||
|| @param oSib  The reference node.
|| @return      Either:
||              1) The closest next sibling to |oSib| that is not
||                 ignorable according to |isIgnorable|, or
||              2) null if no such node exists.
*/

function nodeAfter(oSib)
{
  try
  {
     while ((oSib = oSib.nextSibling))
     {
       if (!isIgnorable(oSib)) return oSib;
     }
  }
  catch(ex)
  {
     if(gbDebug) alert("nodeAfter:\n"+ex);
  }  
  return null;
}

/*
|| Version of |lastChild| that skips nodes that are entirely
|| whitespace or comments.  (Normally |lastChild| is a property
|| of all DOM nodes that gives the last of the nodes contained
|| directly in the reference node.)
||
|| @param oSib  The reference node.
|| @return      Either:
||              1) The last child of |oSib| that is not
||                 ignorable according to |isIgnorable|, or
||              2) null if no such node exists.
*/

function lastChild(oPar)
{
  var oRes=null;
  try
  {
     oRes=oPar.lastChild;
     while (oRes)
     {
       if(!isIgnorable(oRes)) return oRes;
       oRes = oRes.previousSibling;
     }
  }
  catch(ex)
  {
     if(gbDebug) alert("lastChild:\n"+ex);
  }  
  return null;
}

/*
|| Version of |firstChild| that skips nodes that are entirely
|| whitespace and comments.
||
|| @param sib  The reference node.
|| @return     Either:
||             1) The first child of |oSib| that is not
||                ignorable according to |isIgnorable|, or
||             2) null if no such node exists.
*/
 
function firstChild(oPar)
{
  var oRes=null;
  try
  {
     oRes=oPar.firstChild;
     while(oRes)
     {
       if(!isIgnorable(oRes)) return oRes;
       oRes = oRes.nextSibling;
     }
  }
  catch(ex)
  {
     if(gbDebug) alert("firstChild:\n"+ex);
  }  
  return null;
}

/*
|| Version of |data| that doesn't include whitespace at the beginning
|| and end and normalizes all whitespace to a single space.  (Normally
|| |data| is a property of text nodes that gives the text of the node.)
||
|| @param txt  The text node whose data should be returned
|| @return     A string giving the contents of the text node with
||             whitespace collapsed.
*/

function dataOf(ojbNode)
{
  var sData = null;
  try
  {
     sData = ojbNode.data;
  
     // Use ECMA-262 Edition 3 String and RegExp features
  
     sData = sData.replace(/[\t\n\r ]+/g, " ");
     if(sData.charAt(0) == " ")
       sData = sData.substring(1, sData.length);
     if(sData.charAt(sData.length - 1) == " ")
       sData = sData.substring(0, sData.length - 1);
  }
  catch(ex)
  {
     if(gbDebug) alert("dataOf:\n"+ex);
  }
  return sData;
}

/*
|| Determine that value of type variant is valid or not. 
||
|| @param sVal The string argument 
|| @return     A boolean indicating that the string is valid and does not contain
||             whitespace.
*/

function isValidStr(sVal)
{
   try
   {
      if( (sVal != null) && (sVal != undefined) && (sVal.length > 0) )
      {
         if( ( /[^\t\n\r ]/.test(sVal) ) ) return true;
      }      
   }
   catch(ex)
   {
      if(gbDebug) alert("isValidStr:\n"+ex);
   }
   return false;
}




/*
|| Determine that id attribute is valid or not. That is element with given
|| id exists.
||
|| @param sId  The string argument containing element id attribute value.
|| @return     A boolean indicating that the element with sId exist.
*/

function isValidElem(sId)
{
   var oElem=null;
   try
   {
      if( !isValidStr(sId) ) return false;
      oElem = document.getElementById(sId);    
   }
   catch(ex)
   {
      if(gbDebug) alert("isValidElem:\n"+ex);
   }
   if(oElem)
      return true;
   else  
      return false;
}

/*
|| Determine that id attribute is valid or not. That is element with given
|| id exists.
||
|| @param sId  The string argument containing element id attribute value.
|| @param oPar The node type object which is parent. 
|| @return     An oect with given Id attribute.
*/

function getElemByPar(sId, oPar)
{
   var oElem=null;   
   try
   {
      oElem=oPar.getElementById(sId);
   }
   catch(ex)
   {
      if(gbDebug) alert("getNodeName:\n"+ex);
   }
   return oElem;
}

/*
|| Determine that id attribute is valid or not. That is element with given
|| id exists.
||
|| @param sId  The string argument containing element id attribute value.
|| @return     An oect with given Id attribute.
*/

function getElemById(sId)
{
   var oElem=null;   
   try
   {
      oElem=document.getElementById(sId);
   }
   catch(ex)
   {
      if(gbDebug) alert("getElemById:\n"+ex);
   }
   return oElem;
}

function getElemByIdAndDoc(sId, oDoc)
{
   var oElem=null;   
   try
   {
      if(oDoc==null) throw raiseEx("Invalid document object.");
      oElem=oDoc.getElementById(sId);
   }
   catch(ex)
   {
      if(gbDebug) alert("getElemByIdAndDoc:\n"+ex);
   }
   return oElem;
}

/*
|| Determine and return the value of a given attribute.
|| @param oElem  The node type object.
|| @param sAttr  The string type returning value of a given attribute.
|| @return       string if success else null if fail.
*/

function getAttr(oElem, sAttr)
{  
   var sRetVal=null;  
   try
   {
      if(oElem==null) throw raiseEx("The given node is null/invalid.");      
      if(sAttr==null) throw raiseEx("The given attribute is null/invalid.");
      
      sRetVal=oElem.getAttribute(sAttr);
   }
   catch(ex)
   {
      if(gbDebug) alert("getAttr:\n"+ex);
   }
   return sRetVal;
}

/*
|| Determine and returns array of item objects having same tag.
||
|| @param sTag  The string argument containing tag name e.g. "tr".
|| @return      An array of item objects.
*/

function getElemsByTag(sTag, oPar)
{
   var oItems=null;   
   try
   {
      if( ! isValidStr(sTag)) return oItems;
      if(oPar==null)
         oItems=document.getElementsByTagName(sTag);
      else
         oItems=oPar.getElementsByTagName(sTag);
      
   }
   catch(ex)
   {
      if(gbDebug) alert("getElemsByTag:\n"+ex);
   }
   return oItems;
}

/*
|| Determine and returns count of the array of item objects having same tag.
||
|| @param sTag  The string argument containing tag name e.g. "tr".
|| @return      Count of an array of item objects.
*/

function getTagCount(sTag, oPar)
{
   var nCount=0;   
   try
   {
      if( ! isValidStr(sTag)) return nCount;
      if(oPar==null)
         nCount=document.getElementsByTagName(sTag).length;
      else
         nCount=oPar.getElementsByTagName(sTag).length;      
   }
   catch(ex)
   {
      if(gbDebug) alert("getTagCount:\n"+ex);
   }
   return nCount;
}

/*
|| Determine and return particular tag object by index.
||
|| @param  iIndex  The int argument containing index.
|| @param  sTag    The string argument containing tag name e.g. "tr".
|| @return         Tag object by index to be returned.
*/

function getTagByIndex(sTag, oPar, nIndex)
{
   var oNode=null;   
   var oItems=null;
   
   //var nCount=null;
      
   try
   {  
      if(parseInt(nIndex) < 0) return oNode;
      
      /*
      oItems=getElemsByTag(sTag, oPar);      
      if(oItems==null) 
         return oNode;      
      else if(oItems.length==0) 
         return oNode;
      else
         nCount=oItems.length;
         
      if(nIndex >= nCount) return oNode;      
      */
      
      if(oPar==null)
         oNode=document.getElementsByTagName(sTag).item(nIndex);
      else
         oNode=oPar.getElementsByTagName(sTag).item(nIndex);
   }
   catch(ex)
   {
      if(gbDebug) alert("getTagByIndex:\n"+ex);
      oNode=null;   
      oItems=null;
      //nCount=null;
   }
   return oNode;
}

/*
|| Determine and return node type.
||
|| @param oElem  The oElem node whose data type should be returned.
|| @return       A string giving the type of the node.
*/

function getNodeType(oElem)
{
   var nRetVal=null;
   try
   {
      if(typeof(oElem)=="object") nRetVal=oElem.nodeType;  
   }
   catch(ex)
   {
      if(gbDebug) alert("getNodeType:\n"+ex);
   }
   return nRetVal;   
}

/*
|| Determine and return node value.
||
|| @param oElem  The text node whose value should be returned
|| @return       A string giving the value of the node with
||               whitespace collapsed.
*/

function getNodeValue(oElem)
{
   var sRetVal=null;
   try
   {
      if(typeof(oElem)=="object" && getNodeType(oElem) == 3 ) sRetVal= dataOf(oElem);      
   }
   catch(ex)
   {
      if(gbDebug) alert("getNodeValue:\n"+ex);
   }
   return sRetVal;   
}

/*
|| Determine and return node name.
||
|| @param oElem  The  node whose name should be returned
|| @return       A string giving the name of the node
*/

function getNodeName(oElem)
{
   var sRetVal=null; 
   try
   {
      if(typeof(oElem)=="object" && getNodeType(oElem) != null  ) sRetVal=oElem.nodeName;
   }
   catch(ex)
   {
      if(gbDebug) alert("getNodeName:\n"+ex);
   }
   return sRetVal;   
}

/*
|| Determine and return element's child text node data/value.
||
|| @param oElem  The element whose child text node value to be displayed.
|| @return       A string giving value of the child text node which has value as non white space
||               among more than one child text node.
*/

function getChildTxtNodeNonWsVal(oElem)
{    
   var oCurrNode=null; 
   var oFirstChild=null;   
   var sRetVal=null;  
   try
   {    
      oCurrNode = oElem;
      oFirstChild = firstChild(oCurrNode);
      while(oFirstChild != null)
      {
         oCurrNode = oFirstChild;
         oFirstChild = firstChild(oCurrNode);
      }
      sRetVal = getNodeValue(oCurrNode);
   }
   catch(ex)
   {
      if(gbDebug) alert("getChildTxtNodeNonWsVal:\n"+ex);
   }
   finally
   {
      oCurrNode = null; 
      oFirstChild = null; 
   }
   return sRetVal;
}

/*
|| States if a node has child nodes or not.
||
|| @param oElem      The element node.
|| @return           A boolean indicating if the node has child nodes or not.
*/

function hasChild(oElem)
{
   var sRetVal=false;   
   try
   {           
      sRetVal = oElem.hasChildNodes();
   }
   catch(ex)
   {
      if(gbDebug) alert("HasChild:\n"+ex);
   }   
   return sRetVal;
}

/*
|| Returns the parent node of a given node.
|| @param oElem      The element node whose parent to be determined.
|| @return           The parent node of type object.
*/

function getParent(oElem)
{
   var oRetVal=null;
   
   try
   {      
      oRetVal=oElem.parentNode;
   }
   catch(ex)
   {
      if(gbDebug) alert("getParent:\n"+ex);
   }   
   return oRetVal;
}

/*
|| Create a html dom element node.
||
|| @param sTag  The element tag to be created.
|| @return      The element node of type object.
*/

function crElem(sTag)
{
   var oRetVal=null; 
   try
   {
      oRetVal = document.createElement(sTag);
   }
   catch(ex)
   {
      if(gbDebug) alert("crElem:\n"+ex);
   }
   return oRetVal;   
}

/*
|| Create a html dom text node.
||
|| @param sTxt      The text value.
|| @return          The text node of type object.
*/

function crTxtNode(sTxt)
{
   var oRetVal=null; 
   try
   {
      oRetVal=document.createTextNode(sTxt);
   }
   catch(ex)
   {
      if(gbDebug) alert("crTxtNode:\n"+ex);
   }
   return oRetVal;   
}

/*
|| Create attribute of a given element.
||
|| @param oElem      The element node whose attribute should be created.
|| @param sAttr      The attribute to be created.
|| @param sValue     The value of type string to be added to new attribute.
|| @param bPutMtyStr Allow empty string.
|| @return           A boolean indicating success or failure while creating attribute.
*/

function crAttr(oElem, sAttr, sValue, bPutMtyStr)
{
   var bRetVal=false;   
   try
   {
      if(! isValidStr(sAttr) ) return bRetVal;
            
      if(! bPutMtyStr)
      {
         if(! isValidStr(sValue) ) return bRetVal;
      }         
      if( typeof(oElem) != "object" && getNodeType(oElem) != 1 ) return bRetVal;      
      oElem.setAttribute(sAttr, sValue);  
      bRetVal=true;
   }
   catch(ex)
   {
      if(gbDebug) alert("crAttr:\n"+ex);      
   }
   return bRetVal;
}

/*
|| Create/append child of a given element.
||
|| @param oPar   The parent element.
|| @param oChild The child element.
|| @return       A boolean indicating success or failure while creating attribute.
*/

function crChild(oPar, oChild)
{
   var bRetVal=false; 
   try
   {
      if( typeof(oPar) != "object") return bRetVal;
      if( typeof(oChild) != "object") return bRetVal;
      
      oPar.appendChild(oChild);
      bRetVal=true;
   }
   catch(ex)
   {
      if(gbDebug) alert("crChild:\n"+ex);
   }
   return bRetVal;   
}

/*
|| Determine and return the body element.
|| @return       A boolean indicating success or failure while creating attribute.
*/

function getDocBody()
{
   var oDocBody
   oDocBody = document.getElementsByTagName("body").item(0);
   return oDocBody;
}

/*
|| Create tr element.
|| @param sId    The id attr value.
|| @param sClass The className attr value.
|| @return       object of type tr element.
*/

function crRow(sId, sClass)
{
   var oInElem=null;
   
   var sConstElem="tr";
   var sConstWord="element.";
   var sConstSpace=" ";
      
   var sAttr=null;   
   var bRetStatus=false;
   
   try
   {           
      oInElem=crElem(sConstElem);    
      if(! oInElem)  throw raiseEx("Failed to create "+sConstElem+sConstSpace+sConstWord);
      
      sAttr="id";
      if(! isValidStr(sId))
         throw raiseEx("Invalid value."+"Failed to create attr "+sAttr+" of the "+sConstElem+sConstSpace+sConstWord);                  
      bRetStatus=crAttr(oInElem, sAttr, sId, false);            
      if(! bRetStatus) throw raiseEx("Failed to create attr "+sAttr+" of the "+sConstElem+sConstSpace+sConstWord);
      
      sAttr="className";
      if(typeof(sClass)==null)
      {
         ; // do nothing.
      }
      else
      {
         if(typeof(sClass)!="undefined")
            oInElem.className=sClass; 
         else
           if(! bRetStatus) throw raiseEx("Failed to create attr "+sAttr+" of the "+sConstElem+sConstSpace+sConstWord);
      }   
   }
   catch(ex)
   {     
      oInElem=null; 
      if(gbDebug) alert("crRow:\n"+ex);
   }
   finally
   {
      sConstElem=null;
      sConstWord=null;
      sConstSpace=null;
   
      sAttr=null;      
      bRetStatus=null;
   }
   return oInElem;
}

/*
|| Create td element.
|| @param sId      The id attr value.
|| @param sClass   The colspan attr value.
|| @param nColSpan The className attr value.
|| @return         object of type td element.
*/

/*
function crCell(sId, sClass, nColSpan)
{
   var oInElem=null;
   
   var sConstElem="td";
   var sConstWord="element.";
   var sConstSpace=" ";
      
   var sAttr=null;   
   var bRetStatus=false;
   try
   {         
      oInElem=crElem(sConstElem);    
      if(! oInElem)  throw raiseEx("Failed to create "+sConstElem+sConstSpace+sConstWord);
      
      sAttr="id";
      if(! isValidStr(sId))
         throw raiseEx("Invalid value."+"Failed to create attr "+sAttr+" of the "+sConstElem+sConstSpace+sConstWord);
      bRetStatus=crAttr(oInElem, sAttr, sId, false);            
      if(! bRetStatus) throw raiseEx("Failed to create attr "+sAttr+" of the "+sConstElem+sConstSpace+sConstWord);
      
      if(isNaN(nColSpan)) 
         throw raiseEx("Attribute width value is not a number.");                  
      sAttr="colspan";         
      bRetStatus=crAttr(oInElem, sAttr, nColSpan, false);            
      if(! bRetStatus) throw raiseEx("Failed to create attr "+sAttr+" of the "+sConstElem+sConstSpace+sConstWord);
      
      sAttr="className";
      if(typeof(sClass)==null)
      {
         ; // do nothing.
      }
      else
      {
         if(typeof(sClass)!="undefined")
            oInElem.className=sClass; 
         else
           if(! bRetStatus) throw raiseEx("Failed to create attr "+sAttr+" of the "+sConstElem+sConstSpace+sConstWord);
      }
   }   
   catch(ex)
   {
      oInElem=null; 
      if(gbDebug) alert("crCell:\n"+ex);
   }
   finally
   {
      sConstElem=null;
      sConstWord=null;
      sConstSpace=null;
   
      sAttr=null;      
      bRetStatus=null;
   }
   return oInElem;
}
*/

/*
|| Create td element.
|| @param sId    The id attr value.
|| @param sClass The colspan attr value.
|| @return       object of type td element.
*/

function crCell(sId, sClass, nWidVal, sWidInPxlOrPct, sAlign)
{
   var oInElem=null;
   
   var sConstElem="td";
   var sConstWord="element.";
   var sConstSpace=" ";
   
   var sAttr=null;
   var sWidVal=null;
   var bRetStatus=false;
   try
   {         
      oInElem=crElem(sConstElem);    
      if(! oInElem)  throw raiseEx("Failed to create "+sConstElem+sConstSpace+sConstWord);
      
       sAttr="id";
      if(! isValidStr(sId))
         throw raiseEx("Invalid value."+"Failed to create attr "+sAttr+" of the "+sConstElem+sConstSpace+sConstWord);       
              
      bRetStatus=crAttr(oInElem, sAttr, sId, false);            
      if(! bRetStatus) throw raiseEx("Failed to create attr "+sAttr+" of the "+sConstElem+sConstSpace+sConstWord);
            
      if(nWidVal==null)
      {
          ; //do nothing
      }
      else
      {
         sAttr="width";
         if(isNaN(nWidVal)) throw raiseEx("Attribute \"" + sAttr+ "\" value is not a number.");
         if(! isValidStr(sWidInPxlOrPct)) throw raiseEx("Invalid value for attr "+sAttr+" value.");
         if( (sWidInPxlOrPct!="px" && sWidInPxlOrPct!="%") )
            throw raiseEx("Invalid value for attr "+sAttr+" value.\n Value should be \"%\" or \"px\".");       
         
         sWidVal=nWidVal+sWidInPxlOrPct;
         bRetStatus=crAttr(oInElem, sAttr, sWidVal, false);            
         if(! bRetStatus) throw raiseEx("Failed to create attr "+sAttr+" of the "+sConstElem+sConstSpace+sConstWord);
         
      }
      if(sAlign==null)
      {
         ; //do nothing
      }
      else
      {
         sAttr="align";
         sAlign=sAlign.toLowerCase();
         if( (sAlign!="left" && sAlign!="right" && sAlign!="center") ) 
            throw raiseEx("Invalid value for attr "+sAttr+" value.\n Value should be \"left\" or \"right\" or \"center\".");       
         
         bRetStatus=crAttr(oInElem, sAttr, sAlign, false);            
         if(! bRetStatus) throw raiseEx("Failed to create attr "+sAttr+" of the "+sConstElem+sConstSpace+sConstWord);  
         
      }      
      sAttr="className";
      if(typeof(sClass)==null)
      {
         ; // do nothing.
      }
      else
      {
         if(typeof(sClass)!="undefined")
            oInElem.className=sClass; 
         else
            if(! bRetStatus) throw raiseEx("Failed to create attr "+sAttr+" of the "+sConstElem+sConstSpace+sConstWord);
      }
   }   
   catch(ex)
   {
      oInElem=null; 
      if(gbDebug) alert("crCell:\n"+ex);
   }
   finally
   { 
      sConstElem=null;
      sConstWord=null;
      sConstSpace=null;
   
      sAttr=null;
      sWidVal=null;
      bRetStatus=null;
   }
   return oInElem;
}

/*
|| Create input element.
|| @param sType   The element type e.g. "text".
|| @param sName   The element name.
|| @param sId     The id value.
|| @param sClass  The class to be applied.
|| @param nSize   The size of the text element.
|| @param nMaxLen The max length of the text element.
|| @param value   The value of the text element.
|| @return        object of type input element.
*/

function crInputElem(sType, sName, sId, sClass, nSize, nMaxLen, sValue, nWidth, sWidInPxlOrPct)
{
   var oInElem=null;
   try
   {  
      if(! isValidStr(sType) ) raiseEx("Invalid input type.");   
      if(! isValidStr(sName) ) raiseEx("Invalid input name.");
      if(! isValidStr(sId) ) raiseEx("Invalid input id/name.");
      
      if(sType.toLowerCase()=="text" || sType.toLowerCase()=="hidden")
      {
         if( (nSize!=null) )
            if( isNaN(nSize) ) raiseEx("Invalid input size");
         
         if( (nMaxLen!=null) ) 
            if( isNaN(nMaxLen) )raiseEx("Invalid input max length");
      }
      
      if(document.all)
         oInElem=crInputElemIE(sType, sName, sId, sClass, nSize, nMaxLen, sValue, nWidth, sWidInPxlOrPct);
      else
         oInElem=crInputElemW3c(sType, sName, sId, sClass, nSize, nMaxLen, sValue, nWidth, sWidInPxlOrPct);
   }
   catch(ex)
   {
      oInElem=null; 
      if(gbDebug) alert("crInputElem:\n"+ex);
   }
   return oInElem;
}

/*
|| Create input element. It is IE specific method.
|| For parameter reference see crInputElem function.
*/

function crInputElemIE(sType, sName, sId, sClass, nSize, nMaxLen, sValue, nWidth, sWidInPxlOrPct)
{
   var oInElem=null;
   var sHtml=null;
   
   var sConstElem="input";
   var sConstWord="element.";
   var sConstSpace=" ";
  
   try
   {
      sHtml="<input name=\"";
      sHtml+=sName;
      sHtml+="\" id=\"";
      sHtml+=sId;
      sHtml+="\" type=\"";
      sHtml+=sType;
      if(sClass!=null)
      {
         sHtml+="\" class=\"";
         sHtml+=sClass;
      }
      if(sType.toLowerCase()=="text")
      {
         sHtml+="\" size=\"";
         sHtml+=nSize;
         
         sHtml+="\" maxlength=\"";
         sHtml+=nMaxLen;
      }
      sHtml+="\" value=\""
      sHtml+=sValue;     
      
      if(nWidth!=null && sWidInPxlOrPct!=null)
      {
         sHtml+="\" style=width:\""
         sHtml+=nWidth+sWidInPxlOrPct+";";     
         oInElem.style.width=nWidth+sWidInPxlOrPct;  
      }
      sHtml+="\" />";           
      if(gbDebug && 1!=1) alert(sHtml);
      oInElem=crElem(sHtml);    
      if(! oInElem)  throw raiseEx("IE: Failed to create "+sConstElem+sConstSpace+sConstWord);
   }
   catch(ex)
   {
     oInElem=null; 
     if(gbDebug) alert("crInputElemIE:\n"+ex);
   }
   finally
   {
      sHtml=null;
            
      sConstElem=null;
      sConstWord=null;
      sConstSpace=null;
   }
   return oInElem;
}

/*
|| Create input element. It is W3C compliant method.
|| For parameter reference see crInputElem function.
*/

function crInputElemW3c(sType, sName, sId, sClass, nSize, nMaxLen, sValue, nWidth, sWidInPxlOrPct)
{
   var oInElem=null;
   
   var sConstElem="input";
   var sConstWord="element.";
   var sConstSpace=" ";
   
   var bRetStatus=false;
   try
   {  
      oInElem=crElem(sConstElem);    
      if(! oInElem)  throw raiseEx("Failed to create "+sConstElem+sConstSpace+sConstWord);     
      
      if(nWidth!=null && sWidInPxlOrPct!=null)
         oInElem.style.width=nWidth+sWidInPxlOrPct;  
      
      sAttr="type";
      bRetStatus=crAttr(oInElem, "type", sType, false);            
      if(! bRetStatus) throw raiseEx("Failed to create attr "+sAttr+" of the "+sConstElem+sConstSpace+sConstWord);
      
      sAttr="name";
      bRetStatus=crAttr(oInElem, sAttr, sName, false);            
      if(! bRetStatus) throw raiseEx("Failed to create attr "+sAttr+" of the "+sConstElem+sConstSpace+sConstWord);
      
      sAttr="id";              
      bRetStatus=crAttr(oInElem, sAttr, sId, false);            
      if(! bRetStatus) throw raiseEx("Failed to create attr "+sAttr+" of the "+sConstElem+sConstSpace+sConstWord);
      
      if(sType.toLowerCase()=="text")
      {
         sAttr="size";
         bRetStatus=crAttr(oInElem, sAttr, nSize, true);            
         if(! bRetStatus) throw raiseEx("Failed to create attr "+sAttr+" of the "+sConstElem+sConstSpace+sConstWord);
      
         sAttr="maxlength";
         bRetStatus=crAttr(oInElem, sAttr, nMaxLen, true);            
         if(! bRetStatus) throw raiseEx("Failed to create attr "+sAttr+" of the "+sConstElem+sConstSpace+sConstWord); 
      }      
      if(sValue!=null)
      {
         sAttr="value";
         bRetStatus=crAttr(oInElem, sAttr, sValue, true);            
         if(! bRetStatus) throw raiseEx("Failed to create attr "+sAttr+" of the "+sConstElem+sConstSpace+sConstWord);
      }
      
      sAttr="className";
      if(typeof(sClass)==null)
      {
         ; // do nothing.
      }
      else
      {
         if(typeof(sClass)!="undefined")
            oInElem.className=sClass; 
         else
            if(! bRetStatus) throw raiseEx("Failed to create attr "+sAttr+" of the "+sConstElem+sConstSpace+sConstWord);
      }
   }
   catch(ex)
   {
      oInElem=null; 
      if(gbDebug) alert("crInputElemW3c:\n"+ex);
   }
   finally
   {      
      sConstElem=null;
      sConstWord=null;
      sConstSpace=null;
         
      bRetStatus=null;
   }  
   return oInElem;
}

/*
|| Create select element.
|| @param sName   The element name.
|| @param sId     The id value.
|| @param sClass  The class to be applied.
|| @param oArr    The array type variable containing option texts and values. 
|| @return        object of type select element.
*/

function crSelect(sName, sId, sClass, oArr, nWidth, sWidInPxlOrPct)
{
   var oInElem=null;
   var oChild=null;
   
   var sConstElem="select";
   var sConstWord="element.";
   var sConstSpace=" ";
   
   var bRetStatus=false;
   var nIndex=0;
   try
   {  
      oInElem=crElem(sConstElem);    
      if(! oInElem)  throw raiseEx("Failed to create "+sConstElem+sConstSpace+sConstWord);
      
      if(nWidth!=null && sWidInPxlOrPct!=null)
         oInElem.style.width=nWidth+sWidInPxlOrPct;      
         
      //netscape patch to be done.
      
      
      sAttr="name";
      bRetStatus=crAttr(oInElem, sAttr, sName, false);            
      if(! bRetStatus) throw raiseEx("Failed to create attr "+sAttr+" of the "+sConstElem+sConstSpace+sConstWord);
      
      sAttr="id";         
      //alert(sId);     
      bRetStatus=crAttr(oInElem, sAttr, sId, false);            
      if(! bRetStatus) throw raiseEx("Failed to create attr "+sAttr+" of the "+sConstElem+sConstSpace+sConstWord);
      
      
      
      sAttr="className";
      if(typeof(sClass)==null)
      {
         ; // do nothing.
      }
      else
      {
         if(typeof(sClass)!="undefined")
            oInElem.className=sClass; 
         else
            if(! bRetStatus) throw raiseEx("Failed to create attr "+sAttr+" of the "+sConstElem+sConstSpace+sConstWord);
      }
      if(oArr!=null)
      {
         for(nIndex=0; nIndex<oArr.length; nIndex++)
         {
            oChild=crOption(oArr[nIndex]);
            if(!oChild) throw raiseEx("Failed to create child element \"option\".");
            if(! crChild(oInElem, oChild))  throw raiseEx("Failed to create child element \"option\" node:"+nIndex);
         }
      }
      
      
      //oInElem.style.width="30%"; 
   }
   catch(ex)
   {
      oInElem=null; 
      if(gbDebug) alert("crSelect:\n"+ex);
   }
   finally
   {      
      oChild=null;
   
      sConstElem=null;
      sConstWord=null;
      sConstSpace=null;
   
      bRetStatus=null;
      nIndex=null;
   }  
   return oInElem;
}

/*
|| Create option element.
|| @param oArr   The array type variable containing option texts and values. 
|| @return       object of type option element.
*/

function crOption(oArr)
{
   var oInElem=null;
   var oChild=null;
   
   var sConstElem="option";
   var sConstWord="element.";
   var sConstSpace=" ";
   
   var sValue=null;
   var sAttr=null;
   var bRetStatus=false;
   var nIndex=0;   
   try
   {
      oInElem=crElem(sConstElem);    
      if(! oInElem)  throw raiseEx("Failed to create "+sConstElem+sConstSpace+sConstWord);    
      
      sAttr="value";
      sValue=oArr[0];
      
      bRetStatus=crAttr(oInElem, sAttr, sValue, true);            
      if(! bRetStatus) throw raiseEx("Failed to create attr "+ sAttr+" of the"+sConstElem+sConstSpace+sConstWord);  
      
      sValue=oArr[1];
      if(document.all)
         oChild=crTxtNode(sValue);         
      else
         oChild=crTxtNode(sValue.substr(0, 15));      
      if(! crChild(oInElem, oChild))  throw raiseEx("Failed to create "+sConstWord+"\'s text node:"+sValue);      
   }
   catch(ex)
   {
      oInElem=null; 
      if(gbDebug) alert("crOption:\n"+ex);
   }
   finally
   {   
       oChild=null;
       
       sConstElem=null;
       sConstWord=null;
       sConstSpace=null;
       
       sValue=null;
       sAttr=null;
       bRetStatus=null;
       nIndex=null;
   }  
   return oInElem;   
}

/*
|| Returns id of the element whose event got triggered.
|| @param e is of type object representing event.
*/ 

function getEventId(e)
{
   var sCurrId=null;
   try
   {
      if(document.all)  
         sCurrId=e.srcElement.id; 
      else
         sCurrId=e.target.getAttribute("id");
   }
   catch(ex)
   {
      if(gbDebug) alert("getEventId:\n"+ex);
   }
   return sCurrId;
}


/*
|| Opens the window.
*/

function winOpen(sURL, sName, nTop, nLeft, nHeight, nWidth)
{ 
   var oWin; 

   oWin = window.open(sURL, sName, "modal=yes,dependent=yes,status=yes,toolbars=no,directory=no,scrolling=no,scrollbars=no,resizable=no,width="+nWidth +",height="+nHeight +",left="+nLeft+",top=" + nTop +"" );
   oWin.focus(); 
   return;
}

/*
|| Closes a window.
*/

function winClose()
{ 
   window.close();
   return;
}

function getCurrRecPos(sTbl,oRow)
{
   try
   {
      var nCtr=null;
      var nTotRows=null;
      var nRetVal=0;
      var oTbl=null;
      var oCurrRow=null;
    
      if(oRow==null) return nRetVal;
      nTotRows=rowCount(sTbl);    
      for(nCtr=0;nCtr<nTotRows;nCtr++)
      {
         oTbl=getElemById(sTbl);
         oCurrRow=oTbl.getElementsByTagName(gsConstTr).item(nCtr);
         if(oCurrRow.id==oRow.id) 
         {
            nRetVal=++nCtr;
            break;
         }
      }     
      return nRetVal;   
   }
   catch(ex)
   {
   }
   finally
   {
      nCtr=null;
      nTotRows=null;
      nRetVal=0;
      oTbl=null;
      oCurrRow=null;
   }
}

/*
|| Developed By : Atanu Gupta   dated : 27-May-2006
|| Purpose : To call common popups under UICommon
|| @param sPopName of type string. Popup Name
|| @param sFieldname of type string. Field Name which will get populated
|| @param sQName of type string. Query string name
|| @param nConstWidth of type number. Popup width
|| @param nConstHeight of type number. Popup height
|| @param nLength of type number. Length to check
|| @param sMessage of type string. Validation message
|| @param bCheck of type boolean. Whether Validation is required
|| @param sParam of type string. Additonal query string e.g. Place (n)/Port (y) record fetch
|| @param sCodeFName of type string. Field for assigning the code value
*/
function commonPopCall(sPopName,sFieldname,sQName,nConstWidth,nConstHeight,nLength,sMessage,bCheck,sParam,sCodeFName)
{
   var oField=null;
   var sValue=null;
   var nTop=null;
   var nLeft=null;
   
   try
   {
      oField=document.forms[0].elements[sFieldname];
      sValue=oField.value;

      nTop = (self.screen.height/2)- (nConstHeight/2);
      nLeft = (self.screen.width/2)- (nConstWidth/2);
      
      if(bCheck==true) //Validation required
      {
         if(sValue !=null && sValue!=gsMtySpace)
         {  
            if(sValue.length>=nLength)
            { 
               sValue= EncodeVal(sPopName,sValue);               
               strURL="../UICommon/"+sPopName+"?"+sQName+"="+sValue+"&fName="+sFieldname+"&pflag="+sParam+"&cName="+sCodeFName;                  
               winOpen(strURL, "WinCommon", nTop, nLeft, nConstHeight, nConstWidth); 	
            }
            else
            {
		      alert(sMessage);
		      oField.focus();
	        }
         }
         else
         {
            alert(sMessage);   
            oField.focus();
         }
      }
      else
      {
         strURL="../UICommon/"+sPopName+"?"+sQName+"="+sValue+"&fName="+sFieldname+"&pflag="+sParam;
         winOpen(strURL, "WinCommon", nTop, nLeft, nConstHeight, nConstWidth); 
      }
   }
   catch(ex)
   {
      alert(ex);
   }
   finally
   {
      oField=null;
      sValue=null;
      nTop=null;
      nLeft=null;
   }
}

/*
|| Same as CommonPopCall except sName arg has been added because the window name was hardcoded in CommonPopCall
|| but here it has been parameterized with the sName arg.
*/

function openWinWithName(sPopName,sName,sFieldname,sQName,nConstWidth,nConstHeight,nLength,sMessage,bCheck,sParam,sCodeFName)
{
   var oField=null;
   var sValue=null;
   var nTop=null;
   var nLeft=null;
   
   try
   {
      oField=document.forms[0].elements[sFieldname];
      sValue=oField.value;

      nTop = (self.screen.height/2)- (nConstHeight/2);
      nLeft = (self.screen.width/2)- (nConstWidth/2);
      
      if(bCheck==true) //Validation required
      {
         if(sValue !=null && sValue!=gsMtySpace)
         {  
            if(sValue.length>=nLength)
            { 
               strURL="../UICommon/"+sPopName+"?"+sQName+"="+sValue+"&fName="+sFieldname+"&pflag="+sParam+"&cName="+sCodeFName;                  
               winOpen(strURL, sName, nTop, nLeft, nConstHeight, nConstWidth); 	
            }
            else
            {
		      alert(sMessage);
		      oField.focus();
	        }
         }
         else
         {
            alert(sMessage);   
            oField.focus();
         }
      }
      else
      {
         strURL="../UICommon/"+sPopName+"?"+sQName+"="+sValue+"&fName="+sFieldname+"&pflag="+sParam;
         winOpen(strURL, sName, nTop, nLeft, nConstHeight, nConstWidth); 
      }
   }
   catch(ex)
   {
      alert(ex);
   }
   finally
   {
      oField=null;
      sValue=null;
      nTop=null;
      nLeft=null;
   }
}


/*

*/

function adaptMZTblDataWidth(sId,nWidth,sWidInPxlOrPct)
{
   var oTblElem=null;
   //alert("Height : "+screen.height+"\nWidth : "+screen.width);
   try
   {
      if(document.all)
         ;
      else
      {
         oTblElem=getElemById(sId);
         if(oTblElem==null) throw raiseEx("Failed to find element with id:"+sId);
         if(nWidth!=null && sWidInPxlOrPct!=null)
         {
            if(isNaN(nWidth))
               throw raiseEx("Invalid width attribute - should be number.");
            if(sWidInPxlOrPct!="%" && sWidInPxlOrPct!="px")
               throw raiseEx("Invalid unit attribute - should be \"%\" or \"px\".");
               
            oTblElem.style.width=nWidth+sWidInPxlOrPct;  
         }
         else
            throw raiseEx("Attributes: width or unit is null");            
      }
      
   }
   catch(ex)
   {
      if(gbDebug) alert("adaptMZTblDataWidth:\n"+ex);
   }
   finally
   {
      oTblElem=null;
   }
}
/*
|| This function used in CommonPopCall for encoding the value of url parameter.
|| Only Vendor.aspx needs encoding Ref. SATS #: USCLM090331546
*/

function EncodeVal(sPageName,sPramVal)
{
  var sPageFlg=false;
  var sRetVal=null;
  try
  {
    if (sPageName ==null || !isValidStr(sPramVal)) 
      return sPramVal;
    else
      sRetVal=sPramVal;
    if (sPageName.toString().toLocaleUpperCase()=="VENDOR.ASPX")
    {
      sPageFlg=true;
    }
    if (sPageFlg)
    {
      sRetVal=escape(sPramVal);
    }
    else
    {
      sRetVal=sPramVal
    }
  }
  catch(ex)
   {
      if(gbDebug) alert("EncodeVal:\n"+ex);
   }
   finally
   {
      oTblElem=null;
   }
   return sRetVal;
}

