//-------------------------------------------------------------------
//funciones Ajax
//-------------------------------------------------------------------
function menu(pagina,nombrediv,param,metodo)
{
	cargarFragmento(pagina,nombrediv,param,metodo);
}
function cargarFragmento(fragment_url,element_id,param,metodo) 
{
	var myAjax = new Ajax.Request(
	fragment_url, {
	method: metodo,
	parameters: param,
	onSuccess: function(transport) {
		var response = transport.responseText;
		var obj = document.getElementById(element_id);
		if (obj)
		obj.innerHTML = response;
		},
	onFailure: function() {
		alert("ERROR");
	}
	}
	);
}
function nuevoAjax()
{ 
	var xmlhttp=false; 
	try 
	{ 
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
	}
	catch(e)
	{ 
		try
		{ 
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
		catch(E) { xmlhttp=false; }
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp=new XMLHttpRequest(); } 

	return xmlhttp; 
}
//-------------------------------------------------------------------
function EnviaEmail()
{
	if(frm2.Nombre.value=="")
	{
		alert("El Nombre se encuentra vacío, es un dato necesario");
		frm2.Nombre.focus();
		return false;
	}
	if (frm2.Apellidos.value=="") 
	{
		alert("El Apellido se encuentra vacío, es un dato necesario");
		frm2.Apellidos.focus();
		return false;
	}
	if (frm2.Email.value=="") 
	{
		alert("El Email se encuentra vacío, es un dato necesario");
		frm2.Email.focus();
		return false;
	}else{
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(frm2.Email.value))
		{
		}else{
			alert("La dirección de email es incorrecta.");
   			frm2.Email.focus();
			return (false);
		}
	}
	if (frm2.Telefono.value=="") 
	{
		alert("El Teléfono se encuentra vacío, es un dato necesario");
		frm2.Telefono.focus();
		return false;
	}
	if (frm2.Mensaje.value=="") 
	{
		alert("El Comentario se encuentra vacío, es un dato necesario");
		frm2.Mensaje.focus();
		return false;
	}
	//frm2.submit()
	menu('contacto.php','central','Accion=1&Nombre='+frm2.Nombre.value+'&Apellidos='+frm2.Apellidos.value+'&Email='+frm2.Email.value+'&Telefono='+frm2.Telefono.value+'&Mensaje='+frm2.Mensaje.value+'&Referencia='+frm2.Referencia.value+'&Modelo='+frm2.Modelo.value,'post');
}
//-------------------------------------------------------------------
function pantallaCompleta(pagina,ancho,alto) 
{
	window.open(pagina, "", "scrollbars=YES, width="+ancho+", height="+alto+", titlebar=<?echo($NombreEmpresa)?>");
}
//-------------------------------------------------------------------
function Inicio()
{
	menu('cocheCentral.php','central','','get');
	CargaLateral();
}
//-------------------------------------------------------------------
function CargaLateral()
{
	menu('cocheLateral.php','lateral','','get');
}
//-------------------------------------------------------------------
function Busqueda()
{
	var MarcasB=document.getElementById('MarcasB').value;
	var ModelosB=document.getElementById('ModelosB').value;
	var EstadoB=document.getElementById('EstadoB').value;
	menu('resultados.php','central','EstadoB='+EstadoB+'&MarcasB='+MarcasB+'&ModelosB='+ModelosB,'get');
	CargaLateral();
}
//-------------------------------------------------------------------
function MuestraComboBus()
{
	var MarcasB=document.getElementById('MarcasB').value;
	menu('combomodelos.php','cmb_modelos','MarcasB='+MarcasB,'get');
}
function MuestraComboIntro()
{
	var MarcasF=document.getElementById('MarcasF').value;
	menu('combomodelos.php','cmb_modelosIntro','MarcasF='+MarcasF+'&Texto=1','get');
}
//-------------------------------------------------------------------
function MuestraComboLlan(MarcaVe)
{
	var Marca=document.getElementById('Marca').value;
	menu('llantas.php','central','Marca='+Marca+'&MarcaVe='+MarcaVe,'get');
}
//-------------------------------------------------------------------
function DetallesLlanta(Iden,Pag)
{
	var Marca=document.getElementById('Marca').value;
	var MarcaVe=document.getElementById('MarcaVe').value;
	menu('detallesllantac.php','central','Id='+Iden,'get');
	menu('detallesllantal.php','lateral','Id='+Iden+'&Pag='+Pag+'&Marca='+Marca+'&MarcaVe='+MarcaVe,'get');
}
//-------------------------------------------------------------------
function DetallesAlarma(Iden,Pag)
{
	var Marca=document.getElementById('Marca').value;
	menu('detallesalarmasc.php','central','Id='+Iden,'get');
	menu('detallesalarmasl.php','lateral','Id='+Iden+'&Pag='+Pag+'&Marca='+Marca,'get');
}
//-------------------------------------------------------------------
function BuscaLlantas()
{
	var Marca=document.getElementById('Marca').value;
	var MarcaVe=document.getElementById('MarcaVe').value;
	menu('llantas.php','central','Marca='+Marca+'&MarcaVe='+MarcaVe,'get');
	CargaLateral();
}
//-------------------------------------------------------------------
function BuscaAlarmas()
{
	var Marca=document.getElementById('Marca').value;
	menu('alarmas.php','central','Marca='+Marca,'get');
	CargaLateral();
}
//-------------------------------------------------------------------
function VerCondiciones()
{
	var strFeatures = "help:no; dialogTop="+screen.height/3+"; dialogLeft="+screen.width/3+"; dialogWidth=456px;" +
	"dialogHeight=347px; center=yes; help=no"
	var retorno = window.showModalDialog("condiciones.html", "NewWin", strFeatures);
	if(retorno!="" && retorno!=undefined)
	{
		menu('datos_personales.php','central','','get');
		menu('cocheLateral.php','lateral','','get');
	}
}
//-------------------------------------------------------------------
function check_acepta()
{
	var valor=document.getElementById('acepto').checked;
	if (valor==true)
	{
		document.getElementById('bt_aceptar').disabled='';
	}else{
		document.getElementById('bt_aceptar').disabled='disabled';
	}
}
//-------------------------------------------------------------------
function ValidaSte()
{
	var Marca = document.getElementById('MarcasF');
	var Estado = document.getElementById('Estado');
	var Modelo = document.getElementById('Modelo');
	var Motor = document.getElementById('Motor').value;
	var Potencia = document.getElementById('Potencia').value;
	var Combustible = document.getElementById('Combustible');
	var Color = document.getElementById('Color').value;
	var Kilometros = document.getElementById('Kilometros').value;
	var Matriculacion = document.getElementById('MesMatri').value+"/"+document.getElementById('AnioMatri').value;
	var Comentario = document.getElementById('Comentario').value;
	var PrecioS = document.getElementById('PrecioS');
	var IdModelo = document.getElementById('ModelosF');
	var DatosP = document.getElementById('DatosP').value;
	var Extra = "";
	var checkboxes = frmintro.Extra; //Array que contiene los checkbox
	//utf8_encode
	//htmlentities()
	//Validamos obligatorios
	if(Marca.value=="")
	{
		alert("La Marca se encuentra vacia, es un dato necesario");
		Marca.focus();
		return false;
	}
	if(IdModelo.value=="")
	{
		alert("El Modelo se encuentra vacio, es un dato necesario");
		IdModelo.focus();
		return false;
	}
	if(Modelo.value=="")
	{
		alert("El Nombre del vehículo se encuentra vacio, es un dato necesario");
		Modelo.focus();
		return false;
	}
	if (Estado.value=="")
	{
		alert("El Estado se encuentra vacio, es un dato necesario");
		Estado.focus();
		return false;
	}
	if (Combustible.value=="") 
	{
		alert("El tipo de Combustible se encuentra vacio, es un dato necesario");
		Combustible.focus();
		return false;
	}
	if (PrecioS.value=="") 
	{
		alert("El Precio se encuentra vacio, es un dato necesario");
		PrecioS.focus();
		return false;
	}
	//recogemos los extras marcados
	for (var x=0; x < checkboxes.length; x++)
	{
		if (checkboxes[x].checked)
		{
			Extra=Extra+checkboxes[x].value+"e";
		}
	}
	//alert(Extra);
	
	menu('introduce_fotos.php','central','Accion=1&Extras='+Extra+'&Marca='+Marca.value+'&Estado='+Estado.value+'&Modelo='+Modelo.value+'&Motor='+Motor+'&Potencia='+Potencia+'&Combustible='+Combustible.value+'&Matriculacion='+Matriculacion+'&Color='+Color+'&Kilometros='+Kilometros+'&Comentario='+Comentario+'&PrecioS='+PrecioS.value+'&IdModelo='+IdModelo.value+'&DatosP='+DatosP,'post');
}
//-------------------------------------------------------------------
function ValidaDatosP()
{
	
	var Nombre = document.getElementById('Nombre');
	var Apellidos = document.getElementById('Apellidos');
	var Poblacion = document.getElementById('Poblacion');
	var Localidad = document.getElementById('Localidad');
	var Telefono = document.getElementById('Telefono');
	var Email = document.getElementById('Email');
	var Cadena;
	if(Nombre.value=="")
	{
		alert("El Nombre se encuentra vacío, es un dato necesario");
		Nombre.focus();
		return false;
	}
	if (Apellidos.value=="") 
	{
		alert("El Apellido se encuentra vacío, es un dato necesario");
		Apellidos.focus();
		return false;
	}
	if (Poblacion.value=="") 
	{
		alert("La Población se encuentra vacía, es un dato necesario");
		Poblacion.focus();
		return false;
	}
	if (Localidad.value=="") 
	{
		alert("La localidad se encuentra vacía, es un dato necesario");
		Localidad.focus();
		return false;
	}	
	if (Telefono.value=="") 
	{
		alert("El Teléfono se encuentra vacío, es un dato necesario");
		Telefono.focus();
		return false;
	}
	if(Email.value!="")
	{
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(Email.value)))
		{
			alert("La dirección de email es incorrecta");
  			Email.focus();
			return (false);
		}
	}
	Cadena=Nombre.value+","+Apellidos.value+","+Poblacion.value+","+Localidad.value+","+Telefono.value+","+Email.value;
	menu('introduce_vehiculo.php','central','DatosP='+Cadena,'post');
}
//-------------------------------------------------------------------
function ValidaFoto()
{
	alert("validafoto");
	var IdP = document.getElementById('IdP').value;
	var Foto = document.getElementById('Foto').value;
	var Tipo = document.getElementById('Tipo').value;
	if (Foto=="") 
	{
		alert("Debe de seleccionar la foto");
		return false;
	}else{
		if(!(valida_extension_imagen(Foto)))
		{
			alert("La foto no tiene una extensión correcta(.jpg,.gif)");
			document.getElementById('Foto').focus();
			return false;
		}
	}
	if (Tipo=="")
	{
		alert("Debe de seleccionar el tipo de foto");
		document.getElementById('Tipo').focus();
		return false;
	}
	menu('introduce_fotos.php','central','AddFoto=1&Id='+IdP+'&Foto='+Foto+'&Tipo='+Tipo,'post');
}
//-------------------------------------------------------------------
function valida_extension_imagen(imagen)
{
	var extensiones_permitidas;
	var extension;
	var valida;
	valida=false;
	extensiones_permitidas = new Array(".gif", ".jpg");
	extension = (imagen.substring(imagen.lastIndexOf("."))).toLowerCase();
	for (var i = 0; i < extensiones_permitidas.length; i++) 
	{ 
		if (extensiones_permitidas[i] == extension) 
		{ 
			valida = true; 
			break; 
		} 
	}
	return valida;
}