var digits = "0123456789";
var dbg = 0;
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"

var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"


// whitespace characters
var whitespace = " \t\n\r";

var mPrefix = " אנא מלא ערך בשדה "
var mSuffix = " "
//var mPrefix = "You did not enter a value into the "
//var mSuffix = " field. This is a required field. Please enter it now."


var sPrefix = "אנא בחר ערך בשדה ";
var sSuffix = " "

//var sPrefix = "You did not selected a value into the "
//var sSuffix = " field. This is a required field. Please select it now."

// i is an abbreviation for "invalid"

var iEmail = "(ru@domain.com) אימייל לא חוקי! נא להקליד את אימייל הנכון ";
//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


// REGULAR EXPRESSION DECLARATIONS
// Notes which apply to all the regexps below:
// (1) We want to only match strings exactly. In other words,
//     we only want to return true if the string being tested
//     matches the regular expression with no leading or trailing
//     unmatched characters. So, we begin each regexp with
//     the special character ^ (which matches beginning of input)
//     and end each regexp with the special character $ (which
//     matches end of input).
// (2) In the below comments we use these abbreviations:
//     BOI = Beginning Of Input
//     EOI = End Of Input
// (3) For explanations of the regexp special characters such as
//     ^ $ \s + [] \d * ! ? \ .
//     see http://developer.netscape.com/library/documentation/communicator/jsguide/regexp.htm


// BOI, followed by one or more whitespace characters, followed by EOI.
var reWhitespace = '/^\s+$/';


// BOI, followed by one lower or uppercase English letter, followed by EOI.
var reLetter = '/^[a-zA-Z]$/';


// BOI, followed by one or more lower or uppercase English letters, 
// followed by EOI.
var reAlphabetic = '/^[a-zA-Z]+$/';


// BOI, followed by one or more lower or uppercase English letters
// or digits, followed by EOI.
var reAlphanumeric = '/^[a-zA-Z0-9]+$/';


// BOI, followed by one digit, followed by EOI.
var reDigit = '/^\d/';


// BOI, followed by one lower or uppercase English letter
// or digit, followed by EOI.
var reLetterOrDigit = '/^([a-zA-Z]|\d)$/';


// BOI, followed by one or more digits, followed by EOI.
var reInteger = '/^\d+$/';


// BOI, followed by an optional + or -, followed by one or more digits, 
// followed by EOI.
var reSignedInteger = '/^(+|-)?\d+$/';


// BOI, followed by one of these two patterns:
// (a) one or more digits, followed by ., followed by zero or more digits
// (b) zero or more digits, followed by ., followed by one or more digits
// ... followed by EOI.
var reFloat = '/^((\d+(\.\d*)?)|((\d*\.)?\d+))$/';


// BOI, followed by an optional + or -, followed by one of these two patterns:
// (a) one or more digits, followed by ., followed by zero or more digits
// (b) zero or more digits, followed by ., followed by one or more digits
// ... followed by EOI.
var reSignedFloat = '/^(((+|-)?\d+(\.\d*)?)|((+|-)?(\d*\.)?\d+))$/';

// BOI, followed by one or more characters, followed by @,
// followed by one or more characters, followed by ., 
// followed by one or more characters, followed by EOI.
var reEmail = '/^.+\@.+\..+$/';

// Attempting to make this library run on Navigator 2.0, 
// so I'm supplying this array creation routine as per 
// JavaScript 1.0 documentation.  If you're using  
// Navigator 3.0 or later,  you don't need to do this; 
// you can use the Array constructor instead. 

function makeArray(n) { 
//*** BUG: If I put this line in,  I get two error messages: 
//(1) Window.length can't be set by assignment 
//(2) daysInMonth has no property indexed by 4 
//If I leave it out,  the code works fine. 
//   this.length = n; 
   for (var i = 1; i <= n; i++) { 
      this[i] = 0 
   }  
   return this 
} 


var daysInMonth = makeArray(12); 
daysInMonth[1] = 31; 
daysInMonth[2] = 29;   // must programmatically check this 
daysInMonth[3] = 31; 
daysInMonth[4] = 30; 
daysInMonth[5] = 31; 
daysInMonth[6] = 30; 
daysInMonth[7] = 31; 
daysInMonth[8] = 31; 
daysInMonth[9] = 30; 
daysInMonth[10] = 31; 
daysInMonth[11] = 30; 
daysInMonth[12] = 31; 



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")) || (c==" ") )
}

// 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 is float value
function isFloat(s)
{
	var i;

    // 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 (! ((c >= "0") && (c <= "9") || (c==".")) ) return false;
    }

    // All characters are numbers.
    return true;
}
//check if is Numeric value
function isInteger (s)

{   var i;

   // if (isEmpty(s))  return defaultEmptyOK;
      

    // 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;
}


function isNotInteger (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 letters.
    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)
{  
    if (theField.type != 'hidden'){
		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 warnSelect (theField, s)
{  
      alert(sPrefix + s + sSuffix)
    return false
}


function checkSelect(theField, s, emptyOK)
{
	if (checkSelect.arguments.length == 2) emptyOK = defaultEmptyOK;
	if(theField.value == 0)
		return warnSelect(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 checkSelected (theField, s, emptyOK)
{ 
   // if (checkString.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
	
    if (isWhitespace(theField.options[theField.selectedIndex].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;
}
function checkFloat(theField, s, emptyOK)
{
	 if (checkFloat.arguments.length == 2) emptyOK = defaultEmptyOK;
	 if ((emptyOK == true) && (isEmpty(theField.value))) return true;
	 if (isWhitespace(theField.value)) 
       return warnEmpty (theField, s);
	  if(!isFloat(theField.value) )
	  	return warnInvalid(theField, s);
	  else
	  	return true;
}
function checkLetters(theField, s, emptyOK)
{
	 if (checkLetters.arguments.length == 2) emptyOK = defaultEmptyOK;
	 if ((emptyOK == true) && (isEmpty(theField.value))) return true;
	 if (isWhitespace(theField.value)) 
       return warnEmpty (theField, s);
			// alert(isNotInteger(theField.value));
	  if(isNotInteger(theField.value) )
	  	return true;
	  else
	  	return false;
}
// 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 checkEmailNew (theField, s) {
	var re = new RegExp("\\b[A-Z0-9._%-]+@[A-Z0-9._%-]+\\.[A-Z0-9._%-]{2,4}\\b", "i");
	var email = theField.value;
	if (!email.match(re)) {
			alert(s);
			return false;
	}
	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(" " + s+ " מםפרים בשדה " + lengthvalue +" נא למלא את ");
    return false
}

function checkPhone(theField, s, emptyOK)
{
	 if (checkPhone.arguments.length == 2) emptyOK = defaultEmptyOK;
	 if ((emptyOK == true) && (isEmpty(theField.value))) return true;
	 if (isWhitespace(theField.value)) 
       return warnEmpty (theField, s);
	  if(!isPhoneDigits(theField.value) )
	  	return warnInvalid(theField, s);
	  else
	  	return true;
}

function isPhoneDigits (s)

{   var i;

    if (isEmpty(s)) 
       if (isPhoneDigits.arguments.length == 1) 
	   		return defaultEmptyOK;
       else 
	   		return (isPhoneDigits.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 (!isDigitPhone(c)) return false;
    }
    // All characters are numbers.
    return true;
}
function isDigitPhone (c)
{   return ((c >= "0") && (c <= "9") || (c == "-")||(c == "#")||(c == ".")||(c == "/"))
}

function checkCaptcha (theField, s,emptyOK,chkFile)
{   
	if (dbg) alert('In checkCaptcha');
	if (checkCaptcha.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) {
    	return true;
    }
    if (chkFile == null || chkFile == '') {//set deafault check file 
			chkFile = 'checkCaptcha.php';	
    }
	var request ='key='+ theField.value;
	loader = new net.ContentLoader(chkFile,isCaptchaOK,null,'POST',request);
	return false;
}
function isCaptchaOK() {
  var resTxt=this.req.responseText;
  var resArray = resTxt.split(',');
  var input = document.createElement("input"); 
  input.id = resArray[1];
  input.name = input.id;
  input.type='hidden';
  input.value = resArray[0];
  document.contactdatas.appendChild(input);
  validate(true,resArray[1]);
}
// Check that string theField.value is a valid Year. 
// 
// For explanation of optional argument emptyOK, 
// see comments of function isInteger. 

function checkYear (theField,  emptyOK) 
{   if (checkYear.arguments.length == 1) emptyOK = defaultEmptyOK; 
    if ((emptyOK == true) && (isEmpty(theField.value))) return true; 
    if (!isYear(theField.value,  false))  
       return warnInvalid (theField,  iYear); 
    else return true; 
} 


// Check that string theField.value is a valid Month. 
// 
// For explanation of optional argument emptyOK, 
// see comments of function isInteger. 

function checkMonth (theField,  emptyOK) 
{   if (checkMonth.arguments.length == 1) emptyOK = defaultEmptyOK; 
    if ((emptyOK == true) && (isEmpty(theField.value))) return true; 
    if (!isMonth(theField.value,  false))  
       return warnInvalid (theField,  iMonth); 
    else return true; 
} 


// Check that string theField.value is a valid Day. 
// 
// For explanation of optional argument emptyOK, 
// see comments of function isInteger. 

function checkDay (theField,  emptyOK) 
{   if (checkDay.arguments.length == 1) emptyOK = defaultEmptyOK; 
    if ((emptyOK == true) && (isEmpty(theField.value))) return true; 
    if (!isDay(theField.value,  false))  
       return warnInvalid (theField,  iDay); 
    else return true; 
} 



// checkDate (yearField,  monthField,  dayField,  STRING labelString [,  OKtoOmitDay==false]) 
// 
// Check that yearField.value,  monthField.value,  and dayField.value  
// form a valid date. 
// 
// If they don't,  labelString (the name of the date,  like "Birth Date") 
// is displayed to tell the user which date field is invalid. 
// 
// If it is OK for the day field to be empty,  set optional argument 
// OKtoOmitDay to true.  It defaults to false. 

function checkDate (yearField,  monthField,  dayField,  labelString,  OKtoOmitDay) 
{   // Next line is needed on NN3 to avoid "undefined is not a number" error 
    // in equality comparison below. 
    if (checkDate.arguments.length == 4) OKtoOmitDay = false; 
    if (!isYear(yearField.value)) return warnInvalid (yearField,  iYear); 
    if (!isMonth(monthField.value)) return warnInvalid (monthField,  iMonth); 
    if ( (OKtoOmitDay == true) && isEmpty(dayField.value) ) return true; 
    else if (!isDay(dayField.value))  
       return warnInvalid (dayField,  iDay); 
    if (isDate (yearField.value,  monthField.value,  dayField.value)) 
       return true; 
    alert (iDatePrefix + labelString + iDateSuffix) 
    return false 
} 

// isYear (STRING s [,  BOOLEAN emptyOK]) 
//  
// isYear returns true if string s is a valid  
// Year number.  Must be 2 or 4 digits only. 
//  
// For Year 2000 compliance,  you are advised 
// to use 4-digit year numbers everywhere. 
// 
// And yes,  this function is not Year 10000 compliant,  but  
// because I am giving you 8003 years of advance notice, 
// I don't feel very guilty about this ... 
// 
// For B.C. compliance,  write your own function. ;-> 
// 
// For explanation of optional argument emptyOK, 
// see comments of function isInteger. 

function isYear (s) 
{   if (isEmpty(s))  
       if (isYear.arguments.length == 1) return defaultEmptyOK; 
       else return (isYear.arguments[1] == true); 
    if (!isNonnegativeInteger(s)) return false; 
    return ((s.length == 2) || (s.length == 4)); 
} 



// isIntegerInRange (STRING s,  INTEGER a,  INTEGER b [,  BOOLEAN emptyOK]) 
//  
// isIntegerInRange returns true if string s is an integer  
// within the range of integer arguments a and b,  inclusive. 
//  
// For explanation of optional argument emptyOK, 
// see comments of function isInteger. 


function isIntegerInRange (s,  a,  b) 
{   if (isEmpty(s))  
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK; 
       else return (isIntegerInRange.arguments[1] == true); 

    // Catch non-integer strings to avoid creating a NaN below, 
    // which isn't available on JavaScript 1.0 for Windows. 
    if (!isInteger(s,  false)) return false; 

    // Now,  explicitly change the type to integer via parseInt 
    // so that the comparison code below will work both on  
    // JavaScript 1.2 (which typechecks in equality comparisons) 
    // and JavaScript 1.1 and before (which doesn't). 
    var num = parseInt (s); 
    return ((num >= a) && (num <= b)); 
} 



// isMonth (STRING s [,  BOOLEAN emptyOK]) 
//  
// isMonth returns true if string s is a valid  
// month number between 1 and 12. 
// 
// For explanation of optional argument emptyOK, 
// see comments of function isInteger. 

function isMonth (s) 
{   if (isEmpty(s))  
       if (isMonth.arguments.length == 1) return defaultEmptyOK; 
       else return (isMonth.arguments[1] == true); 
    return isIntegerInRange (s,  1,  12); 
} 



// isDay (STRING s [,  BOOLEAN emptyOK]) 
//  
// isDay returns true if string s is a valid  
// day number between 1 and 31. 
//  
// For explanation of optional argument emptyOK, 
// see comments of function isInteger. 

function isDay (s) 
{   if (isEmpty(s))  
       if (isDay.arguments.length == 1) return defaultEmptyOK; 
       else return (isDay.arguments[1] == true);    
    return isIntegerInRange (s,  1,  31); 
} 



// daysInFebruary (INTEGER year) 
//  
// Given integer argument year, 
// returns number of days in February of that year. 

function daysInFebruary (year) 
{   // February has 29 days in any year evenly divisible by four, 
    // EXCEPT for centurial years which are not also divisible by 400. 
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 ); 
} 



// isDate (STRING year,  STRING month,  STRING day) 
// 
// isDate returns true if string arguments year,  month,  and day  
// form a valid date. 
//  

function isDate (year,  month,  day) 
{   // catch invalid years (not 2- or 4-digit) and invalid months and days. 
    if (! (isYear(year,  false) && isMonth(month,  false) && isDay(day,  false))) return false; 

    // Explicitly change type to integer to make code work in both 
    // JavaScript 1.1 and JavaScript 1.2. 
    var intYear = parseInt(year); 
    var intMonth = parseInt(month); 
    var intDay = parseInt(day); 

    // catch invalid days,  except for February 
    if (intDay > daysInMonth[intMonth]) return false;  

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false; 

    return true; 
} 

// isNonnegativeInteger (STRING s [,  BOOLEAN emptyOK]) 
//  
// Returns true if string s is an integer >= 0. 
// 
// For explanation of optional argument emptyOK, 
// see comments of function isInteger. 

function isNonnegativeInteger (s) 
{   var secondArg = defaultEmptyOK; 

    if (isNonnegativeInteger.arguments.length > 1) 
        secondArg = isNonnegativeInteger.arguments[1]; 

    // The next line is a bit byzantine.  What it means is: 
    // a) s must be a signed integer,  AND 
    // b) one of the following must be true: 
    //    i)  s is empty and we are supposed to return true for 
    //        empty strings 
    //    ii) this is a number >= 0 

    return (isSignedInteger(s,  secondArg) 
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) ); 
} 

// isSignedInteger (STRING s [,  BOOLEAN emptyOK]) 
//  
// Returns true if all characters are numbers;  
// first character is allowed to be + or - as well. 
// 
// Does not accept floating point,  exponential notation,  etc. 
// 
// We don't use parseInt because that would accept a string 
// with trailing non-numeric characters. 
// 
// For explanation of optional argument emptyOK, 
// see comments of function isInteger. 
// 
// EXAMPLE FUNCTION CALL:          RESULT: 
// isSignedInteger ("5")           true  
// isSignedInteger ("")            defaultEmptyOK 
// isSignedInteger ("-5")          true 
// isSignedInteger ("+5")          true 
// isSignedInteger ("",  false)     false 
// isSignedInteger ("",  true)      true 

function isSignedInteger (s) 

{   if (isEmpty(s))  
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK; 
       else return (isSignedInteger.arguments[1] == true); 

    else { 
        var startPos = 0; 
        var secondArg = defaultEmptyOK; 

        if (isSignedInteger.arguments.length > 1) 
            secondArg = isSignedInteger.arguments[1]; 

        // skip leading + or - 
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") ) 
           startPos = 1;     
        return (isInteger(s.substring(startPos,  s.length),  secondArg)) 
    } 
} 
