var myrules = {
	// GLOBAL :
	'input.has_callout' : function(element) {
		element.onfocus = function() {
			toggleDisplay("helper_" + this.id)
		},
		element.onblur = function() {
			toggleDisplay("helper_" + this.id)
		}
	},
	'img.has_callout' : function(element) {
		element.onmouseover = function() {
			toggleDisplay("helper_" + this.id)
		},
		element.onmouseout = function() {
			toggleDisplay("helper_" + this.id)
		}
	},
	// PAGE SPECIFIC :
	
	// contactar.php
	'form#contactar_public' : function(element) {
		element.onsubmit = function() {
			val = new Validator()
			val.val_length(this.cont_nombre, 1, 64, I18N._('CampoNombre'))
			val.val_length(this.cont_apellidos, 1, 64, I18N._('CampoApellidos'))
			val.val_email(this.cont_email, I18N._('Email'))
			val.val_length(this.cont_comentarios, 1, 500, I18N._('CampoComentarios'))			
		
			return val.validate()
		}
	},
	// pruebelo.php
	'form#form_pruebelo' : function(element) {
		element.onsubmit = function() {
			val = new Validator()

			val.val_format(this.reg_nombre, /[a-z0-9]+/i, I18N._('Nombre'))
			val.val_format(this.reg_apellidos, /[a-z0-9]+/i, I18N._('Apellidos'))
	
			if (val.val_email(this.reg_email, I18N._('Email')))
				val.val_not_format(this.reg_email, /@(yahoo|gmail|hotmail|lycos|myway|operawebmail|thedoghousemail|wowmail|mail)\.(es|com|co\.uk)$/i, I18N._('UsosIncorrectos'));
	
			val.val_format(this.reg_name, /[a-z0-9]+/i, I18N._('Empresa'))
			val.val_format(this.reg_pais, /[a-z0-9]+/i, I18N._('Pais'))
			val.val_format(this.reg_phone, /[a-z0-9]+/i, I18N._('Telefono'))
	
			val.val_format(this.reg_login, /^[a-z0-9]{6,20}$/i, I18N._('ElFormato'))
			if (val.val_format(this.reg_pwd, /^[a-z0-9]{6,20}$/i, I18N._('ElFormatoPassword')))
				val.val_equal(this.reg_pwd, this.reg_pwd_repeat, I18N._('PasswordNoCoinciden'))
			val.val_not_selected(this.reg_how_hear, 0, I18N._('ComoConocido'))
		
			val.val_checked(this.acepto, I18N._('DebeAceptar'))
			
			return val.validate()
		}
	},
	
	// inscribase.php
	'form#inscribase' : function(element) {
		element.onsubmit = function() {
			val = new Validator()

			val.val_length(this.nombre, 1, 64, I18N._('Nombre'))
			val.val_length(this.apellidos, 1, 64, I18N._('Apellidos'))
	
			if (val.val_email(this.email, I18N._('Email'))){
				val.val_not_format(this.email, /@(yahoo|gmail|hotmail|lycos|myway|operawebmail|thedoghousemail|wowmail|mail)\.(es|com|co\.uk)$/i, I18N._('UsosIncorrectos'));
			}
	
			val.val_format(this.telefono, /^[0-9 +]{9,20}$/i, I18N._('FormatoTelefono'))
			val.val_length(this.cont_empresa, 1, 64, I18N._('Empresa'))
			val.val_length(this.se_dedica, 1, 64, I18N._('SeDedicaA'))
			val.val_not_selected(this.tipoConsulta, 0, I18N._('InteresadoEn'))
			
			return val.validate()
		}
	},
	// contractP2.php
	'form#form_contr1' : function(element) {
		element.onsubmit = function() {
			val = new Validator()
	
			//Información Sobre Administrador De La Cuenta
			val.val_length(this.reg_nombre, 1, 64, I18N._('Nombre'))
			val.val_length(this.reg_apellidos, 1, 64, I18N._('Apellidos'))
			val.val_email(this.reg_email, I18N._('Email'))
			val.val_length(this.reg_postalCode, 1, 64, I18N._('Postal'))
		
	
			//Información sobre la Empresa 
			val.val_length(this.reg_f_name, 1, 64, I18N._('CampoNombreEmpresa'))
			val.val_length(this.reg_invoicing_street, 1, 64, I18N._('CampoDireccionFacturacion'))
			val.val_length(this.reg_f_city, 1, 64, I18N._('CampoCiudad'))
			val.val_length(this.reg_f_country, 1, 64, I18N._('CampoPais'))
			val.val_length(this.reg_f_phone, 1, 64, I18N._('CampoTelefono'))
			val.val_length(this.reg_cif, 1, 64, I18N._('CampoCIF'))
			
			if (this.reg_payment_system.value == "0")
				val.val_length(this.reg_payment_system, 100, 1000, I18N._('OpcionPago'))
			if (this.reg_payment_system.value == "2") {
				val.val_length(this.entidad, 1, 4, I18N._('Entidad'))
				val.val_length(this.oficina, 1, 4, I18N._('Oficina'))
				val.val_length(this.digitos, 1, 2, I18N._('Digitos'))
				val.val_length(this.cuenta, 1, 10, I18N._('Cuenta'))
			}
			
			return val.validate()
		}
	},

	// message_newP5.php
	'form#newMessageSelectDistrList' : function(element) {
		element.onsubmit = function() { 
			if (this.idLista.checked) {
				return true;	
			}
			var valid = false
			for (i = 0; i < this.idLista.length; i++) {
				if (this.idLista[i].checked) {
					valid = true;
				}
			}
			if (!valid)
				alert(I18N._('ListaNoSeleccionada'));
			return valid
		}
	},
	// message_newP6.php
	'form#newMessageSelectWhen' : function (element) {
		element.onsubmit = function() {
			if (this.confirm_extra && !this.confirm_extra.checked) {
				alert(I18N._('AceptarCosteAdicional'));
				return false;
			}
			if (this.optionSend[1].checked) {
				if (this.dateToSend.value == '') {
					alert('Debe introducir una fecha valida para realizar el envio.');
					return false;	
				}	
			}
		}
	},
	'input.hourOnlyTimeField' : function (element) {
		element.onblur = function() {
			if (parseInt(this.value) > 23) { this.value = 0 }
		}
	},
	'img#popupCalendarImage' : function (element) {
		element.onclick = function() {
			popUpCalendar(this, document.forms['frmMsg'].dateToSend,'dd/mm/yyyy');
			return false;
		}
	},
	// contact_edit.php
	'form#form_save_contact': function (element) {
		element.onsubmit = function() {
			val = new Validator()
			val.val_email(this.email, I18N._('Email'))
			return val.validate()
		}
	},
	'input#ajax_contact_email' : function(element) {
		element.onkeyup = function() {
			$('spinner').style.visibility = 'visible';
			if (!this.value || !this.value.is_email()) {
				$('ajax_unique_email_status').innerHTML = "<span class='disabled'>"+I18N._('FormatoNoValido')+"</span>";
			} else {
				new Ajax.Updater('ajax_unique_email_status', 'ajax_unique_email.php', { 
					asynchronous:true, 
					evalScripts:true, 
					parameters: Form.serialize(this.form)
				});
			}
			$('spinner').style.visibility = 'hidden';
			return false;
		}
	},
	'input#aUniqueEmail' : function (element) {
		element.onkeyup = function() {
			if (!document.forms['form_save_contact'].email.value.is_email()) {
				document.getElementById('to_update').innerHTML = 'Introduzca un e-mail válido';
				return false;
			} else {				
				id = document.getElementById('id').value;				
				new Ajax.Updater('to_update', 'ajax_unique_email.php?id='+id, { 
					asynchronous:true, 
					evalScripts:true, 
					parameters: Form.serialize(document.forms['form_save_contact']), 
					onComplete:function(request) { document.forms['form_save_contact'].email.disabled=false; }
				});
			}
			return false;
		}
	},
	// contact import
	'form#form_new_mailing_list': function (element) {
		element.onsubmit = function() {
			if (this['name'].value == this['name'].getAttribute('placeholder'))
				return false
			val = new Validator()
			val.val_length(this['name'], 1, null, I18N._('EntrarNombre'))
			return val.validate()		
		}
	},
	'input#aUniqueEmail' : function (element) {
		element.onkeyup = function() {
			if (!document.forms['form_save_contact'].email.value.is_email()) {
				document.getElementById('to_update').innerHTML = I18N._('ComprobarEmail');
				return false;
			} else {				
				id = document.getElementById('id').value;				
				new Ajax.Updater('to_update', 'ajax_unique_email.php?id='+id, { 
					asynchronous:true, 
					evalScripts:true, 
					parameters: Form.serialize(document.forms['form_save_contact']), 
					onComplete:function(request) { document.forms['form_save_contact'].email.disabled=false; }
				});
			}
			return false;
		}
	},
	// contact import
	'form#formImportContacts': function (element) {
		element.onsubmit = function() {
			val = new Validator()
			if (-1 != this.action.search(/upload_csv/)) {
				val.val_length(this.csv_file, 1, null, I18N._('ElegirCSV')) &&
					val.val_format(this.csv_file, /^.*\.csv$/i, I18N._('TieneQueSerCSV'))
				val.val_checked(this.upload_confirm, I18N._('ConfirmarConsentimiento'))
			} else {
				val.val_length(this.delimiter, 1, null, I18N._('EspecificarCaracter'))
				val.val_length(this.contact_tag, 1, null, I18N._('AsignarEtiqueta'))
			}
			var valid = val.validate()
			if (valid) {
				$('upload_button').disabled = true
				Element.show($('upload_progress'))
			}
			return valid
		}
	},
	// HTML import
	'form#formImportHtml': function (element) {
		element.onsubmit = function() {
			val = new Validator()
			if (val.val_length(this.html_file, 1, null, I18N._('ElegirArchivoHTML')))
				val.val_format(this.html_file, /^.*\.html?$/i, I18N._('FormatoArchivoHTML'));
			return val.validate()
		}
	},
	// New Sender
	'form#form_new_sender': function (element) {
		element.onsubmit = function() {
			val = new Validator()
			val.val_email(this['sender[email]'], I18N._('EmailNoValido2'))
			return val.validate()
		}
	},
	// New ReplyTo
	'form#form_new_replyto': function (element) {
		element.onsubmit = function() {
			val = new Validator()
			val.val_email(this['replyto[email]'], I18N._('EmailNoValido2'))
			return val.validate()
		}
	},
	// New ReplyTo
	'form#form_message_step_1': function (element) {
		element.onsubmit = function() {
			val = new Validator()
			val.val_length(this.nombre, 1, 255, I18N._('NombreNoVacio'))
			val.val_length(this.asunto, 1, 255, I18N._('AsuntoNoVacio'))
			val.val_at_least_one_option(this.remitente, I18N._('SeleccionarRemitente'))
			val.val_at_least_one_option(this.reply_to, I18N._('ResponderA'))
			if (this.template_type) {
				if (this.template_type[3].checked)
					val.val_format(this.import_url, /^http:\/\/[-a-z0-9.]+(\/.*)?/i, I18N._('UrlValido'))
			}
			return val.validate()
		}
	},

	'#button_save_sender': function (element) {
		element.onclick = function() {
			myForm = this.form
			val = new Validator()
			val.val_length(myForm['sender[name]'], 1, 64, I18N._('Nombre'))
			val.val_email(myForm['sender[email]'], I18N._('EmailDireccionValida'))
			
			if (val.validate()) {
				document.forms['frmNewMsg'].remitente.disabled=true;
				new Ajax.Updater('remitente_to_update', 'ajax_add_sender.php', {
								 asynchronous:true,
								 evalScripts:true,
								 parameters:Form.serialize(document.forms['frmNewMsg']),
								 onComplete:function(request){
									 toggleDisclosure('new_remitente_div', 'new_remitente_disclose');
									 document.forms['frmNewMsg'].remitente.disabled=false;
								 }
								});
			}
			return false;	
		}
	},
	
	'#button_save_reply_to': function (element) {
		element.onclick = function() {
			myForm = this.form
			val = new Validator()
			val.val_length(myForm['replyto[name]'], 1, 64, I18N._('Nombre'))
			val.val_email(myForm['replyto[email]'], I18N._('EmailDireccionValida'))
			
			if (val.validate()) {
				document.forms['frmNewMsg'].reply_to.disabled=true;
				new Ajax.Updater('reply_to_to_update', 'ajax_add_replyto.php', {
								 asynchronous:true,
								 evalScripts:true,
								 parameters:Form.serialize(document.forms['frmNewMsg']),
								 onComplete:function(request){
									 toggleDisclosure('new_reply_to_div', 'new_reply_to_disclose');
									 document.forms['frmNewMsg'].reply_to.disabled=false;
								 }
								});
			}
			return false;	
		}
	},
	
	// Change password
	'form#form_account_change_password': function (element) {
		element.onsubmit = function() {
			val = new Validator()
			if (val.val_length(this['password'], 6, 20, I18N._('CaracteresContraseña')) &&
					val.val_length(this['password_check'], 6, 20, I18N._('CaracteresContraseña'))) {
				val.val_equal(this['password'], this['password_check'], I18N._('NoCoincidenContraseñas'))
			}
			return val.validate()
		}
	},
	// Change password
	'form#form_new_user': function (element) {
		element.onsubmit = function() {
			val = new Validator()
			val.val_length(this['user[nombre]'], 1, null, I18N._('NombreNoPuedeVacio'))
			val.val_length(this['user[apellidos]'], 1, null, I18N._('ApellidosNoPuedeVacio'))
			val.val_email(this['user[email]'], I18N._('E_mailValido'))
			val.val_length(this['user[login]'], 5, 20, I18N._('CaracteresLogin'))
			if (val.val_length(this['user[pwd]'], 6, 20, I18N._('CaracteresContraseña')) &&
					val.val_length(this['user[pwd2]'], 6, 20, I18N._('CaracteresContraseña'))) {
				val.val_equal(this['user[pwd]'], this['user[pwd2]'], I18N._('NoCoincidenContraseñas'))
			}
			return val.validate()
		}
	},
	// changePassword.php (external)
	'form#form_change_password_external': function (element) {
		element.onsubmit = function() {
			val = new Validator()
			if (val.val_length(this['pwd1'], 6, 20, I18N._('CaracteresClave')) &&
					val.val_length(this['pwd2'], 6, 20, I18N._('CaracteresClave'))) {
				if (val.val_equal(this['pwd1'], this['pwd2'], I18N._('ClavesNoCoinciden')))
					val.val_format(this['pwd1'], /^[^ ]+$/i, I18N._('ClaveSinEspacios'))
			}
			return val.validate()
		}
	},
	// form_web.php
	'form#form_datos': function (element) {
		element.onsubmit = function() {
			val = new Validator()
			val.val_length(this['clave'], 1, 10, I18N._('clave_no_puede_ser'))
			val.val_domain(this['refer'], I18N._('dominio_invalido'))
			val.val_url(this['url2'], I18N._('url_del_tipo'))
			val.val_url(this['url1'], I18N._('url_tipo_2'))
			return val.validate()
		}
	},
	
	// form_web.php
	'form#smtp_datos': function (element) {
		element.onsubmit = function() {
			val = new Validator()
			if (this['smtp_external'][1].checked) {
				val.val_length(this['smtp_host'], 1, 250, I18N._('servidor_de_correo'))
				val.val_length(this['smtp_login'], 1, 250, I18N._('nombre_vacio_usuario'))
				if (val.val_length(this['smtp_pass'], 1, 250, I18N._('contra_no_puede_vacia')))
					if (val.val_equal(this['smtp_pass'], this['smtp_pass2'], I18N._('las_claves_no_c')))
						val.val_format(this['smtp_pass'], /^[^ ]+$/i, I18N._('no_puede_contener_espacios'))
						 if (this['smtp_return_path'].value != '') val.val_email(this['smtp_return_path'], I18N._('return_debe_ser_valido')) 
			
			}
			return val.validate()
		}
	},
	// message_list.php
	'.aCancelMessage' : function (element) {
		element.onclick = function() {
			return confirm(I18N._('EstasSeguro'));
		}
	},
	'input.chkToggleRow' : function (element) {
		element.onclick = function() {
			toggleRow(this);
		}
	},
	'input#buttonOpenTracking' : function(element) {
		element.onclick = function() {
//			myForm = document.forms[frmMsg]
			window.open("tracking.php?id_message="+this.form.id.value, "tracker", "scrollbars=yes,width=640,height=400,menubar=no")
		}
	},
	'input.has_placeholder' : function(element) {
		Element.addClassName(element, 'disabled')
		element.value = element.getAttribute('placeholder')
		element.onfocus = function() {
			if (element.value == element.getAttribute('placeholder'))
				element.value = ''
			Element.removeClassName(element, 'disabled')
		}
		element.onblur = function() {
			if (element.value == '') {
				element.value = element.getAttribute('placeholder')
				Element.addClassName(element, 'disabled')
			}
		}
	}
};

Behaviour.register(myrules);

function CheckSubmit(myForm){
  if (0 == myForm["relations[]"][0].selectedIndex) {
		alert(I18N._('RelacionarCampoCSV'));
		myForm["relations[]"][0].focus()
		return false;
	}
	return true;
	return false
	if(links.length==0){
		alert(I18N._('NoHayRelacion'));
		return false;
	}
	return true;
}

function valNewReplyTo(myForm){
	val = new Validator()
	val.val_email(myForm['replyto[email]'], I18N._('EmailNoValido2'))
	return val.validate()
}

function valNewSender(myForm){
	val = new Validator()
	val.val_email(myForm['sender[email]'], I18N._('EmailNoValido2'))
	return val.validate()
}