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

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)
{  
    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]);
}


