function CRegistrarse(){
	var EDiv = 'errorDiv';
	var EUl = 'errorUL';
	errores = 0;
	Get(EUl).innerHTML = "";
	emailFilter = /^[A-Za-z][A-Za-z0-9_\.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	if (!VExiste('Cuser')){ ShowError(EDiv,EUl,'<li>Debe ingresar un nombre de usuario</li>'); errores += 1; }
	if (!usuarioValido){ ShowError(EDiv,EUl,'<li>El nombre de usuario ya existe, por favor elija otro</li>'); errores += 1; }
	if (!VLen('Cuser',3,1)){ ShowError(EDiv,EUl,'<li>El nombre de usuario debe tener m&aacute;s de 3 caracteres</li>'); errores += 1; }
	if (!VFilter('Cuser', /^[0-9a-zA-Z\_\-]+$/)){ ShowError(EDiv,EUl,'<li>El nombre de usuario solo puede contener letras, n&uacute;meros y guiones</li>'); errores += 1; }
	if (!VExiste('Cpass')){ ShowError(EDiv,EUl,'<li>Debe ingresar una contrase&ntilde;a</li>'); errores += 1; }
	if (!VLen('Cpass',5,1)){ ShowError(EDiv,EUl,'<li>La contrase&ntilde;a debe tener 6 o m&aacute;s caracteres</li>'); errores += 1; }
	if (!VIgual('Cpass','Cpass2')){ ShowError(EDiv,EUl,'<li>Las contrse&ntilde;as ingresadas no coinciden</li>'); errores += 1; }
	if (!VExiste('Cnombre')){ ShowError(EDiv,EUl,'<li>Debe ingresar su nombre</li>'); errores += 1; }
	if (!VExiste('Capellido')){ ShowError(EDiv,EUl,'<li>Debe ingresar su apellido</li>'); errores += 1; }
	if (!VFilter('Cemail', emailFilter)){ ShowError(EDiv,EUl,'<li>Debe ingresar un email valido</li>'); errores += 1; }
	if (!VExiste('dia') || !VExiste('mes') || !VExiste('ano') ){ ShowError(EDiv,EUl,'<li>Ingrese su fecha de nacimiento</li>'); errores += 1; }
	if ((!VExiste('CColegio') || $('CColegio').value == "otro")){ 
		if(!VExiste('CColegioInput')){ 
				ShowError(EDiv,EUl,'<li>Seleccione su colegio</li>'); 
				errores += 1; 
			}else{
				if (!VFilter('CColegioInput', /^[^\.#\&\''\%]*$/)){ ShowError(EDiv,EUl,'<li>El colegio que ingres&oacute; contiene caracteres invalidos (No use .#&\'%)</li>'); errores += 1; }
			}
		}
	if ($('CTerminos').checked == ""){ ShowError(EDiv,EUl,'<li>Debe aceptar los T&eacute;rminos y Condiciones para continuar</li>'); errores += 1; }
	
	if (errores > 0){ top(); }else{ Get('RForm').submit(); }
	switch(errores){
		case 1:
			ShowError(EDiv,EUl,'<li class="JElinada">&nbsp;</li><li class="JElinada">&nbsp;</li>');
		break;
		case 2:
			ShowError(EDiv,EUl,'<li class="JElinada">&nbsp;</li>');
		break;
	}
	
}

var usuarioValido = true;

function CRuser(id){
	
	var div = $("AFuser");
	var url = 'includes/ARuser.php';
	
	div.innerHTML = '<img src="imgs/load2.gif" alt="cargando" />';
	
	new Ajax(url, { method: 'get', 
			 		data: 'u=' + escape(id.value), 
					evalScript: true, 
					onComplete: function(response){ 
						switch( parseInt(response) ){
							case 1:
							default:
								div.innerHTML = "<img src='imgs/ok.gif' alt='' title='cssheader=[THead] cssbody=[TBody] header=["+ id.value +"] body=[Su nombre de usuario est&aacute; disponible]' />";
								usuarioValido = true;
							break;
							case 0:
								div.innerHTML = "<img src='imgs/no.gif' alt='' title='cssheader=[THead] cssbody=[TBody] header=["+ id.value +"] body=[Su nombre de usuario no est&aacute; disponible]' />";
								usuarioValido = false;
							break;
							case 2:
								div.innerHTML = "<img src='imgs/no.gif' alt='' title='cssheader=[THead] cssbody=[TBody] header=["+ id.value +"] body=[El nombre de usuario solo puede contener letras, n&uacute;meros y guiones]' />";
								usuarioValido = false;
							break;
						}
					} 
					}).request();
	
}

function RPRCheck(){
	
	var pais = $('CPais').value;
	var localidad = $('CLoc');
	var provincia = $('CProv');
	var url = 'includes/ARLoc.php';
	
	localidad.innerHTML = '<option>Cargando..</option>';
	
	new Ajax(url, { method: 'get', 
			 		data: 'p=' + pais + '&pr=' + provincia.value, 
					evalScript: true, 
					onComplete: function(response){ 
					
						var localidad = $('CLoc');
						localidad.innerHTML = '';
						var array = response.split(",");
						for( a = 0; a < array.length; a += 2){
							if ( array[a+1] != "" && array[a] != "" ){
								var nOption = document.createElement("option");
								nOption.value = array[a+1];
								nOption.innerHTML = array[a];
								localidad.appendChild(nOption);
							}
						}
						
						RLCheck();
						
					} 
					}).request();
	
}

function RLCheck(){
	
	var pais = $('CPais');
	var provincia = $('CProv');
	var localidad = $('CLoc');
	var colegioDiv = $("AFcolegio");
	var url = 'includes/ARColegio.php';
	
	colegioDiv.innerHTML = '<option>Cargando..</option>';
	
	new Ajax(url, { method: 'get', 
			 		data: 'p=' + pais.value + '&pr=' + provincia.value + '&l=' + localidad.value, 
					evalScript: true, 
					onComplete: function(response){ 
						var colegioDiv = $("AFcolegio");
						if(response.length){
							
							$("CColegioInput").value = "";							
							
							var colegioInput = $("AFColegioInput");
							colegioInput.value = "";
							colegioInput.style.display = "none";
							
							colegioDiv.removeChild(colegioDiv.firstChild);
							var selectObj = document.createElement("select");
							selectObj.id = "CColegio";
							selectObj.className = "RLselect";
							selectObj.name = "colegio";
							selectObj.onchange = RCInputCheck;
							
							var selec = document.createElement("option");
							selec.innerHTML = "Seleccione su colegio";
							selec.value = "";
							selectObj.appendChild(selec);
							
							colegioDiv.appendChild(selectObj);
							
							var array = response.split(".");
							
							for( a = 0; a < array.length; a += 2 ){
								if(array[a].length){
									var nOption = document.createElement("option");
									nOption.value = array[a];
									nOption.innerHTML = array[a+1];
									selectObj.appendChild(nOption);
								}
							}
							
							
							var otro = document.createElement("option");
							otro.innerHTML = "-- Otro --";
							otro.value = "otro";
							selectObj.appendChild(otro);
						}
						else{
							
							colegioDiv.removeChild(colegioDiv.firstChild);
							var selectObj = document.createElement("select");
							selectObj.id = "CColegio";
							selectObj.className = "RLselect";
							selectObj.name = "colegio";
							selectObj.onchange = RCInputCheck;
						
							colegioDiv.appendChild(selectObj);
							
							var otro = document.createElement("option");
							otro.innerHTML = "-- Otro --";
							otro.value = "otro";
							otro.selected = "selected";
							
							selectObj.appendChild(otro);
							RCinput();
						}
						
					} 
					}).request();
	
}

function RCInputCheck(){
	if ( $('CColegio').value == "otro" ){
		RCinput();
	}else if( $('CColegio').value != "" ){
		$("CColegioInput").value = "";
		$("AFColegioInput").style.display = "none";
	}
}

function RCinput(){
	
	var colegioInputDiv = $("AFColegioInput");
	var colegioInput = $("CColegioInput");	
	
	colegioInput.value = "";
	
	if ( colegioInputDiv.style.display = "none" ){
		colegioInputDiv.style.display = "block";
		colegioInput.focus();
	}else{
		colegioInputDiv.style.display = "none";
	}

}

function ActPromocion(anoActual){
	
	mesDiv = $('mes');
	anoDiv = $('ano');
	promocionCombo = $('CPromocion');
	
	duracion = Number(mesDiv.value) <= 6 ? 17 : 18;
	
	promocion = duracion + Number(anoDiv.value);

	promocionCombo.value = promocion;
	
}
