function initRequest(url) {   
    if (window.ActiveXObject) {
         isIE = true;
        return new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        return new XMLHttpRequest();
    }
}


function completar(resultid,sexo,piscina,eventcode,i,ind) {	 //busqueda de eventos
	var urlx;	
	urlx = "parcialesXML.asp?r="+resultid+"&s="+sexo+"&p="+piscina+"&e="+eventcode;
	parseStatus(urlx,i,ind,eventcode);	
}



function parseStatus(urlXML,i,ind,eventcode){
 	var url = urlXML;
	var req = initRequest(url);
	req.onreadystatechange = function() {
    if (req.readyState == 4) {
    	if (req.status == 200) {
        	parseMessages(req.responseXML,i,ind,eventcode);
            } else if (req.status == 204){
            	
            }
        }
   } 
	req.open("GET", url, true);
	req.send(null);
}


function parseMessages(responseXML,i,ind,eventcode) {
  
	var evento, marca;
	evento = "";
	marca="";
    response = XMLib.parseXMLResponse(responseXML);
   	data = XMLib.processOneLevelXML(response.data);
	
	var splitmark = data.splitmark;

	if(!isUndefinedOrNull(splitmark)) {
		if(!isUndefinedOrNull(splitmark.length)) {
		   	for (loop = 0; loop < splitmark.length; loop++) {		
				evento = evento + " " +splitmark[loop].evento + "\t" + splitmark[loop].marca;
				if (loop +1 < splitmark.length){ //mientras no sea el último
					evento = evento + "\n";
				}			        	
				appendEvent(i,evento,ind,eventcode);
	   		}
		} else {
				evento = evento + " " + splitmark.evento + "\t" + splitmark.marca;		    	   
				appendEvent(i,evento,ind,eventcode);
		}			
   	}
	
}

function pintar(){
}

function appendEvent(i,evento,ind,eventcode) {

	document.getElementById("ipuntos"+eventcode+i+ind).title=" Parciales: \n"+ evento;	
}


	
function mostrarEvento(){

arrayPruebasI = "";
arrayNombrePruebasI = "";
arrayPruebasR = "";
arrayNombrePruebasR = "";

      new Ajax.Request('tipoEvento.asp?s='+document.frmDatos.s.value, {
	  		asynchronous:true,evalScripts:true,			
			onComplete:function(request) {						
                        var doc = request.responseXML.documentElement;
                        tipoEvento = doc.getElementsByTagName("Dato");
                        document.frmDatos.e.options.length=tipoEvento.length+2;
                        if(tipoEvento.length > 0) {  
						
                             for (i=0;i<tipoEvento.length;i++)
                             {								 	
								   	document.frmDatos.e.options[i].value = doc.getElementsByTagName("EVENTCODE")[i].firstChild.nodeValue;
                                   	document.frmDatos.e.options[i].text = doc.getElementsByTagName("EVENTDESC")[i].firstChild.nodeValue;	
									
//alert(doc.getElementsByTagName("EVENTCODE")[i].firstChild.nodeValue.substring(doc.getElementsByTagName("EVENTCODE")[i].firstChild.nodeValue.indexOf("-")+1,doc.getElementsByTagName("EVENTCODE")[i].firstChild.nodeValue.length));
									
									if (doc.getElementsByTagName("EVENTCODE")[i].firstChild.nodeValue.substring(doc.getElementsByTagName("EVENTCODE")[i].firstChild.nodeValue.indexOf("-")+1,doc.getElementsByTagName("EVENTCODE")[i].firstChild.nodeValue.length)=="I"){
										
										arrayPruebasI = arrayPruebasI + doc.getElementsByTagName("EVENTCODE")[i].firstChild.nodeValue.substring(0,doc.getElementsByTagName("EVENTCODE")[i].firstChild.nodeValue.length-2)+",";
										arrayNombrePruebasI = arrayNombrePruebasI + doc.getElementsByTagName("EVENTDESC")[i].firstChild.nodeValue+",";
									}else{
										arrayPruebasR = arrayPruebasR + doc.getElementsByTagName("EVENTCODE")[i].firstChild.nodeValue.substring(0,doc.getElementsByTagName("EVENTCODE")[i].firstChild.nodeValue.length-2)+",";
										arrayNombrePruebasR = arrayNombrePruebasR + doc.getElementsByTagName("EVENTDESC")[i].firstChild.nodeValue+",";
									}
								 
									if (evento== doc.getElementsByTagName("EVENTCODE")[i].firstChild.nodeValue.substring(0,doc.getElementsByTagName("EVENTCODE")[i].firstChild.nodeValue.length-2)) {
										
										document.frmDatos.e.options[i].selected=true;
										document.frmDatos.r.value = doc.getElementsByTagName("EVENTCODE")[i].firstChild.nodeValue.substring(doc.getElementsByTagName("EVENTCODE")[i].firstChild.nodeValue.length-1);
									}
                             }
							 
							
							 if (evento== "TI") {
							 	document.frmDatos.e.options[i].selected=true
							 }
							
							 if (evento== "TR") {
							 	document.frmDatos.e.options[i+1].selected=true
							 }
                        } 						
						document.frmDatos.e.options[i].text = "Todas individuales";						 	
						document.frmDatos.e.options[i].value = "TI";	
						
						document.frmDatos.e.options[i+1].text = "Todas relevos";						 	
						document.frmDatos.e.options[i+1].value = "TR";
							
						arrayPruebasI = arrayPruebasI.substring(0,arrayPruebasI.length-1);						
						document.frmDatos.aPruebasI.value = arrayPruebasI;
						arrayNombrePruebasI = arrayNombrePruebasI.substring(0,arrayNombrePruebasI.length-1);
						document.frmDatos.aNombrePruebasI.value = arrayNombrePruebasI;	
						
						arrayPruebasR = arrayPruebasR.substring(0,arrayPruebasR.length-1);						
						document.frmDatos.aPruebasR.value = arrayPruebasR;
						arrayNombrePruebasR = arrayNombrePruebasR.substring(0,arrayNombrePruebasR.length-1);
						document.frmDatos.aNombrePruebasR.value = arrayNombrePruebasR;
				}     
            }) 				
			
}

function mostrarEventoC(){
      new Ajax.Request('tipoEvento.asp?s='+document.frmDatos.s.value, {
	  		asynchronous:true,onComplete:function(request) {
                        var doc = request.responseXML.documentElement;
                        tipoEvento = doc.getElementsByTagName("Dato");
					
                        document.frmDatos.e.options.length=tipoEvento.length+1;
                        if(tipoEvento.length > 0) {  
							
                             for (i=0;i<tipoEvento.length;i++)
                             {
								   	document.frmDatos.e.options[i].value = doc.getElementsByTagName("EVENTCODE")[i].firstChild.nodeValue;
                                   	document.frmDatos.e.options[i].text = doc.getElementsByTagName("EVENTDESC")[i].firstChild.nodeValue;
								  	if (evento== doc.getElementsByTagName("EVENTCODE")[i].firstChild.nodeValue.substring(0,doc.getElementsByTagName("EVENTCODE")[i].firstChild.nodeValue.length-2)) {
										document.frmDatos.e.options[i].selected=true;										
									}
                             }							 
							 if (evento== "") {
							 	document.frmDatos.e.options[i].selected=true
							 }
                        } 
						document.frmDatos.e.options[i].text = "Todas";						 	
						document.frmDatos.e.options[i].value = "";	
				}     
            }) 				
}


function filtrar(){

var temp = true;

	if ((document.frmDatos.t.value != "h") && (document.frmDatos.fecha_desde.value!="") && (document.frmDatos.fecha_desde.value != "")) {
    					
				
	  new Ajax.Request('rangoTemporada.asp?t='+document.frmDatos.t.value, {
	  		asynchronous:true,
			onComplete:function(request) {
                        var doc = request.responseXML.documentElement;
                        dato = doc.getElementsByTagName("Dato");  
						
						temporada_inicio = doc.getElementsByTagName("FROMDATE")[0].firstChild.nodeValue;								
                        temporada_fin = doc.getElementsByTagName("TODATE")[0].firstChild.nodeValue;		
                     	fecha_inicio = document.frmDatos.fecha_desde.value;
						fecha_hasta = document.frmDatos.fecha_hasta.value;						
	
						
						if ((Comparar_Fecha(temporada_inicio,fecha_inicio)) || (Comparar_Fecha(fecha_hasta,temporada_fin))){
							alert("Rango de fechas incorrecto para la temporada seleccionada.");
							temp= false;
						}		
							   
				}     
            })			 
	}


	if (temp){
		document.frmDatos.filtra.value = "1";
		document.frmDatos.action = "ranking.asp";
		document.frmDatos.submit();
		
	}
}

function comprobarTemporada(){
var temp = true;
	if ((document.frmDatos.t.value != "h") && (document.frmDatos.fecha_desde.value!="") && (document.frmDatos.fecha_desde.value != "")) {
      new Ajax.Request('rangoTemporada.asp?t='+document.frmDatos.t.value, {
	  		asynchronous:true,onComplete:function(request) {
                        var doc = request.responseXML.documentElement;
                        dato = doc.getElementsByTagName("Dato");  
						
						temporada_inicio = doc.getElementsByTagName("FROMDATE")[0].firstChild.nodeValue;								
                        temporada_fin = doc.getElementsByTagName("TODATE")[0].firstChild.nodeValue;		
                     	fecha_inicio = document.frmDatos.fecha_desde.value;
						fecha_hasta = document.frmDatos.fecha_hasta.value;						
									alert(temporada_fin);
						alert(fecha_hasta);
						if ((Comparar_Fecha(temporada_inicio,fecha_inicio)) || (Comparar_Fecha(fecha_hasta,temporada_fin))){
							alert("Rango de fechas incorrecto para la temporada seleccionada.");
							temp= false;
						}		
							   
				}     
            })			 
	}
	
	return temp;
}	


function mostrarProvincia(){
	if (document.frmDatos.f.value != "")  {
      new Ajax.Request('mostrarProvincias.asp?f='+document.frmDatos.f.value, {
	  		asynchronous:true,
			onComplete:function(request) {
                        var doc = request.responseXML.documentElement;
                        dato = doc.getElementsByTagName("Dato");  
						
						if (dato.length > 1){							
							document.frmDatos.pro.options.length=dato.length+1;
							document.frmDatos.pro.options[0].value = "";
	                        document.frmDatos.pro.options[0].text = "";		
							
							for (i=0;i<dato.length;i++)
							{	
								document.frmDatos.pro.options[i+1].value = doc.getElementsByTagName("FEDERATIONCODE")[i].firstChild.nodeValue;
	                            document.frmDatos.pro.options[i+1].text = doc.getElementsByTagName("LONGDESC")[i].firstChild.nodeValue;									
								
								if ("<%=provincia%>"== doc.getElementsByTagName("FEDERATIONCODE")[i].firstChild.nodeValue) {
									document.frmDatos.pro.options[i+1].selected=true;
								}
							}								
						} else{
							document.frmDatos.pro.options.length=0;
						}  
				}     
            })			 
	}

}
function formatear_anio(anio){
 var nYear;
 if(isNaN(anio)) 
    	nYear = parseInt(anio.value,10);
    else
    	nYear = anio;
    	
    c = new String(nYear);
    if(c == "NaN") {
        alert("El año no es correcto.");
        return false;
    } else {
        nYear = c;
        if(nYear < 1850 || nYear > 2020) {
            alert("El año no es correcto.");
            return false;
        }
    }

}

