﻿$(document).ready(function(){
	/* The following code is executed once the DOM is loaded */
	
	$('.sponsorFlip').bind("click",function(){
		
		// $(this) point to the clicked .sponsorFlip element (caching it in elem for speed):
		
		var elem = $(this);
		
		// data('flipped') is a flag we set when we flip the element:
		
		if(elem.data('flipped'))
		{
			// If the element has already been flipped, use the revertFlip method
			// defined by the plug-in to revert to the default state automatically:
			
			elem.revertFlip();
			
			// Unsetting the flag:
			elem.data('flipped',false)
		}
		else
		{
			// Using the flip method defined by the plugin:
			
			elem.flip({
				direction:'lr',
				speed: 350,
				onBefore: function(){
					// Insert the contents of the .sponsorData div (hidden from view with display:none)
					// into the clicked .sponsorFlip div before the flipping animation starts:
					
					elem.html(elem.siblings('.sponsorData').html());
				}
			});
			
			// Setting the flag:
			elem.data('flipped',true);
		}
	});
	
	
	$('#formulario-contacto #btn-enviar, #formulario-contacto-ancho #btn-enviar').click(solicitarInformacionClick);
});

function solicitarInformacionClick()
{
	var bValid = true;
	
	var fld_nombre = $("#fld-nombre");
	var fld_empresa = $("#fld-organizacion");
	var fld_puesto = $("#fld-puesto");
	var fld_email = $("#fld-email");
	var fld_telefono = $("#fld-telefono");
	var fld_motivo = $("#fld-motivo");
	
	$([fld_nombre, fld_empresa, fld_puesto, fld_email, fld_telefono, fld_motivo]).each(function() { $(this).removeClass('ui-state-error') })

	bValid = checkLength(fld_nombre,1,255) && bValid;
	bValid = checkLength(fld_empresa,1,255) && bValid;
	bValid = checkLength(fld_puesto,1,255) && bValid;
	bValid = checkLength(fld_email,1,255) && bValid; 
	bValid = checkLength(fld_telefono,1,255) && bValid;
	
	if (bValid) 
	{
		solicitarInformacion(fld_nombre.val(), fld_empresa.val(), fld_puesto.val(), fld_email.val(), fld_telefono.val(), fld_motivo.val());
	}
}

function solicitarInformacion(nombre, empresa, puesto, email, telefono, motivo) 
{
	var soapEnv =
		"<?xml version=\"1.0\" encoding=\"utf-8\"?> \
		<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> \
		  <soap:Body> \
			<SolicitarInformacion xmlns=\"http://tempuri.org/\"> \
			  <ListName>Solicitud de información</ListName> \
			  <Nombre><![CDATA[" + nombre + "]]></Nombre> \
			  <Empresa><![CDATA[" + empresa + "]]></Empresa> \
			  <Puesto><![CDATA[" + puesto + "]]></Puesto> \
			  <Email><![CDATA[" + email + "]]></Email> \
			  <Telefono><![CDATA[" + telefono + "]]></Telefono> \
			  <Motivo><![CDATA[" + motivo + "]]></Motivo> \
			</SolicitarInformacion> \
		  </soap:Body> \
		</soap:Envelope>";

	$.ajax({
		url: "http://www.trentia.es/_vti_bin/trentia.asmx",
		beforeSend: function(xhr) {
			xhr.setRequestHeader("SOAPAction",
			"http://tempuri.org/SolicitarInformacion");
		},
		type: "POST",
		dataType: "xml",
		data: soapEnv,
		complete: processResult,
		contentType: "text/xml; charset=utf-8"
	});
	
	function processResult(xData, status)
	{
		if (status!='success') 
		{
			showDialog('Error en el envío de la petición.');
			//showDialog(htmlEncode(xData.responseText, false));
		}
		else if ($(xData.responseXML).find("SolicitarInformacionResult").text()!='true')
		{
			showDialog('Error en el envío de la petición.');
			//showDialog(htmlEncode(xData.responseText, false));
		} 
		else
		{		    	
			showDialog('Su petición se ha recibido correctamente.');
		}
	}					
}

