// Javascript helpers 
// ***************************************************************** 
function refreshMenuTree(menu)
{
   var fParent = self;  
   while (fParent != fParent.parent)
   {  
      fParent = fParent.parent;     
      if (fParent.treeFrame != null)
      {
         fParent.treeFrame.location.href=menu + "?rndCache=" + Math.round(Math.random(Date.UTC())*1000000 + 1);
         break;      
    }
  }     
}

function refreshMenuBar(menu)
{
 
   var fParent = self; 
   while (fParent != fParent.parent)
   {  
      fParent = fParent.parent;           
      if (fParent.menubar != null)
      {
         if (fParent.menubar.location.href != menu)
         {
            fParent.menubar.location.href=menu + "?rndCache=" + Math.round(Math.random(Date.UTC())*1000000 + 1);
            break;
         }
      }
   }
}

function clearUserForm()
{
   var fParent = self;  
   while (fParent != fParent.parent)
   {  
      fParent = fParent.parent;     
      if (fParent.basefrm != null)
      {
         fParent.basefrm.location.href="/eqc/apps/formfirst.htm" + "?rndCache=" + Math.round(Math.random(Date.UTC())*1000000 + 1)      
         break;      
      }
   }  
}

function refreshForm(macro)
{
   
   var fParent = self; 
   while (fParent != fParent.parent)
   {  
      fParent = fParent.parent;     
      if (fParent.basefrm != null)
      {
         fParent.basefrm.location.href="/eqc/ExecMacro.asp?macro=" + macro + "&rndCache=" + Math.round(Math.random(Date.UTC())*1000000 + 1);
         break;      
      }
   } 
}

function refreshFrame(frmName,url)
{     
   var fParent = self;     
   while (fParent != fParent.parent)
   {        
      fParent = fParent.parent;                                
      if ((fParent.frames[frmName]) != null)
      {        
         fParent.frames[frmName].location.href = url;         
         break;
      }
   }
}

function refreshFrameSync(frmName,url)
{     
   
   var doc = GetXMLFile(url);
   var fParent = self;     
   while (fParent != fParent.parent)
   {        
      fParent = fParent.parent;                                
      if ((fParent.frames[frmName]) != null)
      {        
         fParent.frames[frmName].document.body.innerHTML=doc;         
         break;
      }
   }
}

function GetXMLFile(sourceURL)
 {
    var xmlhttp = null;
    var doc = null;

    if (document.all)
      xmlhttp = new ActiveXObject('MSXML2.XMLHTTP');
    else
      xmlhttp = new XMLHttpRequest();

    if (xmlhttp)
    {
      xmlhttp.open('GET', sourceURL, false);
      xmlhttp.send(null);
      doc = xmlhttp.responseText;
      var istart = doc.search(/<body/i);      
      doc = doc.substr(istart,doc.length);
      var istart = doc.search(/>/i);
      doc = doc.substr(istart+1,doc.length);
      var iend = doc.search(/<\x2Fbody/i);
      doc = doc.substr(0,iend-1);      
    }

    return doc;
}



function refreshEditFrames(macro)
{

   
   var fParent = self; 
   var i = 0
   while (fParent != fParent.parent)
   {  
      fParent = fParent.parent;
      if (fParent.parmmenu != null)
      {
         fParent.parmmenu.location.href="/eqc/admin/ParmMenu.asp?macro=" + macro + "&rndCache=" + Math.round(Math.random(Date.UTC())*1000000 + 1);      
         fParent.parmedit.location.href="/eqc/admin/EditParm.asp?macro=" + macro + "&rndCache=" + Math.round(Math.random(Date.UTC())*1000000 + 1);
      }
            
      if (fParent.basefrm != null)
      {
         fParent.basefrm.location.href="/eqc/ExecMacro.asp?macro=" + macro + "&rndCache=" + Math.round(Math.random(Date.UTC())*1000000 + 1);
      }           
   }     
}

function refreshMainFrameURL(url)
{
   
   var fParent = self;  
   while (fParent != fParent.parent)
   {  
      fParent = fParent.parent;
      if (fParent.main != null)
      {
         fParent.main.location.href=url;
         break;
      }
   }
}

function printMainFrame() 
{
   var fParent = self; 
   while (fParent != fParent.parent)
   { 

      fParent = fParent.parent;
      if (fParent.main != null)
      {  
         fParent.main.focus();
         
         fParent.main.print();
         break;
      }
   }
}

function ShowHelp(strUrl)
{
   var strHelpOptions = "location=no";
   strHelpOptions += ",toolbar=no";
   strHelpOptions += ",menubar=yes";
   strHelpOptions += ",status=yes";
   strHelpOptions += ",scrollbars=yes";
   strHelpOptions += ",resizable=yes";
   strHelpOptions += ",top=25";
   strHelpOptions += ",left=50";
   strHelpOptions += ",width=750";
   strHelpOptions += ",height=550";

   winhelp = window.open(strUrl, "Help", strHelpOptions);
   winhelp.focus()
}

function validateName(name)
{
   var i, lowerCaseName, valid;
   lowerCaseName = name.toLowerCase();
   valid = true;
   if ((lowerCaseName.charAt(0) >= 'a' && lowerCaseName.charAt(0) <= 'z'))
   {
      for (i = 1; i < lowerCaseName.length; i++)
      { 
         if (lowerCaseName.charAt(i) < 'a' || lowerCaseName.charAt(i) > 'z')
             if (lowerCaseName.charAt(i) < '0' || lowerCaseName.charAt(i) > '9')
                if (lowerCaseName.charAt(i) != '_')
                {
                   valid = false;
                   break;
                }
      }
   }
   else
   {
      valid = false;
   }
   if (valid)
   {
      return 1;
   }
   else
   {
      return 0;
   }
}

function senddata(url)
{ 
   // if (window.confirm("There are xx columns by xxx rows of data.  Are you sure you want to download? ")) 
     parent.graph.location.href=url
}
//checks if the string is empty, null or undefined
function isEmpty(str)
{
   return (str == "" ||str == "undefined" || str == null);
         
}
function Trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}
function conditionText(t) {
    /***********************************************************************************************************************
    This function looks for odd characters that cause problems within ASP/Database code.  These have been most commonly
    seen when copying or pasting from MS Word.  The function attempts to translate "smart quotes", both single and double,
    to the appropriate standard quote.  It also changes html nbsp to a regular space.  Finally, any remaining characters
    with an ordinate of 127 (DEL) or above are stripped.  Calls TrimAll() to remove leading/trailing whitespace.
    ***********************************************************************************************************************/

    var newVal;

    /* Match opening and closing double "smart quotes" */
    var doubleQuotes = new RegExp("[" + String.fromCharCode(8220) + String.fromCharCode(8221) + "]","g");
    /* Match opening and closing single "smart quotes" */
    var singleQuotes = new RegExp("[" + String.fromCharCode(8216) + String.fromCharCode(8217) + "]","g");
    /* Match HTML &nbsp */
    var nbsp = String.fromCharCode(160)
    /* Match any character with an ordinate >=127 */
    var stripThese = new RegExp("[" + String.fromCharCode(127) + "-" + String.fromCharCode(8482) + "]","g");

    newVal = new String(t.value);
    /* Replace double "smart quotes" with standard double quotes */
    newVal = newVal.replace(doubleQuotes,"\"");
    /* Replace single "smart quotes" with standard single quotes */
    newVal = newVal.replace(singleQuotes,"'");
    /* Replace html &nbsp with standard space */
    newVal = newVal.replace(nbsp," ");
    /* Strip any characters with ordinate >=127 */
    newVal = newVal.replace(stripThese,"");

    t.value = newVal;        
    TrimAll(t);
}

function TrimAll(t) {
    /***********************************************************************************************************************
    This function is an "aggressive" trim.  It operates similarly to common trim() functions, but considers anything
    outside the range 33-127 inclusive to be whitespace, and removes it from the beginning or end of the string.
    ***********************************************************************************************************************/
    var newVal = new String(t.value);
    var startIdx = -1;
    var endIdx = -1;

    /* Start at beginning of string and find index of first char not considered whitespace (ASC 33-127 inclusive) */
    for (var i=0; i<newVal.length; i++) {
    if (newVal.charCodeAt(i) > 32 && newVal.charCodeAt(i) < 128) {
        startIdx = i;
        break;
        }
    }

    /* Start at end of string and find index of last char not considered whitespace (ASC 33-127 inclusive) */
    for (var i=newVal.length-1; i >=0; i--) {
        if (newVal.charCodeAt(i) > 32 && newVal.charCodeAt(i) < 128) {
            endIdx = i+1;
            break;
        }
    }

    /* If we found valid start and end indexes, get the substring */
    if (startIdx > -1 && endIdx >= startIdx) {
        t.value = newVal.substring(startIdx,endIdx)
    }
    /* If no valid indexes found, return empty string */
    else {
        t.value = ""
    }
}

//Below code added to handle the click to activate issue of IE
var bo_ns_id = 0;

function startIeFix(){
  if(isIE()){     
    document.write('<div id="bo_ns_id_' + bo_ns_id + '"><!-- ');
  }
}

function endIeFix(){
  if(isIE()){
    document.write('</div>');
    var theObject = document.getElementById("bo_ns_id_" + bo_ns_id++);
    var theCode = theObject.innerHTML;    
    theCode = theCode.substring(4 ,9+theCode.indexOf("</applet>"))        
    document.write(theCode);
  }
}

function isIE(){
  // only for Win IE 6+
  // But not in Windows 98, Me, NT 4.0, 2000
  var strBrwsr= navigator.userAgent.toLowerCase();
  if(strBrwsr.indexOf("msie") > -1 && strBrwsr.indexOf("mac") < 0){
    if(parseInt(strBrwsr.charAt(strBrwsr.indexOf("msie")+5)) < 6){
      return false;
    }
    if(strBrwsr.indexOf("win98") > -1 ||
       strBrwsr.indexOf("win 9x 4.90") > -1 ||
       strBrwsr.indexOf("winnt4.0") > -1 ||
       strBrwsr.indexOf("windows nt 5.0") > -1)
    {
      return false;
    }
    return true;
  }else{
    return false;
  }
}
function eMailCheck(str) 
{
    var at="@"
    var dot="."
    var errMsg = "Invalid Email !"
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)
    if (str.indexOf(at)==-1){
        alert(errMsg)
        return false
    }
    
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
        alert(errMsg)
        return false
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        alert(errMsg)
        return false
    }

    if (str.indexOf(at,(lat+1))!=-1){
        alert(errMsg)
        return false
    }

    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        alert(errMsg)
        return false
    }

    if (str.indexOf(dot,(lat+2))==-1){
        alert(errMsg)
        return false
    }
        
    if (str.indexOf(" ")!=-1){
        alert(errMsg)
        return false
    }

    return true
}