function validateAttributo(frm){
	if( frm.nome.value == '' ){
		alert("Nome è un campo obbligatorio!");
		frm.nome.focus();
		return false;
	}
	setLista('scelte', 'list_scelte');
	return true;
}
function validateCategoria(frm){
	if( frm.padre.value == '' ){
		alert("Categoria di è un campo obbligatorio!");
		frm.padre.focus();
		return false;
	}
	if( frm.nome.value == '' ){
		alert("Nome è un campo obbligatorio!");
		frm.nome.focus();
		return false;
	}
	return true;
}
function validateAnnuncio(frm){
	if( frm.titolo.value == '' ){
		alert("Titolo è un campo obbligatorio!");
		frm.titolo.focus();
		return false;
	}
	var prezzo_reg_exp = /^[1-9]\d{0,}([\,\.][0-9]\d{0,})*$/;
	if( frm.prezzo.value != '' && !prezzo_reg_exp.test(frm.prezzo.value) ){
		alert("Formato prezzo non valido!");
		frm.prezzo.focus();
		return false;
	}
	if( frm.categoria.value == '' ){
		alert("Categoria è un campo obbligatorio!");
		frm.categoria.focus();
		return false;
	}
	return true;
}
function validateLogin(frm){
	if( frm.uid.value == '' ){
		alert("Nome utente è un campo obbligatorio!");
		frm.uid.focus();
		return false;
	}
	if( frm.pwd.value == '' ){
		alert("Password è un campo obbligatorio!");
		frm.pwd.focus();
		return false;
	}
	return true;
}
function validateSearch(frm){
	if( frm.w.value == '' || frm.w.value == 'Ricerca annuncio' ){
		alert('Inserire almeno una parola per effettuare una ricerca!');
		frm.w.focus();
		return false;
	}
	if( frm.w.value.length < 3 ){
		alert('Il termine di ricerca deve contenere almeno 3 caratteri!');
		frm.w.focus();
		return false;
	}
	return true;
}
function validateAgenzia(frm){
	if( frm.ragione_sociale.value == '' ){
		alert('Nome è un campo obbligatorio!');
		frm.ragione_sociale.focus();
		return false;
	}
	if( getId('ar_1') && !returnSelection(frm.categoria) ){
		alert('Settore è un campo obbligatorio!');
		getId('ar_1').focus();
		return false;
	}
	if( frm.username && frm.username.value == '' ){
		alert('Nome utente è un campo obbligatorio!');
		frm.username.focus();
		return false;
	}
	var username_reg_exp = /^(\w{1,32})+$/;
	if( frm.username && frm.username.value != '' && !username_reg_exp.test(frm.username.value) ){
		alert('Formato nome utente non valido, sono ammessi solo caratteri alfanumerici!');
		frm.username.focus();
		return false;
	}
	if( frm.password && frm.password.value == '' ){
		alert('Password è un campo obbligatorio!');
		frm.password.focus();
		return false;
	}
	var password_reg_exp = /^(\w{1,32})+$/;
	if( frm.password && frm.password.value != '' && !password_reg_exp.test(frm.password.value) ){
		alert('Formato password non valido, sono ammessi solo caratteri alfanumerici!');
		frm.password.focus();
		return false;
	}
	if( frm.max_annunci && frm.max_annunci.value == '' ){
		alert('Numero annunci per agenzia è un campo obbligatorio!');
		frm.max_annunci.focus();
		return false;
	}
	var num_reg_exp = /^(\d)+$/;
	if( frm.max_annunci && frm.max_annunci.value != '' && !num_reg_exp.test(frm.max_annunci.value) ){
		alert('Formato numero annunci non valido, è un campo numerico!');
		frm.max_annunci.focus();
		return false;
	}
	if( frm.max_immagini && frm.max_immagini.value == '' ){
		alert('Numero immagini per annuncio è un campo obbligatorio!');
		frm.max_immagini.focus();
		return false;
	}
	if( frm.max_immagini && frm.max_immagini.value != '' && !num_reg_exp.test(frm.max_immagini.value) ){
		alert('Formato numero immagini non valido, è un campo numerico!');
		frm.max_immagini.focus();
		return false;
	}
	if( frm.email.value == '' ){
		alert('Indirizzo e-mail è un campo obbligatorio!');
		frm.email.focus();
		return false;
	}
	var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
	if (!email_reg_exp.test(frm.email.value)){
		 alert("Formato indirizzo e-mail non valido!");
		 frm.email.focus();
		 return false;
	}
	return true;
}
function validatePassword(frm){
	if( frm.old && frm.old.value == '' ){
		alert('Vecchia password è un campo obbligatorio!');
		frm.old.focus();
		return false;
	}
	if( frm.pwd.value == '' ){
		alert('Nuova password è un campo obbligatorio!');
		frm.pwd.focus();
		return false;
	}
	var reg_exp = /^(\w{1,32})+$/;
	if ( frm.pwd.value != '' && !reg_exp.test(frm.pwd.value)){
		alert('Formato password non valido!');
		 frm.pwd.focus();
		 return false;
	}
	if( frm.pwd.value != '' && frm.re_pwd.value == '' ){
		alert('Ripeti password è un campo obbligatorio!');
		frm.re_pwd.focus();
		return false;
	}
	if( frm.re_pwd.value != '' && frm.pwd.value != frm.re_pwd.value ){
		alert("La password ripetuta non è corretta!");
		frm.re_pwd.focus();
		return false;
	}
	return true;
}
function validateContatto(frm){
	if( frm.email.value == '' ){
		alert('Indirizzo e-mail è un campo obbligatorio!');
		frm.email.focus();
		return false;
	}
	var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
	if (!email_reg_exp.test(frm.email.value)){
		 alert("Formato indirizzo e-mail non valido!");
		 frm.email.focus();
		 return false;
	}
	if( frm.corpo.value == '' ){
		alert('Messaggio è un campo obbligatorio!');
		frm.corpo.focus();
		return false;
	}
	return true;
}
function validateMailAgenzia(frm){
	if( frm.nome.value == '' ){
		alert('Nome è un campo obbligatorio!');
		frm.nome.focus();
		return false;
	}
	if( frm.email.value == '' ){
		alert('Indirizzo e-mail è un campo obbligatorio!');
		frm.email.focus();
		return false;
	}
	var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
	if (!email_reg_exp.test(frm.email.value)){
		 alert("Formato indirizzo e-mail non valido!");
		 frm.email.focus();
		 return false;
	}
	if( frm.corpo.value == '' ){
		alert('Messaggio è un campo obbligatorio!');
		frm.corpo.focus();
		return false;
	}
	if( !returnSelection(frm.privacy) || returnSelection(frm.privacy) == 0 ){
		alert('E\' necessario accettare le condizioni del servizio!');
		getId('valore_1').focus();
		return false;
	}
	return true;
}
function validatePrivati(frm){
	if( frm.rubrica.value == '' ){
		alert('Rubrica è un campo obbligatorio!');
		frm.rubrica.focus();
		return false;
	}
	if( frm.testo.value == '' ){
		alert('Testo annuncio è un campo obbligatorio!');
		frm.testo.focus();
		return false;
	}
	if( frm.recapiti.value == '' ){
		alert('Recapiti telefonici è un campo obbligatorio!');
		frm.recapiti.focus();
		return false;
	}
	if( frm.nome.value == '' ){
		alert('Nome e Cognome è un campo obbligatorio!');
		frm.nome.focus();
		return false;
	}
	if( frm.telefono.value == '' ){
		alert('Telefono è un campo obbligatorio!');
		frm.telefono.focus();
		return false;
	}
	var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
	if( frm.email.value!='' && !email_reg_exp.test(frm.email.value)){
		 alert("Formato indirizzo e-mail non valido!");
		 frm.email.focus();
		 return false;
	}
	if( !returnSelection(frm.privacy) || returnSelection(frm.privacy) == 0 ){
		alert('E\' necessario accettare le condizioni del servizio!');
		getId('valore_1').focus();
		return false;
	}
	return true;
}

function openWindow(mypage,myname,w,h,resizable,scrollbars){ 
	w = (Number(w) + 1);
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0; 
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0; 
	settings = 'scrollbars='+scrollbars+',resizable='+resizable+',status=no,height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition;
	win = window.open(mypage,myname,settings);
}

function counterText(limit, field, label){   
	var text = limit-getId(field).value.length;   
	if (text<0){
		getId(field).innerHTML=getId(field).value.substring(0,limit-1);
		getId(label).innerHTML=limit-getId(field).value.length;
	} else {
		getId(label).innerHTML=text;
	}   
}

function goto(url){
	location.href=url;
}

function redirect(redirect){
	setTimeout("location.href='"+redirect+"'", 2000);
}

function check_ip(ip){
	var reg_exp = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	var ip_split = ip.split(".");

	if( !reg_exp.test(ip) )
		 return false;
	else if( ip_split[0] > 255 || ip_split[1] > 255 || ip_split[2] > 255 || ip_split[3] > 255 )
		 return false;
	else
		 return true;
}

function addOption(Sel, theText, theValue){
	if( theText != '' ){
		var theSel = document.getElementById(Sel+'');
		var check = true;
		for( var i=theSel.length-1; i>=0; i-- ){
			 if( theSel.options[i].text == theText ){
				 alert("Elemento già presente nella lista!");
				 check = false;
				 return false;
			 }
		}
		if( check ){
			var newOpt = new Option(theText, theValue);
			theSel.options[theSel.length] = newOpt;
			
			return true;
		}
	}
}

function deleteOption(Sel, theIndex){
	var theSel = document.getElementById(Sel+'');
	if( theSel.length>0 )
		theSel.options[theIndex] = null;
}

function deleteOptions(From){
	var theSelFrom = document.getElementById(From+'');
	for( var i=theSelFrom.length-1; i>=0; i-- ){
		 if( theSelFrom.options[i].selected )
			 deleteOption(From, i);
	}
}

function selectAll(Sel){
	var theSel = document.getElementById(Sel+'');
	if( !theSel.options.length && document.getElementById('st1').checked == false ){
		document.getElementById('dv1').checked = 1;
		document.getElementById('sd1').checked = 1;
	}
	
	for( i=0; i<theSel.options.length; i++ ){
		 theSel.options[i].selected = 1;
	}
	return true;
}

function moveUpDown(From, to){
	var list = document.getElementById(From+'');
	var index = list.selectedIndex;
	var total = list.options.length-1;
	
	if(index == -1) return false;
	if(to == +1 && index == total) return false;
	if(to == -1 && index == 0) return false;
	
	var items = new Array;
	var values = new Array;
	
	for( i = total; i >= 0; i-- ){
		 items[i] = list.options[i].text;
		 values[i] = list.options[i].value;
	}
	for( i = total; i >= 0; i-- ){
		 if( index == i ){
			 list.options[i + to] = new Option(items[i],values[i + to], 0, 1);
			 list.options[i] = new Option(items[i + to], values[i]);
			 i--;
		 }else{
			list.options[i] = new Option(items[i], values[i]);
		 }
	}
}

function setLista(Sel, From){
	var theSel = document.getElementById(Sel+'');
	var theSelFrom = document.getElementById(From+'');

	var stroptions = "";
	for( i = 0; i <= theSelFrom.options.length-1; i++ ){
		 if (i!=0) stroptions += "#$#";
		 stroptions += theSelFrom.options[i].text;
	}
	theSel.value = stroptions;
}

function returnSelection(radioButton){
	var selection=null;
	for( var i=0; i<radioButton.length; i++ ){
		 if( radioButton[i].checked ){
		     selection=radioButton[i].value;
		     return selection;
		 }
	}
	return selection; 
} 

function getId(id){
	var element = document.getElementById(id+"");	
	return element;
}

// simplePreload( '01.gif', '02.gif' );
function simplePreload(){
  	 var args = simplePreload.arguments;
  	 document.imageArray = new Array(args.length);
  	 for(var i=0; i<args.length; i++)
  	 {
    	 document.imageArray[i] = new Image;
    	 document.imageArray[i].src = args[i];
  	 }
}