var alphaRegexp = new RegExp("^[a-zA-Z \\,\\.\\/\\àÀáÁâÂãÃäÄåÅæÆªçÇèÈéÉêÊëËìÌíÍîÎïÏðÐñÑòÒóÓôÔõÕöÖøØùÙúÚûÛüÜýÝþÞÿß&]*$");
//var phoneRegexp = new RegExp("^[a-zA-Z0-9\\ +\\(\\)\\-\\.]*[0-9][a-zA-Z0-9\\ +\\(\\)\\-\\.]*[0-9][a-zA-Z0-9\\ +\\(\\)\\-\\.]*[0-9][a-zA-Z0-9\\ +\\(\\)\\-\\.]*[0-9][a-zA-Z0-9\\ +\\(\\)\\-\\.]*$");

var phoneRegexp = new RegExp("^[0-9\(-)]{2,4}-? ?[0-9]{7,8}$");


var emailRegexp = new RegExp("^([a-zA-Z0-9_\\-]+\\.)*[a-zA-Z0-9_\\-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,})(\\]?)$");
var cpRegexp = new RegExp("^[0-9]{5,5}([- ]?[0-9]{4,4})?$");

function ridiculo()
{
		var errorTxt = "";
		var f = $("#normalPlan form");
		var nombre = f.find("#nombre");
		errorTxt += literal['formDatosPersonales'][12]+"|"; 
		var errores = errorTxt.split("|");
		
			if (errores[0] != "" ) if ($("#msgError").length == 0) $('<div id="msgError"></div>').insertBefore('#pastilla'); else $("#msgError").html("");
			if (errores[0] != "" ) $('<ul></ul>').prependTo('div#msgError');	
			
			for (var i = errores.length-2;i>-1;i--) $('<li>'+errores[i]+'</li>').prependTo('div#msgError ul');
			if (errores[0] != "" ) return false	
			
		
}


function volver(pagina)
{
		
		final = pagina.replace('?p=cm','');
		//alert (final);	
		top.document.location.href=final +"/gracias";
}

var Common={
	validate:function(){	
		var errorTxt = "";
		var f = $("#normalPlan form");
		var nombre = f.find("#nombre");
		
		
		if( nombre.attr("value") != ""){
			if (!alphaRegexp.test(nombre.attr("value") )) errorTxt += literal['formDatosPersonales'][1] + "|"; 
		}else{
			errorTxt += literal['formDatosPersonales'][0]+"|"; 
		}
		
		var apellidos = f.find("#apellidos");
		if( apellidos.attr("value") != ""){
			if (!alphaRegexp.test(apellidos.attr("value") )) errorTxt += literal['formDatosPersonales'][3] + "|"; 
		}else{
			errorTxt += literal['formDatosPersonales'][2]+"|"; 
		}
		var telefono = f.find("#telefono");
		if( telefono.attr("value") != ""){
			if (!phoneRegexp.test(telefono.attr("value") )) errorTxt += literal['formDatosPersonales'][5] + "|"; 
		}else{
			errorTxt += literal['formDatosPersonales'][4]+"|"; 
		}	
		var email = f.find("#email");
		if( email.attr("value") != ""){
			if (!emailRegexp.test(email.attr("value") )) errorTxt += literal['formDatosPersonales'][7] + "|"; 
		}else{
			errorTxt += literal['formDatosPersonales'][6]+"|"; 
		}
		var comentario = f.find("#comentario");
		if( comentario.attr("value").length > 140){
			errorTxt += literal['formDatosPersonales'][11] + "|"; 
		}
		
/*		var foto = f.find("#foto");
		if( foto.attr("value") == "") errorTxt += literal['formDatosPersonales'][8] + "|"; 
		var comentario = f.find("#comentario");
		if( comentario.attr("value") == "") errorTxt += literal['formDatosPersonales'][9] + "|"; 
*/

		
		var condiciones = f.find("#condiciones");
			//if (errorTxt.length == "" )	
			if (!condiciones.attr("checked")) errorTxt += literal['formDatosPersonales'][10] + "|";
			
			var mayor = f.find("#mayor");
			//if (errorTxt.length == "" )	
			if (!mayor.attr("checked")) errorTxt += literal['formDatosPersonales'][13] + "|";
			

		/*
		var condiciones = f.find("#condiciones");
		if (errorTxt.length == "" )	if (!condiciones.attr("checked")) errorTxt += literal['formDatosPersonales'][10] + "|";
		*/
		
		var errores = errorTxt.split("|");
		if (errores[0] != "" ) if ($("#msgError").length == 0) $('<div id="msgError"></div>').insertBefore('#pastilla'); else $("#msgError").html("");
		if (errores[0] != "" ) $('<ul></ul>').prependTo('div#msgError');	
		
		for (var i = errores.length-2;i>-1;i--) $('<li>'+errores[i]+'</li>').prependTo('div#msgError ul');
		if (errores[0] != "" ) return false	
		return true;
		
		
		
	},
	setMsgError:function(errores,f){
		var erroresStr = "";
		for (var i = 0;i<errores.length;i++) erroresStr += errores[i]+'\n';
		if ( erroresStr.length >0 ) alert(erroresStr);
	
	}
}





$(document).ready(function(){
	$("fieldset .cntfile #foto").change(function (){ 
		$("form fieldset .cntfile .fakefile .filetext").attr("value",$(this).attr("value")); 
	});
	
	if ($("#normalPlan form").length > 0) $("#normalPlan form").bind("submit", Common.validate);
	
		var maximos = new Array ();

		$("textarea").attr("maxlength", function (i) {

		if (maximos[i] = this.getAttribute('maxlength')) {

		$(this).keypress(function(event) {

				   return ((event.which == 8) ||(event.which == 9) || (this.value.length < maximos[i]));

		})}});


	
});

