var numericChars = "0123456789";

var lettersEx = "-0123456789!@#$%^&*()_+{}[]';:\/.,<>?|=/`~¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹»¼½¾¿ʻʼʽʾʿˀˁ˂˃˄˅ˆˇˈˉˊˋˌˍˎˏːˑ˒˓˔˕˖˗˘˙˛˜˝˞˟ˠˡˢˣˤ˥˦˧˨˩˪˫ˬ˭ˮ˯˰˱˲˴˳˴˵˶˷˸˹˺˻˼˽˾˿̀́̂̃̄̅̆̇̀́";
var lettersNoEx = "-!@#$%^&*()_+{}[]';:\/.,<>?|=/`~¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹»¼½¾¿ʻʼʽʾʿˀˁ˂˃˄˅ˆˇˈˉˊˋˌˍˎˏːˑ˒˓˔˕˖˗˘˙˛˜˝˞˟ˠˡˢˣˤ˥˦˧˨˩˪˫ˬ˭ˮ˯˰˱˲˴˳˴";
var mHexa = " 0123456789ABCDEF";

//types 
var MASK_ALL						= 0 ;
var MASK_NUMERIC				= 1;
var MASK_LETTERS				= 2;
var MASK_LETTERS_NUMBERS = 3;
var MASK_IP4						= 4;
var MASK_IP6						= 6;
var MASK_HEXA					=7;

HTMLInputElement.prototype.lowercase = false;
HTMLInputElement.prototype.mask = MASK_ALL;
HTMLInputElement.prototype.uppercase = false;
HTMLInputElement.prototype.upperLetter = false;
HTMLInputElement.prototype.upperLetterAfterSpace = false;
HTMLInputElement.prototype._lastchar = null;
HTMLInputElement.prototype._lastvalue = null;
HTMLInputElement.prototype.regex = null;
HTMLInputElement.prototype.regexResult = 0;
HTMLInputElement.prototype.checkRegex = i_checkRegex;
HTMLInputElement.prototype.checkRequired = i_checkRequiredObj;
HTMLInputElement.prototype.checkMask = i_checkMask;
HTMLInputElement.prototype._defaultValue = null;
HTMLInputElement.prototype.maskName = null;
HTMLInputElement.prototype._pastedHTML = null;
HTMLInputElement.prototype.required = false;
HTMLInputElement.prototype.regexFailedText = "ThisIsNotACorrectValue";
HTMLInputElement.prototype.modifed = false;



function installIE(){
	var inputs = document.getElementsByTagName("INPUT");
	for (var i=0;i< inputs.length; i++){
		var _myinput = inputs[i];
		
			if (_myinput.type == "text" &&  typeof (_myinput.extra) == "undefined"){
				
				if (_myinput.required == true){
					
				}	
			//_myinput.style.fontFamily = "Lucida Console,Lucida Sans Unicode";
			 _myinput.maxLength = 255;	
	
			if (_myinput.maskName == MASK_IP4){
					_myinput.mask = "999.999.999.999";
					 i_normalizeValue(_myinput); 
					_myinput.maxLength = 15;
			}
			if (_myinput.maskName == MASK_IP6){
					_myinput.mask = "HHHH:HHHH:HHHH:HHHH:HHHH:HHHH:HHHH:HHHH";
					
			}
			if (_myinput.mask == MASK_HEXA) _myinput.uppercase = true;
			if ("01234567".indexOf(_myinput.mask)==-1){
				_myinput.value = i_normalizeMask(_myinput.mask);
			}
			
			_myinput.onselect = function(evt){
					if (document.selection){
						var rang = document.selection.createRange();
						var trang = rang.duplicate();
						var myselection = trang.htmlText;
						
						var myvalue = this.value;
						var sstart = myvalue.indexOf(myselection);
						var sstop = myselection.length;
						this._selection = true;
						this._myselection = myselection;
						this._sstart = sstart;
						this._sstop = sstop;
					}else{
						var myselection = this.value.substring(this.selectionStart,this.selectionEnd);
						this._myselection = myselection;	
						this._sstart = this.selectionStart;
						this._sstop = this.selectionEnd-this.selectionStart;
						this._selection = true;
						this.modified = true;
					
					}
				
			}

			_myinput.onblur = function(evt){
				this.checkRegex();
			}

			
			_myinput.onkeyup1 = function(evt){
				this.modified = true;
					if (this._paste && this.checkMask() && !i_isFF){
						this.value = this._pastedHTML;	
						
					}

					if (this._paste && !this.checkMask()){
						if (this._defaultValue != null){
							this.value = this._defaultValue;
							this._defaultValue = null;
						}
					}
					this._paste = false;				
					//this.checkRegex();

					try{
						_myinput.onkey();
					}catch(e){}

						try{
						this._onkeyup(evt);
					}catch(e){}
			}
			

			
			_myinput.onbeforepaste = function(evt){
				this._allowPaste = true;
			}

			_myinput.onkeydown1 = function(evt){
				
				try{ if (event) { evt = event; } }catch(x){}
					var mykey = evt.keyCode;
					this._lkey = 0;
					
					if (mykey == 13) {
						this.checkRegex();
						return true;
					}
					
					if ( (this._lastchar == 93 || this._lastchar == 91) && (mykey == 86 || mykey == 67))	{
						return true;
					}
					
					if (this._allowPaste){
						this._allowPaste = false;
						return true;
					}

					if (evt.ctrlKey){
						if ( (mykey == 86 || mykey == 67)){
							return true;
							
						}else{
						}
					}

					
					this._lastchar = mykey;
						if (this._selection)	{
								if (is_accepted(mykey) ) {
									i_clearSelection();
									var _myt = this.value;
									_myt = i_replaceStr(_myt,this._myselection,"");
									this.value = _myt;
									i_setCaretPos(this,this._sstart);
									this._selection = false;
								}
					}
					
					var cpos = i_getCaretPos(this);
					
					var _selection = this._selection;
						
					if (mykey == 46 && !_selection && this.mask != MASK_ALL){
						try{
							var _mychar = this.mask.substring(cpos,cpos+1);							
							if (isAcceptedMask(_mychar)){
							this.value = this.value.substring(0,cpos)+" "+this.value.substring(cpos+1);
							i_setCaretPos(this,cpos+1);
							}
						return false;
						}catch(e){}
					}

					if (mykey == 8 && !_selection && this.mask != MASK_ALL) {
						try{
						if (cpos >0 && this.mask != 0){
							var _mychar = this.mask.substring(cpos-1,cpos);
							if (isAcceptedMask(_mychar)){
							this.value = this.value.substring(0,cpos-1)+" "+this.value.substring(cpos);
							i_setCaretPos(this,cpos-1);
						}
						return false;
					}
					}catch(e){}
			
					}
					
					try{
						this._onkeydown(evt);
					}catch(e){}
						//this.checkRegex();
			}

			_myinput.onkeypress1 = function(evt){
					try{ if (event) { evt = event; } }catch(x){}
					var mykey = evt.keyCode;
					if (i_isFF) mykey = evt.charCode;
					var cpos = i_getCaretPos(this);
				
					if (mykey == 13){
						this.checkRegex();
						return true;
					}
					if (this._allowPaste){
						this._allowPaste = false;
						return true;
					}
						if ( (this._lastchar == 93 || this._lastchar == 91) && (mykey == 86 || mykey == 67))	{
						return true;
					}
					

					if (evt.ctrlKey && (mykey == 118 || mykey == 99)){
						return true;
					}
				
					
					if (this.value.length>this.maxLength){
						return false;
					}
					
					var isValid =  false;
					var _mask = this.mask;
					
					this._lastchar = mykey;

					if ("01234567".indexOf(_mask)==-1){
						var _sm = _mask.substring(cpos,cpos+1);
						if (cpos+1 > _mask.length){
							_sm = _mask.substring(_mask.length-1);
							if (_sm != "Z") _sm = "";
						}
					
						if (_sm == "H"){
							_mask = MASK_HEXA;
							this.uppercase = true;
						}else
						if (_sm =="9"){
							_mask = MASK_NUMERIC;
						}else
						if (_sm == "A"){
							_mask = MASK_LETTERS;
						}else
						if (_sm == "Z")	{
							_mask = MASK_LETTERS_NUMBERS;
						}
					}

					if (this.mask == MASK_ALL){
						isValid = true;
					}else{
						isValid = i_validateKey(mykey,_mask,this.uppercase);
					}
				
				if (isValid && mykey > 0){
						var mychar = String.fromCharCode(mykey);
						if (this.uppercase) mychar = mychar.toUpperCase();
						if (this.lowercase) mychar = mychar.toLowerCase();
						if (this.upperLetter && cpos == 0 ) mychar = mychar.toUpperCase();
						if (this.upperLetterAfterSpace && this.value.substring(0,cpos).endsWith(" ")) mychar = mychar.toUpperCase();

						this.value = this.value.substring(0,cpos)+mychar+this.value.substring(cpos);
						i_setCaretPos(this,cpos+1);
				}
				
				var newPos = this.value.indexOf(String.fromCharCode(mykey),cpos);

				if (!isValid && newPos>-1){
						i_setCaretPos(this,newPos+1);
				}
				if (mykey == 0) return true;

				//this.checkRegex();	
	try{
						this._onkeypress(evt);
					}catch(e){}
				return false;
			}
		}//if
	}
}


function i_checkMask(){
	var _mask = this.mask;
	if (_mask == MASK_ALL) return true;
	if (this.maskName == MASK_IP6){
			//reformating the string

	}
	var _myvalue = this.value;
	if (this._pastedHTML != null){
		_myvalue = this._pastedHTML;
	}
	for (var i=0;i<_myvalue.length ;i++){
		var mc = _myvalue.substring(i,i+1);
			var cpos = i;
					if ("01234567".indexOf(this.mask)==-1){
						var _sm = this.mask.substring(cpos,cpos+1);
					
						if (_sm == "H"){
							_mask = MASK_HEXA;
							this.uppercase = true;
						}else
						if (_sm =="9"){
							_mask = MASK_NUMERIC;
						}else
						if (_sm == "A"){
							_mask = MASK_LETTERS;
						}else
						if (_sm == "Z")	{
							_mask = MASK_LETTERS_NUMBERS;
						}else{
							_mask = _sm;
						}
					}
				
				if (! i_validateChar(mc,_mask)) return false;

	}
	this._defaultValue = this._pastedHTML;
	//this._pastedHTML = null;
	return true;
}

function i_checkRequiredObj(){
	if (this.required && this.value.length == 0){
		i_hideToolTip();
		i_showToolTip(this,"required field");
		
	}
}

function i_checkRegex(){
	if (this.value.length == 0) {
		this.regexResult = 0;
		return;
	}
		if (this.regex != null){
					var re = new RegExp(this.regex);
					if (this.value.match(re)){
							this.regexResult = 1;
					}else{
							this.regexResult = -1; 
					}
				}
				
					if (this.regexResult == -1){
							i_showToolTip(this,this.regexFailedText);
							//this.focus();
					}else{
						i_hideToolTip();
					}
		
}


function i_validateKey(key , mask){
	var mychar = String.fromCharCode(key);
	return i_validateChar(mychar,mask);

}


function i_validateChar(mychar,mask){
	if (mask == MASK_NUMERIC  && numericChars.indexOf(mychar)>=0) {
		return true;
	}else
	if (mask == MASK_LETTERS && lettersEx.toLowerCase().indexOf(mychar.toLowerCase())==-1){
		return true;
	}else
   if (mask == MASK_LETTERS_NUMBERS && lettersNoEx.toLowerCase().indexOf(mychar.toLowerCase())==-1){
		return true;
	}else
	if (mask == MASK_HEXA && mHexa.toLowerCase().indexOf(mychar.toLowerCase())>-1) return true;
	else
	if (mask == mychar){
		return true;
	}
	return false;

}


function _getDotDistance(cpos,value){
		var _mystr = value.substring(0,cpos);
		if (value.lastIndexOf(".") != value.indexOf(".")) _mystr = _mystr.substring(_mystr.lastIndexOf("."));
		var dp = -1;
		if (_mystr.indexOf(".")==-1) _mystr = "."+_mystr;
		dp = _mystr.length; if (dp == 0) dp =1;
		return dp;
}

function i_normalizeIP(v){
	v = i_replaceStr(v," ","");
	return v;
}

function i_normalizeMask(m){
	m = i_replaceStr(m,"H"," ");
	m = i_replaceStr(m,"9"," ");
	m = i_replaceStr(m,"A"," ");
	m = i_replaceStr(m,"Z"," ");
	return m;
}


function i_normalizeMaskedValue(v){
	v = i_replaceStr(v,"_"," ");
	return v;
}

function i_normalizeValue(obj){
	if (obj.value.length == 0){
		obj.value = "   .   .   .   ";
	}else{
		var k=0;
		var group = "";
		var nv = "";
		var _myvalue = obj.value;
		_myvalue = _myvalue + ".";
		while (k<_myvalue.length){
			var _chr = _myvalue.substring(k,k+1);
			group+=_chr;
			if (_chr == "."){
					if (group.length <4){
						for (var i=0;i<4-group.length+1 ;i++ ){
							group = " "+group;
						}
					}
					nv+=group;
					group = "";
			}
			k++;
		}
		nv = nv.substring(0,nv.length -1);
		
		obj.value = nv;
	}//else
}

function isAcceptedMask(m){
	if ("H9A".indexOf(m)>-1){
			return true;
	}

	return false;
}

function is_accepted(key){
	var allc = new Array(9,18,16,17);
	var allc_mac = new Array(9,18,16,93,91);
	
	if (is_mac){
		if (allc_mac.contains(key)) return false;
	}else
	if (allc.contains(key)) return false;
	return true;
}
