// Funciones para validar formularios 


/*Valida la presencia y formato de los campos obligatorios del formulario
de registro*/
function validateRegistration(){
var errors = false;
	var confirmed = true;
    var text = 'Some errors are present: \n';
    var all = 'is a mandatory field\n';
    var form = document.forms[0];
    //hasta llegar a las opciones    
    for(var i=0; i<form.elements.length; i++){
        campo = form.elements[i].value;
        switch (i){
         case 0:
            if (campo==""){
                errors=true;
                 text = text + '  - First Name ' + all;
            }        
             break;
         case 1:
            if (campo==""){
                errors=true;
                 text = text + '  - Last Name ' + all;
            }        
             break;
         case 2:
            if (campo==""){
                errors=true;
                 text = text + '  - Institution ' + all;
            }        
             break;
         //case3 - no es necesario, siempre estara lleno
         case 4:
            if (campo==""){
                errors=true;
                 text = text + '  - E-mail  ' + all;
            }        
             break;	
         case 5:
            if (campo==""){
                errors=true;
                 text = text + '  - Phone ' + all;
            }        
             break;
         case 6:
            if (campo==""){
                errors=true;
                 text = text + '  - Fax ' + all;
            }        
             break;
         default: break;
          }
    }
    if((form.elements[7].checked ==  false)&& (form.elements[8].checked ==  false) ){
        errors = true;
        text = text + '  - You must select Citizenship. \n';
    }

         //elements 9 y 10 - no es necesario, siempre estara lleno
	if( (form.elements[11].checked == false) && 
		(form.elements[12].checked == false) && 
		(form.elements[13].checked == false) && 
		(form.elements[14].checked == false) &&
		(form.elements[15].checked == false)) {
			confirmed = confirm("You haven't selected any additionals. Is that OK?");
			if(!confirmed) {
				text = text + '  - You must specify Additionals. \n';
			}
		}

	if ((form.elements[16].checked == false) &&	(form.elements[17].checked == false)) {
		errors=true;
        text = text + '  - You must specify Acompany Person. \n';
	}

    if((form.elements[16].checked ==  true)){
        campo = form.elements[18].value;
        if(campo==""){
            errors=true;
             text = text + '  - You must specify Acompany Person\'s Full Name. \n';
        }
	}

	if( (form.elements[21].checked == false) &&
		(form.elements[22].checked == false) &&
		(form.elements[23].checked == false)) {
			errors=true;
        	text = text + '  - You must specify Payment Method. \n';
		}

	if( (form.elements[22].checked == true)  ) {
			campo = form.elements[24].value;
        if(campo==""){
            errors=true;
             text = text + '  - You must specify Deposit Number. \n';
        }
	}
   
   if(errors || !confirmed){
		alert(text);
    }
	else{
        form.submit();
    } 

}

/*Calcula el precio que hay que pagar*/
function calculateTotalAmount(){
	var form = document.forms[0];

//Precio de tipo de participante
	if(form.elements[10].selectedIndex==0){
		document.getElementById('total').value = "320";
		document.getElementById('total').style.visibility="visible";		
	}
	else if(form.elements[10].selectedIndex==1){
		document.getElementById('total').value = "350";
		document.getElementById('total').style.visibility="visible";
	}
	else if(form.elements[10].selectedIndex==2){
		document.getElementById('total').value = "370";
		document.getElementById('total').style.visibility="visible";
	}
	else{
		document.getElementById('total').value = "230";
		document.getElementById('total').style.visibility="visible";
	}

//Suma de adicionales
	var total = parseFloat(document.getElementById('total').value);
	if(form.elements[11].checked == true){
		document.getElementById('total').value = ""+(parseFloat(total+65));		
	}
	
	total = parseFloat(document.getElementById('total').value);

	if(form.elements[12].checked == true){
            document.getElementById('total').value = ""+(parseFloat(total+45));
		//total = parseFloat(document.getElementById('total').value);
	}

	total = parseFloat(document.getElementById('total').value);
	if(form.elements[13].checked == true){
            document.getElementById('total').value = ""+(parseFloat(total+45));
		//total = parseFloat(document.getElementById('total').value);
	}
	
	total = parseFloat(document.getElementById('total').value);

//Shortcourse
	if(form.elements[14].checked == true){
	//El costo depende del tipo de participante
		if(form.elements[10].selectedIndex==0){
			document.getElementById('total').value = ""+(parseFloat(total+120));		
		}
		else if(form.elements[10].selectedIndex==1){
			document.getElementById('total').value = ""+(parseFloat(total+120));
		}
		else if(form.elements[10].selectedIndex==2){
			document.getElementById('total').value = ""+(parseFloat(total+240));
		}
		else{
			document.getElementById('total').value = ""+(parseFloat(total+70));
		}	
	}
	total = parseFloat(document.getElementById('total').value);
	
	if(form.elements[15].checked == true){
	//El costo depende del tipo de participante
		if(form.elements[10].selectedIndex==0){
			document.getElementById('total').value = ""+(parseFloat(total+120));		
		}
		else if(form.elements[10].selectedIndex==1){
			document.getElementById('total').value = ""+(parseFloat(total+120));
		}
		else if(form.elements[10].selectedIndex==2){
			document.getElementById('total').value = ""+(parseFloat(total+240));
		}
		else{
			document.getElementById('total').value = ""+(parseFloat(total+70));
		}	
	}

	total = parseFloat(document.getElementById('total').value);

	if(form.elements[7].checked ==  true){ //citizenShip national
		//document.getElementById('currency').innerHTML = "BsF.";
                document.getElementById('currency').value = "BsF.";
		document.getElementById('total').value = ""+(parseFloat(total*3.669));	
	}
	else{
		//document.getElementById('currency').innerHTML = "Eur.";
                document.getElementById('currency').value = "Eur.";
		document.getElementById('currency').style.visibility="visible";
	}
	
}

/* Funcion para calcular el numero de palabras, obtenido de http://javascript.about.com/library/blcount.htm// */
function countWords(w){
	var y=w.value;
	var r = 0;
	a=y.replace(/\s/g,' ');
	a=a.split(' ');
	for (z=0; z<a.length; z++) {if (a[z].length > 0) r++;}
	return r;
}



/*Valida la presencia y formato de los campos obligatorios del formulario
de envio de abstracts*/
function validateAbstractSubmission(){
	var errors = false;
	var confirmed = true;
    var text = 'Some errors are present: \n';
    var all = 'is a mandatory field\n';
    var form = document.forms[0];
    //hasta llegar a las opciones    
    for(var i=0; i<form.elements.length; i++){
        campo = form.elements[i].value;
        switch (i){
         case 3:
            if (campo==""){
                errors=true;
                 text = text + '  - Title of Submission ' + all;
            }        
             break;
          case 4:
            if (campo==""){
                errors=true;
                 text = text + '  - Author(s)  ' + all;
            }        
             break;	
         case 5:
            if (campo==""){
                errors=true;
                 text = text + '  - Afiliation(s) of Author(s) ' + all;
            }        
             break;
         case 6:
            if (campo==""){
                errors=true;
                 text = text + '  - Abstract Text ' + all;
            }        
             break;
         case 8:
            if (campo==""){
                errors=true;
                 text = text + '  - Keywords ' + all;
            }        
             break;
         case 9:
            if (campo==""){
                errors=true;
                 text = text + '  - Presenting Author\'s Name ' + all;
            }        
             break;
         case 10:
            if (campo==""){
                errors=true;
                 text = text + '  - Presenting Author\'s Mailing Address ' + all;
            }        
             break;
         case 11:
            if (campo==""){
                errors=true;
                 text = text + '  - Presenting Author\'s Email ' + all;
            }        
             break;
         default: break;
          }
    }

	// si selecciono yes o no en Best Studen Submission Presentation Competition
    if((form.elements[1].checked ==  false)&& (form.elements[2].checked ==  false) ){
        errors = true;
        text = text + '  - You must select Best Student Submission Presentation Competition. \n';
    }

	// Si el abstract text tiene hasta 250 palabras
	var words = countWords(form.elements[6]);
	if( words > 250){
        errors = true;
        text = text + '  - Abstract text must have 250 words maximum. You provided ' + words + ' words. \n';
    }

	// Si los keywords tienen mas de 5 keywords (se estiman 5 ';'), pero extraniamente el count le suma uno (por eso el if es con 6)
	var current_keywords = form.elements[8].value;
	var splits_current_keyword  = current_keywords.split(";");
	var keyword_count           = splits_current_keyword.length;
	
//	var keywords = form.elements[8].count(";");
	if( keyword_count > 6){
        errors = true;
        text = text + '  - Keywords field must have 5 keywords maximun, separated by semicolon (;). You provided ' + (keyword_count -1) + ' keywords. \n';
    }

    
   if(errors || !confirmed){
		alert(text);
    }
	else{
        form.submit();
    } 

}


/** Valida el formulario de aplicacion de tour (application_form.htm)**/
function validateTourApplication(){
	
	var errors = false;
	var confirmed = true;
    var text = 'Some errors are present: \n';
    var all = 'is a mandatory field\n';
    var form = document.forms[0];
    for(var i=0; i<form.elements.length; i++){
		campo = form.elements[i];
		if(campo.value==""  && campo.disabled==false){
			text = text + '\n- All the fields are mandatory.';
			errors=true;
			break;		
		}
	}

   if(!(document.getElementById('Antispam').value=='cvydm')){
		text = text + '\n- You introduced an incorrect image code.';
		errors=true;
	}

   if(errors){
		alert(text);
    }
	else{
        form.submit();
    } 

}

/** Activa o desactiva campos en el formulario de aplicacion del tour dependiendo de si esta interesado en la oferta especial o no **/
function interestedInSpecialOffer(dec){
	if(dec){
		document.getElementById('ArrivalMargaritaAirport').disabled=true;
		document.getElementById('DepartureMargaritaAirport').disabled=true;	
		document.getElementById('Arrival').disabled=false;
		document.getElementById('Departure').disabled=false;	
	}
	else{
		document.getElementById('ArrivalMargaritaAirport').disabled=false;
		document.getElementById('DepartureMargaritaAirport').disabled=false;	
		document.getElementById('Arrival').disabled=true;
		document.getElementById('Departure').disabled=true;	
	}
}







