window.onload=function() {
	var d=$('dData');
	
	if (d) {
		popCalendar.initialize($('dData'), {format: 'dd/mm/yyyy', language: 'es', onDateClick: cargarReservas, parentNode: $('calendario'), hideOnClick: false });
		cargarReservas();
	}
}

function archivoPass() {
	posaOverlay($('passOver'));
}

function logArchivo() {
	var usu=$('usu').value;
	var pass=$('pass').value;
	
	var url="../xml/passArchivo.php";
	var params="usu="+usu+"&pass="+pass;
	
	var ajax=new Ajax.Request(url, {
		method: 'post',
		postBody: params,
		onComplete: function (reply) {
			if (reply.responseText=="ok") {
				window.location="http://www.golfmaioris.es/public/xml/DownLoadArchivo.php?usu="+usu+"&pass="+pass;
			} else {
				alert('El usuario o la contraseña son incorrectos');
			}
		}
	});
	
	
	
	
}
	

function cargarReservas() {
	var d=$('calendarioSalidas');
	buida(d);
	var img=new Element('img', { 'src':'../img/loader.gif' });
	d.style.textAlign="center";
	d.appendChild(img);


	var url="../xml/datosReservas.php";
	var params="dia="+$('dData').value+"&tee="+$('cboTee').value;
	var ajax=new Ajax.Request(url, {
		method: 'get',
		parameters: params,
		onComplete: function(reply) {
			var tMax=reportPointCoords().split(":");
			tMax[0]=tMax[0]-2;
			var d=$('calendarioSalidas');
			d.style.textAlign="left";
			buida(d);
			
			var table=new Element('table');
			table.setAttribute("cellpadding","1");
			table.setAttribute("cellspacing","1");
			table.setAttribute("width","100%");
			var tbody=new Element('tbody');
			table.appendChild(tbody);
			d.appendChild(table);
			
			var trest=new Element("tr");
			var td1est=new Element("td", { 'class':'celda2','style':'width:100px' }).update("Hora");
			var td2est=new Element("td", { 'class':'celda2','style':'width:100px' }).update("Disponibilidad");
			var td3est=new Element("td", { 'class':'celda2','style':'width:300px' }).update(" ");
			
			trest.appendChild(td1est);
			trest.appendChild(td2est);
			trest.appendChild(td3est);
			tbody.appendChild(trest);
			
			for (var h=9;h<=parseInt(tMax[0]);h++) {
				for (var m=0;m<=50;m=m+10) {
					var sH=""+h;
					
					if (sH.length==1) {
						sH="0"+sH;
					}
					
					if (h==parseInt(tMax[0])) {
						if (m<=parseInt(tMax[1])) {
							var sM=""+m
							
							if (sM.length==1) {
								sM="0"+sM;
							}
							
							var tr=new Element("tr");
							var td=new Element("td").update(sH+":"+sM);
							tr.appendChild(td);
							tbody.appendChild(tr);
							
							compruebaDisp(reply,tr,sH,sM);
						}
					} else {
						var sM=""+m
							
						if (sM.length==1) {
							sM="0"+sM;
						}
						
						var tr=new Element("tr");
						var td=new Element("td").update(sH+":"+sM);
						tr.appendChild(td);
						tbody.appendChild(tr);
						
						compruebaDisp(reply,tr,sH,sM);
					}
				}
			}
		}
	});
}

function compruebaDisp(reply,tr,sH,sM) {
	var nodePart=reply.responseXML.selectNodes("//greenfees/greenfee");
	var trobat=false;
	for (var k=0;k<nodePart.length;k++) {
		if (nodePart[k].firstChild.firstChild.nodeValue==sH+":"+sM) {
			var td=new Element("td").update("Reservado");
			tr.appendChild(td);
			tr.className="red";
			trobat=true;
			
			var td2=new Element("td");
			if (nodePart[k].childNodes[1].getAttribute("cod")!="") {
				td2.update(nodePart[k].childNodes[1].firstChild.nodeValue);
				var a=new Element("a", { 'href':'torneos.php?torncod='+nodePart[k].childNodes[1].getAttribute("cod") });
				var imgtorn=new Element("img", { 'src':'../img/torneo.png' });
				imgtorn.setAttribute("align","absmiddle");
				imgtorn.style.marginLeft="5px";
				a.appendChild(imgtorn);
				td2.appendChild(a);
			}
			
			if (nodePart[k].childNodes[2].getAttribute("jugcod")!="") {
				td2.update(nodePart[k].childNodes[2].firstChild.nodeValue);
			}
			
			tr.appendChild(td2);
		}
	}
	
	if (trobat==false) {
		var td=new Element("td").update("Libre");
		tr.appendChild(td);
		tr.className="green";
		var td2=new Element("td").update("Reservar");
		tr.appendChild(td2);
		var img=new Element("img", { 'src':'../img/reserva.png','style':'cursor:pointer' });
		img.setAttribute("align","absmiddle");
		img.style.marginLeft="5px";
		img.setAttribute("rel",sH+":"+sM);
		
		img.onclick=function() {
			reserva(this);	
		}
		td2.appendChild(img);
	}
}

function reserva(img) {
	window.location="reservas_paso1.php?tee="+$('cboTee').value+"&fecha="+$('dData').value.replace(/\//gi,"-")+"&hora="+img.getAttribute("rel");
}

function mostraTab(i,d) {
	var d2=$$('div.pestanya');
	d2[0].className='pestanya2';
	d.className='pestanya';
	
	var d3=$('tabcontent').childNodes;
	
	for (var k=0;k<d3.length;k++) {
		d3[k].style.display='none';
	}
	
	$('tab'+i).style.display='block';
}

function compruebaJugador() {
	var lic=$('txtLicencia').value;
	
	if (lic!="") {
		var url="../xml/datosJugador.php";
		var params="juglic="+lic;
		var ajax=new Ajax.Request( url, {
					method: 'get',
					parameters: params,
					onComplete: function(reply) {
						var node=reply.responseXML.selectSingleNode("jugador");
						
						if (node) {
							if (node.childNodes[0].firstChild) {
								var nomcomp=node.childNodes[0].firstChild.nodeValue.split(",");
								
								if (nomcomp[1]) {
									$('txtNombre').value=trim(nomcomp[1]);
								}
								
								$('txtApellidos').value=nomcomp[0];
							}
							
							if (node.childNodes[1].firstChild) {
								if (node.childNodes[1].firstChild.nodeValue=="M") {
									$('chkM').checked=true;
								}
								
								if (node.childNodes[1].firstChild.nodeValue=="F") {
									$('chkF').checked=true;
								}
							}
							
							if (node.childNodes[2].firstChild) {
								$('txtMail').value=node.childNodes[2].firstChild.nodeValue;	
							}
							
							if (node.childNodes[4].firstChild) {
								$('txtMov').value=node.childNodes[4].firstChild.nodeValue;
							}
							
							if (node.childNodes[3].firstChild) {
								if (node.childNodes[3].firstChild.nodeValue="1") {
									$('chkR').checked=true;
								} else {
									$('chkR').checked=false;
								}
							}
						}	
					}
			});
	}
}

function enviaInscripcion(torncod) {
	if ($('txtLicencia').value=="") {
		mostraDiv(4);
		return;
	}
	
	if ($('txtNombre').value=="") {
		mostraDiv(4);
		return;
	}
	
	if ($('txtApellidos').value=="") {
		mostraDiv(4);
		return;
	}
	
	if ($('txtMail').value=="") {
		mostraDiv(4);
		return;
	}
	
	if ($('txtMov').value=="") {
		mostraDiv(4);
		return;
	}
	
	if (($('chkM').checked==false) && ($('chkF').checked==false)) {
		mostraDiv(4);
		return;
	}
	
	var url="../xml/guardaParticipanteInscripcion.php";
	var params="lic="+$('txtLicencia').value+"&nom="+$('txtNombre').value+"&apellidos="+$('txtApellidos').value+"&mail="+$('txtMail').value;
	
	if ($('chkM').checked) {
		params=params+"&sex=M";
	}
	
	if ($('chkF').checked) {
		params=params+"&sex=F";
	}
	
	if ($('chkR').checked) {
		params=params+"&rec=1";
	}
	
	params=params+"&torncod="+torncod;
	
	params=params+"&mov="+$('txtMov').value;
	
	mostraDiv(1);
	
	var ajax=new Ajax.Request(url, {
		method: 'post',
		postBody: params,
		onComplete: function (reply) {
			if (reply.responseText=="ok") {
				mostraDiv(2);
			} else {
				mostraDiv(3);
			}
		}
	});
	
	
}

function mostraDiv(i) {
	$('send1').style.display="none";
	$('send2').style.display="none";
	$('send3').style.display="none";
	$('send4').style.display="none";
	
	$('send'+i).style.display="block";
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function buida(el){//clearText(el) elimina tots els "child nodes" de l'element que li passam per parámetre
    if(el!=null){
        if(el.childNodes){
            for(var i=el.childNodes.length-1;i>=0;i--){
                var childNode=el.childNodes[i];
                el.removeChild(childNode);
            }
        }
    }
}

function validar() {
	$('send1').style.display='block';
	$('send2').style.display='none';
	var url="../xml/validaUsu.php";
	var params="usucod="+$('txtUser').value+"&pass="+hex_md5($('txtPass').value);
	var ajax=new Ajax.Request( url, {
		method: 'post',
		postBody: params,
		onComplete: function(reply) {
			if (reply.responseText=="ok") {
				window.location.reload();
			} else {
				$('send1').style.display='none';
				$('send2').style.display='block';
			}
		}
	});
}

function validar2() {
	$('send1').style.display='block';
	$('send2').style.display='none';
	var url="../xml/validaUsu.php";
	var params="usucod="+$('txtUser').value+"&pass="+hex_md5($('txtPass').value);
	var ajax=new Ajax.Request( url, {
		method: 'post',
		postBody: params,
		onComplete: function(reply) {
			if (reply.responseText=="ok") {
				var url=gup("fwd");
				var tee=gup("tee");
				var fecha=gup("fecha");
				var hora=gup("hora");
				
				if (url!="") {
					window.location=url+"?tee="+tee+"&fecha="+fecha+"&hora="+hora;
				} else {
					window.location="online.php";
				}
			} else {
				$('send1').style.display='none';
				$('send2').style.display='block';
			}
		}
	});
}

function guardaUsu() {
	$('send1').hide();
	$('send2').hide();
	$('send3').hide();
	$('send4').hide();
	$('send5').hide();

	var stUsuario=$('txtUsuario').value;
	var stPass1=$('txtPass1').value;
	var stPass2=$('txtPass2').value;
	var stLic=$('txtLicencia').value;
	var stNom=$('txtNombre').value;
	var stApes=$('txtApellidos').value;
	var stMail=$('txtMail').value;
	var stMov=$('txtMov').value;
	
	if ($('chkM').checked) {
		var stSex="M";
	} else {
		var stSex="F";
	}
	
	if ($('chkR').checked) {
		var stR=1;
	} else {
		var stR=0;
	}
	
	var mode=$('hMode').value;
	
	if (mode=="N") {
		if ((stPass1=="") || (stPass2=="")) {
			$('send4').show();
			return;
		}
		
		if (stUsuario=="") {
			stUsuario=stLic;
		}
	}
	
	if ((stUsuario=="") || (stLic=="") || (stNom=="") || (stApes=="") || (stMail=="") || (stMov=="")) {
		$('send4').show();
		return;
	}
	
	if (stPass1!=stPass2) {
		$('send5').show();
	}
	
	$('send1').show();
	
	var url="../xml/guardaUsuario.php";
	var params="usucod="+stUsuario+"&lic="+stLic+"&nom="+stNom+"&apes="+stApes+"&mail="+stMail+"&mov="+stMov+"&sex="+stSex+"&rec="+stR+"&mode="+mode;
	
	if (stPass1!="") {
		params=params+"&pass="+hex_md5(stPass1);
	} else {
		params=params+"&pass=";
	}
	
	var ajax=new Ajax.Request( url, {
			method: 'post',
			postBody: params,
			onComplete: function(reply) {
				$('send1').hide();
				
				resp=reply.responseText.split("||");
				
				if (resp[0]=="ok") {
					$('send2').show();
					$('cmdGuarda').hide();
					$('cmdContinua').show();
				} else {
					$('send3').show();
				}
			}
		});
}

function enviarMensaje() {
	$('send1').hide();
	$('send2').hide();
	$('send3').hide();
	$('send4').hide();
	
	var nom=$('txtNombre').value;
	var apes=$('txtApellidos').value;
	var mail=$('txtMail').value;
	var movil=$('txtMovil').value;
	var mens=$('txtMensaje').value;
	
	if ((nom=="") || (apes=="") || (mail=="") || (movil=="") || (mens=="")) {
		$('send4').show();
		return;
	}
	
	$('send1').show();
	
	var params="nombre="+nom+"&apellidos="+apes+"&tel="+movil+"&mail="+mail+"&comentarios="+mens;
	var url="../xml/enviaMail.php";
	var ajax=new Ajax.Request(url, {
		method: 'post',
		postBody: params,
		onComplete: function(reply) {
			$('send1').hide();
			
			if (reply.responseText=="ok") {
				$('send2').show();
				$('txtNombre').value="";
				$('txtApellidos').value="";
				$('txtMail').value="";
				$('txtMovil').value="";
				$('txtMensaje').value="";
			} else {
				$('send3').show();
			}
		}
	});
}

function gup( name ){
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp ( regexS );
	var tmpURL = window.location.href;
	var results = regex.exec( tmpURL );
	if( results == null )
		return"";
	else
		return results[1];
}
