// Global Variables
var isNav
var isIE
var NavVersion
var range       = "" ;
var styleObj    = "" ;

if ( navigator.appName == "Netscape" )
{
  isNav = true ;
}
else
{
  isIE      = true ;
  range     = "all." ;
  styleObj  = ".style" ;
}

if ( navigator.appVersion.indexOf( "MSIE" ) >= 0 )
{
    navVersion = navigator.appVersion ;
    navVersion = navVersion.substring( navVersion.indexOf( "MSIE" ) + 5 ) ;
    navVersion = parseInt( navVersion ) ;
}    
else    
    navVersion = parseInt( navigator.appVersion ) ;

// Function to Return the Navigator + Version
function getNavigator()
{
    var nav = "" ;

    if (isNav)
        nav = "NS" + navVersion ;
    else
        nav = "IE" + navVersion ;
    return nav ;
}
        

// Convert object name string or object reference
// into a valid object reference
function getObject( obj )
{
  var theObj ;
  if (typeof obj == "string" )
    theObj = eval( "document." + range + obj ) ;
  else  
    theObj = obj ;
    
  return theObj ;
}

// Position an object at a specific pixel coordinate
function shiftTo( obj, x, y )
{
  var theObj = getObject( obj ) ;
  
  if (isNav)
  { 
    theObj.moveTo( x, y );
  }
  else
  {
    theObj.style.pixelLeft = x ;
    theObj.style.pixelRight= y ;
  }
}

// Function to Get a Cookie
function GetCookie (name) 
{
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;

  while (i < clen) 
  {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0)
      break;
  }
  return null;
}

// Set a Cookie
function SetCookie (name, value) 
{
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = "/"; 
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  
  document.cookie = name + "=" + escape (value) + 
                   ((expires == null) ? "" : ("; expires=" +
                    expires.toGMTString())) +
                   ((path == null) ? "" : ("; path=" + path)) +
                   ((domain == null) ? "" : ("; domain=" + domain)) +
                   ((secure == true) ? "; secure" : "");
}

// Get a Cookie Value
function getCookieVal (offset) 
{
  var endstr = document.cookie.indexOf(";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

// Check an Email Address
function checkEmail(email)
{
  var invalid = "";

  if (!email)
    invalid = "No email address found!  Try reloading the page then use the 'email a script' feature again.";
  else 
  {
    if ( (email.indexOf("@") == -1) || (email.indexOf(".") == -1) ) 
      invalid += "\n\nInvalid email address.  Your email address is missing an '@' sign and a '.' in the domain name (like '.com').  Please check your address then submit again.";

    if (email.indexOf("youremailhere") > -1)
      invalid += "\n\nInvalid email address.  Make sure your email address included your username, the '@' sign, and the domain name (like '.com').";

    if (email.indexOf("\\") > -1) 
      invalid += "\n\nEmail address contains an invalid back-slash (\\) character.  Remove the character and submit again.";

    if (email.indexOf("/") > -1) 
      invalid += "\n\nEmail address contains an invalid forward-slash (/) character.  Remove the character and submit again.";

    if (email.indexOf("'") > -1) 
      invalid += "\n\nEmail address contains an invalid apostrophe (') character.  Remove the character and submit again.";

    if (email.indexOf("zaz.com.br") > -1) 
      invalid += "\n\nPlease do not use an email address that has an autoresponder set up for it.  Thanks.";

    if (email.indexOf("!") > -1) 
      invalid += "\n\nEmail address contains an invalid exclamation point (!) character.  Remove the character or correct the email address then submit again.";

    if ( (email.indexOf(",") > -1) || (email.indexOf(";") > -1) )
      invalid += "\n\nPlease only enter one email address in the box at a time.  Remove the extra addresses and submit again.";

    if (email.indexOf("?subject") > -1) 
      invalid += "\n\nPlease do not add '?subject=...' to your email address.  Scriptbot will send you the script with a pre-defined subject already.  Please remove the '?subject=...' from your email address and submit again.";

    if ( (email.indexOf("à") > -1) || (email.indexOf("á") > -1) || (email.indexOf("ä") > -1) || (email.indexOf("è") > -1) || (email.indexOf("é") > -1) || (email.indexOf("ë") > -1) || (email.indexOf("ì") > -1) || (email.indexOf("í") > -1) || (email.indexOf("ï") > -1) || (email.indexOf("ñ") > -1) || (email.indexOf("ò") > -1) || (email.indexOf("ó") > -1) || (email.indexOf("ö") > -1) || (email.indexOf("ù") > -1) || (email.indexOf("ú") > -1) || (email.indexOf("ü") > -1) || (email.indexOf("ý") > -1) || (email.indexOf("ÿ") > -1) )
      invalid += "\n\nAccented characters (like è) are not accepted.  Please enter its unaccented character instead then submit again.";
  }

  return (invalid) ;
}

// Converts a value to a dollar amount ($###,##0.00)
function dollarAmount(FieldValue) 
{ 
  var temp1 = "";
  var temp2 = "";
  var count = 0;

  Num = "" + FieldValue;
  dec = Num.indexOf(".");
  end = ((dec > -1) ? "" + Num.substring(dec,Num.length) : ".00");
  Num = "" + parseInt(Num);

  if (checkNum(Num) != 0) 
  {
    if (end.length > 2) end = end.substring( 0, 3 ) ;
    if (end.length == 2) end += "0";
    if (end.length == 1) end += "00";
    if (end == "") end += ".00";
    
    for (var k = Num.length-1; k >= 0; k--) 
    {
      var oneChar = Num.charAt(k);
      if (count == 3) 
      {
        temp1 += ",";
        temp1 += oneChar;
        count = 1;
        continue;
      } else {
        temp1 += oneChar;
        count ++;
      }
    }
    for (var k = temp1.length-1; k >= 0; k--) 
    {
       var oneChar = temp1.charAt(k);
       temp2 += oneChar;
    }
    temp2 = "$" + temp2 + end;
    return temp2 ;
   }
}

// Converts a value to an integer with commas
function intAmount(FieldValue) 
{ 
  var temp1 = "";
  var temp2 = "";
  var count = 0;

  Num = "" + FieldValue;
  dec = Num.indexOf(".");
  Num = "" + parseInt(Num);

  if (checkNum(Num) != 0) 
  {
    for (var k = Num.length-1; k >= 0; k--) 
    {
      var oneChar = Num.charAt(k);
      if (count == 3) 
      {
        temp1 += ",";
        temp1 += oneChar;
        count = 1;
        continue;
      } else {
        temp1 += oneChar;
        count ++;
      }
    }
    for (var k = temp1.length-1; k >= 0; k--) 
    {
       var oneChar = temp1.charAt(k);
       temp2 += oneChar;
    }
    return temp2 ;
   }
}

// Rounds a number to the desired decimal places
function Round(num, decPlaces)
{
   var powTen = Math.pow(10.0,decPlaces);
   num *= powTen;
   if (num >= 0)
     num = Math.floor(num + 0.5);
   else
     num = Math.ceil(num - 0.5);

   return num / powTen;
}

// checks if all characters 
// are valid numbers or a "."
function checkNum(data) 
{                                 
  var valid = "0123456789.";      
  var ok = 1; var checktemp;
  for (var i=0; i < data.length; i++) 
  {
    checktemp = "" + data.substring(i, i+1);
    if (valid.indexOf(checktemp) == "-1") 
      return 0; 
  }
  return 1;
}

// A utility function that returns true if a string contains only
// whitespace characters.
function isblank(s)
{
  for (var i = 0; i < s.length; i++) 
  {
    var c = s.charAt(i);
    if ((c != ' ') && (c != '\n') && (c != '\t')) 
      return false;
  }
  return true;
}


// A Utility function that returns true if a string contains only 
// alphanumeric characters
function isAlphaNumeric(s) 
{
  var valid = "abcdefghijklmnopqrstuvwxyz0123456789"
  var temp;
  for (var i=0; i<s.length; i++) 
  {
     temp = "" + s.substring(i, i+1);
     if (valid.indexOf(temp) == "-1") 
       return false;
  }
  return true;
}
