var digits = "0123456789";

var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"

var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"


// whitespace characters
var whitespace = " \t\n\r";

var mPrefix = "You did not enter a value into the "
var mSuffix = " field. This is a required field. Please enter it now."
//var mPrefix = " אנא מלא ערך בשדה "
//var mSuffix = ""


// i is an abbreviation for "invalid"


var iEmail = "This field must be a valid email address (like ru@domain.com). Please reenter it now."
var iCreditCardPrefix = "This is not a valid "
var iCreditCardSuffix = " credit card number. Please reenter it now."
var iDay = "This field must be a day number between 1 and 31.  Please reenter it now."
var iMonth = "This field must be a month number between 1 and 12.  Please reenter it now."
var iYear = "This field must be a 2 or 4 digit year number.  Please reenter it now."
var iDatePrefix = "The Day, Month, and Year for "
var iDateSuffix = " do not form a valid date.  Please reenter them now."

//var iPrefix = " שדה ";
//var iSuffix = " לא תקין\nאנא מלא שוב"; 

var iPrefix = "The field ";
var iSuffix = " is invalid.Please reenter it now."; 

// p is an abbreviation for "prompt"

var pEntryPrompt = "Please enter a "

var pZIPCode = "5 digit ZIP Code (like 54446)."
var pWorldPhone = "international phone number."
var pEmail = "valid email address (like ru@yahoo.com)."
var pDay = "day number between 1 and 31."
var pMonth = "month number between 1 and 12."
var pYear = "2 or 4 digit year number."

var defaultEmptyOK = false

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.
    //return false if not found,else 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;
}

// Returns true if character c is an English letter 
// (A .. Z, a..z).
//
function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{  
	 //return ( (c >= "0") && (c <= "9") || (c==".") )
	 return ( (c >= "0") && (c <= "9") )
}

//check if s Numeric value
function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) 
	   		return defaultEmptyOK;
       else 
	   		return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

// Returns true if character c is a letter or digit.

function isLetterOrDigit (c)
{   return (isLetter(c) || isDigit(c))
}

// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// 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 and after the .
// * the characters @ and . are both required

function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}


// Display prompt string s in status bar.

function prompt (s)
{   
	window.status = s
}



// Display data entry prompt string s in status bar.

function promptEntry (s)
{   
	window.status = pEntryPrompt + s
}




// Notify user that required field theField is empty.


function warnEmpty (theField, s)
{  
    theField.focus()
    alert(mPrefix + s + mSuffix)
    return false
}



// Notify user that contents of field theField are invalid.

function warnInvalid (theField, s)
{  
    theField.focus()
    //theField.select()
    alert(iPrefix + s + iSuffix)
    return false
}




function checkSelect(theField, s, emptyOK)
{
	if (checkSelect.arguments.length == 2) emptyOK = defaultEmptyOK;
	if(theField.value == 0)
		return warnEmpty(theField, s);
	else
		return true;	
}

// checkString (TEXTFIELD theField, STRING s, [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is not all whitespace.

function checkString (theField, s, emptyOK)
{ 
    if (checkString.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value)) 
       return warnEmpty (theField, s);
    else return true;
}

function checkInteger(theField, s, emptyOK)
{
	 if (checkInteger.arguments.length == 2) emptyOK = defaultEmptyOK;
	 if ((emptyOK == true) && (isEmpty(theField.value))) return true;
	 if (isWhitespace(theField.value)) 
       return warnEmpty (theField, s);
	  if(!isInteger(theField.value) )
	  	return warnInvalid(theField, s);
	  else
	  	return true;
}

// checkEmail (TEXTFIELD theField [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is a valid Email.
//

function checkEmail (theField, s,emptyOK)
{   if (checkEmail.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isEmail(theField.value, false)) 
       return warnInvalid (theField, s);
    else return true;
}

function checkLength (theField, s, lengthvalue)
{ 
    if (theField.value.length < lengthvalue) 
       return warnSrtLen (theField, s, lengthvalue);
    else return true;
}

function warnSrtLen (theField, s, lengthvalue)
{  
    theField.focus();
    alert("Your need to enter " + lengthvalue+ " digits in field '" +s +"'");
    return false
}