function recommenderEmailRepetido (id)
{
	var valor_actual = $('email'+id).value; 
	var valor_item;
	var texto = new Array();
	texto =	dametextos();

	// hack
	if (valor_actual == "")	{	return true; }

	for(i=1; i < 6; i++ )
	{
		valor_item = $('email'+i).value;
		if (i != id && valor_item == valor_actual ) {
		
			msg = texto['email_recommender_existe'];
			alert(texto['form_error']+'\n'+msg);
			$('email'+id).focus ();		
			return false;
		}
	}

	return true;
}

function capaEstados (valor)
{
	if( valor == 7 ) {
		mostrarDIV("capa_interview");
	} else {
		ocultarDIV("capa_interview");
	}

	if (valor == 10 ) {
		mostrarDIV("capa_final");
	} else {
		ocultarDIV("capa_final");
	}

	if (valor == 11 ) {
		mostrarDIV("capa_respuesta");
	} else {
		ocultarDIV("capa_respuesta");
	}
}

function selectedCombo(obj1)
{
	$(obj1).disabled = false; 
	var option =  $(obj1).options;
 	for(i=0; i< option.length; i++ )
 	{
		option[i].selected = true;
	}
}
 
function delArea(obj1)
{
	if(confirm(" Are you sure you want to delete this item? ")) {

		// Intentamos borrar el área
		if  ($(obj1).selectedIndex == -1 ) {
			alert ("It has not selected any option"); return false;
		}

		var index=$(obj1).selectedIndex;
		$(obj1).options[index] = null;
	}
}

function saveArea(obj, obj1, obj2)
{
	if  ($(obj1).selectedIndex == -1 ) {
		alert ("It has not selected any option");
		return false;
	}
	
    var option2 =  $(obj2).options;
	if (option2.length >= 4) {
		alert ("There can choose more than 4 options");
		return false;
	}
	
	var valor=$(obj1).value;
	for(i=0; i< option2.length; i++ )
	{
		if (option2[i].value == valor ) {
			alert ("Already select this option");
			return false;
		}
	}
	var index = $(obj1).selectedIndex; 
	var index = document.getElementById (obj1).selectedIndex; 
	var indexArea = $(obj).selectedIndex; 
	var texto = $(obj).options[indexArea].text +" => "+$(obj1).options[index].text;
	var item = $(obj1).options[index].value;
	opt = new Option(texto, item);
	option2[option2.length] = opt; 
}

// *** Agregar un input para upload a file 
function agregarArchivo (div, name)
{
	siguiente = 1 + parseInt ( $("count_"+ name).value ) ;
	var nuevoINPUT = document.createElement("input");
	nuevoINPUT.setAttribute("id", "file");
	nuevoINPUT.setAttribute("type", "file");
	nuevoINPUT.setAttribute("alt", "File");
	nuevoINPUT.setAttribute("class", "in_largo");
	nuevoINPUT.setAttribute("name", name + ""+ siguiente );
	$(div).appendChild(nuevoINPUT);
	$("count_"+ name).value = siguiente;
}

function backerror()	{ return "#EBEBEB"; }
function backok()	{ return "#FFFFFF"; }

//***
function checkSubmitUser (id)
{
	var texto = new Array();
	texto =	dametextos();

	// checkSubmit original + passwords iguales
	if ($('password').value != $('confirm_password').value ){

		msg = texto['password_nocoincide'];
		alert(texto['form_error']+'\n'+msg);
		$('password').focus ();
		return false;

	} else {
	/*
		// expresion para control de password segura
		//var regexpr = /^(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,32})$/;
		//if($('password').value != "" && !regexpr.test($('password').value)) {

		//	msg = texto['password_noseguro'];
		//	alert(texto['form_error']+'\n'+msg);
		//	$('password').focus ();
		//	return false;
		//}
*/
		// listo, grabamos
		return true;
	}

	// che ... ke mal, no grabamos nada
	return false;
}

function checkFormSubmitApplyAdmin(id)
{
	selectedCombo('groups'); 
	var texto = new Array();
	texto =	dametextos();

	// checkSubmit original + passwords iguales
	//idStatusAplicaciones
	/*status0 = checkFormSubmitWarning (id);
	if (status0) // es true ta completo sino no
	 { $("idStatusAplicaciones").value = "2";	 }
	else
	 { $("idStatusAplicaciones").value = "1";	 }*/
	
	status1 = checkSubmitUser (id);
	return ( status1 && status2 ) ;
}

function checkFormSubmitApply (id)
{
	selectedCombo('groups'); 
	var texto = new Array();
	texto =	dametextos();

	// checkSubmit original + passwords iguales
	//idStatusAplicaciones
	//status0 = checkFormSubmitWarning (id);
	status0 = false;
	if (status0) // es true ta completo sino no
	{ 
		$("idStatusAplicaciones").value = "2";
	} else {
		$("idStatusAplicaciones").value = "1";
	}

	status1 = checkSubmitUser (id);
	status2 = checkFormSubmit (id);
	return ( status1 && status2 ) ;
}

//***
//  checkForm con submit
//*** 
function checkFormSubmit (id) 
{  	   // ponemos error en false porque no hay errores actualmente.
		var error = false;
		var texto = new Array();
		texto =	dametextos();
		var msg='';

		// salvemos el contenido de los Editores TinyMCE 
//		if (tinyMCE != "noeditor")	tinyMCE.triggerSave();

		// veamos los nodos con editor
		tinyMCEsaved = false;
		$$('form#'+id+' .editor').each(function(node)
			{
				//alert ("Antes: " + node.value);	
				if (tinyMCEsaved == false) {
					tinyMCE.triggerSave();
					tinyMCEsaved = true;
				}
			//	alert ("Despues: " + node.value);
		});
			
		// comenzamos recorriendo los campos con marca "requerido"
		$$('form#'+id+' .requerido').each(function(node)
			{
				// si un (NODO=miembro que cumple la condicion de arriba para prototype) es vacio
				// o sea un "requerido" sin rellenar hacemos lo siguiente
//				alert ("nombre: " + node.alt + " Tipo: " + node.type + " Valor: " + node.value);
				if (node.value == "") {
					// si error todavia es false o sea que todavia no hay error ponemos el foco en el primer campo a arreglar
					if(!error) node.focus();
					// ahora si ponemos error en true (despues de poner el foco)
					error = true;
					// y modificamos el background de ese campo en el form
					node.style.background = backerror();
					// seteamos el mensaje de error
					if (node.type == "text" || node.type == "password") {
						tit=node.alt;
					}else {
						tit=node.title;
					}
					msg+=tit+texto['requerido']+"\n";
	           	} else { 
					// ponemos el back en el color original. (esto es por si es la segunda vez que entra y antes tuvo error)
					node.style.background = backok();
				}
		});

		// comenzamos recorriendo los campos con marca "email"
        $$('form#'+id+' .email').each(function(node)
			{
				// expresion para control de emails
				var regexpr = /^([a-zA-Z0-9_.+-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,6})+$/;
				// si un (NODO=miembro que cumple la condicion de arriba para prototype) es vacio
				// o sea un "email" sin rellenar hacemos lo siguiente
				if (node.value != "") {
					if(!regexpr.test(node.value)) {
						// si error todavia es false o sea que todavia no hay error ponemos el foco en el primer campo a arreglar
						if(!error) node.focus();
						// ahora si ponemos error en true (despues de poner el foco)
						error = true;
						// y modificamos el background de ese campo en el form
						node.style.background = backerror();
						tit=node.alt;
						msg+=tit+texto['email_invalido']+"\n";
	
					} else { 
						// ponemos el back en el color original. (esto es por si es la segunda vez que entra y antes tuvo error)
						node.style.background = backok();
					}
				}
        });											   

		// comenzamos recorriendo los campos con marca "website"
        $$('form#'+id+' .url').each(function(node)
			{
				// expresion para control de emails
//				var regexpr = /^([a-zA-Z0-9_.+-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,6})+$/;
				var regexpr = /^http:\/\/(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,6})+$/;
				// si un (NODO=miembro que cumple la condicion de arriba para prototype) es vacio
				// o sea un "email" sin rellenar hacemos lo siguiente
				if(!regexpr.test(node.value) && (node.value!="")) {
				// si error todavia es false o sea que todavia no hay error ponemos el foco en el primer campo a arreglar
				if(!error) node.focus();
				// ahora si ponemos error en true (despues de poner el foco)
                error = true;
				// y modificamos el background de ese campo en el form
				node.style.background = backerror();
				tit=node.alt;
				msg+=tit+texto['url_invalido']+"\n";
            } else { 
				// ponemos el back en el color original. (esto es por si es la segunda vez que entra y antes tuvo error)
				node.style.background = backok(); }
        });											   
		
    	// comenzamos recorriendo los campos con marca "numerico"
	  	$$('form#'+id+' .numerico').each(function(node)
			{
            	var strChars = "0123456789.-";
            	for (i = 0; i < node.value.length; i++) 
					{
	    	            strChar = node.value.charAt(i);
    	    	        if (strChars.indexOf(strChar) == -1) 
							{
								if(!error) node.focus();
            			        error = true;
                    			node.style.background = backerror();
								tit=node.alt;
								msg+=tit+texto['numerico']+"\n";
		          			} else { 
								// ponemos el back en el color original. (esto es por si es la segunda vez que entra y antes tuvo error)
								node.style.background = backok(); 
							}
            		}
        	});
	
			// tip
			//alert ("Texto: " + $('texto').value);

		// al finalizar si hay error
        if (error == true) {
			alert(texto['form_error']+'\n'+msg);
			// y vuelve al form
			return false;
        } else {
			return true;
//			document.id.submit();
			/*var rForm = new Ajax.Request("ajax_Index.php?id="+id, {
			onCreate: waiting,
			onComplete: procesaRegistro,
			parameters: $(id).serialize()
			});*/
        }
    
}

function checkFormSubmitWarning (id) 
{  	   // ponemos error en false porque no hay errores actualmente.
		var error = false;
		var texto = new Array();
		texto =	dametextos();
		var msg='';

		// salvemos el contenido de los Editores TinyMCE 
//		if (tinyMCE != "noeditor")	tinyMCE.triggerSave();

		// veamos los nodos con editor
		tinyMCEsaved = false;
		$$('form#'+id+' .editor').each(function(node)
			{
				//alert ("Antes: " + node.value);	
				if (tinyMCEsaved == false) {
					tinyMCE.triggerSave();
					tinyMCEsaved = true;
				}
			//	alert ("Despues: " + node.value);
		});
			
		// comenzamos recorriendo los campos con marca "requerido"
		$$('form#'+id+' .warning').each(function(node)
			{
				// si un (NODO=miembro que cumple la condicion de arriba para prototype) es vacio
				// o sea un "requerido" sin rellenar hacemos lo siguiente
//				alert ("nombre: " + node.alt + " Tipo: " + node.type + " Valor: " + node.value);
				if (node.value == "") {
					// si error todavia es false o sea que todavia no hay error ponemos el foco en el primer campo a arreglar
					if(!error) node.focus();
					// ahora si ponemos error en true (despues de poner el foco)
					error = true;
					// y modificamos el background de ese campo en el form
					node.style.background = backerror();
					// seteamos el mensaje de error
					if (node.type == "text" || node.type == "password") {
						tit=node.alt;
					}else {
						tit=node.title;
					}
					msg+=tit+texto['warning']+"\n";
	           	} else { 
					// ponemos el back en el color original. (esto es por si es la segunda vez que entra y antes tuvo error)
					node.style.background = backok();
				}
		});

		// comenzamos recorriendo los campos con marca "email"
        $$('form#'+id+' .email').each(function(node)
			{
				// expresion para control de emails
				var regexpr = /^([a-zA-Z0-9_.+-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,6})+$/;
				// si un (NODO=miembro que cumple la condicion de arriba para prototype) es vacio
				// o sea un "email" sin rellenar hacemos lo siguiente
				if (node.value != "") {
					if(!regexpr.test(node.value)) {
						// si error todavia es false o sea que todavia no hay error ponemos el foco en el primer campo a arreglar
						if(!error) node.focus();
						// ahora si ponemos error en true (despues de poner el foco)
						error = true;
						// y modificamos el background de ese campo en el form
						node.style.background = backerror();
						tit=node.alt;
						msg+=tit+texto['email_invalido']+"\n";
	
					} else { 
						// ponemos el back en el color original. (esto es por si es la segunda vez que entra y antes tuvo error)
						node.style.background = backok();
					}
				}
        });											   

		// comenzamos recorriendo los campos con marca "website"
        $$('form#'+id+' .url').each(function(node)
			{
				// expresion para control de emails
//				var regexpr = /^([a-zA-Z0-9_.+-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,6})+$/;
				var regexpr = /^http:\/\/(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,6})+$/;
				// si un (NODO=miembro que cumple la condicion de arriba para prototype) es vacio
				// o sea un "email" sin rellenar hacemos lo siguiente
				if(!regexpr.test(node.value) && (node.value!="")) {
				// si error todavia es false o sea que todavia no hay error ponemos el foco en el primer campo a arreglar
				if(!error) node.focus();
				// ahora si ponemos error en true (despues de poner el foco)
                error = true;
				// y modificamos el background de ese campo en el form
				node.style.background = backerror();
				tit=node.alt;
				msg+=tit+texto['url_invalido']+"\n";
            } else { 
				// ponemos el back en el color original. (esto es por si es la segunda vez que entra y antes tuvo error)
				node.style.background = backok(); }
        });											   
		
    	// comenzamos recorriendo los campos con marca "numerico"
	  	$$('form#'+id+' .numerico').each(function(node)
			{
            	var strChars = "0123456789.-";
            	for (i = 0; i < node.value.length; i++) 
					{
	    	            strChar = node.value.charAt(i);
    	    	        if (strChars.indexOf(strChar) == -1) 
							{
								if(!error) node.focus();
            			        error = true;
                    			node.style.background = backerror();
								tit=node.alt;
								msg+=tit+texto['numerico']+"\n";
		          			} else { 
								// ponemos el back en el color original. (esto es por si es la segunda vez que entra y antes tuvo error)
								node.style.background = backok(); 
							}
            		}
        	});
	
			// tip
			//alert ("Texto: " + $('texto').value);

		// al finalizar si hay error
        if (error == true) {
			alert(texto['form_warning']+'\n'+msg);
			// y vuelve al form
			return false;
        } else {
			return true;
//			document.id.submit();
			/*var rForm = new Ajax.Request("ajax_Index.php?id="+id, {
			onCreate: waiting,
			onComplete: procesaRegistro,
			parameters: $(id).serialize()
			});*/
        }
    
}

//***
//  checkForm
//***
function checkForm (id) {
		// ponemos error en false porque no hay errores actualmente.
		var error = false;
		var texto = new Array();
		texto =	dametextos();
		var msg='';
		
		// salvemos el contenido de los Editores TinyMCE 
		tinyMCE.triggerSave();

		// comenzamos recorriendo los campos con marca "requerido"
		$$('form#'+id+' .requerido').each(function(node)
			{
				// si un (NODO=miembro que cumple la condicion de arriba para prototype) es vacio
				// o sea un "requerido" sin rellenar hacemos lo siguiente
		        if (node.value == "") {
					// si error todavia es false o sea que todavia no hay error ponemos el foco en el primer campo a arreglar
					if(!error) node.focus();
					// ahora si ponemos error en true (despues de poner el foco)
					error = true;
					// y modificamos el background de ese campo en el form
					node.style.background = backerror();
					tit=node.alt;
					msg+=tit+texto['requerido']+"\n";
	           	} else { 
					// ponemos el back en el color original. (esto es por si es la segunda vez que entra y antes tuvo error)
					node.style.background = backok(); }
		});

		// comenzamos recorriendo los campos con marca "email"
        $$('form#'+id+' .email').each(function(node)
			{
				// expresion para control de emails
				var regexpr = /^([a-zA-Z0-9_.+-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,6})+$/;
				// si un (NODO=miembro que cumple la condicion de arriba para prototype) es vacio
				// o sea un "email" sin rellenar hacemos lo siguiente
				if(!regexpr.test(node.value)) {
				// si error todavia es false o sea que todavia no hay error ponemos el foco en el primer campo a arreglar
				if(!error) node.focus();
				// ahora si ponemos error en true (despues de poner el foco)
                error = true;
				// y modificamos el background de ese campo en el form
				node.style.background = backerror();
				tit=node.alt;
				msg+=tit+texto['email_invalido']+"\n";
            } else { 
				// ponemos el back en el color original. (esto es por si es la segunda vez que entra y antes tuvo error)
				node.style.background = backok(); }
        });											   
			
    	// comenzamos recorriendo los campos con marca "numerico"
	  	$$('form#'+id+' .numerico').each(function(node)
			{
            	var strChars = "0123456789.-";
            	for (i = 0; i < node.value.length; i++) 
					{
	    	            strChar = node.value.charAt(i);
    	    	        if (strChars.indexOf(strChar) == -1) 
							{
								if(!error) node.focus();
            			        error = true;
                    			node.style.background = backerror();
								tit=node.alt;
								msg+=tit+texto['numerico']+"\n";
		          			} else { 
								// ponemos el back en el color original. (esto es por si es la segunda vez que entra y antes tuvo error)
								node.style.background = backok(); 
							}
            		}
        	});
	
		// hago el control captcha
		/*
		if(!jcap()) 
			{ 
				if(!error) node.focus();
				error = true;
				$(id).uword.style.background = backerror();
				tit=$(id).uword.alt;
				msg+=tit+texto['captcha']+"\n";
			}
 		*/
		// al finalizar si hay error
        if (error == true) {
			alert(texto['form_error']+'\n'+msg);
			// y vuelve al form
			return false;
        } else {
			var rForm = new Ajax.Request("ajax_Index.php?id=registrar", {
			onCreate: waiting,
			onComplete: procesaRegistro,
			parameters: $(id).serialize()
			});
        }
    }

function waiting()
{
	$("divWaiting").innerHTML = "<br><div><img src='images/preload.gif'><div>";
	$("divWaiting").show();
	$("divWaiting").focus();
}

function procesaRegistro( resp )
{
	$("divWaiting").hide();	
	$("divRegistro").hide();
	$("divSaludo").style.height="500px";
	$("divSaludo").innerHTML = resp.responseText;
	$("divSaludo").show();
}

function procesaRespuesta( resp ){

//		effect_1 = Effect.SlideUp('divRegistro',{duration:1.0});
//		$("divSaludo").innerHTML = resp.responseText;
//		setTimeout("mostrar('divSaludo')",2600);
		$("divWaiting").hide();	
        $("divRegistro").hide();
		$("calculator").style.height="900px";
		//$('divRegistro').hide();
		$("divSaludo").innerHTML = resp.responseText;
		$("divSaludo").show();
		
		}

function cargaSelect(id,idin,idout)
	{
		var valor=$(idin).value;
		var selectForm = new Ajax.Updater(idout,"ajax_index.php?id="+id, {
			parameters: "value="+valor });
	}

function checkEmail(idin,idout)
	{
		var valor=$(idin).value;
		var selectForm = new Ajax.Updater(idout,"ajax_Index.php?id=checkemail", {
			parameters: "value="+valor });
		$('usrFrm').tabindex=5;
	}

function cargaInput(idin,idout)
	{
		var valor=$(idin).value;
		var selectForm = new Ajax.Updater(idout,"ajax_Index.php?id=selectinput", {
			parameters: "value="+valor });
	}

function cargaImagen(idin,idout)
	{
		var valor=$(idin).value;
		var texto=$(idin).text;
		var imageForm = new Ajax.Updater( idout ,"ajax_Index.php?id=actualizaimagen", {
			parameters: "value="+valor+"&texto="+texto });
	}
		

function setAyuda(div) 	{ $('ayuda_'+div).show(); }
function unsetAyuda(div) { $('ayuda_'+div).hide();}
function unsetError(div) { if($('error_'+div).style.display!='none') { $('error_'+div).hide();} }

function actualizaDatosRemotos(url, target, params)
{
	var Pars = $H(params).toQueryString();
	var Update = new Ajax.Updater(target, url, {method: 'get', parameters: Pars});
}

function paginateAjax (relink, divContent )
{
    document.getElementById(divContent).innerHTML = "<div style='padding-left:30px; padding-top:20px'>      PROCESS ...................     </DIV>  "; 
	var url = relink;
	var CacheAleatoria = Math.random() * 1000000
	var pars = {
				Funcion: '',
			    Param1: "",
				Param2: "",
				Cache: CacheAleatoria
	};

	actualizaDatosRemotos(url,divContent, pars);
}

// ISSUE 1354
function confirmMarcar () {

	if(!confirm(" Are you sure you want to check this item? "))
		return false;
	return true;

}

function confirmBorrar () {

	if(!confirm(" Are you sure you want to delete this item? "))
		return false;
	return true;

}

function confirmReject () {

	if(!confirm(" Are you sure you want to reject this Application? "))
		return false;
	return true;

}

function confirmBorrarAjax (id, url, urlBack)
{
		var urlAjax = "ajax_index.php?id=confirmDelete";
		var param = "&registro=" + id + "&seccion=" + url+"&back="+urlBack;
		var divNews = new Ajax.Updater('contenidoSummary', urlAjax, { parameters: param });
}

//function borrarRecomendador(id, mail)
function borrarRecomendador(idAplicacion, mail)
 { 
 	if(confirm(" Are you sure you want to delete this item? ")) {
		
		// Intentamos borrar un Recomendador de la lista
		//temp1 = ( document.getElementById("email1").value == "")? 0 : 1;
		//temp2 = ( document.getElementById("email2").value == "")? 0 : 1;
		//temp3 = ( document.getElementById("email3").value == "")? 0 : 1;
		//temp4 = ( document.getElementById("email4").value == "")? 0 : 1;
		//temp5 = ( document.getElementById("email5").value == "")? 0 : 1;
		//cantidad = ( temp1 + temp2 + temp3 + temp4 + temp5 ) - 1 ;

		// veamos si podemos borrarlo
		//if (cantidad < 3) {
		//	alert (" Must be at least 3 recommenders in your Applicaction. Sorry, you can not Delete.");
		//	return false;
		//} else {
			// Delete recomender
			var urlAjax = "ajax_index.php?id=deleteRedomendador";
			var param = "&idAplicacion=" + idAplicacion +"&mail="+mail;
			var divNews = new Ajax.Updater('contenidoSummary', urlAjax, { parameters: param});
		//}
	}
	
	return false;

 }


function borrarAjax (id, url)
{
	//$("messageBox").hide();
	var urlAjax = "ajax_index.php?id="+url;
	var param = "&registro="+id;
	var divNews = new Ajax.Updater('contenidoSummary', urlAjax, { parameters: param});	

}

function cancelarAjax (div)
{
	    $(div).innerHTML = "";
		$(div).show();
//		$(div).focus();	
}

function ocultarDIV (div)
{
//	$(div).innerHTML = "";
	$(div).hide();
}

function mostrarDIV (div)
{
	$(div).show();
}

function mostrarDIVover (div, evento)
{
   	evento = evento || window.event;
   	var posX=evento.clientX;
   	var posY=evento.clientY;

   	// reposicionamos el div
	$(div).style.left = posX;
	$(div).style.top = posY;
}
