function datepicker(dia,mes,ano){
	edad='edad';
	document.write("<select name='edad-dia' id='edad-dia' value=''> <option value=''>Dia</option> ");		
	for (i=1; i<=31; i=i+1){
		if(dia==i)
			document.write("<option value="+i+" selected='selected'>"+i+"</option>");
		else
			document.write("<option value="+i+">"+i+"</option>");
	}
		
	document.write("</select><select name='edad-mes' id='edad-mes' value=''> <option value=''>Mes</option>");
	
	for (i=1; i<=12; i=i+1){
		if(mes==i)
			document.write("<option value="+i+" selected='selected'>"+i+"</option>");
		else
			document.write("<option value="+i+">"+i+"</option>");
	}
		
	document.write("</select><select name='edad-ano' id='edad-ano'  value=''> <option value=''>A&ntilde;o</option>");
	
	for (i=2005; i>=1900; i=i-1){
		if(ano==i)
			document.write("<option value="+i+" selected='selected'>"+i+"</option>");
		else
			document.write("<option value="+i+">"+i+"</option>");
	}
		
	document.write("</select>");
	
}

function regionpicker(name,sel){

	document.write("<select name='"+name+"_reg' title='"+name+"' class='caja' id='"+name+"_reg'  onchange='escribir_comunas(this.value,title);' value=''> <option value=''>Region</option></select>");
	document.write("<select name='"+name+"_com' title='"+name+"' class='caja' id='"+name+"_com'  value=''> <option value=''>Comuna</option></select>");

	escribir_regiones(name+"_reg");

}

function escribir_regiones(name)
{
	var req = new XMLHttpRequest(); 
	if (req) { 
		req.onreadystatechange = function() { 
			if (req.readyState == 4 && (req.status == 200 || req.status == 304)) { 
				var temp = new Array();
				temp = req.responseText.split(',');
				for (i=0; i<temp.length-1; i=i+1){
					appendOptionLast(temp[i],name);
				}
			} 
		}; 
		req.open('GET', patineta_plugin_url+'/patineta_censo.php?regiones=yes');
 		req.send(null); 
	}
}

function appendOptionLast(texto,selectX)
{
  var elOptNew = document.createElement('option');
  elOptNew.text = texto.toLowerCase();
  elOptNew.value = texto;
  var elSel = document.getElementById(selectX);

  try {
    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    elSel.add(elOptNew); // IE only
  }
}

function removeOldOptions(selectX)
{
	var elSel = document.getElementById(selectX);
	for (i = elSel.length - 1; i>=0; i--) {
      elSel.remove(i);
  }
}

function escribir_comunas(value,name){
	name += '_com';
	var req = new XMLHttpRequest(); 
	if (req) { 
		req.onreadystatechange = function() { 
			if (req.readyState == 4 && (req.status == 200 || req.status == 304)) { 
				var temp = new Array();
				temp = req.responseText.split(',');
				removeOldOptions(name);
				appendOptionLast("Comuna",name);
				for (i=0; i<temp.length-1; i=i+1){
					appendOptionLast(temp[i],name);
				}
			} 
		}; 
		req.open('GET', patineta_plugin_url+'/patineta_censo.php?comunas='+value); 
		req.send(null); 
	}

}


//VALIDACIONS


function valida(inputValue, fieldID)
{
	if(inputValue!="")
		document.getElementById(fieldID+"Fail").style.color = "black";
	else
		document.getElementById(fieldID+"Fail").style.color = "red";
	checkAll();
}

function valida_edad(){
	if(document.getElementById("edad-ano").value == "" || document.getElementById("edad-mes").value == "" || document.getElementById("edad-dia").value == "")
		document.getElementById("edadFail").style.color = "red";
	else
		document.getElementById("edadFail").style.color = "black";
		
}

function valida_location(name){
	if(document.getElementById(name+"_reg").value == "" || document.getElementById(name+"_com").value == "" || document.getElementById(name+"_com").value == "Comuna")
		document.getElementById(name+"Fail").style.color = "red";
	else
		document.getElementById(name+"Fail").style.color = "black";		
}

function valida_sexo(){
	if(!(document.getElementById("sexo1").checked || document.getElementById("sexo2").checked))
		document.getElementById("sexoFail").style.color = "red";
	else
		document.getElementById("sexoFail").style.color = "black";		
}

function valida_estilo(){
	if(!(document.getElementById("estilo_street").checked || document.getElementById("estilo_skatepark").checked || document.getElementById("estilo_miniramp").checked || document.getElementById("estilo_bowl").checked))
		document.getElementById("estiloFail").style.color = "red";
	else
		document.getElementById("estiloFail").style.color = "black";		
}

function checkAll()
{
	var i=0;
	if(document.getElementById("nombre").value == "" ||
	document.getElementById("apellido").value == "" || 
	(document.getElementById("sexo1").value == "" && document.getElementById("sexo2").value == "") ||
	(document.getElementById("estilo_street").value == "" &&  document.getElementById("estilo_skatepark").value == "" &&  document.getElementById("estilo_miniramp").value == "" &&  document.getElementById("estilo_bowl").value == "") ||
	document.getElementById("email").value == "" ||
	document.getElementById("edad-ano").value == "" ||
	document.getElementById("edad-mes").value == "" ||
	document.getElementById("edad-dia").value == "" ||
	document.getElementById("residencia_reg").value == "" ||
	document.getElementById("residencia_com").value == "" ||
	document.getElementById("patinas1_reg").value == "" ||
	document.getElementById("patinas1_com").value == "" ||
	document.getElementById("tiempopatinando").value == "" ||
	document.getElementById("plata").value == "" ||
	document.getElementById("frecuencia").value == "")
	i=1;
		
	
	if(i==1){
		document.getElementById("submit").style.display = "none";
		document.getElementById("checkX").style.display = "block";
		}
	else{
		document.getElementById("submit").style.display = "block";
		document.getElementById("checkX").style.display = "none";
		}
		
}

function checkFinal()
{
	valida(document.getElementById("nombre").value,"nombre");
	valida(document.getElementById("apellido").value,"apellido");
	valida_sexo();
	valida_estilo();
	checkEmail(document.getElementById("email").value,"email");
	valida_edad();
	valida_location("residencia");
	valida_location("patinas1");
	valida(document.getElementById("tiempopatinando").value,"tiempopatinando");
	valida(document.getElementById("plata").value,"plata");
	valida(document.getElementById("frecuencia").value,"frecuencia");
}

function checkEmail(inputValue, fieldID)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(inputValue))
		document.getElementById(fieldID+"Fail").style.color = "black";
	else{
		document.getElementById(fieldID+"Fail").style.color = "red";
		document.getElementById(fieldID).value="";
	}

}
function checkEstilo()
{
	i=1;
	if(document.getElementById("estilo_street").value != "0")
		i=0;
	
	if(i=0)
		document.getElementById("estiloFail").style.color = "#333333";
	else
		document.getElementById("estiloFail").style.color = "#ff0000";
		
	checkAll();
}


