var formDiganos = null;

function agregarDemanda(f) {
	formDiganos = f;	
	var params = [];
	for(var i in f.elements) {
		try {
			if(f.elements[i].value != undefined && 
			  f.elements[i].value != "Solicitar" &&
			  f.elements[i].name != undefined &&
			  f.elements[i].name != null)
				params.push(f.elements[i].name +"="+ f.elements[i].value);
		} catch(e) {}
	}
	params = encodeURIComponent(params.join("&"));
	request2("diganos.asp", params, onAgregadaDemanda);	
}

function onAgregadaDemanda(transport) {
	alert(transport.responseText);
	formDiganos.reset();
}
	
function request2(url, params, func) {
	new Ajax.Request(url, {	method: 'post',	parameters: params, onSuccess: func});
}

var formAgregarDemanda;

function validarAgregarDemanda(f) {

	formAgregarDemanda = f;
	var camposVacios = "";

	if (f.elements.id_marca.value == "0") {
		alert("Debe seleccionar una marca");
		return false;
	}
	
	if (f.elements.id_gama.value == "0") {
		alert("Debe seleccionar una gama");
		return false;
	}

	for(var i=0 in f.elements) {
		try {
			if(f.elements[i].value == "") {
				if (f.elements[i].name != "comentarios") {
					camposVacios += "El campo \""+f.elements[i].name +"\" no puede estar vacio\n";
				}
			}
		} catch(e) {}
	}
	if(camposVacios != "") {
		alert(camposVacios);
		return false;
	} else {
		var params = "nombre="+ 	encodeURIComponent(f.nombre.value);
		params += "&apellidos="+ 	encodeURIComponent(f.apellidos.value);
		params += "&email="+ 		encodeURIComponent(f.email.value);
		params += "&telefono="+ 	encodeURIComponent(f.telefono.value);
		params += "&id_modelo="+ 	encodeURIComponent(f.id_modelo.value);
		params += "&id_gama="+ 		encodeURIComponent(f.id_gama.value);
		params += "&kms="+ 			encodeURIComponent(f.kms.value);
		params += "&precio_desde="+ encodeURIComponent(f.precio_desde.value);
		params += "&precio_hasta="+ encodeURIComponent(f.precio_hasta.value);
		params += "&puertas="+ 		encodeURIComponent(f.puertas.value);
		params += "&comentarios="+ 	encodeURIComponent(f.comentarios.value);
		
		var url ="gestion/util/notifyTellUs.php";													
		new Ajax.Request(url, {	method: 'post', parameters: params, onSuccess: onNotificadaDemanda});
	}
}

function onNotificadaDemanda(transport) {
	if(transport.responseText) {
		alert("email enviado");	
	}
	formAgregarDemanda.submit();
}


function sendForm(theForm) {
	theForm.elements.id_marca_txt.value = theForm.elements.id_marca.options[theForm.elements.id_marca.selectedIndex].text;
	theForm.elements.id_modelo_txt.value = theForm.elements.id_modelo.options[theForm.elements.id_modelo.selectedIndex].text;
	theForm.elements.id_gama_txt.value = theForm.elements.id_gama.options[theForm.elements.id_gama.selectedIndex].text;
	theForm.elements.precio_desde_txt.value = theForm.elements.precio_desde.options[theForm.elements.precio_desde.selectedIndex].text;
	theForm.elements.precio_hasta_txt.value = theForm.elements.precio_hasta.options[theForm.elements.precio_hasta.selectedIndex].text;
	theForm.elements.kms_txt.value = theForm.elements.kms.options[theForm.elements.kms.selectedIndex].text;
	theForm.submit();
}