var PHONES  = "0123456789-/()"; // define valid characters for PHONE fields
var NUMERIC  = "0123456789"; // define valid characters for PHONE fields
var HEBREW = "אבגדהוזחטיכלמנסעפצקרשתץףךןם '";
var ENGLISH = "azswxcderfvbgtyhnmjuiklopABCDEFGHIJKLMNOPQRSTUVWXYZ ";
var ENGLISH_MAIL = NUMERIC + "azswxcderfvbgtyhnmjuiklopABCDEFGHIJKLMNOPQRSTUVWXYZ";
var NONE = "";
var PUNCTUATION = "!~@#$%^&)(._-";
var HEBREW_NUMERIC= HEBREW+NUMERIC;
var DATE = "0123456789/";
var EMAIL = ENGLISH_MAIL + "@.-_";
var STRING = "אבגדהוזחטיכלמנסעפצקרשתץףךןם '"+"azswxcderfvbgtyhnmjuiklopABCDEFGHIJKLMNOPQRSTUVWXYZ "+"'\"_-:)(./\\";


// use: onKeyPRess="KeyPressFilter(ENGLISH,event);"
// advanced KeyPress Filter, works both for IE and NN4 and firefox
// accepts a string of allowed characters, all other chars will not be allowed
// and will not appear in the filtered field

function KeyPressFilter(filterObj,e){
  
	var evtobj=window.event? event : e ;//distinguish between IE's explicit event object (window.event) and Firefox's implicit.
	var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode;
	var key=String.fromCharCode(unicode);
    for (var i=0; i< filterObj.length; i++) {
    	var c = String.fromCharCode(filterObj.charCodeAt(i));
			if (c == key){
				return  false;
			}
	}
	if (evtobj.keyCode==0){
		evtobj.preventDefault() ;
	}
   	evtobj.returnValue = false;	
}

function checkIdentificationValue(idValue) {
	if (idValue != null && idValue == "0") {
		return false;
	}
	if (idValue != null && !idValue == "" ) {
		//remove empty spaces from idValue String
		idValue = trim(idValue);
		if (!isInteger(idValue)) {
			return false;
		}
		//if idValue's length is not 9, new message will be added to messagesContainer
		if (idValue.length < 9) {
			var zeroNum = 9- idValue.length;
			for(var i=0; i<zeroNum; i++) {
				idValue = "0" +idValue;
			}
			//return false;
		}
		else {
			if (idValue.length>9) {
				return false;
			}
		}

		//if no errors is messagesContainer, means, that passed idValue contains numbers
		//and it's length is valid, we will check it's "check digit" 
		//to get "check digit" we will get idValue with 8 characters only, so
		//if passed idValue contain 8 characters we will ignore it's last character
		var idValToControlDigit =
			(idValue.length == 9)
				? idValue.substring(0, idValue.length - 1)
				: idValue;

		//get checkDigit of passed idValue
		var checkDigit = getCheckDigit(idValToControlDigit);

		//ifpassed idValue's length is 8, we will add checkDigit to passed idValue
		if (idValue.length == 8) {
			idValue = idValue.concat(String.valueOf(checkDigit));
		} else if (idValue.length == 9) {
			//we will check that last character of idValue is equals to checkDigit
			//if it is not equals, new error message will be added to messagesContainer
			var lastDigit = idValue.charAt(idValue.length - 1) - '0';
			if (lastDigit != checkDigit) {
				return false;
			}
		}
	}
	return true;
}

function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

function trim( value ) {
	return LTrim(RTrim(value));
}

function isUnsignedInteger(s) {
	return (s.toString().search(/^[0-9]+$/) == 0);
}

function isInteger(s) {
	return (s.toString().search(/^-?[0-9]+$/) == 0);
}

function getCheckDigit(idValue) {
	var idLength = idValue.length;
	var sumDigits = 0;
	
	for (i = 0; i < idLength; i++) {
		//get currNum of idValue
		var currNum = idValue.charAt(i) - '0';
		if ((i + 1) % 2 == 0) {
			currNum *= 2;
			if (currNum >= 10) {
				currNum -= 9;
			}
		}
		sumDigits += currNum;
	}
	return (100 - sumDigits) % 10;
}
	
function convertToSingleSpaces(str) {
	return str.replace(/\s  /g, " ");
}

