var club_year="actual"
var last_id_2
var last_id_3
var respuesta
var tablas
var satelite
function poll_vote(idpoll){
	opcion=document.getElementById('opcion_'+idpoll).value
	consultar('admin/save_data.php','response','POST','tipo=poll_vote&idpoll='+idpoll+'&opcion='+opcion)
}
function validarEmail(valor) {
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(valor)){
   return (true)
  } else {
   return (false);
  }
 }
function validarUrl(valor) {
  if (/(http):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/.test(valor)){
   return (true)
  } else {
   return (false);
  }
 }
 function TrimLeft( str ) {
	var resultStr = "";
	var i = len = 0;

	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null)	
		return null;

	// Make sure the argument is a string
	str += "";

	if (str.length == 0) 
		resultStr = "";
	else {	
  		// Loop through string starting at the beginning as long as there
  		// are spaces.
//	  	len = str.length - 1;
		len = str.length;
		
  		while ((i <= len) && (str.charAt(i) == " "))
			i++;

   	// When the loop is done, we're sitting at the first non-space char,
 		// so return that char plus the remaining chars of the string.
  		resultStr = str.substring(i, len);
  	}

  	return resultStr;
}
function TrimRight( str ) {
	var resultStr = "";
	var i = 0;

	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null)	
		return null;

	// Make sure the argument is a string
	str += "";
	
	if (str.length == 0) 
		resultStr = "";
	else {
  		// Loop through string starting at the end as long as there
  		// are spaces.
  		i = str.length - 1;
  		while ((i >= 0) && (str.charAt(i) == " "))
 			i--;
 			
 		// When the loop is done, we're sitting at the last non-space char,
 		// so return that char plus all previous chars of the string.
  		resultStr = str.substring(0, i + 1);
  	}
  	
  	return resultStr;  	
}
function Trim( str ) {
	var resultStr = "";
	
	resultStr = TrimLeft(str);
	resultStr = TrimRight(resultStr);
	
	return resultStr;
}
function contactus(seccion){
	name = document.getElementById("contact_name").value
	mail = document.getElementById("contact_mail").value
	subject = document.getElementById("contact_subject").value
	message = document.getElementById("contact_message").value
	captcha_value = document.getElementById("captcha").value
	if((Trim(name)!="")&&(validarEmail(mail))&&(Trim(subject)!="")&&(Trim(message)!="")&&(Trim(captcha_value)!=""))
	{
		switch(seccion){
			case "report":
				consultar("send_mail.php","response","POST","tipo=mail_report&name="+name+"&mail="+mail+"&subject="+subject+"&message="+escape(message)+"&captcha="+captcha_value)
				break
			default:
				consultar("send_mail.php","response","POST","tipo=mail_contacto&name="+name+"&mail="+mail+"&subject="+subject+"&message="+escape(message)+"&captcha="+captcha_value)
		}
	}else{
		alert("Must complete all fields")
	}
}
function validate_form(){
	document.getElementById("site_name_tr").style.background="#FFFFFF";
	document.getElementById("url_tr").style.background="#FFFFFF";
	document.getElementById("reciprocal_url_tr").style.background="#FFFFFF";
	document.getElementById("email_tr").style.background="#FFFFFF";
	document.getElementById("image_tr").style.background="#FFFFFF"
	name_site=document.getElementById("site_name").value
	site_url=document.getElementById("url").value
	reciprocal_site_url=document.getElementById("reciprocal_url").value
	email_value=document.getElementById("email").value
	captcha_value=document.getElementById("captcha").value
	seccion_value=document.getElementById("seccion").value
	id_value=document.getElementById("id").value
	error=0
	if(Trim(name_site)==""){
		document.getElementById("site_name_tr").style.background="#FFCC33"
		error = 1
	}
	if((Trim(site_url)=="")||(validarUrl(site_url)==false)){
		document.getElementById("url_tr").style.background="#FFCC33"
		error = 1
	}
	if((Trim(reciprocal_site_url)=="")||(validarUrl(reciprocal_site_url)==false)){
		document.getElementById("reciprocal_url_tr").style.background="#FFCC33"
		error = 1
	}
	if((Trim(email_value)=="")||(validarEmail(email_value)!=true)){
		document.getElementById("email_tr").style.background="#FFCC33"
		error = 1
	}
	if(Trim(captcha_value)==""){
		document.getElementById("image_tr").style.background="#FFCC33"
		error = 1
	}
	if(error==0){
		document.getElementById("boton").disabled="disabled"
		consultar("admin/save_data.php","response","POST","tipo=site_url&site_name="+name_site+"&url="+site_url+"&reciprocal_url="+reciprocal_site_url+"&email="+email_value+"&captcha="+captcha_value+"&seccion="+seccion_value+"&id="+id_value)
	}		
}
function cerrar_search(){
		document.getElementById('search_club').innerHTML='<a tabindex="4" href="javascript:;" onClick="search_club_select()">Search Club</a>'
		document.getElementById("search_club_select").innerHTML=''
}
function cerrar_search2(){
		document.getElementById('search_club2').innerHTML='<a tabindex="4" href="javascript:;" onClick="search_club_select2()">Search Club</a>'
		document.getElementById("search_club_select2").innerHTML=''
}
function onChange_club_select(){
		value=document.getElementById('search_club_value').value
		if(value.length>=3){		
				consultar('list_club.php','search_club_select','GET','value='+value)
		}else{
				alert("Debe ingresar 3 letras o mas")
		}
}
function onChange_club_select2(){
		value2=document.getElementById('search_club_value2').value
		if(value2.length>=3){	
			consultar('list_club.php','search_club_select2','GET','value2='+value2)
		}else{
			alert("3 letras o mas")
		}
}
function search_club_select(){
		document.getElementById('search_club').innerHTML="<input type='text' tabindex='5' id='search_club_value'><img class='Flecha' onClick='onChange_club_select();' src='images/search.gif' tabindex='6' width='18' title='search club' valign='middle' height='15'><a href='javascript:;' onClick='cerrar_search()'>[X]</a>";
}
function search_club_select2(){
		document.getElementById('search_club2').innerHTML="<input type='text' tabindex='5' id='search_club_value2'><img class='Flecha' onClick='onChange_club_select2();' src='images/search.gif' tabindex='6' width='18' title='search club' valign='middle' height='15'><a href='javascript:;' onClick='cerrar_search2()'>[X]</a>";
}
function search_round(id_tour){
		id_team1=document.getElementById("team_1").value
		id_team2=document.getElementById("team_2").value
		if(id_team1!=id_team2){
				consultar("search_round.php","no_exit","GET","id_tournament="+id_tour+"&team1="+id_team1+"&team2="+id_team2)
		}
}
function abrir_ronda(estado){
		switch(estado){
				case "si":
					document.getElementById("titulo_qualifyng_round").innerHTML='QUALIFYING ROUND <img class="flecha" src="images/minus.gif" onClick="abrir_ronda(\'no\');" width="9" height="9">'
					partes=tablas.split("<noscript>");
					document.getElementById("qualifyng_round").innerHTML=tablas
					for(a=1;a<=3;a++){
						funcion=partes[a].split("\"")
						vars=funcion[0].split(",");
						vars2=vars[0].split("(")
						vars[0]=vars2[1]
						vars2=vars[7].split(")")
						vars[7]=vars2[0]		
						torneo_detalle(parseInt(vars[0]),parseInt(vars[1]),parseInt(vars[2]),parseInt(vars[3]),parseInt(vars[4]),parseInt(vars[5]),parseInt(vars[6]),parseInt(vars[7]))
					}
					break;
				case "no":
					document.getElementById("titulo_qualifyng_round").innerHTML='QUALIFYING ROUND <img src="images/plus.gif" onClick="abrir_ronda(\'si\');" class="flecha" width="9" height="9">'
					document.getElementById("qualifyng_round").innerHTML=''
					break
		}
 };		
function torneo_detalle(id,id_tournament,id_round,type,goback,bandera,group,type2){		
		switch(type2){
			case 2:
				if((group!=0)&&(group!="")){
					document.getElementById("round_number").innerHTML="Group "+String.fromCharCode(64+parseInt(group))
				}
				before=document.getElementById("tr_"+id)
				father=before.parentNode
				if(bandera==1){
					if((last_id_2!=0)&&(last_id_2!="")){
						if(document.getElementById("tr_"+last_id_2+"_child")){
						child=document.getElementById("tr_"+last_id_2+"_child")
						father=child.parentNode
						father.removeChild(child)
						}
					}
				last_id_2=id
				}
				child_tr=document.createElement("tr")
				child_td=document.createElement("td")
				child_div=document.createElement("div")
				child_tr.setAttribute("id","tr_"+id+"_child")
				child_div.setAttribute("id","div_"+id+"_child")
				child_td.setAttribute("colspan","2")
				child_td.appendChild(child_div)
				child_tr.appendChild(child_td)
				father.insertBefore(child_tr,before.nextSibling)
				consultar("rounds.php","div_"+id+"_child","GET","id_tournament="+id_tournament+"&id_round="+id_round+"&type="+type+"&goback="+goback+"&group="+group)
				break
		}
		
}
function show_countrys(tipo,color){
		document.getElementById("list_countrys").innerHTML=""
		document.getElementById("list_titles").innerHTML=""
		consultar("titles.php","list_countrys","GET","type="+tipo+"&accion=country")
 }
function show_titles(pais,color){
		document.getElementById("list_titles").innerHTML=""
		consultar("titles.php","list_titles","GET","country="+pais+"&accion=title")
 }
function select_round(tournament,id_round){
	document.getElementById("round_number").innerHTML="Round "+id_round
	consultar("rounds.php","round_list","GET","id_tournament="+tournament+"&id_round="+id_round+"&type=0")
}
function ordenar(tabla,campo,tipo,salida){
		consultar("order_field.php","lista_jugadores","GET","tabla="+tabla+"&campo="+campo+"&type="+tipo+"&salida="+salida)
}	
function busqueda_clave(categoria,palabra,div_central,value_inicial,div_secundario){
	if (palabra.length >= 1){
			switch(categoria){
				case "add_person_history":
					consultar("select_clubs.php",div_secundario,"GET","palabra_clave="+palabra+"&div_central="+div_central+"&value_inicial="+value_inicial+"&div_secundario="+div_secundario+"&categoria="+categoria);
					break
			}
 } }
function cerrar_ventana(){
		window.close()
	}
function consultar(pagina, respuesta, metodo, variables){
 variables+="&nocache="+Math.random() // Podríamos indicar que estamos cargando los datos.... que espere
  // Preparación de la variable 'pagina' en caso de que se haga uso del metodo GET
  document.getElementById(respuesta).innerHTML='<div align="center"><img src="images/ajax-loader.gif" width="16" height="16" /></div>'
if(metodo == 'GET'){
    pagina = pagina + '?' + variables;
  }else{
    if(metodo == ''){
      metodo = 'POST';
    }
  }
  // Compatibilidad con FireFox, Opera y cualquier otro BUEN navegador
  if(typeof(XMLHttpRequest) != 'undefined'){
    try{
      var satelite = new XMLHttpRequest();
    }
    catch(e){ }
  }else{
    // Compatibilidad para el navegador más ASQUEROSO del planeta [ IE ]
    try{
      var satelite = new ActiveXObject('Microsoft.XMLHTTP');
    }
    catch(e){
      var satelite = new ActiveXObject('Msxml2.XMLHTTP');
    }
  }
  // una vez incializado el objeto y definido en el contexto de nuestro script
  // Intentaremos abrir la conexión a la dirección indicada por la variable 'pagina'
  try{
    // esto bien podría cambiar a "satelite.open('GET',pagina,true);" si se desea pasar los datos mediante método GET
    satelite.open(metodo,pagina,true);
  }
  catch(e){
    return false;
  }
  // Esta variable envía una cabecera indicando que enviaremos los datos método 'post' en forma urlencoded
  // ejemplo ("mi_variable=cosa&otra_variable=otra_cosa");
  satelite.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  // Enviamos los datos 'elementos'
  satelite.send(variables);
  try{
    // Justo cuando el estatus de la petición cambia esperamos a que sea igual a 4 que es igual a OK.
	satelite.onreadystatechange = function(){
   switch(satelite.readyState){	
		case 4:	
				document.getElementById(respuesta).innerHTML=''
				partes=satelite.responseText.split("||")
				switch(partes[0]){
					case "poll_vote":
						consultar("poll.php","poll_table_"+partes[1],"GET","idpoll="+partes[1]+"&accion=1");
						break
					case "mail_report":
						switch(partes[1]){
							case "error":
								document.getElementById("response").innerHTML = "image validation error, try again"
								break
							case "bien":
								document.getElementById("mail_message").innerHTML = "<table width='60%' align='center'><tr><td align='center' class='title'>Mail send to ABCGoal</td></tr></table>"
								break
						}
					break
					case "mail_contacto":
						switch(partes[1]){
							case "error":
								document.getElementById("response").innerHTML = "image validation error, try again"
								break
							case "bien":
								document.getElementById("mail_message").innerHTML = "Mail send to ABCGoal"
								break
						}
					break
					case "image":
						if(partes[1]==1){
							window.close()
						}else{
							document.getElementById("boton").disabled=false
							document.getElementById("response").innerHTML=partes[1]
						}
						break
					case "order_person":
							listar('paginado_person',25,0,0)
						break
					case "order_club":
							listar('paginado_club',25,0,0)
						break
					case "order_stadium":
							listar('paginado_stadium',25,0,0)
						break
					case "order_contract":
							listar('paginado_contract',25,0,0)
						break
					case "order_transfer":
							listar('paginado_transfer',25,0,0)
						break
					case "search_round":
							if(partes[1]!=""){
								id=partes[1]
								id_round=partes[2]
								document.getElementById("round_select").options[id_round-1].selected='selected'
								select_round(id,id_round)
							}
							break
					default:
							document.getElementById(respuesta).innerHTML = satelite.responseText
				}
				break
		}
		
	}
  
  }
  catch(e){
    return false;
  }
  return true;
  satelite.abort()
 }
function div_busqueda(categoria,paso,div_central,value_inicial,div_secundario,salida){
	switch(categoria){
		case "add_person_history":
			switch(paso){
				case 0:
					document.getElementById(div_central).innerHTML='<input name="club" type="hidden" id="club" value="0"><div align="center"><input name="buscar_equipo" onDblClick="div_busqueda(\''+categoria+'\',1,\''+div_central+'\',\''+value_inicial+'\',\''+div_secundario+'\');" onBlur="busqueda_clave(\''+categoria+'\',this.value,\''+div_central+'\',\''+value_inicial+'\',\''+div_secundario+'\');" type="text" value="'+value_inicial+'" id="buscar_equipo"></div><div align="center" id="'+div_secundario+'"></div>'
					break
				case 1:
					document.getElementById(div_central).innerHTML='<div align="center"><a href="javascript:;" onClick="div_busqueda(\''+categoria+'\',0,\''+div_central+'\',\''+value_inicial+'\',\''+div_secundario+'\')">Abrir Busqueda</a><input name="club" type="hidden" id="club" value="0"></div>'
					break
				case 2:
					partes = value_inicial.split("_")
					id_club=partes[0]
					nombre_club=partes[1]
					document.getElementById(div_central).innerHTML='<input name="club" type="hidden" id="club" value="'+id_club+'"><div align="center"><a href="javascript:;" onClick="div_busqueda(\''+categoria+'\',0,\''+div_central+'\',\''+partes[1]+'\',\''+div_secundario+'\')">'+partes[1]+'</a></div>'
					break
			}
		break

	}
 }
function listar(accion,value,estado,pagina){
	switch(accion){
		case "paginado_person":
			consultar("listar.php","lista_jugadores","GET","accion="+accion+"&cantidad=25&page="+pagina)
			break
		case "paginado_stadium":
			consultar("listar.php","lista_jugadores","GET","accion="+accion+"&cantidad=25&page="+pagina)
			break
		case "paginado_club":
			consultar("listar.php","lista_jugadores","GET","accion="+accion+"&cantidad=25&page="+pagina)
			break
		case "paginado_contract":
			consultar("listar.php","lista_jugadores","GET","accion="+accion+"&cantidad=25&page="+pagina)
			break
		case "paginado_transfer":
			consultar("listar.php","lista_jugadores","GET","accion="+accion+"&cantidad=25&page="+pagina)
			break
		}
 }
function popUp(URL,width,height) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+width+",height="+height+",left = 312,top = 184');")}
function selective_search(tipo,value,country,position){
	switch(tipo){
		case "contract":
			search_value=document.getElementById("menu_buscar").value
			if(document.getElementById("type")){
				type_value = document.getElementById("type").value
			}else{
				type_value = 5
			}
			if(document.getElementById("club_select")){
				clubs_value=document.getElementById("club_select").value
			}else{
				clubs_value="all"
			}
			country_value=document.getElementById("country").value
			consultar("order_contract.php","lista_jugadores","GET","search="+search_value+"&country="+country_value+"&type="+type_value+"&clubs="+clubs_value)
			break
		case "transfer":
			search_value=document.getElementById("menu_buscar").value
			if(document.getElementById("type")){
				type_value = document.getElementById("type").value
			}else{
				type_value = 5
			}
			if(document.getElementById("club_select")){
				clubs_value=document.getElementById("club_select").value
			}else{
				clubs_value="all"
			}
			if(document.getElementById("club_select2")){
				clubs_value2=document.getElementById("club_select2").value
			}else{
				clubs_value2="all"
			}
			consultar("order_transfer.php","lista_jugadores","GET","search="+search_value+"&type="+type_value+"&clubs="+clubs_value+"&clubs2="+clubs_value2)
			break
		case "person2":
			if(country!=0){
				search_value=document.getElementById("menu_buscar").value
				if(document.getElementById("position")){
					position_value = document.getElementById("position").value
				}else{
					position_value = 5
				}
				if(document.getElementById("club_select")){
					clubs_value=document.getElementById("club_select").value
				}else{
					clubs_value="all"
				}
				country_value=document.getElementById("country").value
				if(search_value.length <3){
					if((document.getElementById("country").value!="all")||(document.getElementById("club_select"))||((document.getElementById("position"))&&(document.getElementById("position").value!="all"))){		
						consultar("order_person.php","lista_jugadores","GET","search="+search_value+"&country="+country_value+"&position="+position_value+"&clubs="+clubs_value)
					}else{
						alert("Enter 3 letters minimum or choose another option")
					}
				}else{
					consultar("order_person.php","lista_jugadores","GET","search="+search_value+"&country="+country_value+"&position="+position_value+"&clubs="+clubs_value)
				}
			}else{
				consultar("order_person.php","lista_jugadores","GET","search=&country=&position=5&clubs=")
			}
			break
		case "person":
			country_value = country
			position_value = position
			buscar_value = value
			consultar("order_person.php","lista_jugadores","GET","search="+buscar_value+"&country="+country_value+"&position="+position_value)
			break
		case "club":
			if(document.getElementById("menu_buscar")){
				club_value=document.getElementById("menu_buscar").value	
			}else{
				club_value=value
			}
			if(document.getElementById("country")){
				country_value=document.getElementById("country").value
			}else{
				country_value=country
			}
			consultar("order_club.php","lista_jugadores","GET","club="+club_value+"&country="+country_value)
			break
		case "stadium":
			if(document.getElementById("menu_buscar")){
				stadium_value=document.getElementById("menu_buscar").value	
			}else{
				stadium_value=value
			}
			if(document.getElementById("country")){
				country_value=document.getElementById("country").value
			}else{
				country_value=country
			}
			if(document.getElementById("club_select")){
				clubs_value=document.getElementById("club_select").value
			}else{
				clubs_value="all"
			}
			consultar("order_stadium.php","lista_jugadores","GET","stadium="+stadium_value+"&country="+country_value+"&clubs="+clubs_value)
			break
	}}
function show_titles2(pais,color){
		document.getElementById("list_titles").innerHTML=""
		document.getElementById("list_each").innerHTML=""
		consultar("tournament_list.php","list_titles","GET","country="+pais+"&accion=title")
 }
function show_tournament(title,pais){
		document.getElementById("list_each").innerHTML=""
		consultar("tournament_list.php","list_each","GET","title="+title+"&accion=each&country="+pais)
 }
function ventana(px){
		if (parseInt(navigator.appVersion)>3) {
			 if (navigator.appName=="Netscape") {
  				winH = window.innerHeight
				window.innerHeight=winH+px
 			}
 			if (navigator.appName.indexOf("Microsoft")!=-1) {
  				winH = window.body.offsetHeight
				window.body.offsetHeight=winH+px
 			}
		}}