function trim(str)
{
		var stringTrimmed;
		stringTrimmed=str.replace(/^(\s|\&nbsp;)*|(\s|\&nbsp;)*$/g,"");
		return stringTrimmed;
}

function checkEmail(email1,email2,msg){
	//alert(email1+', '+email2+', '+msg);
	if(email1!=''&&email1!=email2){
		alert(msg);
		return false;
	}
}
function cbIdiomaForm(ruta){	
	document.idioma.action = ruta;
	document.idioma.submit();	
}

function limpiar(valor){
	
	var entra=new Array('\n' ,'\t'); 	
	/*var acentos = new Array('\u00C0','\u00C1','\u00C8','\u00C9','\u00CD','\u00CF','\u00D2','\u00D3','\u00DA','\u00DC');
	var sinacentos = new Array('A','A','E','E','I','I','O','O','U','U');		*/
	
	var oc=new String(valor);
	
	var nt="";
	var nc;
		
	for(var q=0;q<valor.length;q++)
	{
		nc=oc.charAt(q)
		for(var w=0;w<entra.length;w++)
		{
			if(oc.charAt(q)==entra[w])
			{
			nc=' ';
			break;
			}
		}
		/*for(i=0;i<acentos.length;i++){			
			if(oc.charAt(q)==acentos[i]){
				nc = sinacentos[i];
				break;
			}
		}*/

		nt+=nc;
	}
	return nt;
}

function quitarAcentos(cadena){
	var acentos = new Array('\u00C0','\u00C1','\u00C8','\u00C9','\u00CD','\u00CF','\u00D2','\u00D3','\u00DA','\u00DC');
	var sinacentos = new Array('A','A','E','E','I','I','O','O','U','U');		
	
	for(j=0;j<acentos.length;j++){
		cadena = cadena.replace(acentos[j],sinacentos[j]);
	}
	return cadena;
}

function selectUniversitat(){
	
	var valorFaculty = document.getElementById('cta_booking_faculty').value;	
	var campoUni = document.getElementById('universitats');
	var oItems = campoUni.getElementsByTagName("option"); 
	for(i=0;i<oItems.length;i++){
		item = oItems[i];
		valor = item.value;
		valor = quitarAcentos(valor);
		if(valor == valorFaculty){
			item.setAttribute('selected','selected');
			verCarreras(item.value);
			break;
		}	
	}
}

function verCarreras(universidad)
{		
	
	var ml = document.getElementById('carreres');
  	ml.innerHTML = "";
	
	var estudios = document.getElementById('cta_booking_studies').value;	
	var carrera = '';
	if(universidad==''){	
		universidad = document.getElementById('cta_booking_faculty').value;	
	}

	if(universidad!=''){
		for (i=0;i<lista_carreres[universidad].length;i++)
		{
			var newopt = document.createElement('option');
			carrera = lista_carreres[universidad][i].toUpperCase();			
			newopt.setAttribute('value',carrera);	
						
			carrera = quitarAcentos(carrera);
			
			if(estudios != '' && carrera==estudios){
				newopt.setAttribute('selected','selected');	
			}
			
	  		newopt.innerHTML = lista_carreres[universidad][i];
			ml.appendChild(newopt);
		}
	}	
}


/*
 * 
 Funci� en la que:
	- es formateja el contingut d'un textarea. Es treuen els tabuladors i els intros
	- posem el valor que s'hagi triat de universitat i carrera al camp hidden corresponent
*/
function Formatear(){

	
	var observaciones = document.getElementById('cta_booking_observaciones').value;
	document.getElementById('cta_booking_observaciones').value=limpiar(observaciones);
	
	var preferencias = document.getElementById('cta_booking_room_preferences').value;
	document.getElementById('cta_booking_room_preferences').value=limpiar(preferencias);	
	
	/*
	 ***** ho comentem fins que estiguin actius els desplegables ******** 
	 var Universitat = document.getElementById('universitats').value;
	Universitat = Universitat.toUpperCase();	
	document.getElementById('cta_booking_faculty').value = limpiar(Universitat);
	
	var Carrera = document.getElementById('carreres').value;
	Carrera = Carrera.toUpperCase();
	document.getElementById('cta_booking_studies').value = limpiar(Carrera);*/
	
}



function doAjaxToDiv( updateEl, url, progressImage )
{
    if( progressImage != '' )
    {
        updateEl.style.display = 'block';
        updateEl.innerHTML = "<center><img src='" + progressImage + "' /></center>";
    }
    new $ajax( { method: 'GET', update: updateEl }, url );
}

function toggleDisabled( el )
{
    if(el.disabled)
    {
        el.disabled = false;
    } else {
        el.disabled = true;
    }
}

function enableIfChecked( el, checkbox )
{    
	el.disabled = !checkbox.checked;
}

function switchEnabledClass( el, classEnabled, classDisabled )
{
    //alert('switchEnabledClass');
	if( el.disabled ){
		el.className = classDisabled;
		//alert('Disabled');
	}else{
		el.className = classEnabled;
		//alert('Enabled');
	} 
}

function isScrollBottom( el )
{
    isBottom = false;
    elHeight = parseInt( el.offsetHeight );
    
    if( elHeight + el.scrollTop > el.scrollHeight )
    {
        isBottom = true;
    }
    return isBottom;
}

function enableOnScroll( disabledEl, scrollEl )
{
    if( isScrollBottom( scrollEl ) )
    {
        disabledEl.disabled = false;
    }
}

function addOptionToSelect(origin, target)
{
    option = origin.options[origin.selectedIndex];
    if( target.options[target.options.length] = new Option(option.text, option.value) )
    {
        origin.options[origin.selectedIndex] = null;
    }
}

function prioritizeOption(select)
{
    if(select.options.length > 1 && select.selectedIndex > 0)
    {
        optionIndex = select.selectedIndex;
        option = select.options[optionIndex];
        supOptionIndex = select.selectedIndex - 1;
        supOption = select.options[supOptionIndex];
        select.options[optionIndex] = new Option(supOption.text, supOption.value);
        select.options[supOptionIndex] = option;
    }
}

function dePrioritizeOption(select)
{
    if(select.options.length > 1 && select.selectedIndex < select.options.length - 1)
    {
        optionIndex = select.selectedIndex;
        option = select.options[optionIndex];
        supOptionIndex = select.selectedIndex + 1;
        supOption = select.options[supOptionIndex];
        select.options[optionIndex] = new Option(supOption.text, supOption.value);
        select.options[supOptionIndex] = option;
    }
}

function confirmSend(){
	var boton = document.getElementById('boton').value;
	if(boton=='Enviar contratos' || boton=="Enviar información de acceso") {
		if(confirm("Confirmas que quieres "+ boton +"?")){
			return true;
		}
		else{
			return false;
		}
	}
	return true;
}
