function closeWindow(){
    window.opener.location.href = window.opener.location.href;
    window.close();
}

function lTrim(sStr){
    while (sStr.charAt(0) == " ")
        sStr = sStr.substr(1, sStr.length - 1);
        return sStr;
}

function rTrim(sStr){
    while (sStr.charAt(sStr.length - 1) == " ")
        sStr = sStr.substr(0, sStr.length - 1);
        return sStr;
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function isBlank(strValue)
{
    return strValue.trim() == "";
}

function allTrim(sStr){
    return rTrim(lTrim(sStr));
}

// Funciones en Cliente

function validar(){
	for (i=0; i<validar.arguments[0].length; i++){ 
		ok = ValidaCampo(validar.arguments[0][i],validar.arguments[1][i],validar.arguments[2][i],validar.arguments[3][i],validar.arguments[4])
		if (!ok) break 
	}
	return(ok);
}

function validateEmail(email)
{
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
	return false;
}

function ValidaCampo(){ // Nombre de campo, Tipo 0=>txt 1=>num 2=>fecha 3=>jpg/gif 4=>e-mail 5=>Combo 6=>Video, Obligatorio? (boolean)
	var error = "";
	frm = eval(document.forms[ValidaCampo.arguments[4]]) 

	var idForm = ValidaCampo.arguments[4];
	if (document.all) frm=document.all[idForm];
	else if (document.getElementById) frm = document.getElementById(idForm);
	else if (document.layers) frm = document.forms[idForm];

	var valor = frm[ValidaCampo.arguments[0]].value + "";
	valor = valor.replace(/(\s+)/,"") // Eliminamos blancos al inicio de la cadena		
	if (!ValidaCampo.arguments[2] && valor.length == 0) // Comprobamos nulos
		error = "The field " + ValidaCampo.arguments[3] + " is required" ;
	else if (ValidaCampo.arguments[1] == 0 && valor.length > 0){
	}
	else if (ValidaCampo.arguments[1] == 1 && valor.length != 0 && valor + "" != parseFloat(valor) + "") // Comprobamos numerico
		error = "The field " + ValidaCampo.arguments[3] + " has to be a number";
	else if (ValidaCampo.arguments[1] == 2 && isNaN(Date.parse(valor))) // Comprobamos fecha
		error = "The field " + ValidaCampo.arguments[3] + " is a wrong date";
	else if (ValidaCampo.arguments[1] == 3 && valor.length > 0){
			re1 = /(GIF)$/
			re2 = /(JPG)$/	
			valor = valor.toUpperCase();
			if (!re1.test(valor) && !re2.test(valor))
				error = "The file " + ValidaCampo.arguments[3] + " has to be GIF or JPG";
	}
	else if (ValidaCampo.arguments[1] == 4 && valor.length > 0){
			if (!validateEmail(valor))
				error = "The field " + ValidaCampo.arguments[3] + " is not correct";
		}
	else if (ValidaCampo.arguments[1] == 5 && (frm[ValidaCampo.arguments[0]].selectedIndex < 0 || frm[ValidaCampo.arguments[0]].options[frm[ValidaCampo.arguments[0]].selectedIndex].value < 0))
		error = "The field " + ValidaCampo.arguments[3] + " has to be selected";
	else if (ValidaCampo.arguments[1] == 6 && valor.length > 0){
			re1 = /(MPG)$/
			re2 = /(AVI)$/	
			valor = valor.toUpperCase();
			if (!re1.test(valor) && !re2.test(valor))
				error = "The file " + ValidaCampo.arguments[3] + " has to be MPG or AVI";
		}
	if (error == "")
		return(true);
	else{
		alert(error);
		frm[ValidaCampo.arguments[0]].focus();	
		return(false);
	}
}
	

//Funcíón que elimina el contenido de textareas y cajas de texto
function clearForm(formulario){
	var formu;
	if (document.all) formu = eval("document.all."+formulario);
	else if (document.layers) formu = document.forms[formulario];
	else if (document.getElementById) formu = document.getElementById(formulario);
	for (var i=0; i<formu.elements.length;i++){
		var tempRef = formu.elements[i];
		if (tempRef.type=="text" || tempRef.type=="textarea") tempRef.value="";
	}
}

function restaurarContenido(contenido,Formu){
	Formu.value=contenido;
}
