
var digits = "0123456789";
var errBg = errorColor;
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
var dbg = 0;
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 "
var sSuffix = " . 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;
	var flag_found_dot = false;
   // 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(c == '.' && !flag_found_dot){//So it expects floats as well
			flag_found_dot = true; //Only one dot allowed...
			continue;
		}*/
        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;
}

function isUrl(s){
	if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
	url_exp= /((((https?)|(ftp)):\/\/)?([\-\w]+\.)+\w{2,}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\/+@&#;`~=%!]*)(\.\w{2,})?)*\/?)/i;
	return url_exp.test(s);

}

// 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.

var globalfield;
function warnEmpty (theField, s)
{
	globalfield=theField;
	//setTimeout("globalfield.focus()",250);
  //  theField.focus();
    theField.style.backgroundColor = errBg;
    //alert(mPrefix + s + mSuffix)
    return false;
}



// Notify user that contents of field theField are invalid.

function warnInvalid (theField, s)
{  
    //theField.focus();
    theField.style.backgroundColor = errBg;
    //theField.select()
    //alert(iPrefix + s + iSuffix)
    return false
}

function warnSelect (theField, s)
{  

	if ((!theField.disabled))
	{
		//theField.focus();
	}

	theField.style.backgroundColor = errBg;

    //  alert(sPrefix + s + sSuffix)
    return false
}

function unwarnInvalid(theField){
	
    theField.style.backgroundColor = '';
	return true;
}

function checkSelect(theField, s, emptyOK)
{
	if (checkSelect.arguments.length == 2) emptyOK = defaultEmptyOK;
	if(theField.value == 0)
		return warnSelect(theField, s);
	else
		return unwarnInvalid(theField);	
}

function checkSelectOther(theField, s, emptyOK)
{

	if (checkSelectOther.arguments.length == 2) emptyOK = defaultEmptyOK;
	if(theField.value == 0)
	{
		return warnSelect(theField, s);
	}
	else if (theField.value == -1)
	{
		theField.style.backgroundColor = '';
		return -1;
	}
	else
	{
		return unwarnInvalid(theField);	
	}
}
// checkString (TEXTFIELD theField, STRING s, [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is not all whitespace.

// The paramater s is currently unused! (The required field is just painted in Yellow)
function checkString (theField, s, emptyOK, minlength)
{ 
    if (checkString.arguments.length == 2) emptyOK = defaultEmptyOK;
   // if ((emptyOK == true) && (isEmpty(theField.value))) return true;
   if ((emptyOK == true) && (isEmpty(theField.value))) 
		return unwarnInvalid(theField);	;
   if ((emptyOK != true) && theField.value.length < minlength) return warnEmpty(theField, s);
//   else	alert('emptyOK is '+emptyOK+' minlength is '+minlength+' and the length is '+theField.value.length);
    if (isWhitespace(theField.value)) 
       return warnEmpty(theField, s);
    else {
		return unwarnInvalid(theField);	
	}
}

function checkSelected (theField, s, emptyOK)
{ 
   // if (checkString.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) 
		return unwarnInvalid(theField);	;
	
    if (isWhitespace(theField.options[theField.selectedIndex].value)) 
       return warnEmpty (theField, s);
    else 
		return unwarnInvalid(theField);	;
}
function checkSelected2 (theField, s, emptyOK)
{ 
    if ((theField.options[theField.selectedIndex].value ==0)) {
    	return warnEmpty (theField, s);
    }
       
    else {
		return unwarnInvalid(theField);	
	}
}
function checkInteger(theField, s, emptyOK)
{
	 if (checkInteger.arguments.length == 2) emptyOK = defaultEmptyOK;
	 if ((emptyOK == true) && (isEmpty(theField.value))){
		return unwarnInvalid(theField);	
	 }
	 if (isWhitespace(theField.value)) 
       return warnEmpty (theField, s);
	  if(!isInteger(theField.value) )
	  	return warnInvalid(theField, s);
	  else
	    {
	     
			return unwarnInvalid(theField);	
	  	}
}
function checkFloat(theField, s, emptyOK)
{
	 if (checkFloat.arguments.length == 2) emptyOK = defaultEmptyOK;
	 if ((emptyOK == true) && (isEmpty(theField.value))){
		return unwarnInvalid(theField);	
	 }
	 if (isWhitespace(theField.value)) 
       return warnEmpty (theField, s);
	  if(!isFloat(theField.value) )
	  	return warnInvalid(theField, s);
	  else{
		return unwarnInvalid(theField);	
	  }
}
function checkLetters(theField, s, emptyOK)
{
	 if (checkLetters.arguments.length == 2) emptyOK = defaultEmptyOK;
	 if ((emptyOK == true) && (isEmpty(theField.value))) 
		return unwarnInvalid(theField);	
	 if (isWhitespace(theField.value)) 
       return warnEmpty (theField, s);
			// alert(isNotInteger(theField.value));
	  if(isNotInteger(theField.value) )
		return unwarnInvalid(theField);	
	  else
       return warnInvalid (theField, s);
}

function checkEqualFields (theField2, theField1) {
	if (theField2.value.toUpperCase()==theField1.value.toUpperCase()) {
		return unwarnInvalid(theField2);	
	}
	else {
        return warnInvalid (theField2, '');		
	}
}

function checkLengthFields (theField2, theField1) {
	if (theField2.value.length<6) 
	{
		alert ("The password field must contain at least 6 characters. Currently it has "+theField2.value.length+" characters"); 
      return warnInvalid (theField2, '');
	}
	else 
	{
		unwarnInvalid(theField1);	
		return unwarnInvalid(theField2);	
            		
	}
}
 
function checkIfIntFields (theField2, theField1) {
	if (isInteger(theField2.value)) 
	{
		//alert ("Password does not have to contain numbers only"); 
      return warnInvalid (theField2, '');
	}
	else 
	{
		unwarnInvalid(theField1);	
		return unwarnInvalid(theField2);	
            		
	} 
}
function checkIfNotCharFields (theField1) 
{
	var i;
	var f;
	f=0;
    for (i = 0; i < theField1.value.length; i++)
    {    
        var c = theField1.value.charAt(i);
        if ((c >= "0") && (c <= "9")) f=f+1;
    }
    if (f==0) 
   	{
   	  //alert("Password does not have to contain characters only");
   	  return warnInvalid (theField1, '');
   	}
   	else return unwarnInvalid(theField1);
   	
	
	
}
// 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 unwarnInvalid(theField);
    else if (!isEmail(theField.value, false)) 
       return warnInvalid (theField, s);
    else {
		return unwarnInvalid(theField);
	}
}

function checkUrl (theField, s,emptyOK)
{   if (checkUrl.arguments.length < 3) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) 
		return unwarnInvalid(theField);
    else if (!isUrl(theField.value, false)) 
       return warnInvalid (theField, s);
    else {
		return unwarnInvalid(theField);
	}
}

function checkLength (theField, s, lengthvalue)
{ 
    if (theField.value.length < lengthvalue) 
       return warnSrtLen (theField, s, lengthvalue);
    else 
		return unwarnInvalid(theField);
}

function warnSrtLen (theField, s, lengthvalue)
{  
    //theField.focus();
    alert("Your need to enter " + lengthvalue+ " digits in field '" +s +"'");
    return false;
}

function checkPhone(theField, s, emptyOK)
{
	 if (checkPhone.arguments.length == 2) emptyOK = defaultEmptyOK;
	 if ((emptyOK == true) && (isEmpty(theField.value))) 
		return unwarnInvalid(theField);
	 if (isWhitespace(theField.value)) 
       return warnEmpty (theField, s);
	  if(!isPhoneDigits(theField.value) )
	  	return warnInvalid(theField, s);
	  else
		return unwarnInvalid(theField);
}

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,form_name)
{   
	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 + '&form_name='+form_name;
	
	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"); 
  var form_name = resArray[2];
  //alert(resArray[1]);
  input.id = resArray[1];
  input.name = input.id;
  input.type='hidden';
  input.value = resArray[0];
  document.forms[form_name].appendChild(input);//change form name as need
//  validate(true,resArray[1]);
  validate(true,form_name,input.id);
}

// flag indicates whether the captcha is ok
var captcha_ok = false;

// new version of "check captcha" function
// the parameter is the string that the user has inserted..
function check_captcha_v2(captcha_input_text)
{
	var chkFile = "checkCaptcha.php";
	var request = "key=" + captcha_input_text;
	
	// send the request
	loader = new net.ContentLoader
		(chkFile,parse_captcha_response,null,'POST',request);
	for(i=0;i<150;i++){
		
	}
	result = parse_captcha_response2(loader) || captcha_ok; //Why we do this? Because javascript is STUPID and doesn't know how to handle global variables well
	return result;
}

// The function parses the response and raises "captcha_ok" flag if needed..
function parse_captcha_response()
{
	res = this.req.responseText;
	var resArray = res.split(',');
	captcha_ok = (resArray[0] == "1")?(true):(false);
	return captcha_ok;
}

function parse_captcha_response2(loader)
{
	res = loader.req.responseText;
	var resArray = res.split(',');
	captcha_ok = (resArray[0] == "1")?(true):(false);
	return captcha_ok;
}

