/*
========================================================================================================
Creado por: César Tapia Mori
Fecha de creación: 08-Ago-2008
E-mail: cesar.tapia@e-sav.net
========================================================================================================
Key Codes
--------------------------------------------------------------------------------------------------------
	Tecla		Código		Tecla		Código		Tecla			Código	
	------------------------------------------------------------------------
	backspace		8		tab				9		enter				13	
	shift			16		ctrl			17		alt					18	
	pause/break		19		caps lock		20		escape				27	
	page up			33		page down		34		end					35	
	home			36		left arrow		37		up arrow			38	
	right arrow		39		down arrow		40		insert				45	
	delete			46		0				48		1					49	
	2				50		3				51		4					52	
	5				53		6				54		7					55	
	8				56		9				57		a					65	
	b				66		c				67		d					68	
	e				69		f				70		g					71	
	h				72		i				73		j					74	
	k				75		l				76		m					77	
	n				78		o				79		p					80	
	q				81		r				82		s					83	
	t				84		u				85		v					86	
	w				87		x				88		y					89	
	z				90		left window key	91		right window key	92	
	select key		93		numpad 0		96		numpad 1			97	
	numpad 2		98		numpad 3		99		numpad 4			100	
	numpad 5		101		numpad 6		102		numpad 7			103	
	numpad 8		104		numpad 9		105		multiply			106	
	add				107		subtract		109		decimal point		110	
	divide			111		f1				112		f2					113	
	f3				114		f4				115		f5					116	
	f6				117		f7				118		f8					119	
	f9				120		f10				121		f11					122	
	f12				123		num lock		144		scroll lock			145	
	semi-colon		186		equal sign		187		comma				188	
	dash			189		period			190		forward slash		191	
	grave accent	192		open bracket	219		back slash			220	
	close braket	221		single quote	222					
	
shortcut.add("Ctrl+Shift+h",function() {
    alert("Hola Mundo te dije");
});

anchor.setAttribute('onKeyPress','borraritem()');
anchor.getAttribute("onKeyPress")
evt.srcElement

*/

var emailRe = "/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/";
var phoneRe = "/^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/";

var nav4 = window.event ? true : false;

function onlyNum(evt){ 
// note: backspace = 8, enter = 13, '0' = 48, '9' = 57 
	var key = nav4 ? evt.which : evt.keyCode;
	if (key == 13 || (key >= 48 && key <= 57)){}
		else{nav4 ? evt.which = 0 : evt.keyCode = 0}
}

function onlyDecimal(evt){ 
// note: backspace = 8, enter = 13, '0' = 48, '9' = 57 
	var key = nav4 ? evt.which : evt.keyCode;
	var punto=0;
	if (key == 13 || (key >= 48 && key <= 57) || key == 46 ) {
			if (key == 46){
				cadena=(evt.srcElement).value
               	if(cadena.indexOf(".")!= -1){punto=1}
            	punto++;
               	if(punto>1){nav4 ? evt.which = 0 : evt.keyCode = 0}
			}
			
		}
		else{nav4 ? evt.which = 0 : evt.keyCode = 0}
}


function onlyTexto(evt,spacerOk){ 
// note: backspace = 8, enter = 13, 'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122 & /=47
	var key = nav4 ? evt.which : evt.keyCode;
	if (spacerOk==undefined) spacerOk = '';
	if (spacerOk=='spOk'){
		if (key == 13 || (key >= 65 && key <= 90) || (key >= 97 && key <= 122) || key == 32 || key==47 || key==58 || key==46){}
			else{nav4 ? evt.which = 0 : evt.keyCode = 0}
	}else{
		if (key == 13 || (key >= 65 && key <= 90) || (key >= 97 && key <= 122)  || key==47 || key==58 || key==46){}
			else{nav4 ? evt.which = 0 : evt.keyCode = 0}
	}
}

function onlyEmail(evt){ 
// note: backspace = 8, enter = 13, 'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122, '.' = 46, '@' = 64
	var key = nav4 ? evt.which : evt.keyCode;
	if (key == 13 || (key >= 48 && key <= 57) || (key >= 65 && key <= 90) || (key >= 97 && key <= 122) || key == 46 || key == 64 || key == 45 || key == 95){}
		else{nav4 ? evt.which = 0 : evt.keyCode = 0}
}

function onlyNumTexto(evt,spacerOk){ 
// note: backspace = 8, enter = 13, '0' = 48, '9' = 57 
// 'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122
	var key = nav4 ? evt.which : evt.keyCode;
	if (spacerOk==undefined) spacerOk = ''
	if (spacerOk=='spOk'){
		if (key == 13 || (key >= 48 && key <= 57) || (key >= 65 && key <= 90) || (key >= 97 && key <= 122) || key == 32 || key==58 || key==46){}
			else{nav4 ? evt.which = 0 : evt.keyCode = 0}
	}else{
		if (key == 13 || (key >= 48 && key <= 57) || (key >= 65 && key <= 90) || (key >= 97 && key <= 122) || key==58 || key==46){}
			else{nav4 ? evt.which = 0 : evt.keyCode = 0}
	}
}


function onlyNumTexto1(evt,spacerOk){ 
// note: backspace = 8, enter = 13, '0' = 48, '9' = 57 
// 'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122
	var key = nav4 ? evt.which : evt.keyCode;
	if (spacerOk==undefined) spacerOk = ''
	if (spacerOk=='spOk'){
		if (key == 13 || (key >= 48 && key <= 57) || (key >= 65 && key <= 90) || (key >= 97 && key <= 122) || key == 32 || key==58 || key==46){}
			else{nav4 ? evt.which = 0 : evt.keyCode = 0}
	}else{
		if (key == 13 || (key >= 48 && key <= 57) || (key >= 65 && key <= 90) || (key >= 97 && key <= 122)){}
			else{nav4 ? evt.which = 0 : evt.keyCode = 0}
	}
}


function textLogin(evt){ 
// note: backspace = 8, enter = 13, '0' = 48, '9' = 57 
// 'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122
	var key = nav4 ? evt.which : evt.keyCode;
	if (key == 13 || (key >= 48 && key <= 57) || (key >= 65 && key <= 90) || (key >= 97 && key <= 122) || key==58){}
		else{nav4 ? evt.which = 0 : evt.keyCode = 0}

}

// registra un evento de acuerdo a IE y Firefox(DOM)
function agregarEvento(elemento, nombre_evento, funcion, captura){
    // para IE
    if (elemento.attachEvent){
        elemento.attachEvent('on' + nombre_evento, funcion);
        return true;
    }else   // para navegadores respetan Estándares DOM(Firefox,safari)
        if (elemento.addEventListener){
            elemento.addEventListener(nombre_evento, funcion, captura);
            return true;
        }else
            return false;
}

function verfExp(campo,tipo)
 {
    
  switch (tipo)
  {
	case 'p':
		var checkOK = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ" + "abcdefghijklmnñopqrstuvwxyzáéíóú" + "0123456789";
		break;
	case 'a':
		var checkOK = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ" + "abcdefghijklmnñopqrstuvwxyzáéíóú " + "/";
		break;
	case 'n':
		var checkOK = "0123456789-";
		break;
	default:
		var checkOK = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ" + "abcdefghijklmnñopqrstuvwxyzáéíóú " + "0123456789" + "()_@-,./";
		break;
  }
  
  //var checkStr = new string;
  var checkStr = campo.value;
  var allValid = true; 
  longi=checkStr.length;
  for (i = 0; i < checkStr.length; i++) {
    ch = checkStr.charAt(i); 
    for (j = 0; j < checkOK.length; j++)
      if (ch == checkOK.charAt(j))
        break;
	 if (j == checkOK.length) { 
	  //campo.value=Left(campo.value,(longi-1))	
	  campo.value=clearChars(campo,ch);
      allValid = false; 
      break; 
    }
  }
  if (!allValid) { 
   // alert("Escriba sólo letras en el campo \"Nombre\"."); 
   // campo.focus(); 
    return (false); 
  }   
  return (true); 
}


function clearChars(campo,lt) {
	out = lt.toUpperCase(); // reemplazar esto
	add = ""; // por esto

	var entry=campo.value;
	temp = "" + entry;

	while ((temp.toUpperCase()).indexOf(out)>-1) 
	{
		pos= (temp.toUpperCase()).indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function verDec(campo,lt) {
	out = lt; // reemplazar esto
	add = ""; // por esto

	var entry=campo.value;
	temp = "" + entry;
	pnt = 0

	while (temp.indexOf(out)>-1) 
	{
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function verNeg(campo,lt) {
	out = lt; // reemplazar esto
	add = ""; // por esto
	var entry=campo.value;
	temp = "" + entry;
	temp = "" + (temp.substring(0, 0) + add + temp.substring((0 + out.length), temp.length));
	while (temp.indexOf(out)>-1)
	{
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

//Funciones de Texto
function trim(s){
	s = s.replace(/\s+/gi, ' '); //sacar espacios repetidos dejando solo uno
	s = s.replace(/^\s+|\s+$/gi, ''); //sacar espacios blanco principio y final
	return s;
}

String.prototype.trim = 
function(){ 
	return this.replace(/^\s+|\s+$/g,''); //sacar espacios blanco principio y final
	};
String.prototype.allTrim = 
function(){ 
	var str;
	str = this.replace(/\s+/gi, ' '); //sacar espacios repetidos dejando solo uno
	str = str.replace(/^\s+|\s+$/g,''); //sacar espacios blanco principio y final
	return str;
	};

function addLoadEvent(func) {    
	var oldonload = window.onload;    
	if (typeof window.onload != 'function') {
	        window.onload = func;    
	} else {
	        window.onload = function() {
	                    oldonload();
	                    func();
			}
	}
}
function hola(){alert('hola')}
function hola2(){alert('hol2a')}
function asignarEvents(){
	/*-----------------------------------------------------------------
	valExp
		* texto
		* stexto - sin espacion
		* numero
		* correo
		* moneda
		* login
	-----------------------------------------------------------------*/
	var _inputs    = document.getElementsByTagName("INPUT"); //-- Array de Objetos Input
	var _textareas = document.getElementsByTagName("TEXTAREA"); //-- Array de Objetos TextArea
	//var arrKeyPress = new Array()
	for(var i=0;i<_inputs.length;i++){
		//------------------------------------------------->
		//var _input, _blur, _newBlur, _keypress, _newKeypress, _valExp;
		_input = _inputs[i]
		_valExp = _input.valExp
		if (_valExp == undefined) _valExp = '';
		_valExp = _valExp.toLowerCase();
		_input.valExp = _valExp;
		//-------------------------------------------------<
		if (_input.type=='text' || _input.type=='password'){// || _input.type=='hidden'){
		//--------------------------------------------------------------------->
		//evento Blur (Fuera de Foco)
		//_blur = _input.onblur;
		var _sBlur = _input.onblur ? _input.onblur : ''
		_sBlur = _sBlur.toString().replace(/\n/g, "")
		//_sBlur = _sBlur.replace(/this.className='alpha40';/g,"")
		//_sBlur = _sBlur.replace(/this.className='alpha40'/g,"")
		_sBlur = _sBlur.replace(/{/g,"")
		_sBlur = _sBlur.replace(/}/g,"")
		_sBlur = _sBlur.replace('function anonymous()',"")
		_sBlur = _sBlur.replace('javascript:',"")
		//_sBlur = _sBlur.toString().replace(/this.id/g,"'"+_input.id+"'")
		//_sBlur = _sBlur.toString().replace('(this)',"(document.getElementById('"+_input.name+"'))")
		//if (_sBlur=='') {_sBlur = function(){}}
		//eval('var _blur'+i+'  =  '+_sBlur+';') 
		if (typeof _input.onblur != 'function'){
			_input.onblur = function(){
				this.value = clearChars(this,"'");
				this.value = clearChars(this,"´");
				this.value = clearChars(this,'+');
				//this.value = clearChars(this,"=");
				this.value = clearChars(this,"script");
				this.value = clearChars(this,"</");
				this.value = clearChars(this,'"');
				switch (this.valExp){
					case 'login':
						this.value = clearChars(this,"-");
						this.value = clearChars(this,"(");
						this.value = clearChars(this,")");
						this.value = clearChars(this,"^");
						this.value = clearChars(this,"[");
						this.value = clearChars(this,"]");
						this.value = clearChars(this,'[]');
						this.value = clearChars(this,'()');
						this.value = clearChars(this,'{}');
						this.value = clearChars(this,'}');
						this.value = clearChars(this,'{');
						this.value = clearChars(this,',');
						this.value = clearChars(this,';');
						break;
				}
			}
		}else{
			//"this.className='alpha40';
			eval("_input.onblur = function(){"+
				//"this.className='alpha40';"+
				_sBlur+";"+//"_blur"+i+"();"+
				"this.value = clearChars(this,'´');"+
				"this.value = clearChars(this,\"'\");"+ 
				"this.value = clearChars(this,'+');"+
				//"this.value = clearChars(this,'=');"+
				"this.value = clearChars(this,'script');"+
				"this.value = clearChars(this,'</');"+
				"this.value = clearChars(this,'"+'"'+"');"+
				"switch (this.valExp){"+
				"	case 'login':"+
				"		this.value = clearChars(this,'-');"+
				"		this.value = clearChars(this,'(');"+
				"		this.value = clearChars(this,')');"+
				"		this.value = clearChars(this,'^');"+
				"		this.value = clearChars(this,'[');"+
				"		this.value = clearChars(this,']');"+
				"		this.value = clearChars(this,'[]');"+
				"		this.value = clearChars(this,'()');"+
				"		this.value = clearChars(this,'{}');"+
				"		this.value = clearChars(this,'}');"+
				"		this.value = clearChars(this,'{');"+
				"		this.value = clearChars(this,',');"+
				"		this.value = clearChars(this,';');"+
				"		break;"+
				"	default:"+
				"		break;"+
				"}"+
			"}");
		}
		//---------------------------------------------------------------------<
		//--------------------------------------------------------------------->
		//evento KeyPress (Presionar Tecla)
		//_keypress = _input.onkeypress;
		var _sKeyPress = _input.onkeypress ? _input.onkeypress : ''
		_sKeyPress = _sKeyPress.toString().replace(/\n/g, "")
		_sKeyPress = _sKeyPress.replace(/{/g,"")
		_sKeyPress = _sKeyPress.replace(/}/g,"")
		_sKeyPress = _sKeyPress.replace('function anonymous()',"")
		_sKeyPress = _sKeyPress.replace('javascript:',"")
		//eval('var _keypress'+i+'  =  _input.onkeypress') 
		if (typeof _input.onkeypress != 'function'){
			_input.onkeypress = function() {
				switch (this.valExp){
					case 'texto':
						onlyNumTexto(event,'spOk');
						break;
					case 'stexto':
						onlyTexto(event,'spOk');
						break;
					case 'numero':
						onlyNum(event);
						break;
					case 'correo':
						onlyEmail(event);
						//return this.match(emailRe);
						break;
					case 'moneda':
						onlyDecimal(event);
						break;
				    case 'login':
						textLogin(event,'spNo');
						break;
					//default:
					//	onlyNumTexto(event,'spOk');
					//	break;
					}
				}
		}else{
			eval("_input.onkeypress = function() {"+
				_sKeyPress+";"+//"_keypress"+i+"();"+
				"switch (this.valExp){"+
				"	case 'texto':"+
				"		onlyNumTexto(event,'spOk');"+
				"		break;"+
				"	case 'stexto':"+
				"		onlyTexto(event,'spOk');"+
				"		break;"+
				"	case 'numero':"+
				"		onlyNum(event);"+
				"		break;"+
				"	case 'correo':"+
				"		onlyEmail(event);"+
				//"		return this.match(emailRe);"+
				"		break;"+
				"	case 'moneda':"+
				"		onlyDecimal(event);"+
				"		break;"+
				"	case 'login':"+
				"		textLogin(event,'spNo');"+
				"		break;"+
				//"	default:"+
				//"		onlyNumTexto(event,'spOk');"+
				//"		break;"+
				"}"+
				"}");
		}
		//---------------------------------------------------------------------<
		}
	}
	
	for(var i=0;i<_textareas.length;i++){
		//------------------------------------------------->
		//var _input, _blur, _newBlur, _keypress, _newKeypress, _valExp;
		_textarea = _textareas[i]
		_valExp = _textarea.valExp
		if (_valExp == undefined) _valExp = '';
		_valExp = _valExp.toLowerCase();
		_textarea.valExp = _valExp;
		//-------------------------------------------------<
		
		//--------------------------------------------------------------------->
		//evento Blur (Fuera de Foco)
		//_blur = _textarea.onblur;
		var _sBlurTa = _textarea.onblur ? _textarea.onblur : ''
		_sBlurTa = _sBlurTa.toString().replace(/\n/g, "")
		_sBlurTa = _sBlurTa.replace(/{/g,"")
		_sBlurTa = _sBlurTa.replace(/}/g,"")
		_sBlurTa = _sBlurTa.replace('function anonymous()',"")
		//if (_sBlurTa=='') {_sBlurTa = function(){}}
		//eval('var _blurTA'+i+'  =  '+_sBlurTa+';') 
		if (typeof _textarea.onblur != 'function'){
			_textarea.onblur = function(){
				this.value = clearChars(this,"'");
				this.value = clearChars(this,"´");
				this.value = clearChars(this,'+');
				this.value = clearChars(this,'=');
				this.value = clearChars(this,'"');
				this.value = clearChars(this,"script");
				this.value = clearChars(this,"</");
			}
		}else{//"this.className='alpha40';
			eval("_textarea.onblur = function(){"+
				//"this.className='alpha40';"+
				_sBlurTa+";"+//"_blurTA"+i+"();"+
				"this.value = clearChars(this,'´');"+
				"this.value = clearChars(this,\"'\");"+ 
				"this.value = clearChars(this,'+');"+
				"this.value = clearChars(this,'=');"+
				"this.value = clearChars(this,'\"'\);"+
				"this.value = clearChars(this,'script');"+
				"this.value = clearChars(this,'</');"+
			"}");
		}
		//---------------------------------------------------------------------<
		//--------------------------------------------------------------------->
		//evento KeyPress (Presionar Tecla)
		//_keypress = _textarea.onkeypress;
		var _sKeyPressTa = _textarea.onkeypress ? _textarea.onkeypress : ''
		_sKeyPressTa = _sKeyPressTa.toString().replace(/\n/g, "")
		_sKeyPressTa = _sKeyPressTa.replace(/{/g,"")
		_sKeyPressTa = _sKeyPressTa.replace(/}/g,"")
		_sKeyPressTa = _sKeyPressTa.replace('function anonymous()',"")
		_sKeyPressTa = _sKeyPressTa.replace('javascript:',"")
		//eval('var _keypressTA'+i+'  =  _textarea.onkeypress') 
		if (typeof _textarea.onkeypress != 'function'){
			_textarea.onkeypress = function() {
				switch (this.valExp){
					case 'texto':
						onlyNumTexto(event,'spOk');
						break;
					default:
						onlyNumTexto(event,'spOk');
						break;
					}
				}
		}else{
			eval("_textarea.onkeypress = function() {"+
				_sKeyPressTa+";"+//"_keypressTA"+i+"();"+
				"switch (this.valExp){"+
				"	case 'texto':"+
				"		onlyNumTexto(event,'spOk');"+
				"		break;"+
				"	default:"+
				"		onlyNumTexto(event,'spOk');"+
				"		break;"+
				"}"+
				"}");
		}
		//---------------------------------------------------------------------<
	}
}

function right(e) {
	if (navigator.appName == 'Netscape' && e.which == 3) {
		return false;
	}else{
		if (navigator.appName == 'Microsoft Internet Explorer' && event.button==2) {
			return false;
			}
		return true;
	}
}
//agregarEvento(document,'onmousedown',right,false);  
//addLoadEvent(asignarEvents)
agregarEvento(window,'load',asignarEvents,false);

