// JavaScript Document

function validarCampos(form) {
	var error_txt = "";
	for(var i=0;i<form.elements.length;i++) {
		var error=false;
		var type=form.elements[i].type;
		var id=form.elements[i].id;
		
		if(type!="button" && type!="hidden" && type!="submit" && type!="reset"  && type!="file" && type!="radio") {
			var valor = form.elements[i].value;
			
			if(id != "fax" && id != "web") {
				if(valor == "" || valor == "0") {
					if(!error) {
						error = true;
						error_txt = "Faltan datos \n";
					}
				} else if(error_txt == "") {
					if(id == "telefono") {
						if(isNaN(valor) || valor.length!=9) {
							error_txt += "-No has introducido un telefono valido \n";
							error = true;
						}
					}
					
					if(id == "cp") {
						if(isNaN(valor) || valor.length!=5){
							error_txt += "-No has introducido un Codigo Postal valido \n";
							error = true;
						}
					}
					
					if(!valor.match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/)) {
						if(id == "email" || id == "email_envio") {
							error_txt += "-No has introducido un e-mail valido \n";
							error = true;			
						}
					}
				}
				
				if(error) {
					$("#"+id).css({'background-color':'#FF3535'});
				} else {
					$("#"+id).css({'background-color':'#ECECEC'});
				}
			}
		}
	}
	
	if(error_txt != "") {
		alert(error_txt);
	} else {
		form.submit();
	}
}
