﻿jQuery(document).ready ( function() {
	jQuery(".nav li").unbind();
	jQuery(".nav li").mouseover( function(){
	    var id = jQuery(this).attr("id");
		id = id.substr(1);
	    jQuery("#children"+id+" ul").css("top","38px");
	});
	jQuery(".nav li").mouseout( function(){
		var id = jQuery(this).attr("id");
		id = id.substr(1);
		jQuery("#children"+id+" ul").css("top","-999em");
	});
	jQuery("input, textarea, select").keypress(function(e){
		var cls = jQuery(this).attr("class");
		var cls = cls.split(" ",1);
		var code = e.which;
		var c = String.fromCharCode(code);
		var puedeValidar = true;
		var validChars = "";
		var invalidChars = "'\\\"";
		if(cls == "alphabetic")
			validChars = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ.,@";
		if(cls == "numeric")
			validChars = "0123456789.,";
		if(cls == "phone")
			validChars = "0123456789";
		if(cls == "mail")
			validChars = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ0123456789._-@";
		var valid = false;
		for(i=0; i<validChars.length;i++){
			if(validChars.charAt(i) == c) 
				valid = true; 
		}
		if(cls != "alphabetic" && cls != "numeric" && cls != "mail" && cls != "phone") 
			valid = true; 
		var error = 0; 
		for(i=0; i<invalidChars.length;i++){ 
			if(invalidChars.charAt(i) == c) 
				error++; 
		} 
		if(c==false || code==0 || code==8 || code==13) 
			valid = true; 
		if(cls =="numeric" && c==" ") 
			valid = false; 
		if(!valid || error > 0)
			e.preventDefault();
	});
	$("form.thickbox").submit(function(e){
		var t = this.title || this.name || null;
		var g = this.rel || false;
		if(validar(e)){
			tb_show(t,this.action,g);
			this.blur();
			return true;
		}
		else
			return false;
	});
	jQuery("form").submit(function(e){
		if(this.id!="form")
			validar(e);
	});
});

function validar(e){
	var fields = $("input[type='radio']");
	var valid = false;
	for(i = 0; i < fields.length; i++){
		if(i == 0){
			var name = fields[0].name;
		}
		if(name != fields[i].name){
			if(!valid){
				alert("Favor de llenar todas las respuestas");
				e.preventDefault();
				return;
			}
			else{
				valid = false;
			}
		}
		if(fields[i].checked)
			valid = true;
		name = fields[i].name;
	}
	var fields = $(".requiredField");
	for(i = 0; i < fields.length; i++){
		if(fields[i].value == ""){
			alert("Favor de llenar los campos obligatorios (*)");
			e.preventDefault();
			return false;
		}
	}
	var fields = $(".mail");
	for(i = 0; i < fields.length; i++){
		var email = document.getElementById('mail');
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test(fields[i].value) && fields[i].value != "") {
			alert("Favor de capturar mails validos");
			e.preventDefault();
			return false;
		}
	}
	var fields = $(".phone");
	for(i = 0; i < fields.length; i++){
		if(fields[i].value.length < 10 && fields[i].value != ""){
			alert("Favor de llenar los campos de telefono con 10 digitos");
			e.preventDefault();
			return false;
		}
	}
	if(document.getElementById('email').value != document.getElementById('confirma').value){
		alert("Favor de confirmar el E-Mail");
		e.preventDefault();
		return false;
	}
	return true;
}