//Funcion para envio de formulario, donde form es el atributo name del mismo

function guardar(action, formulario){



	resultado=megavalidacion(formulario);

//	alert('Acabe validacion = '+resultado);

//	resultado=true;

	if (resultado!=false)

	{

		if (formulario=="cuentaform"){

				envia_solicitud(action);

		}

		else if(formulario=="cobform")

		{

		envia_cobertura(action);

		}

		else if(formulario=="promform")

		{

		envia_prom(action);

		}

		else if(formulario=="contacto")

		{

		envia_correo(action);

		}

		else if(formulario=="duda")

		{

		envia_duda(action);

		}

		else if(formulario=="wizardform")

		{

		envia_wiz(action);

		}

		else

		{

			if(document.forms[formulario].TelefonoFijo.value == '' && document.forms[formulario].TelefonoMovil.value == '')

				alert('Debes completar uno de los campos de telefono');

			else

				envia_datos(action);

		}		

		 //setTimeout("cambio_pag('"+dondevoy+"')", 2600);

	}

	else

	{

		alert('Hay datos incorrectos y/o campos obligatorios sin rellenar');

	}

		

	resultado="";

}

function BorrarArchivo(ref, line)

{

  respuesta = document.getElementById('messageform');

  envia('ajax/deletefile.php', 'file='+ref);

  document.getElementById('documents').deleteRow(line);

  respuesta = document.getElementById('uploadform');

  envia('zonausuario/showform.php', '');  

}

function GetAyuda()

{

document.getElementById('operadora').style.display='block';

respuesta = document.getElementById('operadora');

envia('ayuda_contacto.php','status=show');

}

function ShowMessage()

{

respuesta = document.getElementById('operadora');

document.getElementById('operadora').style.display='none';

envia('ayuda_contacto.php','status=hide');

}

function comsubv(lineform)

{

	if(megavalidacion(lineform))

	{

		respuesta = document.getElementById('response');

		envia('ajax/subvencion.php',

		'nombre='+document.subvform.nombre.value+

		'&linea='+document.subvform.linea.value+

		'&email='+document.subvform.email.value+

		'&telefono='+document.subvform.telefono.value);

	}

	else

	{

		alert('Hay datos incorrectos y/o campos obligatorios sin rellenar');

		return false;

	}

}

function comlinea(lineform, adsl)

{

	if(megavalidacion(lineform))

	{
		var nom = document.getElementById('nombre'+adsl).value;
		var lin = document.getElementById('linea'+adsl).value;
		var email = document.getElementById('email'+adsl).value;
		var tel = document.getElementById('telefono'+adsl).value;
		respuesta = document.getElementById('compra');

		envia('ajax/pedidoadsl.php',

		'articulo='+adsl+

		'&nombre='+nom+

		'&linea='+lin+

		'&email='+email+

		'&telefono='+tel);

		return true;

	}

	else

	{

		alert('Hay datos incorrectos y/o campos obligatorios sin rellenar');

		return false;

	}

}

function lineaadsl(adsl)

{

respuesta = document.getElementById('compra');

envia('servicios/agregarform.php','articulo='+adsl);

}

function guardar_t_p_medida(formulario)

{

	resultado=megavalidacion(formulario);



	if (resultado!=false)

	{

		procesar_tpmform();

	}

	else

	{

		alert('Hay datos incorrectos y/o campos obligatorios sin rellenar');

	}

	resutado="";

}

function guardar_empresa(formulario)

{

	resultado=megavalidacion(formulario);



	if (resultado!=false)

	{

		procesar_gran_empresa();

	}

	else

	{

		alert('Hay datos incorrectos y/o campos obligatorios sin rellenar');

	}

	resutado="";

}

function guardar_facturacion(action,formulario)

{

	if(document.forms[formulario].facturacion.checked == true)

		rellenar(0);

		

resultado=megavalidacion(formulario);

//	resultado=true;

	if (resultado!=false){

		if(document.getElementById('TelefonoFijo').value != '' || document.getElementById('TelefonoMovil').value != '') 

			facturacion(action);

		else

		{

			document.getElementById('TelefonoFijo').className='error';	

			document.getElementById('TelefonoMovil').className='error';

		}

			

	}

	else

	{

		alert('Hay datos incorrectos y/o campos obligatorios sin rellenar');

	}

	resutado="";

}



function guardar_facturacion2(action,formulario)

{

	if(document.forms[formulario].facturacion.checked == true)

		rellenar(0);

		

resultado=megavalidacion(formulario);

//	resultado=true;

	if (resultado!=false){

		if(document.getElementById('TelefonoFijo').value != '' || document.getElementById('TelefonoMovil').value != '') 

			facturacion2(action);

		else

		{

			document.getElementById('TelefonoFijo').className='error';	

			document.getElementById('TelefonoMovil').className='error';

		}

			

	}



	else

	{

		alert('Hay datos incorrectos y/o campos obligatorios sin rellenar');

	}

	

	resutado="";

}





var scroll_top = function(){



	if(window.pageYOffset){

		return window.pageYOffset;

	 }

	 else {

		return  Math.max(document.body.scrollTop,document.documentElement.scrollTop);

	 }



}

function compEmail(formulario)

{

	if(formulario.email.value!=formulario.confirmacion.value)

	{

	alert("Los campos de e-mail no coinciden");

	formulario.email.focus();

	formulario.email.select();

	return false;

	}

	else

	{

		return true;

	}

}

function comprobarEmail(formulario)

{

	if(formulario.email.value!=formulario.confirmacion.value)

	{

	alert("Los campos de e-mail no coinciden");

	formulario.email.focus();

	formulario.email.select();

	return false;

	}

	else

	{

		if(formulario.clave.value!=formulario.claveconfirm.value)

		{

		alert("Los campos de clave no coinciden");

		formulario.clave.focus();

		formulario.clave.select();

		return false;

		}

		else{

		return true; 

		}

	}

}

function blanco(){

  this.className="ok";

}



function cambiaImagen(indice, movil){



	document.images["top"].src = arrayImages[indice-1].src;



	var myDiv1=document.getElementById("modseleccionado");



	myDiv1.innerHTML=movil;

}



function rellenar(valor){

	if(valor==0)

	{
	
	document.getElementById("nombreEnvio").value=document.getElementById("nombre3").value;

	document.getElementById("tipoviaEnvio").selectedIndex=document.getElementById("tipovia").selectedIndex;	

	document.getElementById("direccionEnvio").value=document.getElementById("direccion").value;

	document.getElementById("numeroEnvio").value=document.getElementById("numero").value;

	document.getElementById("escaleraEnvio").value=document.getElementById("escalera").value;

	document.getElementById("codigopostalEnvio").value=document.getElementById("codigopostal").value;

	document.getElementById("provinciaEnvio").selectedIndex=document.getElementById("provincia").selectedIndex;

	document.getElementById("localidadEnvio").value=document.getElementById("localidad").value;

	document.getElementById("datosenvio").style.display = 'none';

	}

	else

	{
	document.getElementById("datosenvio").style.display = 'block';

	}

}


//Borra el contenido de formularios que ya tenian contenido inicialmente ("edicion")



function borrar(formulario)

{

campos = document.forms[formulario].elements;



	for (i=0; i<document.forms[formulario].elements.length; i++) 

	{

		objeto = document.forms[formulario].elements[i] ;

		

		if(objeto.value != undefined)

		{	

			if(objeto.tagName == 'select')

				objeto.selectedIndex = 0;

			else if(objeto.type == 'checkbox')

					objeto.checked = "false";

					else

						objeto.value='';

			

		}

	}

}





function buscarrapido(){

document.forms['busqterminales'].submit();

}



//Funcion que cambia el action del formulario para procesar la busqueda

function cambiarclienterapido()

{	

	if(document.compraRapida.clienteder.value == 1)

		document.compraRapida.action = 'moviles-orange/resultados.php';

	else if(document.compraRapida.clienteder.value == 2)

			document.compraRapida.action = 'empresas-orange/resultados.php';

		 else

			document.compraRapida.action = 'libres-orange/resultados.php';

}		





//Funciones para el comparador de terminales

function comparadorterminales(objeto,refrescar)

{



	if(objeto.tagName == 'INPUT') //agregamos/eliminamos desde el resultado de busqueda

	{	

		if(objeto.checked == true)

			accion = 'agregar';

		else

			accion = 'eliminar';

		

		movil = objeto.value;

		

		respuesta = document.getElementById('semaforo');

		envia('movil/comparador-de-terminales.php','movil='+movil+'&accion='+accion+'&input='+objeto.id+'&refrescar='+refrescar);

	}

	

	if(objeto.tagName == 'A') //eliminamos desde el propio comparador o añadimos desde ficha

	{	

		if(refrescar == 'si')

		{

			window.scrollTo(0,0);

			respuesta = document.getElementById('contenido');
			
			if(objeto.id.indexOf('mov')!=-1)
			var term = objeto.id.replace("mov","" )
			else 
			var term = objeto.id;

			envia('movil/comparador-de-terminales.php','movil='+objeto.id+'&accion=eliminar&refrescar='+refrescar);		

		}

		

		else

		{
			if(objeto.id.indexOf('mov')!=-1)
			var term = objeto.id.replace("mov","" )
			else 
			var term = objeto.id;
			
			respuesta = document.getElementById('semaforo');

			envia('movil/comparador-de-terminales.php','movil='+term+'&accion=agregar&refrescar='+refrescar);		



		}

	}

	

}



//Refresca el resumen de tu compra ("cesta")

function refrescarresumen()

{		
		$('#micompra').load('resumencompra.php');
		//respuesta = document.getElementById('micompra');
		//envia('resumencompra.php','');

}



function valorar(mivaloracion,id)

{

respuesta = document.getElementById('valoracion');

envia('movil/valoracion.php','valoracion='+mivaloracion+'&id='+id);

}

function searchadsl()

{

	var email = document.getElementById('mailadsl').value;



	if( email != '')

    {			

		if(megavalidacion('adslform'))

		{

			showSol(document.getElementById('mailadsl').value)

			
			/*
			respuesta = document.getElementById('adslrespuesta');

			envia('ajax/newsletter.php','linea='+document.getElementById('mailadsl').value);

			return true;
			*/
			

		}

		

		else

			alert('El numero introducido no es valido');

	}

	

	else

		alert('Debes introducir un numero de telefono valido');

}

function showSol(campo)

{

document.getElementById('hiddenline').value=campo	

document.getElementById('line').value=campo

document.getElementById('Asistente').style.display='block'
document.getElementById('Asisback').style.display='block';
return true

}

function AddDatos(que)

{

	respuesta = document.getElementById('adslrespuesta');	

/*

	contact_val=getCheckedValue(document.adslfrm.envio);	

	if(contact_val=='0')

	{	

		envia('ajax/mailadsl.php',

		'coment='+que+

		'&linea='+document.getElementById('hiddenline').value);

		document.getElementById('Asistente').style.display='none'

	}

	else

	{

*/

	if(megavalidacion('adslform'))

		{

		envia('ajax/mailadsl.php',

		'coment='+que+

		'&linea='+document.adslfrm.hiddenline.value+

		'&nombre='+document.adslfrm.nombre.value+

		'&telefono='+document.adslfrm.telefono.value);
		document.getElementById('Asisback').style.display='none';
		document.getElementById('Asistente').style.display='none';

		}		

	//}

}




function boletin()

{

	var email = document.getElementById('mailboletin').value;

	

	if( email != '' && email != 'introduce tu e-mail')

    {			

		if(megavalidacion('boletinform'))

		{

			respuesta = document.getElementById('semaforo');

			envia('boletin.php','mailboletin='+document.getElementById('mailboletin').value);

		}

		

		else

			alert('El e-mail introducido no es valido');

	}

	

	else

		alert('Debes introducir un e-mail valido');

}





//Funcion de busqueda en noticias y reviews

function buscar(criterio,quebusco)

{



	if(criterio == 'palabras')

	{

		if(document.getElementById('palabras').value == '')

			document.getElementById('palabras').value = ' ';

		

		document.getElementById('archivo').selectedIndex = 0;

	}

	else

		document.getElementById('palabras').value = '';

	

	

	if(quebusco == 'noticias')		

		document.forms['noticiasfrm'].submit();	

	else

		document.forms['reviewsfrm'].submit();	

}



//Funcion que muestra vista ampliada foto noticias/reviews

function popup(accion)

{

	if(accion == true)

		document.getElementById("popup").style.display="block";

	

	else

		document.getElementById("popup").style.display="none";

		

}



//Funcion que muestra vista ampliada foto noticias/reviews

function popuppubli(accion)

{

	if(accion == true)

		document.getElementById("popuppublicidad").style.display="block";

	

	else

		document.getElementById("popuppublicidad").style.display="none";

		

}







//Funcion que selecciona automaticamente tipo documento segun tipo cliente

function tipodecliente(tipo)

{

selector = document.getElementById('tipodocumento');

opciones = document.getElementById('tipodocumento').options;



	if(tipo == 'empresa')

	{

		for(opcion in opciones)

		{

			if(opciones[opcion].value == 'C')

				selector.selectedIndex = opcion;

		}

	}		

	

	else

	{

		for(opcion in opciones)

		{

			if(opciones[opcion].value == 'N')

				selector.selectedIndex = opcion;

		}

	}

}



//Funcion para recomendar terminales

function recomendar()

{



	if(megavalidacion('recomendarfrm'))

	{	

		document.getElementById('recomendar').style.display='none';

		urlcod = document.location.href;

		urlcod = urlcod.replace(/&/g,"%");

		respuesta = document.getElementById('semaforo');

		envia('movil/recomendar.php',

		'nombre='+document.getElementById('recomendarfrm').txtnombre.value+

		'&mensaje='+document.getElementById('mensaje').value+

		'&email='+document.getElementById('email').value+

  		'&url='+urlcod);

  		

  		document.forms['recomendarfrm'].reset();

	}

}



function error404()

{

 document.getElementById('contenido').innerHTML = '<div id="error" style="margin: 80px auto;text-align:center;"><img src="images/error404.jpg" /></div>';

}



//Funcion para recordar password cuenta usuario

function recordar()

{

	if(megavalidacion('cuentaform'))

	{

	respuesta = document.getElementById('motor');

	envia('inicio/recordar.php','email='+document.getElementById("email").value);

	}

}



//Funciones para contenedor de carga en procesos AJAX

function aparecer()

{	

	//if(document.getElementById("procesandomsg") == 'undefined' || document.getElementById("procesandomsg").style.display != "block")

	//{

	if(document.getElementById("procesandomsg"))

		document.getElementById("procesandomsg").style.display = "block";

	//document.getElementById("procesando").style.display = "block";

	//fader.fadeIn();

	//}

}



function desvanecer()

{

	document.getElementById("procesandomsg").style.display = "none";

	//fader.fadeOut();

}





//Mostrar tarifas empresa con ancla en ventan nueva

function verTarifas(ancla,segmento)

{

	if(segmento=='empresas')

	ventanaSecundaria('http://www.latiendaconexion.com/empresas/tarifas-empresas.php'+ancla,700);

	

	if(segmento=='particular')

	ventanaSecundaria('http://www.latiendaconexion.com/movil/tarifas-particular.php'+ancla,700);



}





//Funcion para nueva ventana personalizada sin barra estado, navegacion, no redimensionable

function ventanaSecundaria(URL,ancho)

{		

   window.open(URL,null,"width="+ancho+",scrollbars=YES,resizable=NO,status=NO,toolbar=NO");

}





//Funciones que devuelven dimensiones y scroll del area de contenido del navegador

function f_clientWidth() {

	return f_filterResults (

		window.innerWidth ? window.innerWidth : 0,

		document.documentElement ? document.documentElement.clientWidth : 0,

		document.body ? document.body.clientWidth : 0

	);

}

function f_clientHeight() {

	return f_filterResults (

		window.innerHeight ? window.innerHeight : 0,

		document.documentElement ? document.documentElement.clientHeight : 0,

		document.body ? document.body.clientHeight : 0

	);

}

function f_scrollTop() {

	return f_filterResults (

		window.pageYOffset ? window.pageYOffset : 0,

		document.documentElement ? document.documentElement.scrollTop : 0,

		document.body ? document.body.scrollTop : 0

	);

}

function f_scrollLeft() {

    return f_filterResults (

        window.pageXOffset ? window.pageXOffset : 0,

        document.documentElement ? document.documentElement.scrollLeft : 0,

        document.body ? document.body.scrollLeft : 0

    );

}



function f_filterResults(n_win, n_docel, n_body) {

	var n_result = n_win ? n_win : 0;

	if (n_docel && (!n_result || (n_result > n_docel)))

		n_result = n_docel;

	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;

}



//Funcion guarda contacto de promocion

function guardar_contacto(segmento)

{

	resultado=megavalidacion('dudaformulario');



	if (resultado!=false)

	{

		respuesta = document.getElementById('semaforo');

		envia('guardar_contacto.php','nombre='+document.dudaformulario.nombre.value+'&email='+document.dudaformulario.email.value+'&telefono='+document.dudaformulario.telefono.value+'&producto='+document.dudaformulario.producto.value+'&segmento='+segmento);

	}

	else

	{

		alert('Hay datos incorrectos y/o campos obligatorios sin rellenar');

	}

	resultado="";



}



//Funcion guarda contacto de promocion con codigo promocional

function guardar_contacto2()

{

	var segmento;



	url = window.location.search;

	

	if(url.search('movil')>-1)

		segmento = 'residencial';

	

	else if(url.search('empresas')>-1)

			segmento = 'empresas';

	

		 else

			segmento = 'libres';



	resultado=megavalidacion('dudaformulario');



	if (resultado!=false)

	{

		respuesta = document.getElementById('semaforo');

		envia('guardar_contacto.php','nombre='+document.dudaformulario.nombre.value+'&email='+document.dudaformulario.email.value+'&telefono='+document.dudaformulario.telefono.value+'&producto='+document.dudaformulario.producto.value+'&segmento='+segmento+'&codigopromocional='+document.dudaformulario.codigopromocional.value);

	}

	else

	{

		alert('Hay datos incorrectos y/o campos obligatorios sin rellenar');

	}

	resultado="";



}

function ejec_procesoCompra(evento,direccion)
{
	direccion = direccion.replace(/&/gi,'-_-');
	
	respuesta = document.getElementById('procesoCompra');
	
	envia('procesoCompra.php','evento='+evento+
			'&url='+direccion);
}

function paginador(url,capa,pagina)
{
	respuesta=document.getElementById(capa);
	envia(url,'pagina='+pagina);
}