$(function() {
	$('#content a').filter(function(){
		return this.hostname && this.hostname !== location.hostname;
	}).addClass('external').attr("target","_new");
	$('#gallery a').lightBox();
	
	/*contact form*/
	var formArray = new Array('name','telephone','mail','services','mobile');
	for(i=0; i<=formArray.length; i++){
		if((formArray[i] == 'name')||(formArray[i] == 'telephone')||(formArray[i] == 'mobile')||(formArray[i] == 'mail')){
			$("#" + formArray[i]).blur(function(){
				var thisId = $(this).attr("id");
				$("#" + thisId + "_error").hide();
				$("#" + thisId + "_error2").hide();				
				$("#" + thisId + "_label").css("color","#333");
				$("#" + thisId).css("color","#777");
			});
		}
		if(formArray[i] == 'name'){
			$("#" + formArray[i]).blur(function(){			
				var thisId = $(this).attr("id");
				var thisIdVal = $("#" + thisId).val();
				if (/^[a-zA-Z\.\-\´\`\W\w\s\à\è\ì\ò\ù\á\é\í\ó\ú\ü\ñ\ç\À\È\Ì\Ò\Ù\Á\É\Í\Ó\Ú\Ü\Ñ\Ç]{2,50}$/.test(thisIdVal) == 0){
					$("#" + thisId + "_error").show();
					$("#" + thisId + "_label").css("color","#af2424");
					$("#" + thisId).css("color","#af2424");
					return false;
				}		
			});
		}	
		else if(formArray[i] == 'telephone'){
			$("#" + formArray[i]).blur(function(){			
				var thisId = $(this).attr("id");
				var thisIdVal = $("#" + thisId).val();
				if (/^[0-9\s\+\(\)\-]{9,50}$/.test(thisIdVal) == 0) {
					$("#" + thisId + "_error").show();
					$("#" + thisId + "_error2").show();
					$("#" + thisId + "_label").css("color","#af2424");
					$("#" + thisId).css("color","#af2424");
					return false;
				}		
			});
		}
		else if(formArray[i] == 'mobile'){
			$("#" + formArray[i]).blur(function(){			
				var thisId = $(this).attr("id");
				var thisIdVal = $("#" + thisId).val();
				if (/^[0-9\s\+\(\)\-]{0,50}$/.test(thisIdVal) == 0) {
					$("#" + thisId + "_error").show();
					$("#" + thisId + "_label").css("color","#af2424");
					$("#" + thisId).css("color","#af2424");
					return false;
				}		
			});
		}
		else if(formArray[i] == 'mail'){
			$("#" + formArray[i]).blur(function(){			
				var thisId = $(this).attr("id");
				var thisIdVal = $("#" + thisId).val();
				if (/(^[0-9a-zA-Z]+(?:[._-][0-9a-zA-Z]+)*)@([0-9a-zA-Z]+(?:[._-][0-9a-zA-Z]+)*\.[0-9a-zA-Z]{2,4})$/.test(thisIdVal) == 0){
					$("#" + thisId + "_error").show();
					$("#" + thisId + "_label").css("color","#af2424");
					$("#" + thisId).css("color","#af2424");
					return false;
				}
			});
		}
		else if(formArray[i] == 'services'){
			$("#" + formArray[i]).change(function(){			
				var thisId = $(this).attr("id");
				var thisIdVal = $("#" + thisId).val();
				if(thisIdVal == 0){
					$("#" + thisId + "_error").show();
					$("#" + thisId + "_error2").show();					
					$("#" + thisId + "_label").css("color","#af2424");
					$("#" + thisId).css("color","#af2424");
					return false;
				}else{
					$("#" + thisId + "_error").hide();
					$("#" + thisId + "_error2").hide();
					$("#" + thisId + "_label").css("color","#333");
					$("#" + thisId).css("color","#777");
					return false;
				}
			});
		}
	}
	$("#send").click(function() {
		for(i=0; i<=formArray.length; i++){
			if(formArray[i] == 'name'){
				var thisId = $("#" + formArray[i]).attr("id");
				var thisIdVal = $("#" + thisId).val();
				if (/^[a-zA-Z\.\-\´\`\_\W\w\s\à\è\ì\ò\ù\á\é\í\ó\ú\ü\ñ\ç\À\È\Ì\Ò\Ù\Á\É\Í\Ó\Ú\Ü\Ñ\Ç]{2,50}$/.test(thisIdVal) == 0){
					$("#" + thisId + "_error").show();
					$("#" + thisId + "_label").css("color","#af2424");
					$("#" + thisId).css("color","#af2424");
					return false;
				}		
			}	
			else if(formArray[i] == 'telephone'){
				var thisId = $("#" + formArray[i]).attr("id");
				var thisIdVal = $("#" + thisId).val();
				if (/^[0-9\s\+\(\)\-]{9,50}$/.test(thisIdVal) == 0) {
					$("#" + thisId + "_error").show();
					$("#" + thisId + "_error2").show();
					$("#" + thisId + "_label").css("color","#af2424");
					$("#" + thisId).css("color","#af2424");
					return false;
				}		
			}
			else if(formArray[i] == 'mobile'){
				var thisId = $("#" + formArray[i]).attr("id");
				var thisIdVal = $("#" + thisId).val();
				if (/^[0-9\s\+\(\)\-]{0,50}$/.test(thisIdVal) == 0) {
					$("#" + thisId + "_error").show();
					$("#" + thisId + "_error2").show();
					$("#" + thisId + "_label").css("color","#af2424");
					$("#" + thisId).css("color","#af2424");
					return false;
				}		
			}
			else if(formArray[i] == 'mail'){
				var thisId = $("#" + formArray[i]).attr("id");
				var thisIdVal = $("#" + thisId).val();
				if (/(^[0-9a-zA-Z]+(?:[._-][0-9a-zA-Z]+)*)@([0-9a-zA-Z]+(?:[._-][0-9a-zA-Z]+)*\.[0-9a-zA-Z]{2,4})$/.test(thisIdVal) == 0){
					$("#" + thisId + "_error").show();
					$("#" + thisId + "_label").css("color","#af2424");
					$("#" + thisId).css("color","#af2424");
					return false;
				}
			}
			else if(formArray[i] == 'services'){
				var thisId = $("#" + formArray[i]).attr("id");
				var thisIdVal = $("#" + thisId).val();
				if(thisIdVal == 0){
					$("#" + thisId + "_error").show();
					$("#" + thisId + "_error2").show();					
					$("#" + thisId + "_label").css("color","#af2424");
					$("#" + thisId).css("color","#af2424");
					return false;
				}
			}
		}
		var company = $('#company').val();
		var mobile = $("#mobile").val();
		var name = $("#name").val();
		var telephone = $("#telephone").val();
		var mail = $("#mail").val();
		var services = $("#services").val();
		var comment = $('#comment').val();
		var imv = $('#imv').val();
		var dataString = "";
		var urlProcess = "/php/contact.process.php?company=" + company + "&name=" + name + "&telephone=" + telephone + "&mobile=" + mobile + "&imv=" + imv + "&mail=" + mail + "&services=" + services + "&comment=" + comment;
		//alert(urlProcess);return false;
		$.ajax({
			beforeSend: function() {
				$('#message').hide();
				$('#send').hide();
				$('#company').attr("disabled","disabled").css("color","#bebebe");
				$('#name').attr("disabled","disabled").css("color","#bebebe");
				$('#mobile').attr("disabled","disabled").css("color","#bebebe");
				$('#telephone').attr("disabled","disabled").css("color","#bebebe");
				$('#mail').attr("disabled","disabled").css("color","#bebebe");
				$('#services').attr("disabled","disabled").css("color","#bebebe");
				$('#comment').attr("disabled","disabled").css("color","#bebebe");
				$('#load').fadeIn(600);
			},
			type: "POST",
			url: urlProcess,
			data: dataString,
			success: function() {
				$("#message").html("<h3>La informaci&oacute;n ha sido enviada.</h3>")
				.append("<p><strong>En breve nos pondremos en contacto contigo.</strong><br>Puedes pedir m&aacute;s informaci&oacute;n de otros servicios si lo deseas.</p>")
				.fadeIn(1000, function() {
					$("#message");
				});
				$('#load').hide();
				$('#send').show();
				$('#company').removeAttr('disabled').css("color","#777777");
				$('#name').removeAttr("disabled").css("color","#777777");
				$('#telephone').removeAttr("disabled").css("color","#777777");
				$('#mobile').removeAttr("disabled").css("color","#777777");
				$('#mail').removeAttr('disabled').css("color","#777777");
				$('#services').removeAttr('disabled').css("color","#777777");
				$('#comment').removeAttr('disabled').css("color","#777777");
			}
		});
		return false;
	});

	/*presupuesto*/
	var presupArray = new Array('NomEmpresa','Actividad','NumTrabajadores','NumCentros','Tel','Email','Poblacion','Provincia');
	for(i=0; i<=presupArray.length; i++){
		if((presupArray[i] == 'NomEmpresa')||(presupArray[i] == 'Actividad')||(presupArray[i] == 'NumTrabajadores')||(presupArray[i] == 'Tel')||(presupArray[i] == 'Email')||(presupArray[i] == 'Poblacion')){
			$("#" + presupArray[i]).blur(function(){
				var thisId = $(this).attr("id");
				$("#" + thisId + "_error").hide();
				$("#" + thisId + "_error2").hide();				
				$("#" + thisId + "_label").css("color","#333");
				$("#" + thisId).css("color","#777");
			});
		}
		if((presupArray[i] == 'NomEmpresa')||(presupArray[i] == 'Actividad')||(presupArray[i] == 'Poblacion')){
			$("#" + presupArray[i]).blur(function(){			
				var thisId = $(this).attr("id");
				var thisIdVal = $("#" + thisId).val();
				if (/^[a-zA-Z\.\-\´\`\W\w\s\à\è\ì\ò\ù\á\é\í\ó\ú\ü\ñ\ç\À\È\Ì\Ò\Ù\Á\É\Í\Ó\Ú\Ü\Ñ\Ç]{2,50}$/.test(thisIdVal) == 0){
					$("#" + thisId + "_error").show();
					$("#" + thisId + "_label").css("color","#af2424");
					$("#" + thisId).css("color","#af2424");
					return false;
				}		
			});
		}	
		else if(presupArray[i] == 'NumTrabajadores'){
			$("#" + presupArray[i]).blur(function(){			
				var thisId = $(this).attr("id");
				var thisIdVal = $("#" + thisId).val();
				if (/^[0-9\s\+\(\)\-]{1,50}$/.test(thisIdVal) == 0) {
					$("#" + thisId + "_error").show();
					$("#" + thisId + "_error2").show();
					$("#" + thisId + "_label").css("color","#af2424");
					$("#" + thisId).css("color","#af2424");
					return false;
				}		
			});
		}
		else if(presupArray[i] == 'Tel'){
			$("#" + presupArray[i]).blur(function(){			
				var thisId = $(this).attr("id");
				var thisIdVal = $("#" + thisId).val();
				if (/^[0-9\s\+\(\)\-]{9,50}$/.test(thisIdVal) == 0) {
					$("#" + thisId + "_error").show();
					$("#" + thisId + "_label").css("color","#af2424");
					$("#" + thisId).css("color","#af2424");
					return false;
				}		
			});
		}
		else if(presupArray[i] == 'Email'){
			$("#" + presupArray[i]).blur(function(){			
				var thisId = $(this).attr("id");
				var thisIdVal = $("#" + thisId).val();
				if (/(^[0-9a-zA-Z]+(?:[._-][0-9a-zA-Z]+)*)@([0-9a-zA-Z]+(?:[._-][0-9a-zA-Z]+)*\.[0-9a-zA-Z]{2,4})$/.test(thisIdVal) == 0){
					$("#" + thisId + "_error").show();
					$("#" + thisId + "_label").css("color","#af2424");
					$("#" + thisId).css("color","#af2424");
					return false;
				}
			});
		}
		else if((presupArray[i] == 'NumCentros')||(presupArray[i] == 'Provincia')){
			$("#" + presupArray[i]).change(function(){			
				var thisId = $(this).attr("id");
				var thisIdVal = $("#" + thisId).val();
				if(thisIdVal == 0){
					$("#" + thisId + "_error").show();
					$("#" + thisId + "_error2").show();					
					$("#" + thisId + "_label").css("color","#af2424");
					$("#" + thisId).css("color","#af2424");
					return false;
				}else{
					$("#" + thisId + "_error").hide();
					$("#" + thisId + "_error2").hide();
					$("#" + thisId + "_label").css("color","#333");
					$("#" + thisId).css("color","#777");
					return false;
				}
			});
		}
	}
	$("#sendContact").click(function() {
		for(i=0; i<=presupArray.length; i++){
			if((presupArray[i] == 'NomEmpresa')||(presupArray[i] == 'Actividad')||(presupArray[i] == 'Poblacion')){
				var thisId = $("#" + presupArray[i]).attr("id");
				var thisIdVal = $("#" + thisId).val();
				if (/^[a-zA-Z\.\-\´\`\_\W\w\s\à\è\ì\ò\ù\á\é\í\ó\ú\ü\ñ\ç\À\È\Ì\Ò\Ù\Á\É\Í\Ó\Ú\Ü\Ñ\Ç]{2,50}$/.test(thisIdVal) == 0){
					$("#" + thisId + "_error").show();
					$("#" + thisId + "_label").css("color","#af2424");
					$("#" + thisId).css("color","#af2424");
					return false;
				}		
			}	
			else if(presupArray[i] == 'NumTrabajadores'){
				var thisId = $("#" + presupArray[i]).attr("id");
				var thisIdVal = $("#" + thisId).val();
				if (/^[0-9\s\+\(\)\-]{1,50}$/.test(thisIdVal) == 0) {
					$("#" + thisId + "_error").show();
					$("#" + thisId + "_error2").show();
					$("#" + thisId + "_label").css("color","#af2424");
					$("#" + thisId).css("color","#af2424");
					return false;
				}		
			}
			else if(presupArray[i] == 'Tel'){
				var thisId = $("#" + presupArray[i]).attr("id");
				var thisIdVal = $("#" + thisId).val();
				if (/^[0-9\s\+\(\)\-]{0,50}$/.test(thisIdVal) == 0) {
					$("#" + thisId + "_error").show();
					$("#" + thisId + "_error2").show();
					$("#" + thisId + "_label").css("color","#af2424");
					$("#" + thisId).css("color","#af2424");
					return false;
				}		
			}
			else if(presupArray[i] == 'Email'){
				var thisId = $("#" + presupArray[i]).attr("id");
				var thisIdVal = $("#" + thisId).val();
				if (/(^[0-9a-zA-Z]+(?:[._-][0-9a-zA-Z]+)*)@([0-9a-zA-Z]+(?:[._-][0-9a-zA-Z]+)*\.[0-9a-zA-Z]{2,4})$/.test(thisIdVal) == 0){
					$("#" + thisId + "_error").show();
					$("#" + thisId + "_label").css("color","#af2424");
					$("#" + thisId).css("color","#af2424");
					return false;
				}
			}
			else if((presupArray[i] == 'NumCentros')||(presupArray[i] == 'Provincia')){
				var thisId = $("#" + presupArray[i]).attr("id");
				var thisIdVal = $("#" + thisId).val();
				if(thisIdVal == 0){
					$("#" + thisId + "_error").show();
					$("#" + thisId + "_error2").show();					
					$("#" + thisId + "_label").css("color","#af2424");
					$("#" + thisId).css("color","#af2424");
					return false;
				}
			}
		}

		if($("#SPA").attr("checked") == 1){ var SPA = $("#SPA").val(); }
		else{ var SPA = ""; }
		if($("#CFNB").attr("checked") == 1){ var CFNB = $("#CFNB").val(); }
		else{ var CFNB = ""; }
		if($("#PSSO").attr("checked") == 1){ var PSSO = $("#PSSO").val(); }
		else{ var PSSO = ""; }
		
		var Actividad = $("#Actividad").val();
		var NomEmpresa = $("#NomEmpresa").val();
		var NumTrabajadores = $("#NumTrabajadores").val();
		var Email = $("#Email").val();
		var NumCentros = $("#NumCentros").val();
		var Tel = $('#Tel').val();
		var Fax = $("#Fax").val();
		var Contacto = $("#Contacto").val();
		var Dir = $('#Dir').val();
		var CodPostal = $("#CodPostal").val();
		var Poblacion = $("#Poblacion").val();
		var Provincia = $("#Provincia").val();
		var Herramientas = $('#Herramientas').val();
		var Trabajadores = $("#Trabajadores").val();
		var Comentarios = $("#Comentarios").val();
		var imvP = $('#imvP').val();
		var dataString = "";
		var urlProcess = "/php/presupuesto.process.php?Actividad=" + Actividad + "&NomEmpresa=" + NomEmpresa + "&NumTrabajadores=" + NumTrabajadores + "&NumCentros=" + NumCentros + "&imvP=" + imvP + "&Email=" + Email + "&Tel=" + Tel + "&Fax=" + Fax + "&Contacto=" + Contacto + "&CodPostal=" + CodPostal + "&Dir=" + Dir + "&Poblacion=" + Poblacion + "&Provincia=" + Provincia + "&SPA=" + SPA + "&CFNB=" + CFNB + "&PSSO=" + PSSO + "&Herramientas=" + Herramientas + "&Trabajadores=" + Trabajadores + "&Comentarios=" + Comentarios;
		//alert(urlProcess);return false;
		$.ajax({
			beforeSend: function() {
				$('#message').hide();
				$('#send').hide();
				$('#Actividad').attr("disabled","disabled").css("color","#bebebe");
				$('#NomEmpresa').attr("disabled","disabled").css("color","#bebebe");
				$('#Contacto').attr("disabled","disabled").css("color","#bebebe");
				$('#NumTrabajadores').attr("disabled","disabled").css("color","#bebebe");
				$('#NumCentros').attr("disabled","disabled").css("color","#bebebe");
				$('#Fax').attr("disabled","disabled").css("color","#bebebe");
				$('#Email').attr("disabled","disabled").css("color","#bebebe");
				$('#CodPostal').attr("disabled","disabled").css("color","#bebebe");
				$('#Dir').attr("disabled","disabled").css("color","#bebebe");
				$('#Tel').attr("disabled","disabled").css("color","#bebebe");
				$('#Poblacion').attr("disabled","disabled").css("color","#bebebe");
				$('#Provincia').attr("disabled","disabled").css("color","#bebebe");
				$('#Herramientas').attr("disabled","disabled").css("color","#bebebe");
				$('#SPA').attr("disabled","disabled").css("color","#bebebe");
				$('#CFNB').attr("disabled","disabled").css("color","#bebebe");
				$('#PSSO').attr("disabled","disabled").css("color","#bebebe");
				$('#Trabajadores').attr("disabled","disabled").css("color","#bebebe");
				$('#Comentarios').attr("disabled","disabled").css("color","#bebebe");
				$('#load').fadeIn(600);
			},
			type: "POST",
			url: urlProcess,
			data: dataString,
			success: function() {
				$("#message").html("<h3>La informaci&oacute;n ha sido enviada.</h3>")
				.append("<p><strong>En breve nos pondremos en contacto contigo</strong>.</p>")
				.fadeIn(1000, function() {
					$("#message");
				});
				$('#load').hide();
				$('#send').show();
				$('#Actividad').removeAttr('disabled').css("color","#777777");
				$('#NomEmpresa').removeAttr("disabled").css("color","#777777");
				$('#NumTrabajadores').removeAttr("disabled").css("color","#777777");
				$('#NumCentros').removeAttr("disabled").css("color","#777777");
				$('#Fax').removeAttr('disabled').css("color","#777777");
				$('#CodPostal').removeAttr("disabled").css("color","#777777");
				$('#Contacto').removeAttr("disabled").css("color","#777777");
				$('#Dir').removeAttr("disabled").css("color","#777777");
				$('#Email').removeAttr('disabled').css("color","#777777");
				$('#Tel').removeAttr('disabled').css("color","#777777");
				$('#Poblacion').removeAttr('disabled').css("color","#777777");
				$('#Provincia').removeAttr("disabled").css("color","#777777");
				$('#Herramientas').removeAttr("disabled").css("color","#777777");
				$('#CFNB').removeAttr('disabled').css("color","#777777");
				$('#SPA').removeAttr("disabled").css("color","#777777");
				$('#PSSO').removeAttr('disabled').css("color","#777777");
				$('#Trabajadores').removeAttr('disabled').css("color","#777777");
				$('#Comentarios').removeAttr("disabled").css("color","#777777");
			}
		});
		return false;
	});
});

