var nav4 = window.Event ? true : false;
function imposeNumber(evt)
{
	var key = nav4 ? evt.which : evt.keyCode;
	return (key <= 13 || (key >= 48 && key <= 57) || key == 46);
}

function solotelefono(input)
{
	$("#"+input).keyup(function(){
								
	var cadenan = $(this).val();
	  
	var cadena=new Array();
	for(k=0; k<cadenan.length; k++) 
	{
		cadena[k]=cadenan.substring(k, k+1);
	}
	
	if(cadena[0] == 9 || cadena[0] == 8 || cadena[0] == 6 )
	{
		if ($(this).val() != '')
		$(this).val($(this).attr('value').replace(/[^0-9]/g, ""));
	}
	else
		$(this).val('');
	});
}
function solonumero(input)
{
	$("#"+input).keyup(function(){
		if ($(this).val() != '')
		$(this).val($(this).attr('value').replace(/[^0-9]/g, ""));
	});
}
//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))
	{
		respuesta = document.getElementById('compra');
		envia('ajax/pedidoadsl.php',
		'articulo='+adsl+
		'&nombre='+document.lineaform.nombre.value+
		'&linea='+document.lineaform.linea.value+
		'&email='+document.lineaform.email.value+
		'&telefono='+document.lineaform.telefono.value);
		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("nombreFacturacion").value;
	document.getElementById("tipoviaEnvio").selectedIndex=document.getElementById("tipoviaFacturacion").selectedIndex;	
	document.getElementById("direccionEnvio").value=document.getElementById("direccionFacturacion").value;
	document.getElementById("numeroEnvio").value=document.getElementById("numeroFacturacion").value;
	document.getElementById("escaleraEnvio").value=document.getElementById("escaleraFacturacion").value;
	document.getElementById("codigopostalEnvio").value=document.getElementById("codigopostalFacturacion").value;
	document.getElementById("provinciaEnvio").selectedIndex=document.getElementById("provinciaFacturacion").selectedIndex;
	document.getElementById("localidadEnvio").value=document.getElementById("localidadFacturacion").value;
	document.getElementById("datosenvio").style.display = 'none';
	}
	else
	{
	document.getElementById("nombreEnvio").value='';
	document.getElementById("tipoviaEnvio").selectedIndex=0;	
	document.getElementById("direccionEnvio").value='';
	document.getElementById("numeroEnvio").value='';
	document.getElementById("escaleraEnvio").value='';
	document.getElementById("codigopostalEnvio").value='';
	document.getElementById("provinciaEnvio").selectedIndex='0';
	document.getElementById("localidadEnvio").value='';
	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.cliente.value == 1)
		document.compraRapida.action = 'http://www.latiendaconexion.com/moviles-orange/catalogo-de-terminales.php';
	else if(document.compraRapida.cliente.value == 2)
			document.compraRapida.action = 'http://www.latiendaconexion.com/empresas-orange/catalogo-de-terminales.php';
		 else
			document.compraRapida.action = 'http://www.latiendaconexion.com/moviles-orange/catalogo-de-terminales.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');
			envia('movil/comparador_de_terminales.php','movil='+objeto.id+'&accion=eliminar&refrescar='+refrescar);		
		}
		
		else
		{
			respuesta = document.getElementById('semaforo');
			envia('movil/comparador_de_terminales.php','movil='+objeto.id+'&accion=agregar&refrescar='+refrescar);		

		}
	}
	
}

//Refresca el resumen de tu compra ("cesta")
function refrescarresumen()
{		
		respuesta = document.getElementById('micompra');
		envia('resumencompra.php','');
}

function valorar(mivaloracion)
{
respuesta = document.getElementById('valoracion');
envia('movil/valoracion.php','valoracion='+mivaloracion+'&'+window.location.search);
}
function searchadsl()
{
	var email = document.getElementById('mailadsl').value;

	if( email != '')
    {			
		if(megavalidacion('adslform'))
		{
			showSol(document.getElementById('mailadsl').value)
			/*
			respuesta = document.getElementById('respuesta');
			envia('ajax/mailadsl.php','linea='+document.getElementById('mailadsl').value);
			return true;
			*/
		}
		
		else
			alert('El telefono introducido no es valido');
	}
	
	else
		alert('Debes introducir un telefono valido');
}
function showSol(campo)
{
document.getElementById('hiddenline').value=campo	
document.getElementById('line').value=campo
document.getElementById('Asistente').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('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').nombre.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.compratuorange.com/empresas/tarifas_empresas.php'+ancla,700);
	
	if(segmento=='particular')
	ventanaSecundaria('http://www.compratuorange.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(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+'&codigopromocional='+document.dudaformulario.codigopromocional.value);
	}
	else
	{
		alert('Hay datos incorrectos y/o campos obligatorios sin rellenar');
	}
	resultado="";

}

