/*
 *------------------------------------------------------------------
 * sitewide.js -- Sitewide Tools
 *
 * Copyright (c) 2003 by cisco Systems, Inc.
 * All rights reserved.
 *------------------------------------------------------------------
 */

// declare variable and assign graphics for rollovers 

if (document.images) {
    var sitewide_print_off = new Image();  sitewide_print_off.src = "startup_images/sitewide_print_off.gif";
    var sitewide_print_on = new Image();  sitewide_print_on.src = "startup_images/sitewide_print_on.gif";

    var sitewide_glossary_off = new Image();  sitewide_glossary_off.src = "startup_images/sitewide_glossary_off.gif";
    var sitewide_glossary_on = new Image();  sitewide_glossary_on.src = "startup_images/sitewide_glossary_on.gif";

    var toolkit_start_text = new Image(); toolkit_start_text.src = "startup_images/sitewide_text_start.gif";
    var toolkit_glossary_text = new Image(); toolkit_glossary_text.src = "startup_images/sitewide_text_glossary.gif";
    var toolkit_print_text = new Image(); toolkit_print_text.src = "startup_images/sitewide_text_print.gif";

}

// called on mouseover, swaps icon and site_wide_text.gif images 
function img_over( name ) {
    if (document.images) {
        imgOn = eval( "sitewide_" + name + "_on.src" );
        document.images[ name+"img" ].src = imgOn;

	imgtoolkit = eval( "toolkit_" + name + "_text.src" );
	document.images[ "toolkit_text_img" ].src = imgtoolkit;
	}
}

// called on mouseout, Replaces icon and site_wide_text.gif images 
function img_out( name ) {
    if (document.images) {
	    imgOff = eval( "sitewide_" + name + "_off.src" );
            document.images[ name+"img" ].src = imgOff;

	    imgtoolkit = eval( "toolkit_start_text.src" );
	    document.images[ "toolkit_text_img" ].src = imgtoolkit;
	}
}

//helpRoot must be document.forms[0].HELP_ROOT.value
function helpPage(helpRoot) {
  var filename ="";
   if(helpRoot == "home_help")
   {
	filename = "home_help.htm";
   } else if(helpRoot == "exp_help") {
	filename = "es_help.htm";
   } else if(helpRoot == "tools_help") {
	filename = "tools_help.htm";
   } else if(helpRoot == "doc_help")
   {
	filename = "doc_help.htm";
   }

   if(filename != "")
   {
   	helpwindow = window.open(filename, 'es_help', "toolbar=no,status=no,scrollbars=yes,resizable=yes");
        helpwindow.focus();
   }
}

function printPage(){  
	if (window.print) {
	    window.print() ;  
	} else {
	    var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
		document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
	    WebBrowser1.ExecWB(6, 2);
		// Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";  
	}
}

// called on menu selection in drop downs, jumps to specified value.
function land(ref, target)
{
lowtarget=target.toLowerCase();
if (lowtarget=="_self") {window.location=loc;}
else {if (lowtarget=="_top") {top.location=loc;}
else {if (lowtarget=="_blank") {window.open(loc);}
else {if (lowtarget=="_parent") {parent.location=loc;}
else {parent.frames[target].location=loc;};
}}}
}

// Helper function for calcNetMask
function calcNetMaskField(numberOfBits) {
   if (numberOfBits <= 0) {
      return "0";
   } else if (numberOfBits >= 8) {
      return "255";
   } else if (numberOfBits == 7) {
      return "254";
   } else if (numberOfBits == 6) {
      return "252";
   } else if (numberOfBits == 5) {
      return "248";
   } else if (numberOfBits == 4) {
      return "240";
   } else if (numberOfBits == 3) {
      return "224";
   } else if (numberOfBits == 2) {
      return "192";
   } else if (numberOfBits == 1) {
      return "128";
   }
}

// Calculate the net mask based on value from show interface command
// <example>>  Internet address is 172.19.12.46/20
function calcNetMask_string(numberOfBits) {
   var subNetMask = "";
   subNetMask += calcNetMaskField(numberOfBits);
   subNetMask += ".";
   numberOfBits -= 8;
   subNetMask += calcNetMaskField(numberOfBits);
   subNetMask += ".";
   numberOfBits -= 8;
   subNetMask += calcNetMaskField(numberOfBits);
   subNetMask += ".";
   numberOfBits -= 8;
   subNetMask += calcNetMaskField(numberOfBits);
   return subNetMask;
}

// print the net mask
function calcNetMask(numberOfBits) {
   var str;
   if (numberOfBits) {
      if (numberOfBits == "undefined") {
         document.write("unassigned");
      } else {
         str = calcNetMask_string(numberOfBits);
         document.write(str);
      }
   } else {
      document.write("unassigned");
   }
}


// delimiters
var DLIM_WhSp = /[\s]+/; 
var DLIM_WhSp_slash = /[\/\s]+/; 
var DLIM_WhSp_comma = /[\,\s]+/; 
var DLIM_WhSp_parens = /[\(\)\s]+/; 
var DLIM_WhSp_colon = /[\:\s]+/; 
var DLIM_newline = /[\r\n]+/;
var DLIM_nonword          = /[\W]+/;
var DLIM_nonword_period   = /[^a-zA-Z0-9_\.]+/;   //use if IP addresses
var DLIM_nonword_colon    = /[^a-zA-Z0-9_:]+/;    //use if clock time

// Split() - Fixes Netscape bugs with "split" method
function split (delimiter, string) { 
   localstring = string;
   word = localstring.split(delimiter);
   if (word.length>1) {
      // Fix leading blank problem with Netscape 4.72
      while ((word.length>1)&&(word[0]=="")) {
         localstring = localstring.substr(1);
         word = localstring.split(delimiter);
      }
      // Fix trailing blank problem with Netscape 4.72
      while ((word.length>1)&&word[word.length-1]=="") {
         localstring = localstring.substr(0,localstring.length-1);
         word = localstring.split(delimiter);
      }
   }
   return word;
}

// token routine to parse CLI output into individual tokens.
function token_string (delimiter, index, string) { 
    tok = null;
    word = split(delimiter, string);
    last_token = word.length - 1;
    if (index >= 0) {
       if (index <= last_token) {
          tok = word[index]; 
       }
    } else {
       backindex = last_token + index + 1;
       if (backindex >= 0) {
          tok = word[backindex]; 
       }
    }
    return tok;
}

// Missing tokens are displayed as non-breaking-space
function token (delimiter, index, string) {
   if (-1 != string.search(/exec cmd/)) {
      document.write("*No Switch*");
   } else {
      tok = token_string(delimiter, index, string);
      if (tok) {
          document.write(tok);
      } else {
          document.write("&nbsp;");
      }
   }
}

// Missing tokens are displayed as "undefined"
function token_undefined (delimiter, index, string) {
   if (-1 != string.search(/exec cmd/)) {
      document.write("*No Switch*");
   } else {
      tok = token_string(delimiter, index, string);
      if (tok) {
         document.write(tok);
      } else {
         document.write("undefined");
      }
   }
}

// Use token_help() during development to see how the tokens are being parsed.
function token_help (delimiter, index, string) { 
   local_index = 0;
   tok = token_string(delimiter, local_index, string);
   if (33 == tok.charCodeAt(1)) {
      document.write("missing");
   } else {
      if (tok) {
         while (tok) {
            document.write('\n');
            if (index == local_index) {
               document.write('<FONT COLOR="red">');
            }
            document.write('('+local_index+')="'+tok+'"');
            if (index == local_index) {
               document.write("</FONT>");
            }
            document.write("<br>");
            local_index += 1;
            tok = token_string(delimiter, local_index, string);
         }
      } else {
         document.write('<FONT COLOR="red">NULL</FONT>');
      }
   }
}

function trim_string(string)
{ 
  //Match spaces at beginning and end of text and replace
  //with null strings
  return string.replace(/^\s+/,'').replace(/\s+$/,'');
} 

// search string for target, if found print found else print notfound
function replace (target, found, notfound, string)
{
   if (-1 != string.search(target)) {
      document.write(found);
   } else {
      document.write(notfound);
   }
}

// grep routine to return first matching line
function grep_string (pattern, string) {
   line = split(DLIM_newline, string);
   for (curline = 0; curline < line.length; curline++) {
      if (-1 != line[curline].search(pattern)) {
          return line[curline];
      }
   }
}

// grep routine to return first matching line
function grep (pattern, string) {
   line = grep_string(pattern, string);
   if (line) {
      document.write(line);
   }
}


// return token # index in line that contains pattern
function find (pattern, delimiter, index, string) {
   if (-1 != string.search(/exec cmd/)) {
      document.write("*No Switch*");
   } else {
      line = grep_string(pattern, string);
      if (line) {
         tok = token_string(delimiter, index, line);
         if (tok) {
            document.write(tok);
         } else {
            document.write("&nbsp;");
         }
      } else {
         document.write("&nbsp;");
      }
   }
}


// create an array with n number of elements
function makeArray(n)
{
  this.length = n;
  return this;
}
