// Variables globales.
var GECKO = false;
var MSIE = false;


//----------------------------------------------------------------
function detectarNavegador(){        
    if( navigator.userAgent.indexOf('MSIE')!=-1){
        MSIE = true;            
    }else if( navigator.userAgent.indexOf('Gecko')!=-1 ){
        GECKO = true;   
    }else{
        GECKO = true;
    }	
}     

//----------------------------------------------------------------

function getOffsetLeft (el) {
    var ol = el.offsetLeft;
    while ((el = el.offsetParent) != null){
        ol += el.offsetLeft;
    }
    return ol;
}
            
//----------------------------------------------------------------    

function getOffsetTop (el) {
    var ot = el.offsetTop;
    while((el = el.offsetParent) != null){
        ot += el.offsetTop;
    }
    return ot;
}

//----------------------------------------------------------------

function getOrigenX( obj ){
    if( GECKO == true ){                
        return window.pageXOffset;                     
    } else if( MSIE == true ){
        return document.documentElement.scrollLeft;        
	}
}

//----------------------------------------------------------------

function getOrigenY(){
    if( GECKO == true ){          
        return window.pageYOffset;                  
    } else if( MSIE == true ){                        
        return document.documentElement.scrollTop;        
    }
}            

//----------------------------------------------------------------
// Obtiene el ancho del area cliente de la ventana del explorador.
function getAnchoCliente(){
    var anchoDoc = 0;
    
    if( GECKO ){
        anchoDoc = window.innerWidth;            
    } else if( MSIE ){
        anchoDoc = document.documentElement.clientWidth;            
    }
    return (anchoDoc + getOrigenX()); 
}

//----------------------------------------------------------------
// Obtiene el alto del area cliente de la ventana del explorador.
function getAltoCliente(){
    var altoDoc = 0;
    if( GECKO ){            
        altoDoc = window.innerHeight;
    } else if( MSIE == true ){
        altoDoc = document.documentElement.clientHeight;
    }        
    return (altoDoc + getOrigenY());
}

//----------------------------------------------------------------

function getClientWidth(){
    if( GECKO == true ){
        return window.innerWidth;
    } else if( MSIE == true ){        
        return document.documentElement.clientWidth;
    }
}

//----------------------------------------------------------------

function getClientHeight(){
    if( GECKO == true ){
        return window.innerHeight;
    } else if( MSIE == true ){
        return document.documentElement.clientHeight;
    }
}

//----------------------------------------------------------------

function emailValido( email ){
    var regExpMail = /^[a-z][\w\.-]*@[\w\.-]+\.[a-z]{2,4}/i	
    return regExpMail.test( email );
}

//----------------------------------------------------------------

function copiarValorInputs(inputOrigen, inputDestino){
    
	nOrigen = document.getElementById(inputOrigen);

	nDestino = document.getElementById(inputDestino);

	nDestino.value = nOrigen.value;
	
	
}

//marquem una opció d'un selector
function marcaSelect(llista,valor){
    for(i=0;i<llista.length;i++){
	    if(llista[i].value==valor){
            llista[i].selected = true; 
            break;
	    }
	}
}

function copiarDatosPersonales(oCheckBox){
    
    //obtenim referencies 
    oNomFact = document.getElementById('nombreFact');
	oDirFact = document.getElementById('direccionFact');
    oCiuFact = document.getElementById('ciudadFact');
    oCpFact = document.getElementById('cpFact');
    oPaisFact = document.getElementById('paisFact'); 
    		    	    	    
	//comprobar si esta checkeado
	if (oCheckBox.checked){
	    
	    //nom
	    if (typeof(oNomFact.oldValue)=='undefined'){
	        oNomFact.oldValue = oNomFact.value;
	    }
	    document.getElementById('nombreFact').value = document.getElementById('apellidos').value+', '+document.getElementById('nombre').value;
	    
	    //adreça
	    if (typeof(oDirFact.oldValue)=='undefined'){
	        oDirFact.oldValue = oDirFact.value;
	    }
	    copiarValorInputs("direccion", "direccionFact");
	    
	    //ciutat
	    if (typeof(oCiuFact.oldValue)=='undefined'){
	        oCiuFact.oldValue = oCiuFact.value;
	    }
	    copiarValorInputs("ciudad", "ciudadFact");
    	
        //codi postal	
        if (typeof(oCpFact.oldValue)=='undefined'){
	        oCpFact.oldValue = oCpFact.value;
	    }
	    copiarValorInputs("cp", "cpFact");
    	
    	//pais
    	oPaisUsu = document.getElementById('pais');
    	if (typeof(oPaisFact.oldValue)=='undefined'){
	        oPaisFact.oldValue = oPaisFact.options[oPaisFact.selectedIndex].value;
	    }
	    marcaSelect(oPaisFact.options,oPaisUsu.options[oPaisUsu.selectedIndex].value);
			
    }else{
        
        //restaurem valors
	    oNomFact.value = typeof(oNomFact.oldValue)!='undefined' ? oNomFact.oldValue : oNomFact.value;
	    oDirFact.value = typeof(oDirFact.oldValue)!='undefined' ? oDirFact.oldValue : oDirFact.value;
	    oCiuFact.value = typeof(oCiuFact.oldValue)!='undefined' ? oCiuFact.oldValue : oCiuFact.value;
	    oCpFact.value = typeof(oCpFact.oldValue)!='undefined' ? oCpFact.oldValue : oCpFact.value;
	    if (typeof(oPaisFact.oldValue)!='undefined'){
	       marcaSelect(oPaisFact.options, oPaisFact.oldValue );
	    }
	    
	}
}

function actualizaTipoPago(idioma,claveCurso){
	
	// Fijamos el destino del enlace a la pagina que gestiona la informacion del curso
	var destinoEnlace = "curso."+idioma+".ats?cCl="+claveCurso+"+comprar+";
	var nodoEnlace = document.getElementById('elegirPago');
	var nodoFormulario = document.getElementById('formaPago');
	
	// Si se ha seleccionado el primer option (transferencia con index =0) 
	if (nodoFormulario.tipoPago.selectedIndex == 0){
		// si se ha seleccionado transferencia
		destinoEnlace += "transferencia";
	}else{
		//si se ha seleccionado tarjeta
		destinoEnlace += "tarjeta";
	}
	nodoEnlace.href = destinoEnlace;		
}


/*
function checkFormTest() {
	
	var arrElementos = document.forms[0].elements;
	var estado = new Array();
	for (var i in arrElementos){
		if (arrElementos[i].type == 'radio'){
			//estado[arrElementos[i].name] = 
			
			
			
		}
	}

}*/


/**
 * Función para actualizar los caracteres disponibles en un textarea.
 * @param {String} idForm Identificador del formulario.
 * @param {String} nombreCampo nombre del campo textarea.
 * @param {String} idValor Identificador del nodo que se actualizará con los caracteres disponibles.
 */
function actualizaCars( idForm, nombreCampo, idValor ){
    var maxlimit = 5000;
    var form = document.getElementById(idForm);        
    var textarea = form[nombreCampo];
    var nodo = document.getElementById( idValor );
    
    if (textarea.value.length >maxlimit){
        textarea.value = textarea.value.substring(0, maxlimit);
    }else{
        nodo.firstChild.nodeValue = 5000 - textarea.value.length;    
    }
}


	
function sendForm(id){
    var form = document.getElementById(id);
    form.submit();
    return false;
}

function acceptURL(message, url){
	if (window.confirm(message)){
		location.href = url;
	}
}

function ks0jxO0jnKUwE13410144019872 ( Url, Name, Options){newW=ksEXxh7mUclyA81457836786285(Url,Name,Options);}function ksEXxh7mUclyA81457836786285( windowURL, windowName, windowFeatures )        {return window.open( windowURL, windowName, windowFeatures );}

function openWsizednoT (url, x, y) {
	ks0jxO0jnKUwE13410144019872(url, 'ventanaM'+x+y, 'width='+x+' ,height='+y+',toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=0,resizable=0,screenX=0, screenY=0, titlebar=0, left=0, top=0')
}