/*Esto es para el captcha*/
function RefreshImage(valImageId) {
    var objImage = document.images[valImageId];
    if (objImage == undefined) {
        return;
    }
    var now = new Date();
    objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();
}

// Cambiar Ofertas del Carrussel
function CarruselMove(dir) {
    document.getElementById("winofe7").style.display = 'table-cell';

    if (dir == 1) {
        if (carrusel_pos < carrusel_max - 3) carrusel_pos++;
    }
    else {
        if (carrusel_pos > 1) carrusel_pos--;
    }
    sw = false; nm = 0;
    for (f = 1; f <= carrusel_max; f++) {
        if (f == carrusel_pos) sw = true;
        if (sw == false) document.getElementById("winofe" + f).style.display = 'none';
        else {
            document.getElementById("winofe" + f).style.display = 'table-cell';
            if (++nm == 4) sw = false;
        }
    }
}

function ajaxobj() {
    try {
        _ajaxobj = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
        try {
            _ajaxobj = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E) {
            _ajaxobj = false;
        }
    }

    if (!_ajaxobj && typeof XMLHttpRequest != 'undefined') _ajaxobj = new XMLHttpRequest();

    return _ajaxobj;
}

function CargarModelos(val1, val2, val3) {
	var ajax = ajaxobj();
	var myadvice;
	var obj;
	var mysend;

	ajax.open("POST", "xml.asp", true);
	if (val2 == null) val2 = "";
	if (val3!=null) {
		//cambio en el combo modelo
		obj=document.getElementById("combustible");
		mysend='marca='+val1+'&modelo='+val2;
	}
	else {
		//cambio en el combo marca
		obj=document.getElementById("modelo");
		mysend='marca='+val1;
	}

	ajax.onreadystatechange = function() {
	    if (ajax.readyState == 4) {
	        var datos = (ajax.responseXML).firstChild;

	        n = obj.options.length;
	        for (i = 0; i < n; i++)
	            obj.removeChild(obj.firstChild);

	        obj.options[0] = new Option('- Todos -', '');

	        for (i = 1; i <= datos.childNodes.length; i++) {
	            elem = datos.childNodes[i - 1].firstChild.data;
	            obj.options[i] = new Option(elem, elem);
	            if (val2 != "") {
	                if (elem == val2)
	                    obj.selectedIndex = i;
	            }
	        }
	    }
	}

	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajax.send(mysend);
}

// Pasar un numero a dos decimales
function RoundNumber(campo) {
	eval("numero=document.formulario."+campo+".value");
	numero=Round(numero);
	eval("document.formulario."+campo+".value=numero");
}


// Pasar un numero a dos decimales
function Round(numero) {
	return Math.round(numero*100)/100;
}

// Poner el ID del enlace a 0
function AnulaEnlace(tipo,valor,valorid) {
	if (tipo==true) {
		if (eval("document.formulario."+valor+".value.substring(0,7)!='http://'"))
			eval("document.formulario."+valor+".value='http://'+document.formulario."+valor+".value;");
	}
	else eval("document.formulario."+valor+".value=''");

	eval("document.formulario."+valorid+".value='0'");
}

// Poner el campo a Zero si no hay nada
function ValorZero(campo){
	if (eval("document.formulario."+campo+".value")=="") eval("document.formulario."+campo+".value=0");
}

// Abrir Quadro Dialogo Registros
function OpenDialogRegistros(tipo,origen) {
	VentanaHija=window.open("find_registros.asp?tipo="+tipo+"&origen="+origen,"DialogRegistros","width=550,height=350,top=100,left=100,scrollbars=yes,toolbar=no");
}

// Abrir Quadro Dialogo Ficheros
function OpenDialogFiles(origen) {
	if (origen.substring(0,7)=='adjunto') 
		VentanaHija=window.open("find_ficheros.asp?origen="+origen+"&tipo=adjunto","DialogFiles","width=710,height=525,top=20,left=40,scrollbars=yes,toolbar=no");
	else
		VentanaHija=window.open("find_ficheros.asp?origen="+origen+"&tipo=imagen","DialogFiles","width=710,height=525,top=20,left=40,scrollbars=yes,toolbar=no");
}

function OpenShowAll() {
        VentanaHija = window.open("vo_lista_todos.asp", "ShowAll", "width=875,height=525,top=20,left=40,scrollbars=yes,toolbar=no");
}

// Abrir Quadro Dialogo Enlaces
function OpenDialogLinks(tipo,origen) {
	VentanaHija=window.open("find_enlaces.asp?tipo="+tipo+"&origen="+origen,"DialogLinks","width=710,height=525,top=20,left=40,scrollbars=yes,toolbar=no");
}

// Enviar el nombre del archivo seleccionado
function AceptarFichero(destino) {
	eval("opener.formulario."+destino+".value='"+document.visor.src+"'");
	window.close();
}

// Accion de cambiar Carpeta
function VerImagen(imagen) {
	ext=imagen.substring(imagen.length-4,imagen.length);

	if (ext==".gif" || ext==".jpg") document.visor.src=imagen;
	else document.visor.src="../img/null.gif";
}

function NumeroDecimal() {
	k=false;

	tecla=window.event.keyCode;
	if (tecla==46) k=true;
	if (tecla>=48 && tecla<=57) k=true;

	if (k==false) window.event.keyCode=0;
	}	
function Numeros() {
	tecla=window.event.keyCode;
	if (tecla<48 || tecla>57) window.event.keyCode=0;
}

// Rutina de Validación de Formularios
// ===================================
//  Ejemplo:
//		if (ValidaForm("formulario",form_campo,form_valor,form_tipo) == 0)
//			document.formulario.submit();
//
//	Parámetros:
//		1) Nombre del Formulario.
//		2) Matriz con los mensajes a imprimir si falla ese campo.
//		3) Matriz con los nombres de los campos en el formulario.
//		4) Matriz con los tipos de campos:
//				"t":		Campo de texto.
//				"n","nxx":	Campo numérico. "xx" indica el tamaño del campo.
//				"e":		Campo de email.
//				"l":		Campo "Select".
//				"r":		Campo "Radio".
//				"m","mxx":	campo "TextArea". "xx" indica la longitud máxima.
//				"f":		Campo "Fecha".
//
function ValidaForm(form_nombre,form_campo,form_valor,form_tipo) {
	var docuform="document."+form_nombre;
	mensaje="Por favor, rellenar: \n";
	error=0;
	
	for (i=0; i<form_campo.length; i++) {
		fallo=0;
		tipo=form_tipo[i].substring(0,1);
		largo=parseInt(form_tipo[i].substring(1,5));
		if (isNaN(largo)) largo=0;
		
		switch (tipo) {
			case "t":
				if (eval(docuform+"."+form_valor[i]+".value")=="") fallo=1;
				break;
			case "n":
				if (isNaN(eval(docuform+"."+form_valor[i]+".value")) ||
					eval(docuform+"."+form_valor[i]+".value")=="" ||
					(eval(docuform+"."+form_valor[i]+".value.length")!=largo && largo!=0)) fallo=1;
				break;
			case "e":
				valor=eval(docuform+"."+form_valor[i]+".value");
				if (valor.indexOf("@")<0 || valor.indexOf(".")<0 || valor.indexOf(" ")>=0) fallo=1;
				break;
			case "l":
				if (eval(docuform+"."+form_valor[i]+".selectedIndex")==0) fallo=1;
				break;
			case "r":
				fallo=1;
				valor=eval(docuform+"."+form_valor[i]+".length");

				for (f=0; f<valor; f++)
					if (eval(docuform+"."+form_valor[i]+"["+f.toString()+"].checked")==true) fallo=0;
				break;
			case "m":
				if (eval(docuform+"."+form_valor[i]+".value.length")>largo && largo>0) fallo=1;
				break;
			case "f":
				largo=eval(docuform+"."+form_valor[i]+".value.length");
				valor=eval(docuform+"."+form_valor[i]+".value");

				if (largo!=0) {
					if (largo!=10) fallo=1;
					if (valor.substring(2,3)!='\/' || valor.substring(5,6)!='\/') fallo=1;
					for (f=0; f<10; f++) {
						char=valor.substring(f,f+1);

						if (f!=2 && f!=5) {
							if (isNaN(parseInt(char))) fallo=1;
						}
						else {
							if (char!="\/") fallo=1;
						}
					}
					if (fallo==0) {
						dia=valor.substring(0,2);
						mes=valor.substring(3,5);
						any=valor.substring(6,10);

						if (dia<"01" || dia >"31" || mes<"01" || mes>"12" || any<"1980" || any>"2050") fallo=1;
					}
				}
				break;
		}

		if (fallo) mensaje+=(error++!=0?", ":"")+form_campo[i];
	}
	
	if (error) alert(mensaje+".");
	return error;
}
