function check_form_registrazione_utente() {
	var form = document.forms['userRegisterForm']; 

	//controllo campo user obbligatorio
	if ((form.profile_nome.value == "") || (form.profile_nome.value == "undefined")) {
		alert("Attenzione, manca il campo Nome");
		return false;
	} else {	
		form.submit();
	}
}



var requestProgress=0;

function getElementById_s(id){
	var obj = null;
	if(document.getElementById){
		obj = document.getElementById(id);
	}else if(document.all){
		obj = document.all[id];
	}
	return obj;
}



function IsEmpty_s (obj){
  if( (obj.value==null)||(obj.value=="") ){
		return true;
	}
	return false; 
}  

function chkInputField(obj, msg, focus ){
	var ret = true;
	if (obj==null || IsEmpty_s(obj) ) {
		jAlert('warning',msg);
		if (focus){
			obj.focus();
		}
		ret= false;
	}
	return ret;
}

function isAnInteger(e) {
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	var charCode  = code;
	return ( charCode >= 48 && charCode <= 57 );
}

function isSpecialChar(e) {
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	var charCode  = code;
	
	return  ( charCode == 60 || charCode == 62 );
}

function checkExt(obj, extf) {
	if ( obj != null ){ 
		var ext = obj.value;
		ext = ext.substring(ext.length-3,ext.length);
		ext = ext.toLowerCase();
		if(ext!= extf) {
		  return false; 
		}
		else{
		  return true; 
		}
	}else{
		return false; 
	}
}

function checkRadioButton(obj, msg, focus ){
    var check=0;
	for (counter = 0; counter < obj.length; counter++){ 
       if (obj[counter].checked){ 
        check=1;
       }
    } 
	if (check==0){ 
		jAlert('warning',msg);
        return false;
   }
	return true;
}



function disableObj(strObj){
	if (strObj != null ){
	 	var obj =  document.getElementById(strObj);
		if( obj!= null ){
			obj.disabled="disabled";
		}
	}
} 

//Form modifica profilo utente
function ValidateUpdateForm(){
	
	var fieldNome=getElementById_s('edit-profile-nome');
	var fieldCognome=getElementById_s('edit-profile-cognome');
	var fieldMail=getElementById_s('edit-mail-1');
	var fieldAnnoNascita=getElementById_s('edit-profile-anno-nascita');
	var fieldPassword=getElementById_s('edit-pass-1');
	var fieldPasswordConfirm=getElementById_s('edit-pass2');		
		
	// Nome
	if (! chkInputField(fieldNome, "Campo Nome obbligatorio" , true) ){
		return false;
	}
	// Cognome
	if (! chkInputField(fieldCognome, "Campo Cognome obbligatorio" , true) ){
		return false;
	}
	//Mail
	if (! chkInputField(fieldMail, "Campo Email obbligatorio" , true) ){
		return false;
	}else if (!echeckEmail(fieldMail.value)){
			fieldMail.focus();
			return false;
	}
	// Anno di nascita
	if (! chkInputField(fieldAnnoNascita, "Campo Anno di Nascita obbligatorio" , true) ){
			return false;
	}
	
	//Password
	if (fieldPassword==null || ! IsEmpty_s(fieldPassword) ) {
		if (fieldPassword.value.length < 6 || fieldPassword.value.length > 12) {
			jAlert('warning','La password deve essere di almeno 6 caratteri e massimo di 12');
			return false;
		}
			if (! chkInputField(fieldPasswordConfirm, "Campo Conferma Password obbligatorio" , true) ){
			return false;
		} 
		if (fieldPassword.value != fieldPasswordConfirm.value) {
			jAlert('warning','Le password inserite non coincidono');
			return false;
		}
	}
	
	return true; 
}

//Form inserimento utente
function ValidateForm(){
	var fieldNome=getElementById_s('edit-profile-nome-1');
	var fieldCognome=getElementById_s('edit-profile-cognome-1');
	var fieldMail=getElementById_s('edit-mail-1');
	var fieldAnnoNascita=getElementById_s('edit-profile-anno-nascita-1');
	var fieldUsername=getElementById_s('edit-name-1');
	var fieldPassword=getElementById_s('edit-pass-1');
	var fieldPasswordConfirm=getElementById_s('edit-pass2');		
	var chkPrivacy=getElementById_s('edit-profile-privacy-1');
		
	// Nome
	if (! chkInputField(fieldNome, "Campo Nome obbligatorio" , true) ){
		return false;
	}
	// Cognome
	if (! chkInputField(fieldCognome, "Campo Cognome obbligatorio" , true) ){
		return false;
	}
	//Mail
	if (! chkInputField(fieldMail, "Campo Email obbligatorio" , true) ){
		return false;
	}else if (!echeckEmail(fieldMail.value)){
			fieldMail.focus();
			return false;
	}
	// Anno di nascita
	if (! chkInputField(fieldAnnoNascita, "Campo Anno di Nascita obbligatorio" , true) ){
			return false;
	}
	//Username
	if (! chkInputField(fieldUsername, "Campo Username obbligatorio" , true) ){
		return false;
	}
	if (fieldUsername.value.length < 6 || fieldUsername.value.length > 12) {
		jAlert('warning','La username deve essere di almeno 6 caratteri e massimo di 12');
		return false;
	}
	//Password
	if (! chkInputField(fieldPassword, "Campo Password obbligatorio" , true) ){
		return false;
	} 
	if (! chkInputField(fieldPasswordConfirm, "Campo Conferma Password obbligatorio" , true) ){
		return false;
	} 
	if (fieldPassword.value.length < 6 || fieldPassword.value.length > 12) {
		jAlert('warning','La password deve essere di almeno 6 caratteri e massimo di 12');
		return false;
	}
	if (fieldPassword.value != fieldPasswordConfirm.value) {
		jAlert('warning','Le password inserite non coincidono');
		return false;
	}
	// check privacy
	if  ( ! (  chkPrivacy!=null && chkPrivacy.checked==true ) ){
		jAlert('warning',"Prendere visione dell'informativa sulla privacy prima di proseguire");
		if ( chkPrivacy!=null ) {
			chkPrivacy.focus();
		}
		return false;
	}
	
	return true;
}


function echeckEmail(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	var msg = "L'email sembra non essere valida.\n  Verifica di averla scritta correttamente.";
	if (str.indexOf(at)==-1){
	   jAlert('warning',msg);
	   return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   jAlert('warning',msg);
	   return false;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    jAlert('warning',msg);
	    return false;
	}
	 if (str.indexOf(at,(lat+1))!=-1){
	    jAlert('warning',msg);
	    return false;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    jAlert('warning',msg);
	    return false;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    jAlert('warning',msg);
	    return false;
	 }
	
	 if (str.indexOf(" ")!=-1){
	    jAlert('warning',msg);
	    return false;
	 }
	 
	 return true;
	 
}

//Form carica video
function ValidateUploadVideo(){
	var fieldFile=getElementById_s('edit-field-upload-video-0-upload');
	var fieldTitle=getElementById_s('edit-title');
	var fieldImg=getElementById_s('edit-field-anteprima-video-0-upload');
	var checkCategoriaVideo = document.getElementsByName("taxonomy[6]");
	var fieldDescr=getElementById_s('edit-body');
	
	//file upload
	if (! chkInputField(fieldFile, "Seleziona il tuo video" , false) ){
		return false;
	}else{
		var bext  = checkExt(fieldFile, "flv") || checkExt(fieldFile, "avi") || checkExt(fieldFile, "mp4") || checkExt(fieldFile, "wmv") ;
		if (!bext){
			jAlert("warning", "Il video selezionato non ha estensione supportabile.");
			return false;
		}
		 
	}
	//Immagine anteprima
	if (fieldImg==null || ! IsEmpty_s(fieldImg) ) {
		var bext  = checkExt(fieldImg, "jpg") || checkExt(fieldImg, "gif") || checkExt(fieldImg, "png");
		if (!bext){
			jAlert("warning", "L'immagine selezionata non ha estensione supportabile.");
			return false;
		}	
	}
	// Categoria video		
	if (! checkRadioButton(checkCategoriaVideo, "Campo Categoria Video obbligatorio" , true) ){
		return false;
	}
	// Titolo
	if (! chkInputField(fieldTitle, "Campo Titolo obbligatorio" , true) ){
		return false;
	}
	// Descrizione
	if (fieldDescr.value.length > 600) {
		jAlert('warning','La descrizione deve essere massimo di 600 caratteri');
		return false;
	}

}	

//Form carica video
function ValidateUpdateVideo(){
	var fieldTitle=getElementById_s('edit-title');
	var fieldDescr=getElementById_s('edit-body');

	// Titolo
	if (! chkInputField(fieldTitle, "Campo Titolo obbligatorio" , true) ){
		return false;
	}
	// Descrizione
	if (fieldDescr.value.length > 600) {
		jAlert('warning','La descrizione deve essere massimo di 600 caratteri');
		return false;
	}
}	

//Form recupera pwd
function checkSendMail(){
	var fieldUsername=getElementById_s('edit-name');
	var fieldMail=getElementById_s('edit-mail');
	
	// Username
	if (! chkInputField(fieldUsername, "Inserisci la tua username" , true) ){
		return false;
	}
	//Mail
	if (! chkInputField(fieldMail, "Campo Email obbligatorio" , true) ){
		return false;
	}else if (!echeckEmail(fieldMail.value)){
			fieldMail.focus();
			return false;
	}
}

//Form login utente
function checkLogin(){
	var fieldUsername=getElementById_s('edit-name');
	var fieldPwd=getElementById_s('edit-pass');
	
	// Username
	if (! chkInputField(fieldUsername, "Inserisci la tua username" , true) ){
		return false;
	}
	// Pwd
	if (! chkInputField(fieldPwd, "Inserisci la tua password" , false) ){
		return false;
	}
}	

 //Buca di ricerca
function checkSearch(){
	var fieldToSearch=getElementById_s('search_text');
	
	// Paramentro ricerca
	if (! chkInputField(fieldToSearch, "Inserisci la chiave di ricerca" , false) ){
		return false;
	}
	if (fieldToSearch.value =="Inserisci il testo") {
		jAlert('warning','Inserisci la chiave di ricerca');
		return false;
	}
	if (fieldToSearch.value.length < 2) {
		jAlert('warning','La chiave di ricerca deve essere di almeno 2 caratteri');
		return false;
	}
}	

function captureEnter(e) {
	e=e||window.event;
	if (e.keyCode == 13) 
		document.formName.submit();
}

function checkSubmitLogin(){
	var en = document.getElementById('edit-name');
	var ep = document.getElementById('edit-pass');
	if(document.attachEvent){
		en.attachEvent("onkeypress",captureEnter);
		ep.attachEvent("onkeypress",captureEnter);
	}
	else{
		en.addEventListener("keydown", captureEnter, true);
		ep.addEventListener("keydown", captureEnter, true);
	}
}
