
// VALIDACION DE FORMULARIOS 

/* Codifica Parametro Para Valores */
function URLEncodeValue(obj)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = obj;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for
	return encoded;
}

/* NAVEGADOR DEL USUARIO */
function getNav(){
	var browser=navigator.appName;
	var b_version=navigator.appVersion;
	if(browser=="Netscape"){
		return "mozilla";
	}else if(browser=="Microsoft Internet Explorer"){
		return "ie";
	}else{
		return "none";
	}
}

/* FUNCIONES VALIDACIÓN FORMULARIOS */

var whitespace = " \t\n\r";
var reWhitespace = /^\s+$/

/** Verifica que no este vacio **/
function isEmpty(s){
	return ((s == null) || (s.length == 0)) 
}
 
/*** Verifica que no sean espacios en blanco o vacio ***/
function isWhitespace (s){
    return (isEmpty(s) || reWhitespace.test(s));
}
 
/*** corta espacios en blanco al principio y al final de una variable ***/
function trimAll(sString) 
{
    while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	};
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

/*** Valida un email mediante expresiones regulares ***/
function validarEmail(valor) {
        if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(valor)){
                return false;
        } else {
                return true;
        }
}
function isEmail(s){
	return (isWhitespace(s) || validarEmail(s));
}

// Parseador de textos normarles a url
function parseToURL(str){
	str=str.replace(/á/ig,'a');
	str=str.replace(/à/ig,'a');
	str=str.replace(/â/ig,'a');
	str=str.replace(/é/ig,'e');
	str=str.replace(/è/ig,'e');
	str=str.replace(/ê/ig,'e');
	str=str.replace(/ë/ig,'e');
	str=str.replace(/í/ig,'i');
	str=str.replace(/ì/ig,'i');
	str=str.replace(/î/ig,'i');
	str=str.replace(/ï/ig,'i');
	str=str.replace(/ó/ig,'o');
	str=str.replace(/ò/ig,'o');
	str=str.replace(/ö/ig,'o');
	str=str.replace(/ô/ig,'o');
	str=str.replace(/ú/ig,'u');
	str=str.replace(/ù/ig,'u');
	str=str.replace(/ü/ig,'u');
	str=str.replace(/û/ig,'u');
	str=str.replace(/&/ig,'and');
	str=str.replace(/ñ/ig,'n');
	str=str.replace(/ /ig,'-');
	str=str.replace(/·/ig,'-');
	var noChar = new Array('\'','?','¿','!','¡',':','"','(',')','[',']','=','%','$','#','@','|','{','}','¬','~','*',';',',','.',',','[',']','^','´','`','¨','+',';','<','>','~','\\','ª','º');
	for(i=0;i<noChar.length;i++){
		for (j=0;j<str.length;j++)
		{
			str=str.replace(noChar[i],'');
		}
	}
	str=str.replace(/--/ig,'-');
	str=str.toLowerCase();
	return str;
}

// FUNCION QUE MUESTRA U OCULTA ELEMENTOS DE LAS TABLAS
function setDisplay (id_in, id_out, value_in, value_out){
	document.getElementById(id_out).style.display=value_out;
	document.getElementById(id_in).style.display=value_in;
	return true;
}

function openPopUp(url,width,height,param){
	if(!width){ width = 100; }
	if(!height){ height = 100; }
	var top = (screen.height - height) / 2;
	var left = (screen.width - width) / 2;
	window.open(url,'popup','height='+height+',width='+width+',top='+top+', left='+left+','+param);
}

// devuelve el elemento clickado
function whichElement(e)
{
var targ;
if (!e) {
  var e=window.event;
  }
if (e.target)
  {
  targ=e.target;
  }
else if (e.srcElement)
  {
  targ=e.srcElement;
  }
if (targ.nodeType==3) // defeat Safari bug
  {
  targ = targ.parentNode;
  }

 return targ;
}
