﻿function solicitarInformacion(nombre, empresa, email, telefono, motivo, movil) 
{
	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> \
			  <Email><![CDATA[" + email + "]]></Email> \
			  <Telefono><![CDATA[" + telefono + "]]></Telefono> \
			  <Movil><![CDATA[" + movil + "]]></Movil> \
			  <Motivo><![CDATA[" + motivo + "]]></Motivo> \
			</SolicitarInformacion> \
		  </soap:Body> \
		</soap:Envelope>";

	$.ajax({
		url: "http://www.femete.es/_vti_bin/femete.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.');
		}
	}					
}

function solicitarInformacionFormacion(nombre, empresa, email, telefono, desempleados, motivo, movil, convenio) 
{
	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> \
			<SolicitarInformacionFormacion xmlns=\"http://tempuri.org/\"> \
			  <ListName>Solicitud de información</ListName> \
			  <Nombre><![CDATA[" + nombre + "]]></Nombre> \
			  <Empresa><![CDATA[" + empresa + "]]></Empresa> \
			  <Email><![CDATA[" + email + "]]></Email> \
			  <Telefono><![CDATA[" + telefono + "]]></Telefono> \
			  <Desempleados><![CDATA[" + (desempleados?1:0) + "]]></Desempleados> \
			  <Movil><![CDATA[" + movil + "]]></Movil> \
			  <Convenio><![CDATA[" + convenio + "]]></Convenio> \
			  <Motivo><![CDATA[" + motivo + "]]></Motivo> \
			</SolicitarInformacionFormacion> \
		  </soap:Body> \
		</soap:Envelope>";

	$.ajax({
		url: "http://www.femete.es/formacion_empleo/_vti_bin/femete.asmx",
		beforeSend: function(xhr) {
			xhr.setRequestHeader("SOAPAction",
			"http://tempuri.org/SolicitarInformacionFormacion");
		},
		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("SolicitarInformacionFormacionResult").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.');
		}
	}					
}

/**
 * HTML-Encode the supplied input
 * 
 * Parameters:
 *
 * (String)  source    The text to be encoded.
 * 
 * (boolean) display   The output is intended for display.
 *
 *                     If true:
 *                     * Tabs will be expanded to the number of spaces 
 *                       indicated by the 'tabs' argument.
 *                     * Line breaks will be converted to <br />.
 *
 *                     If false:
 *                     * Tabs and linebreaks get turned into &#____;
 *                       entities just like all other control characters.
 *
 * (integer) tabs      The number of spaces to expand tabs to.  (Ignored 
 *                     when the 'display' parameter evaluates to false.)
 *
 * v 0.3 - January 4, 2006
 */
function htmlEncode(source, display, tabs)
{
	function special(source)
	{
		var result = '';
		for (var i = 0; i < source.length; i++)
		{
			var c = source.charAt(i);
			if (c < ' ' || c > '~')
			{
				c = '&#' + c.charCodeAt() + ';';
			}
			result += c;
		}
		return result;
	}
	
	function format(source)
	{
		// Use only integer part of tabs, and default to 4
		tabs = (tabs >= 0) ? Math.floor(tabs) : 4;
		
		// split along line breaks
		var lines = source.split(/\r\n|\r|\n/);
		
		// expand tabs
		for (var i = 0; i < lines.length; i++)
		{
			var line = lines[i];
			var newLine = '';
			for (var p = 0; p < line.length; p++)
			{
				var c = line.charAt(p);
				if (c === '\t')
				{
					var spaces = tabs - (newLine.length % tabs);
					for (var s = 0; s < spaces; s++)
					{
						newLine += ' ';
					}
				}
				else
				{
					newLine += c;
				}
			}
			// If a line starts or ends with a space, it evaporates in html
			// unless it's an nbsp.
			newLine = newLine.replace(/(^ )|( $)/g, '&nbsp;');
			lines[i] = newLine;
		}
		
		// re-join lines
		var result = lines.join('<br />');
		
		// break up contiguous blocks of spaces with non-breaking spaces
		result = result.replace(/  /g, ' &nbsp;');
		
		// tada!
		return result;
	}

	var result = source;
	
	// ampersands (&)
	result = result.replace(/\&/g,'&amp;');

	// less-thans (<)
	result = result.replace(/\</g,'&lt;');

	// greater-thans (>)
	result = result.replace(/\>/g,'&gt;');
	
	if (display)
	{
		// format for display
		result = format(result);
	}
	else
	{
		// Replace quotes if it isn't for display,
		// since it's probably going in an html attribute.
		result = result.replace(new RegExp('"','g'), '&quot;');
	}

	// special characters
	result = special(result);
	
	// tada!
	return result;
}
