﻿function GetXmlHttpObject()
{
    var XMLHttp = null;
    try
    {
        XMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
        try
        {
            XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e)
        {
        }
    }
    if (XMLHttp == null)
    {
        XMLHttp = new XMLHttpRequest();
    }
    
    return XMLHttp;
}

function AjaxGetRequest(link, callback)
{
    gXMLHttp = GetXmlHttpObject();
    
    if(gXMLHttp == null)
    {
        alert("Browser does not support HTTP Request");
        return;
    }
    
    gXMLHttp.onreadystatechange = callback;
    gXMLHttp.open("GET", link, true);
    gXMLHttp.send(null);
}

function confirm_prompt(msg)
{
    if(confirm(msg))
        return true;
    else
        return false;
}

function string2xml(text)
{
    var doc;
    
    // code for IE
    if (window.ActiveXObject)
    {
        doc = new ActiveXObject("Microsoft.XMLDOM");
        doc.async="false";
        doc.loadXML(text);
    }
    // code for Mozilla, Firefox, Opera, etc.
    else
    {
        var parser=new DOMParser();
        doc = parser.parseFromString(text,"text/xml");
    }
    return doc;
}

/****************************************************************/

// Check whether string s is empty.
function isEmpty(s)
{ return ((s == null) || (s.length == 0)) }

/****************************************************************/

function isWhitespace (s)
{
   var i;

   // Is s empty?
   if (isEmpty(s)) return true;

   // Search through string's characters one by one
   // until we find a non-whitespace character.
   // When we do, return false; if we don't, return true.

   for (i = 0; i < s.length; i++)
   {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
   }

   // All characters are whitespace.
   return true;
}

function validatePwd(oldPwd, newPwd)
{
    if(validate(oldPwd, 'Old password cannot be blank'))
    {
        if(validate(newPwd, 'New password cannot be blank'))
        {
            return true;
        }
        else
        {
            var input = document.getElementById(oldPwd);
            input.focus();
            return false;
        }
    }
    
    return false;
}

function validate(name, msg)
{
    try
    {
        var input = document.getElementById(name);
        if(Blank(input))
        {
            document.getElementById(name).style.backgroundColor = "yellow";
            alert(msg);
            document.getElementById(name).focus();
            return false;
        }
        else
        {
            return true;
        }
    }
    catch(e)
    {
        alert(e);
    }
}

function validateEmail(email) {
  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(" e-mail address") > -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 (invalid == "") {
    return true;
  } else {
    alert("Oops, something is wrong...." + invalid);
    return false;
  }
}

function validateEmailold(name, msg)
{
    try
    {
        var input = document.getElementById(name);
        if(Email(input))
        {
            document.getElementById(name).style.backgroundColor = "yellow";
            alert(msg);
            document.getElementById(name).focus();
            return false;
        }
        else
        {
            return true;
        }
    }
    catch(e)
    {
        alert(e);
    }
}

// JScript File

/*
 *  AUTHOR: Michael Nino (mnino@insight.com)
 *    DATE: Feb 21, 2000
 * PURPOSE: JavaScript Form Validation Library
 *    NOTE: Save this file as validate.js
 *
 */

/*
 * FUNCTION: Match( field, value, equals)
 *  PURPOSE: Returns true if field's value matches/equals the 
 *           value argument otherwise returns false. Setting the
 *           the equal argument to true indicates you wish the
 *           the value argument to equal the field's value. 
 *
 */
  function Match( field, value, equals ) {
  var pattern;
  var r;
  var checked = 0;
  var blank = new RegExp("^[ ]*$");


  if ( equals ) {
     pattern = new RegExp("^"+value+"$");
  } else {
     pattern = new RegExp(value);
  } // if else

  /* Text Fields/Areas/Hidden */
  /* 
	NOTE: DOES NOT SUPPORT MULTIPLE HIDDEN FIELDS 
	WITH THE SAME NAME NOR DOES IT SUPPORT FIELDS
	WITH DIFFERENT TYPES AND USING THE SAME NAME.
	THIS IS A FEATURE TO BE IMPLEMENTED IN THE
	NEXT RELEASE OF THIS LIBRARY!
  */
  
  if ( (field.type == "text") 
		|| 
       (field.type == "textarea") 
		|| 
       (field.type == "hidden") 
        ||
       (field.type == "password")
     ) 
  {
     return field.value.match(pattern);
  } // if

  /* Select Fields */
  if ( (field.type == "select-one") || ( field.type == "select" ) ) {
     if (field.selectedIndex == -1) {
        return false;
     } // if
     return field.options[field.selectedIndex].value.match(pattern);
  } // if

  /* Radio Button Groups */
  if ( field[0].type == "radio" ) {
     for (r=0; r<field.length; r++) {
        if ( field[r].checked ) {
           checked++;
        } // if 
        if ( (field[r].checked) && (field[r].value.match(pattern)) ) {
           return true;
        } // if
     } // for 
     return false;
  } // if


  } // fn

/*
 * FUNCTION: Equal( field, value )
 *  PURPOSE: Returns true if field's value is equals 
 *           the value argument otherwise returns false.
 *           This function is a wrapper to a Match function
 *           call with the equal argument specified. 
 *
 */
  function Equal( field, value ) {
  return Match(field,value,true);
  } // fn

/*
 * FUNCTION: Number( field )
 *  PURPOSE: Returns true if field's value is a integer 
 *           the value argument otherwise returns false.
 *           This function is a wrapper to a Match function
 *           call with the equal argument specified.
 *
 */
  function Number( field ) {
  return Match(field,'[0-9]+',true);
  } // fn

/*
 * FUNCTION: DateValue( field )
 *  PURPOSE: Returns true if field's value is a valid date string
 *           the value argument otherwise returns false.
 *           This function is a wrapper to a Match function
 *           call with the equal argument specified.
 *
 */
  function DateValue( field ) {
  if (
	Match(field,'(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|30|31)-[1-9][0-9][0-9][0-9]',true) 
		||
	Match(field,'(0[1-9]|1[0-2])\\/(0[1-9]|[1-2][0-9]|30|31)\\/[1-9][0-9][0-9][0-9]',true) 
     )
  { return true; } else { return false; }
  } // fn

/*
 * FUNCTION: DateValue( field )
 *  PURPOSE: Returns true if field's value is a valid date string
 *           the value argument otherwise returns false.
 *           This function is a wrapper to a Match function
 *           call with the equal argument specified.
 *
 */
  function TimeValue( field ) {
  return Match(field,'(0[0-9]|1[0-2]):[0-5][0-9](:[0-5][0-9]){0,1}',true);
  } // fn



/*
 * FUNCTION: Currency( field )
 *  PURPOSE: Returns true if field's value is a currency value 
 *           the value argument otherwise returns false.
 *           This function is a wrapper to a Match function
 *           call with the equal argument specified.
 *
 */
  function Currency( field ) {
  return Match(field,'[$]*([0-9]{1,3}[,])*[0-9]{0,3}\.[0-9]{2}',true);
  } // fn

/*
 * FUNCTION: SSN( field )
 *  PURPOSE: Returns true if field's value is a currency value
 *           the value argument otherwise returns false.
 *           This function is a wrapper to a Match function
 *           call with the equal argument specified.
 *
 */
  function SSN( field ) {
  return Match(field,'[0-9]{3}-[0-9]{2}-[0-9]{4}',true);
  } // fn

/*
 * FUNCTION: Phone( field )
 *  PURPOSE: Returns true if field's value is a currency value
 *           the value argument otherwise returns false.
 *           This function is a wrapper to a Match function
 *           call with the equal argument specified.
 *
 */
  function Phone( field ) {
  if (
	Match(field,'(1-|1 ){0,1}[0-9]{3}-[0-9]{3}-[0-9]{4}',true) 
	||
	Match(field,'(1-|1 ){0,1}\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}',true) 
     )
  { return true; } else { return false; } 
  } // fn


/*
 * FUNCTION: Email( field )
 *  PURPOSE: Returns true if field's value is a currency value
 *           the value argument otherwise returns false.
 *           This function is a wrapper to a Match function
 *           call with the equal argument specified.
 *
 */
  function Email( field ) {
  // Regular Expression is closer to RFC822
  return Match(field,'[^ \\(\\)\\<\\>\\[\\]@,;:"\'\\\\]+[@][^ \\(\\)\\<\\>\\[\\]@,;:"\'\\\\]+(\\.[^ \\(\\)\\<\\>\\[\\]@,;:"\'\\\\]+)+',true);
  } // fn


/*
 * FUNCTION: Blank( field )
 *  PURPOSE: Returns true if field's value is blank or 
 *           contains spaces otherwise returns false.
 *           This function is a wrapper to a Match function
 *           call with the value argument specified.
 *
 */
  function Blank( field ) {
  var r;

  if ( typeof(field.type) == "undefined" ) { // radio button group
     for (r=0; r<field.length; r++) {
        if (field[r].checked) {
           return false;
        } // if
     } // for
     return true;
  } else {
     return Match(field,"[ ]*",true);
  } // if else

  } // fn
