var digits      = "0123456789";
var letters_low = "abcdefghijklmnopqrstuvwxyz";
var letters_up  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var all_banned  = "!?.,;:/\'\"@#$&^%~|+=()[]{}<>_*-";
var banned      = "!?,;:/\"@#$&^%~|+=[]{}<>_*";
var letters = letters_low + letters_up;
var simbols = digits + letters;

<!--
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// DEFINE VARIABLES FOR VALIDATION ROUTINES BELOW

// whitespace characters
var whitespace = " \t\n\r";



/****************************************************************/

// PURPOSE:  Check to see if the string passed in is a valid time.
//  A valid time is defined as a string which is postfixed with either
//  "PM" or "AM".  Next it checks to see if there is a colon in the
//  string.  If there is, it makes sure that at least one digit preceeds
//  it and two proceed it.
/*
    function IsTime(strTime)
    {
        var strTestTime = new String(strTime);
        strTestTime.toUpperCase();

        var bolTime = false;

        if (strTestTime.indexOf("PM",1) != -1 || strTestTime.indexOf("AM",1))
            bolTime = true;

        if (bolTime && strTestTime.indexOf(":",0) == 0)
            bolTime = false;

        var nColonPlace = strTestTime.indexOf(":",1);
        if (bolTime && ((parseInt(nColonPlace) + 5) < (strTestTime.length - 1) || (parseInt(nColonPlace) + 4) > (strTestTime.length - 1)))
            bolTime = false;

        return bolTime;
    }
*/
/****************************************************************/

function replaceAll (s, fromStr, toStr)
{
    var new_s = s;
    for (i = 0; i < 100 && new_s.indexOf (fromStr) != -1; i++)
    {
        new_s = new_s.replace (fromStr, toStr);
    }
    return new_s;
}

/****************************************************************/

/* PURPOSE:  Since we are using the single tick mark as the
    string delimiter to construct our SQL queries, a string with
    a tick mark in it will cause a SQL error.  Therefore we replace
    all "'" with "''", which eliminates the possibility of a SQL error.
*/

function sqlSafe (s)
{
    var new_s = s;
    new_s = replaceAll (new_s, "'", "|");
    new_s = replaceAll (new_s, "|", "''");
    new_s = replaceAll (new_s, "\"", "|");
    new_s = replaceAll (new_s, "|", "''");
    return new_s;
}

/****************************************************************/

function makeSafe (i)
{
    i.value = sqlSafe (i.value);
}

/****************************************************************/

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

/****************************************************************/

// Returns true if string s is empty or 
// whitespace characters only.

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;
}


// Email address must be of form a@b.c ... in other words:
//   there must be at least one character before the "@"
//   there must be at least one character before the "."
//   there must be at least two character after the "."
//   the characters @ and . are both required
function isEmail(oField)
{
  var s = oField.value;
  var l = s.length;
  var max_length = 150;
  var sError = "E-mail address is not valid, please re-enter.";

  if (isEmpty(s)) return true;
  if (!checkLength(max_length, oField)) return false;

// there must be >= 1 character before "@", so we
// start looking at character position 1 
// (i.e. second character)
  var i = 1;

// look for "@"
  while ((i < l) && (s.charAt(i) != "@"))
  {
    i++;
  }

  if ((i >= l) || (s.charAt(i) != "@"))
  {
    error(oField, sError);
    return false;
  }
  else
  {
//  Check First Part
    if (!checkAllowed(s.substring(0, i), simbols + "-_."))
    {
      error(oField, sError);
      return false;
    }

//  Check Last Part
    if (!checkAllowed(s.substring(i + 1, s.length), simbols + "-."))
    {
      error(oField, sError);
      return false;
    }

    i += 2;
  }

// look for "."
  while ((i < l) && (s.charAt(i) != "."))
  {
    i++;
  }

// there must be at least two character after the "."
  if ((i >= l - 2) || (s.charAt(i) != "."))
  {
    error(oField, 'E-mail address requires at least two characters after the dot simbol.\n\n' + sError);
    return false;
  }
  else
  {
    return true;
  }
}


/****************************************************************/

// Returns true if the string passed in is Phone Number (including "+", "-", ".", " ", "(", ")" ),
// else it displays an error message

function isPhone(oField) {
    var strField = new String(oField.value);
    
    if (isWhitespace(strField)) return true;

    var i = 0;

    for (i = 0; i < strField.length; i++)
    {
        sSimb = strField.charAt(i);
        if ((sSimb < '0' || sSimb > '9') && (sSimb != '-') && (sSimb != '.') && (sSimb != '+') && (sSimb != ' ') && (sSimb != '(') && (sSimb != ')'))
        {
            PromptErrorMsg(oField, "Phone must be a valid numeric entry.")
            return false;
        }
    }

    return true;
}


/****************************************************************/

// Returns true if the string passed in is 9 digits Social Security Number,
// else it displays an error message

function isSocialNum(oField) {
    var strField = new String(oField.value);
    
    if (isWhitespace(strField)) return true;

    var i = 0;

    for (i = 0; i < strField.length; i++)
        if (strField.charAt(i) < '0' || strField.charAt(i) > '9') {
            alert("Social Security Number must be a valid numeric entry. Please do not use any non-numeric symbols.");
            oField.focus();
            return false;
        }

    if (strField.length != 9) {
        alert("Please make sure Social Security Number is 9 digits");
        oField.focus();
        return false;
    }

    return true;
}


/****************************************************************/

// Checks to see if a required field is blank.  If it is, a warning
// message is displayed...

function ForceEntry(objField)
{
    var strField = new String(objField.value);
    if (isWhitespace(strField)) {
        objField.focus();
        objField.select();
        alert("Enter information for " + objField.name);
        return false;
    }

    return true;
}
        
/****************************************************************/

// Returns true if the string passed in is a valid number
//  (no alpha characters), else it displays an error message

function ForceNumber(objField)
{
    var strField = new String(objField.value);
    
    if (isWhitespace(strField)) return true;

    var i = 0;

    for (i = 0; i < strField.length; i++)
        if (strField.charAt(i) < '0' || strField.charAt(i) > '9') {
            alert("Field value must be a valid numeric entry.  Please do not use commas, dollar signs or non-numeric symbols.");
            objField.focus();
            return false;
        }
    return true;
}

/****************************************************************/

//  Returns true if the string passed in is a valid money
//  (no alpha characters except a decimal place), 
//   else it displays an error message

function ForceMoney(objField)
{
    var strField = new String(objField.value);
    
    if (isWhitespace(strField)) return true;

    var i = 0;

    for (i = 0; i < strField.length; i++)
        if ((strField.charAt(i) < '0' || strField.charAt(i) > '9') && (strField.charAt(i) != '.')) {
            alert("Field value must be a valid numeric entry.  Please do not use commas, dollar signs or non-numeric symbols.");
            objField.focus();
            return false;
        }

    return true;
}

/****************************************************************/

// Returns true if the string passed in is a valid US zip code.
// It accepts #####.  If the string is valid, it returns true, else false.

function ForceZip(objField)
{
    var strField = new String(objField.value);

    if (isWhitespace(strField)) return true;

    var i = 0;

    for (i = 0; i < strField.length; i++)
      if (strField.charAt(i) < '0' || strField.charAt(i) > '9') {
        alert("ZipCode must be a valid numeric entry.  Please do not use non-numeric symbols.");
        objField.focus();
        return false;
      }

        if (strField.length != 5) {
        alert("Please, make sure ZIP Code is 5 digits.");
        objField.focus();
        return false;
      }

    return true;
}

/****************************************************************/

// Right trims the string...  Useful for SQL datatypes of CHAR

function RTrim(strTrim)
{
    var str = new String(strTrim);
    var i = 0;
    var c = "";
    var endpos = 0

    for (i = str.length; i >= 0 && endpos == 0; i = i - 1) {
        c = str.charAt(i);
        if (whitespace.indexOf(c) == -1)
            endpos = i;
    }

    return str.substring(0,endpos+1);
}

/****************************************************************/

/* PURPOSE:  Returns true if the string is a valid date number.
    A method is passed in (1 = month, 2 = day).  If the string is
    nonnumeric, false is passed back.  If the day in the date string
    is greater than 31, false is returned.  If the month is greater
    than 12, an error is returned.
*/

function isDateNumber(strNum,method)
{
    var str = new String(strNum);
    var i = 0;

    if (isNaN(parseInt(str)) || parseInt(str) < 0) return false;

    if (method == 2)
        if (parseInt(str) > 31)
            return false;
    if (method == 1)
        if (parseInt(str) > 12)
            return false;

    for (i = 0; i < str.length; i++)
        if (str.charAt(i) < '0' || str.charAt(i) > '9')
            return false;


    return true;
}

/****************************************************************/

// Displays an alert box with the passed in string...

function PromptErrorMsg(Field,strError)
{
    alert(strError);
    Field.focus();
}

/****************************************************************/

/* PURPOSE: Checks to see if the string is a valid date.  A valid
    date is defined as any of the following:

        MM/DD/YY, MM/DD/YYYY, M/D/YY, M/D/YYYY,
        MM-DD-YY, MM-DD-YYYY, M-D-YY, M-D-YYYY
*/

function ForceDate(oDate)
{   
    var str = new String(oDate.value);

    dateError = "Field value must be in MM/DD/YYYY format.";

    if (isWhitespace(str)) {
        return true;
        // if the field is empty, just return true...
    }

    var i = 0, count = str.length, j = 0;
    while ((str.charAt(i) != "/" && str.charAt(i) != "-") && i < count)
        i++;

    if (i == count || i > 2) {
        PromptErrorMsg(oDate, dateError);
        return false;
    }

    var addOne = false;
    if (i == 2) addOne = true;

    if (!isDateNumber(str.substring(0,i),1)) {
        PromptErrorMsg(oDate, dateError);
        return false;
    }

    j = i+1;
    i = 0;

    while ((str.charAt(i+j) != "/" && str.charAt(j+i) != "-") && i+j < count)
        i++;

    if (i+j == count || i > 2) {
        PromptErrorMsg(oDate, dateError);
        return false;
    }

    if (!isDateNumber(str.substring(j,i+j),2)) {
        PromptErrorMsg(oDate, dateError);
        return false;
    }

    j = i+3;
    i = 0;

    if (addOne) j++;

    while (i+j < count)
        i++;


    if (i != 2 && i != 4) {
        PromptErrorMsg(oDate, dateError);
        return false;
    }

    if (!isDateNumber(str.substring(j,i+j),3)) {
        PromptErrorMsg(oDate, dateError);
        return false;
    }

    return true;
}

/****************************************************************/

// This function determines if the string passed in is a valid
// US zip code.  It accepts either ##### or #####-####.  If the
// string is valid, it returns true, else false.

function isZipcode(strZip)
{
    var s = new String(strZip);

    if (s.length != 5 && s.length != 10)
        // inappropriate length
        return false;


    for (var i=0; i < s.length; i++)
        if ((s.charAt(i) < '0' || s.charAt(s) > '9') && s.charAt(i) != '-')
            return false;

    return true;
}

/****************************************************************/

// This function ensures that a field is less than or equal to the
// Length passed in.  You must call this function with the element
// name in your form (for example: "ForceLength(document.forms[0].txtElement)"
// as opposed to "ForceLength(document.forms[0].txtElement.value)"
// If the field's value is too large, an error message is displayed
// and false is returned, else true is returned.

function ForceLength(objField, nLength, strWarning)
{
    var strField = new String(objField.value);

    if (strField.length > nLength) {
        alert(strWarning);
        return false;
    } else
        return true;
}

/****************************************************************/

// This function ensures that a field value is less than or equal to the Value passed in.

function ForceValue(objField, nValue, strWarning)
{
  if (objField.value > nValue) {
    alert(strWarning);
    objField.focus();
    return false;
  }
  else
    return true;
}


function isEmptyField(oField, strWarning)
{
  if (isEmpty(oField.value)) {
    alert(strWarning);
    oField.focus();
    return false;
  }
  else
    return true;
}

function isFloat(objField)
{
    var strField = new String(objField.value);
    
    if (isWhitespace(strField)) return true;

    var i = 0;

    for (i = 0; i < strField.length; i++)
        if ((strField.charAt(i) < '0' || strField.charAt(i) > '9') && (strField.charAt(i) != '.')) {
            alert(objField.name + " must be a valid numeric entry.  Please do not use non-numeric symbols.");
            objField.focus();
            return false;
        }

    return true;
}

/***********************************************************************************/
// Checs pair of passwords to be match

function CheckPasswords(oPass1, oPass2)
{
  if (oPass2.value.length <= 0)
      return true;

  if (String(oPass1.value) != String(oPass2.value))
  {
      PromptErrorMsg(oPass2, "Password entries did not match");
      oPass2.value = "";
      return false;
  }

  return true;
}

function isTime(oField)
{
    var s = new String(oField.value);
    sError = "Please, enter time in format 'HH:MM'";

    if (isWhitespace(s)) return true;
    bFlag = false;

//  Find Separator (:)
    var i = 0;
    while ((s.charAt(i) != ":") && (i < s.length))
        i++;

    if ((s.charAt(i) != ':') || (s.length > 5))
        bFlag = true;

    iHour = parseInt(s.substr(0, i));

    if (isNaN(iHour) || (parseInt(iHour) > 24) || (parseInt(iHour) < 0))
        bFlag = true;

    iMin = parseInt(s.substr(i + 1, s.length - i));
//    if  (iMin != s.substr(i + 1, s.length - i))
//        bFlag = true;

    if (isNaN(iMin) || (parseInt(iMin) > 60) || (parseInt(iMin) < 0))
        bFlag = true;

    if (s == '24:00')
        oField.value = '23:59';

    if (bFlag) 
    {
        alert(sError);
        oField.focus();
        return false;
    }
    else
        return true;
}


function checkMandatory(oField, sName)
{
    if (oField.value.length > 0)
        return true;
    else
    {
        alert ("Please, fill '" + sName + "' field!");

        if (oField.type != 'hidden')
            oField.focus();

        return false;
    }
}


function checkLength(oField, iMin)
{
    if (oField.value.length == 0)
        return true;

    if (oField.value.length < iMin)
    {
        alert ("Sorry, value must be at least " + iMin + " characters!");
        oField.focus();
        return false;
    }
    else
        return true;
}


function new_window(sLink, sName, iWidth, iHeight)
{
    window.open(sLink, sName, 'left=' + (screen.width - iWidth)/2 + ',top=' + (screen.height - iHeight)/2 + ',width=' + iWidth + ',height=' + iHeight + ',location=0,toolbar=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,channelmode=0,fullscreen=0');
    return false;
}
//--> 

// Show error message and return focus to target field
function error(oField, sError)
{
  alert(sError);
  oField.focus();
}

function checkLength(maximum, oField)
{
  if (isEmpty(oField.value)) return true;

  if (oField.value.length > maximum)
  {
    error(oField, 'Invalid value length!\n\nMaxumum allowed length is ' + maximum + '.\nCurrent length is ' + oField.value.length + '.\nPlease, provide a correct value!');
    return false;
  }
  else
  {
    return true;
  }
}

function checkAllowed(value, sValid)
{
  var temp;

  for (var i = 0; i < value.length; i++)
  {
    temp = value.substring(i, i + 1);

    if (sValid.indexOf(value.substring(i, i + 1)) == "-1")
    {
      return false;
    }
  }

  return true;
}

function isSame(src1,src2,str)
{
	if (src1.value!=src2.value)
	{
		alert(str);
		src1.focus();
		return false;
	}
	else
		return true;
}// end isSame
