// JavaScript Document

	/******************************************************************************************************
	*
	*
	* AJAXAS
	*
	*
	*******************************************************************************************************/
	
	var myGlobalHandlers = {

		onCreate: function() {
			$('dialog').style.display = 'block';
			$('dialog').innerHTML = '<img src="imagenes/progress.gif" width="16" height="16" />';
		},

		onComplete: function() {
			if(Ajax.activeRequestCount == 0) {
				$('dialog').style.display = 'none';
			}
			
		},
		
		onFailure: function () {
			$('dialog').innerHTML = "Oops!!, parece que ha ocurrido un error. Intente Nuevamente";
		}
	};

	Ajax.Responders.register(myGlobalHandlers);



	function Llamar(myURL,myDIV,myFRM) {
		var myPAR = ArmaURL(myFRM);
		var myAjax = new Ajax.Updater(  { success: myDIV } , myURL,  { method: 'post',  parameters: myPAR,  evalScripts: true} );
		return true;
	}


	/******************************************************************************************************
	*
	*
	* DEVUELVE LOS VALORES DE UN FORM PARA PASAR COMO PARAMETRO
	*
	*
	*******************************************************************************************************/
	function ArmaURL(obj) {
		formu = obj;

		var i = 0;
		var vars = "";
		var fl=formu.length;

		for (i=0;i<fl;i++){	
			var tempobj=formu.elements[i];

			if (tempobj.type != "button" && tempobj.type != "reset" ) {
	
				if (tempobj.type == "radio") {
					if (tempobj.checked) {
						vars += tempobj.name + "=" + encodeURIComponent(tempobj.id) + '&';
						vars += tempobj.id   + "=" + encodeURIComponent(tempobj.value) + '&';
					}		
				}
				else {

					// COMENTO ESTA LINEA PARA DEBUG
					// if (!valida(tempobj)) return '';
					vars += tempobj.name + "=" + encodeURIComponent(tempobj.id) + '&';
					vars += tempobj.id   + "=" + encodeURIComponent(tempobj.value) + '&';
				}
			}
		}
		return vars;
	}


	/******************************************************************************************************
	*
	*
	* VALIDA UN FORMULARIO
	*
	*
	*******************************************************************************************************/
	function ValidaForm(obj) {
		formu = obj;

		var i = 0;
		var vars = "";
		var fl=formu.length;

	
		for (i=0;i<fl;i++){	
			var tempobj=formu.elements[i];

			if (tempobj.type != "button" && tempobj.type != "reset" && tempobj.type != "radio") {
	
				if (!valida(tempobj)) {
					return false;
				}
				
				if (tempobj.name == "txtEmail") {
					var re = '^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$';
					var ok = new RegExp(re).test(tempobj.value);

					if (!ok) {
						alert("Direccion de Correo invalida");
						return false;
					}
				}
				
			}
		}

		obj.lleno.value="1";
		obj.submit();
		return true;
	}
		

	function valida(obj) {
		
		if( obj && obj.attributes && obj.attributes['mandatory'] ) {
			
			if ( obj.attributes['mandatory'].value != undefined) {

				if (obj.attributes['mandatory'].value == "true" && (obj.value == undefined || obj.value == '')) {
					alert("Faltan completar campos");
					return false;
				}
			}
		}
		
		return true;
	}


	function ShowProgress() {
		pp = document.getElementById('cargando');
		if (pp) pp.style.visibility = 'inherit';	
		return true;
	}
	
	