// JavaScript Document

function nuevoAjax(){ 
	var objeto=false; 
	if(window.XMLHttpRequest){
		objeto = new XMLHttpRequest(); 
	}else{ 
		try{
			objeto = new ActiveXObject("Microsoft.XMLHTTP"); 
		}
		catch (e){
			alert('El navegador utilizado no cumple los requisitos para ver esta página.');
		}
	}
	return objeto; 
}

function cargaContenido(){
	
	var valor=document.getElementById('s_curso').options[document.getElementById('s_curso').selectedIndex].value;

		ajax=nuevoAjax();
		ajax.open("GET", "duracion.php?seleccionado="+valor, true);
		ajax.onreadystatechange=function() { 
			if ((ajax.readyState==4) && (ajax.status==200) ){ 
				document.getElementById('id_duracion').innerHTML=ajax.responseText;
			} 
			if (ajax.readyState==1) { 
				document.getElementById('id_duracion').innerHTML = "<select  ><option>Loading...</option></select>";
			} 		
		}
		ajax.send(null);
}

function  cargarfecha(v){

		if(v==19){
			document.getElementById('id_fecha').innerHTML="<select name='fecha' ><option selected value='0'>Choose a star date</option></select>";
		}else{
			ajax=nuevoAjax();
	
			ajax.open("GET", "fecha.php?valor="+v, true);
			ajax.onreadystatechange=function() { 
				if ((ajax.readyState==4) && (ajax.status==200) ){ 
					document.getElementById('id_fecha').innerHTML=ajax.responseText;
				} 
				if (ajax.readyState==1) { 
					document.getElementById('id_fecha').innerHTML = "<select  ><option>Loading...</option></select>";
				} 		
			}
			ajax.send(null);
		}
}

function cargaContenido2(pag,des)
{
	var valor=document.getElementById('id_nivel').options[document.getElementById('id_nivel').selectedIndex].value;
	var valor2=document.getElementById('id_ciudad').options[document.getElementById('id_ciudad').selectedIndex].value;
		ajax=nuevoAjax();

		ajax.open("GET", pag+".php?seleccionado="+valor+"&c="+valor2, true);
		ajax.onreadystatechange=function() { 
			if ((ajax.readyState==4) && (ajax.status==200) ){ 
				document.getElementById(des).innerHTML=ajax.responseText;
			} 
			if (ajax.readyState==1) { 
				document.getElementById(des).innerHTML="<select  ><option>Loading...</option></select>"
			} 
		}
		ajax.send(null);
}


function fciudad(){
	cargaContenido2('curso','id_curso');
}

function carga(){
	
	var v=document.getElementById('s_curso').options[document.getElementById('s_curso').selectedIndex].value;
	
	
		if(v==19){
			document.getElementById('id_fecha').innerHTML="<select name='fecha' ><option selected value='0'>Choose a star date</option></select>";
		}else{
			ajax=nuevoAjax();
	
			ajax.open("GET", "fecha.php?valor="+v, true);
			ajax.onreadystatechange=function() { 
				if ((ajax.readyState==4) && (ajax.status==200) ){ 
					document.getElementById('id_fecha').innerHTML=ajax.responseText;
				} 
				if (ajax.readyState==1) { 
					document.getElementById('id_fecha').innerHTML = "<select  ><option>Loading...</option></select>";
				} 		
			}
			ajax.send(null);
		}

	
	var valor=document.getElementById('s_curso').options[document.getElementById('s_curso').selectedIndex].value;

		ajax2=nuevoAjax();
		ajax2.open("GET", "duracion.php?seleccionado="+valor, true);
		ajax2.onreadystatechange=function() { 
			if ((ajax2.readyState==4) && (ajax2.status==200) ){ 
				document.getElementById('id_duracion').innerHTML=ajax2.responseText;
			} 
			if (ajax2.readyState==1) { 
				document.getElementById('id_duracion').innerHTML = "<select  ><option>Loading...</option></select>";
			} 		
		}
		ajax2.send(null);
	
	
	//cargarfecha(valor);
	//setTimeout ("cargaContenido()",200);
	
}

function validacion(){
	var bien=0
	var b =false
	
	error = new Array(5);

	for(j=0;j<5;j++){
		error[j]=0
	}
	
	if(document.form1.ciudad.value==0){
		error[0]=0
	}else{
		error[0]=1
	}
	if(document.form1.nivel.value==0){
		error[1]=0
	}else{
		error[1]=1
	}
	if(document.form1.curso.value==0){
		error[2]=0
	}else{
		error[2]=1
	}
	if(document.form1.fecha.value==0){
		error[3]=0
	}else{
		error[3]=1
	}
	if(document.form1.duracion2.value==0){
		error[4]=0
	}else{
		error[4]=1
	}

	if (error[0]==0) {
		bien=1
	}
	if (error[1]==0) {
		bien=1
	}
	if (error[2]==0) {
		bien=1
	}
	if (error[3]==0) {
		bien=1
	}
	if (error[4]==0) {
		bien=1
	}

	if(bien==1){
		alert("Hay datos sin completar.")
	}else{
		b=true
	}
	return b
}



function Email(e){
    this.emailAddr=e;
    this.message="";
    this.valid=false;
}

function validate(){
    //do a basic check for null, zero-length string, ".", "@",
    //and the absence of spaces
    if (this.emailAddr == null || this.emailAddr.length == 0 ||
        this.emailAddr.indexOf(".") == -1 ||
        this.emailAddr.indexOf("@") == -1 ||
        this.emailAddr.indexOf(" ") != -1){
        this.message="Make sure the email address does not contain any spaces "+
                     "and is otherwise valid (e.g., contains the \"commercial at\" @ sign).";
        this.valid=false;
        return;
    }

    /*Get the user; they cannot begin or end with a "."
Regular expression specifies: the group of characters before the @ symbol, which
are made up of word characters, followed by zero or one period char,
followed by at least 2 word characters. */
    regex=/(^\w{2,}\.?\w{2,})@/;
    _match = regex.exec(this.emailAddr);

    if ( _match){
        user=RegExp.$1;
        
    } else {
        this.message="Make sure the user name is more than two characters"+
                     ", does not begin or end with a period (.), or is not otherwise "+
                     "invalid!";
        this.valid=false;
        return;
    }
    //get the domain after the @ char
    //first take care of domain literals like @[19.25.0.1] however rare
    regex=/@(\[\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}\])$/;
    _match = regex.exec(this.emailAddr);

    if( _match){
        domain=RegExp.$1;
         //alert("domain: "+domain);
        this.valid=true;
    } else {
        /*the @ character followed by at least 2 chars that are not a period (.),
followed by a period, followed by zero or one instances of at least two
characters ending with a period, followed by two-three chars that are not periods */
        regex=/@(\w{2,}\.(\w{2,}\.)?[a-zA-Z]{2,3})$/;
        _match = regex.exec(this.emailAddr);
        if( _match){
            domain=RegExp.$1;
            
        } else {
            this.message="The domain portion of the email had less than 2 chars "+
                         "or was otherwise invalid!";
            this.valid=false;
            return;
        }
    }//end domain check
    this.valid=true;

}

//Make validate() an instance method of the Email object
Email.prototype.validate=validate;

function eMsg(msg,sColor,id){
    var div = document.getElementById(id);
    div.style.color=sColor;
    div.style.fontSize="1em";
    //remove old messages
    if(div.hasChildNodes()){
        div.removeChild(div.firstChild);
    }
    div.appendChild(document.createTextNode(msg));

}
//a pull-it-all-together function
function checkAddress(){
var val= document.form2.email.value
    var eml = new Email(val);
    eml.validate();
    if (! eml.valid) {
		eMsg(eml.message,"red","message")
		bien=false
	}else{
		bien=true
	}
	
	return bien
}


