/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

function conferma(messaggio, url){
	if(confirm(messaggio))
		window.location.href = url;
}

function chk_txt(o,v){
	if(o.value.search('[a-zA-Z0-9]') == -1){
		if(arguments.length==1) alert('Campo ' + o.name + ' errato');
		else if(arguments.length==2) alert(v);
		o.focus();
		return false;
	}
	return true;
}

function chk_int(o,d){
	if(!o.value || o.value.search('[^0-9]') != -1){
		alert(d);
		o.focus();
		return false;
	}
	return true;
}
//togliere http!!!
function chk_url(o){
	if(o.value.search('[a-z]+://[a-zA-Z0-9]+\.[a-zA-Z0-9]+') == -1){
		alert('Campo ' + o.name + ' errato');
		o.focus();
		return false;
	}
	return true;
}
function chk_url1(o,m){
	if(o.value.search('[a-zA-Z0-9]+\.[a-zA-Z0-9]+') == -1){
		if(arguments.length==2){
			alert(m);
			o.focus();
		}
		return false;
	}
	return true;
}

function azzera(url){
	window.location.href = url;
}
function chk_email(o,d){
	if(o.value.search("^[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-.]?[0-9a-zA-Z])*\\.[a-zA-Z]{2,4}$") == -1){
		alert(d);
		o.focus();
		return false;
	}
	return true;
}
function chk_select(o,d){
	if(!o[o.selectedIndex].value || !o.selectedIndex){
		alert(d);
		o.focus();
		return false;
	}
	return true;
}

function chk_radio(o,d){
	with(document.dati){
	if (o[0].checked==true || o[1].checked==true){
	 return true;
	
	}else{
	 alert (d);
	 o[0].focus;
	  return false;
	}
	}
}

function chk_privacy(){
	with(document.dati){

// CHECK 1° Verifica
	if (
	privacy[0].checked==true ||
	privacy[1].checked==true
	)
	{
	// compilato
	} else {
	alert ("Autorizzazione al trattamento dei dati personali ai sensi del d.lgs. n.196/2003");
	privacy[0].focus();
	return false;
	}
	
// CHECK 1B° Verifica
	if (
	privacy[1].checked!=true
	)
	{
	// compilato
	} else {
	alert ("Selezionare Sì per proseguire con l'iscrizione");
	privacy[0].focus();
	return false;
	}

// FINE Verifica

	}
	return true;
}

function chk_privacy2(){
	with(document.dati){

// CHECK 1° Verifica
	if (
	privacy2[0].checked==true ||
	privacy2[1].checked==true
	)
	{
	// compilato
	} else {
	alert ("Autorizzazione al trattamento dei dati personali ai sensi del d.lgs. n.196/2003");
	privacy2[0].focus();
	return false;
	}
	
// CHECK 1B° Verifica
	if (
	privacy2[1].checked!=true
	)
	{
	// compilato
	} else {
	alert ("Selezionare Sì per proseguire con l'iscrizione");
	privacy2[0].focus();
	return false;
	}

// FINE Verifica

	}
	return true;
}

function chk_lingue(m){
   with(document.dati){
      var vcat = elements['lingua[]'];
      var len = vcat.length;
//    alert('len:'+len);
        
     
          for(i=0; i<len; i++){
             if(vcat[i].checked !== false){
                return true;
             }
          }

      alert(m);
      vcat[0].focus();
      return false;
   }
}

function chk_lingua_altra(m){
   with(document.dati){
      var vcat = elements['lingua[]'];
      var len = vcat.length;
//    alert('len:'+len);
        
      if((vcat[10].checked !== false)&&(altraLingua.value.search('[a-zA-Z0-9]') == -1)){
     // if((vcat[9].checked !== false)){   
        alert(m);
        altraLingua.focus();
        return false;
      } else {
      return true;
      }
   }
}


function chk_cap(o){
var i=0;
with(document.dati){
var str=o.value.length;
if(str==5){
		while(i<5){
				var controllo=1;
				var carattere=o.value.charCodeAt(i);
				if (carattere<48||carattere>57){
						controllo=1;
						alert('Controlla di avere inserito il CAP esatto')
						break;
				}else{
						controllo=2;
				}
				i++;
				}
				if(controllo==1){
						o.focus();	
						return(false);
				}else{
						return(true);
				}
}else{
		alert('Devi inserire il Cap della tua zona');
		o.focus(); 
		return(false);
}
}
}

function chk_pwd(p,p2,m){
	with(document.dati){
		if(p.value==p2.value) return true;
		alert(m);
		p2.focus();
		return false;
	}	
}

function doGetFCK(cp) { 
        var oFCKeditor = FCKeditorAPI.GetInstance(cp) ; 
        //return oEditor.GetHTML();
		var txt =   oFCKeditor.GetHTML();
		if (txt =='' ){
		 	alert ('Inserire il testo');
			return(false);
		}else{
			return(true);
		}

}