function objetoAjax()
{ 
	var xmlhttp=false;
	try
	{
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(E)
		{
			if (!xmlhttp && typeof XMLHttpRequest!='undefined') xmlhttp=new XMLHttpRequest();
		}
	}
	return xmlhttp; 
}

function cargaFecha(id) {
	
	if (id=="undefined") { id="";}	
	
	idD = id+'D'; idM=id+'M';	idY=id+'Y';
	d  = document.getElementById(idD); m = document.getElementById(idM); y = document.getElementById(idY);
	dd = document.getElementById(idD).value;
	
	var x=objetoAjax();
	
	x.open("GET", "fecha.php?m="+m.value+"&y="+y.value, true);
	x.onreadystatechange=function() 
	{ 
		if (x.readyState==1)
		{
			d.length=0;
			d.disabled=true;	
			d.options[d.length] = new Option("..", 0);
		}
		if (x.readyState==4)
		{
			d.length=0;
			for(i = 1; i <= parseInt(x.responseText); i++){
				t = "0"+i;
				t = t.substr(-2,2);
				d.options[d.length] = new Option(t, i);
				if (dd==i) {
					d.selectedIndex=(d.length-1);
				}
			}
			d.disabled=false;			
		} 
		
		cat = document.getElementById("categoria");
		id_cat = document.getElementById("id_categoria");
		
		yyyy =y.value;
		
		calcCat(yyyy);
//Juvenil (de 16  cumplidos a la fecha del evento hasta 19 a&ntilde;os)               1995 -1992            
//Libre (de 20 a 29 a&ntilde;os)                                                                             1991-1982
//Sub Master A (de 30 a 34 a&ntilde;os)                                                               1981-1977
//Sub Master B (de 35 a 39 a&ntilde;os)                                                               1976-1972
//Master A (de 40 a 44 a&ntilde;os)                                                                      1971-1967
//Master B (de 45 a 49 a&ntilde;os)                                                                      1966-1962
//Master C (de 50 a 54 a&ntilde;os)                                                                      1961-1957
//Master D (de 55 a 59 a&ntilde;os)                                                                      1956-1952                                              
//Master E (de 60 a 99 a&ntilde;os)                                                                       1951-…….
		
	}
	x.send(null);

}

function cargaCombo(c) {
	
	id = c.id
	c = document.getElementById(id); 

	var x=objetoAjax();

	switch (id){
		case "pais":
			carga = "estado";
			id1 = document.getElementById("pais").value; 
			d=document.getElementById(carga); 
			document.getElementById("estado").disabled=(id1!=1); 	// Venezuela	
			document.getElementById("ciudad").disabled=(id1!=1); 	// Venezuela	
			break;
		case "estado":
			carga = "ciudad";
			id1 = document.getElementById("estado").value; 		
			d=document.getElementById(carga); 
			break;
	}


	x.open("GET", carga + ".php?id="+id1, true);
	x.onreadystatechange=function() 
	{ 
		if (x.readyState==1)
		{
			d.length=0;
			d.disabled=true;	
			d.options[d.length] = new Option("..", 0);
		}
		if (x.readyState==4)
		{
			d.length=0;
			d.innerHTML=x.responseText
			d.disabled=!(id1==1 && id=="pais");			
		} 
	}
	x.send(null);

}

function isMail(_email) {
	 var emailReg = /^[a-z][a-z-_0-9\.]+@[a-z-_=>0-9\.]+\.[a-z]{2,3}$/i
	 return emailReg.test(_email);
}

function IsNumeric(sText) {
   var ValidChars = "0123456789.,";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
	  { 
	  Char = sText.charAt(i); 
	  if (ValidChars.indexOf(Char) == -1) 
		 {
		 IsNumber = false;
		 }
	  }
   return IsNumber;   
}

function validar(frm) {
	
	if (frm.elements["planilla"].value.length == 0) {
		alert("Por favor escriba el Numero de Planilla o Transferencia");
		frm.elements["planilla"].focus();
		return false;
	}
	
	if (frm.elements["nombre"].value.length == 0) {
		alert("Por favor escriba su Nombre");
		frm.elements["nombre"].focus();
		return false;
	}		

	if (frm.elements["apellido"].value.length == 0) {
		alert("Por favor escriba su Apellido");
		frm.elements["apellido"].focus();
		return false;
	}	

	if (frm.elements["cedula"].value.length == 0) {
		alert("Por favor escriba su Cedula");
		frm.elements["cedula"].focus();
		return false;
	}

	if ( (frm.elements["dia"].value==0) || (frm.elements["mes"].value==0)) {
		alert("Por favor indique su Fecha de Nacimiento");
		frm.elements["dia"].focus();
		return false;
	}


	if (validarradio("sexo1",frm)==false ) {
		alert("Por favor indique su Sexo");
		document.getElementById('sexo1').focus();
		return false;
	}
	
	if (frm.elements["cedula"].value.length == 0) {
		alert("Por favor escriba su Cedula");
		frm.elements["cedula"].focus();
		return false;
	}	

	if (frm.elements["nacionalidad"].value.length == 0) {
		alert("Por favor escriba su Nacionalidad");
		frm.elements["nacionalidad"].focus();
		return false;
	}
/*	
	if (frm.elements["equipo"].value.length == 0) {
		alert("Por favor escriba su Equipo");
		frm.elements["equipo"].focus();
		return false;
	}
*/
	if (frm.elements["telefono"].value.length == 0) {
		alert("Por favor escriba su Telefono");
		frm.elements["telefono"].focus();
		return false;
	}

	if (!isMail(frm.elements["email"].value)) {
		alert("Debe escribir su E-Mail correctamente");
		frm.elements["email"].focus();
		return false;
	}	


	if (frm.elements["captcha"].value.length == 0) {
		alert("Por favor escriba el Codigo");
		frm.elements["captcha"].focus();
		return false;
	}



	datos = "";
	datos = datos +  "\nFecha: " + frm.elements["diadep"].value + "/" + frm.elements["mesdep"].value + "/" + frm.elements["anodep"].value ;
		
	if (frm.elements["tipo"].value == "D") {
		datos = datos +   "\nForma de Pago: Deposito";	
	}
	else {
		datos = datos +   "\nForma de Pago: Transferencia";		
	}

	datos = datos +  "\nN#: " + frm.elements["planilla"].value;

	if (frm.elements["tipo"].value == 2) {
		datos = datos + "\nBanco: " +  frm.elements["banco"].options[frm.elements["banco"].selectedIndex].text ;
	}

	var answer = confirm ("Por favor confirme que los datos de su pago sean los correctos\n" + datos)
	
	if (answer) {
		frm.submit();
	}	
		

}

function validarradio(campo,frm) {
 	for ( var i = 0; i < frm.elements[campo].length; i++ ) {
      if ( frm.elements[campo][i].checked ) {
      	return true;
      	break;
      }
    }
	return false;
}


/*

*/


function calcular_edad(fecha){

    //calculo la fecha de hoy
    hoy=new Date()
    //alert(hoy)

    //calculo la fecha que recibo
    //La descompongo en un array
    var array_fecha = fecha.split("/")
    //si el array no tiene tres partes, la fecha es incorrecta
    if (array_fecha.length!=3)
       return false

    //compruebo que los ano, mes, dia son correctos
    var ano
    ano = parseInt(array_fecha[2]);
    if (isNaN(ano))
       return false

    var mes
    mes = parseInt(array_fecha[1]);
    if (isNaN(mes))
       return false

    var dia
    dia = parseInt(array_fecha[0]);
    if (isNaN(dia))
       return false


    //si el a&ntilde;o de la fecha que recibo solo tiene 2 cifras hay que cambiarlo a 4
    if (ano<=99)
       ano +=1900

    //resto los a&ntilde;os de las dos fechas
    edad=hoy.getYear()- ano - 1; //-1 porque no se si ha cumplido a&ntilde;os ya este a&ntilde;o

    //si resto los meses y me da menor que 0 entonces no ha cumplido a&ntilde;os. Si da mayor si ha cumplido
    if (hoy.getMonth() + 1 - mes < 0) //+ 1 porque los meses empiezan en 0
       return edad
    if (hoy.getMonth() + 1 - mes > 0)
       return edad+1

    //entonces es que eran iguales. miro los dias
    //si resto los dias y me da menor que 0 entonces no ha cumplido a&ntilde;os. Si da mayor o igual si ha cumplido
    if (hoy.getUTCDate() - dia >= 0)
       return edad + 1

    return edad
} 

function calcCat(yyyy) {
	
	cat = document.getElementById("categoria");
	id_cat = document.getElementById("id_categoria");
	
	if (yyyy <= 1995 && yyyy >= 1992) {
		cat.value="Juvenil";
		id_cat.value = 1;	
	}	
	else if (yyyy <= 1991 && yyyy >= 1982) {
		cat.value="Libre";
		id_cat.value = 2;	
	}
	else if (yyyy <= 1981 && yyyy >= 1977) {
		cat.value="Sub Master A";	
		id_cat.value = 3;				
	}
	else if (yyyy <= 1976 && yyyy >= 1972) {
		cat.value="Sub Master B";	
		id_cat.value = 4;				
	}
	else if (yyyy <= 1971 && yyyy >= 1967) {
		cat.value="Master A";	
		id_cat.value = 5;	
	}
	else if (yyyy <= 1966 && yyyy >= 1962) {
		cat.value="Master B";	
		id_cat.value = 6;				
	}
	else if (yyyy <= 1961 && yyyy >= 1957) {
		cat.value="Master C";	
		id_cat.value = 7;	
	}
	else if (yyyy <= 1956 && yyyy >= 1952) {
		cat.value="Master D";	
		id_cat.value = 8;				
	}
	else if (yyyy <= 1951) {
		cat.value="Master E";	
		id_cat.value = 9;	
	}
	else {
		cat.value="";	
		id_cat.value = 0;			
	}

}
