$(document).ready(function(){
	$('#presupuestoProe').bind('change',function(){
		var valor = $('#presupuestoProe option:selected').attr('id');
		switch (valor){
			case '3':
				$('#cad').show("slide", {}, 1000);
				$('#cae').hide();
				$('#cam').hide();
				$('#otros').hide();
				break;
			case '4':
				$('#cad').hide();
				$('#cae').show("slide", {}, 1000);
				$('#cam').hide();
				$('#otros').hide();
				break;
			case '5':
				$('#cad').hide();
				$('#cae').hide();
				$('#cam').show("slide", {}, 1000);
				$('#otros').hide();
				break;
			case '6':
				$('#cad').hide();
				$('#cae').hide();
				$('#cam').hide();
				$('#otros').show("slide", {}, 1000);
				break;
			default:
				$('#cad').hide();
				$('#cae').hide();
				$('#cam').hide();
				$('#otros').hide();
				break;
		}
	});
	
	valor=$('#presupuestoProe option:selected').attr('id');
	switch (valor){
		case '3':
			$('#cad').show("slide", {}, 1000);
			$('#cae').hide();
			$('#cam').hide();
			$('#otros').hide();
			break;
		case '4':
			$('#cad').hide();
			$('#cae').show("slide", {}, 1000);
			$('#cam').hide();
			$('#otros').hide();
			break;
		case '5':
			$('#cad').hide();
			$('#cae').hide();
			$('#cam').show("slide", {}, 1000);
			$('#otros').hide();
			break;
		case '6':
			$('#cad').hide();
			$('#cae').hide();
			$('#cam').hide();
			$('#otros').show("slide", {}, 1000);
			break;
		default:
			$('#cad').hide();
			$('#cae').hide();
			$('#cam').hide();
			$('#otros').hide();
			break;
	}
	
	
	if ($('#presupuestoMeca option:selected').attr('id')=='6')
			$('#otroPresupuesto').show("slide", {}, 1000);
	$('#presupuestoMeca').bind('change',function(){
		if ($('#presupuestoMeca option:selected').attr('id')=='6')
			$('#otroPresupuesto').show("slide", {}, 1000)
		else
			$('#otroPresupuesto').hide();
	});
	
	if ($('#tipoCad option:selected').attr('id')=='6')
			$('#otroCad').show("slide", {}, 1000);
	$('#tipoCad').bind('change',function(){
		if ($('#tipoCad option:selected').attr('id')=='6'){
			$('#otroCad').show("slide", {}, 1000)
		}
		else{
			$('#otroCad:visible').hide();
		}
	});
		
	$('#cmdBIDALI_onclick').click(function(){
		if(validarEmail())
		{
			//if(validarCampos()
			var stSubject = "ARITU WEB - " + $('p.formulario').attr('name'); 
			var stBody = creaMensaje();
				
			$.ajax({
				url: "../enviarMail.asp",
				cache: false,
				type: "GET",
				data: "stFromName="+ 
					  "&stSubject="+escape(stSubject)+
					  "&stBody="+escape(stBody),
				success: function(html){
					$("#Nuevo").html(html);
					},
			    error: function(xhr, ajaxOptions, thrownError){
                    $("#Nuevo").html(xhr.responseText);
					}
			});
		}
		else{
			alert('Email incorrecto');
		}
	});		
});

function creaMensaje(){
	var mensaje = '';
	var tipoForm=$('input[name=txtTIPO_FORM]').val();
	//comun	
	var cia = $("input[name=txtEMPRESA]").val();
	cia==undefined?cia='':cia=cia;
	
	var nombre = $("input[name=txtNOMBRE]").val();
	nombre==undefined?nombre='':nombre=nombre;
	
	var email = $("input[name=txtEMAIL]").val();
	email==undefined?email='':email=email;
	
	var tfno = $("input[name=txtTFNO]").val();
	tfno==undefined?tfno='':tfno=tfno;
	
	mensaje = "Compañía: <strong>" + cia + "</strong>";
	mensaje += "<br/> Nombre: <strong>" + nombre + "</strong>";
	mensaje += "<br/> Email: <strong>" + email + "</strong>";
	mensaje += "<br/> Teléfono: <strong>" + tfno + "</strong>";
	
	
	if(tipoForm==""){
		var consulta = $("textarea[name=txtCONSULTA]").val();
		consulta==undefined?consulta='':consulta=consulta;
	
		mensaje += "<br/> Comentarios: <strong>" + consulta + "</strong>";
	}
	else{
		//mensaje frm comun
		var empresa = $("input[name=txtEMPRESA]").val();
		empresa==undefined?empresa='':empresa=empresa;
	
		var ciudad = $("input[name=txtCIUDAD]").val();
		ciudad==undefined?ciudad='':ciudad=ciudad;
	
		var puesto = $("input[name=txtPUESTO]").val();
		puesto==undefined?puesto='':puesto=puesto;
	
		mensaje += "<br/> Empresa: <strong>" + empresa + "</strong>";
		mensaje += "<br/> Ciudad: <strong>" + ciudad + "</strong>";
		mensaje += "<br/> Puesto: <strong>" + puesto + "</strong>";
	
		if(tipoForm=="1"){
			//mensaje frm=1
			var presupuestoMeca = $("#presupuestoMeca").val();
			presupuestoMeca==undefined?presupuestoMeca='-':presupuestoMeca=presupuestoMeca;
	
			var otroPresupuesto = $("input[name=txtOTROPRESUPUESTO]").val();
			otroPresupuesto==undefined?otroPresupuesto='-':otroPresupuesto=otroPresupuesto;
	
			var tipoCad = $("#tipoCad").val();
			tipoCad==undefined?tipoCad='-':tipoCad=tipoCad;
	
			var otroCad = $("input[name=txtOTROCAD]").val();
			otroCad==undefined?otroCad='-':otroCad=otroCad;
	
			mensaje += "<br/> Presupuesto mecánica: <strong>" + presupuestoMeca + "</strong>";
			mensaje += "<br/> Otro presupuesto: <strong>" + otroPresupuesto + "</strong>";
			mensaje += "<br/> Tipo CAD: <strong>" + tipoCad + "</strong>";
			mensaje += "<br/> Otro CAD: <strong>" + otroCad + "</strong>";
		}
		else{
			//mensaje frm=2
			var presupuestoProe = $("#presupuestoProe").val();
			presupuestoProe==undefined?presupuestoProe='':presupuestoProe=presupuestoProe;
	
			var presupuesto = '';
			$('input[type=checkbox]').each(function(){
			    if($(this).attr('checked')==true)
			        presupuesto += $(this).val() + ",";
			});
			presupuesto=presupuesto.substr(0,presupuesto.length-1);
	
			var cad = $("input[name=txtCAD]").val();
			cad==undefined?cad='-':cad=cad;
				
			var numCad = $("input[name=txtNUMCAD]").val();
			numCad==undefined?numCad='-':numCad=numCad;
	
			mensaje += "<br/> Presupuesto Proe: <strong>" + presupuestoProe + "</strong>";
			mensaje += "<br/> Detalles presupuesto: <strong>" + presupuesto + "</strong>";
			mensaje += "<br/> CAD Actual: <strong>" + cad + "</strong>";
			mensaje += "<br/> Nº puestos de CAD: <strong>" + numCad + "</strong>";
		}
	}
	
	return mensaje;
}

function validarEmail()
{
	var email = $('input[name=txtEMAIL]').val();
	if(email!='')
	{
	  // validar la cuenta de correo usando una expresión regular (RegExp)
		if(email.search(/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/ig))
			return false;
		else
			return true;
	}
	else
		return false;
}
		  
function validar(e)
{ 
	tecla = (document.all) ? e.keyCode : e.which; 
	if (tecla==8) return true; //Tecla de retroceso (para poder borrar) 
	patron = /\d/; //ver nota 
	te = String.fromCharCode(tecla); 
	return patron.test(te);  
} 