/*
*
*	Funzioni generiche per il funzionamento del sito
*
*/	
	
	function checkForm (form){
		if (form.titolo.value.length == 0) {
		alert("inserire un parametro di ricerca!");
		return false;
		}
	}
	
	
	function checkFormRegione(form){
	if (form.regione.selectedIndex== 0) {
		alert("Selezionare una regione.")
	return false}
}

function checkFormProvincia(form){
	if (form.prov.selectedIndex== 0) {
		alert("Selezionare una provincia.")
	return false}
}


/** valida la form di registrazione */

function Check(){
	var frase=""
	var categ=""
	if (document.form1.nom.value=="")
		frase+="- Il campo nome e' obbligatorio \n"
	if (document.form1.ind.value=="")
		frase+="- Il campo indirizzo e' obbligatorio \n"
	if (document.form1.cap.value=="")
		frase+="- Il campo cap e' obbligatorio \n"
	if (document.form1.cit.value=="")
		frase+="- Il campo citt? e' obbligatorio \n"
	if (document.form1.pro.selectedIndex==0)
		frase+="- Il campo provincia e' obbligatorio \n"
	if (document.form1.reg.selectedIndex==0)
		frase+="- Il campo regione e' obbligatorio \n"		
	if (document.form1.tel.value=="")
		frase+="- Il campo telefono e' obbligatorio \n"
	if (document.form1.pdr.value=="")
		frase+="- Il campo persona di riferimento e' obbligatorio \n"
	
	if (document.form1.mail.value!="" && document.form1.mail.value.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1 )
		frase+="- Il campo e-mail non e' valido \n"
	
	ok=0
	with(document.form1){
		for(x=0;x<tap.value;x++){
			if (cat[x].checked==true) {
			categ=cat[x].value
			ok=1;
			}
		}
	}
	
	
	
	if (ok!=1){
		frase+="- Selezionare almeno una categoria primaria\n"
	}
	
	
	ok=1
	if ((categ=="FOR")||(categ=="STA")) {
		ok=0
		with(document.form1){
			for(x=1;x<=tip.value;x++){
				if (eval("sca"+x+".checked")==true)  {
				ok=1;
				}
			}
		}
	}
	if (ok!=1){
		frase+="- Selezionare i settori della vostra attivit?\n"
	}
	
	if (!document.form1.radiobutton[0].checked)
			frase+="- E necessario fornire il proprio consenso al trattamento dei dati \n"	
	
	
	if (frase!=""){ 
		alert(frase)
		return false
		}
	else
		document.form1.submit()	
		return false;
	
}


function Blocca1(){
	document.forms[0].SignUp1.disabled=true;
	document.forms[0].mailaction.value = 0;
	document.forms[0].submit();
}
function Blocca2(){
	document.forms[0].SignUp2.disabled=true;
	document.forms[0].mailaction.value = 2;
	document.forms[0].submit();
}

function checkLenght(field, maxlenght){
	testo = field;
	if (testo.value.length >= maxlenght){
		field.value=testo.value.substr(0,maxlenght);
		return false;
      	}
	return true;
}

// controlla la form di inserimento offerta/ricerca lavoro
function CheckFormLavoro(){
	var frase=""
	if (document.form.nome.value=="")
		frase+="- Il contatto nome e' obbligatorio \n"
	if (document.form.tel.value=="")
		frase+="- Il campo telefono e' obbligatorio \n"
		
	if (!document.form.radiobutton[0].checked)
			frase+="- E necessario fornire il proprio consenso al trattamento dei dati \n"	
	
	if (frase!=""){ 
		alert(frase)
		return false
		}
	else
		document.form.submit()	
}	

function checkFormPubblicita(form){
if (form.azienda.value.length == 0) {
	alert("Inserire il nome dell\'azienda.")
return false}

if (form.indirizzo.value.length == 0) {
	alert("Inserire l\'indirizzo.")
return false}

if (form.cap.value.length == 0) {
	alert("Inserire il cap.")
return false}

if (form.citta.value.length == 0) {
	alert("Inserire la citta`.")
return false}

if (form.provincia.value.length == 0) {
	alert("Inserire la provincia.")
return false}

if (form.telefono.value.length == 0) {
	alert("Inserire il telefono.")
return false}

if (form.mail.value.length == 0) {
	alert("Inserire la e-mail.")
return false}

if (form.p_riferime.value.length == 0) {
	alert("Inserire il nome della persona di riferimento.")
return false}

if (!form.radiobutton[0].checked){
	alert("E necessario fornire il proprio consenso al trattamento dei dati \n");	
	return false		
}

document.forms[0].invia.disabled=true;
return true;
}

function checkFormTestoA (form){
	if (form.nomeaz.value == "") {
		alert("Inserisci l'azienda da ricercare!")
		return false	
	}	
	if (form.nomeaz.value.length < 2) {
		alert("Inserisci almeno 2 lettere!")
		return false	
	}	
}	



function CheckFormAffari(){
	var frase=""
	if (document.form.nome.value=="")
		frase+="- Il campo nome e' obbligatorio \n"
	if (document.form.tel.value=="")
		frase+="- Il campo telefono e' obbligatorio \n"
	if (!document.form.radiobutton[0].checked)
			frase+="- E necessario fornire il proprio consenso al trattamento dei dati \n"	
		
	if (frase!=""){ 
		alert(frase)
		return false
		}
	else
		document.form.submit()	
}	


function checkFormVendita(form){
	var frase=""
	with(form){
		
		if (nome.value=="")
		frase+="- Il campo nome e' obbligatorio \n"
		if (tel.value=="")
			frase+="- Il campo telefono e' obbligatorio \n"
		if (textfield2.value=="")
			frase+="- Inserire il testo dell'annuncio \n"
		if (!radiobutton[0].checked)
			frase+="- E necessario fornire il proprio consenso al trattamento dei dati \n"	
		
		if (frase!=""){ 
			alert(frase);
			return false
		}
		
		if (immagine.value!="") {
			//controllo estensione valida
			if(immagine.value.substring(immagine.value.length-4).toLowerCase()!=".gif"
			&& immagine.value.substring(immagine.value.length-4).toLowerCase()!=".jpg"){
				alert("ATTENZIONE! Il file immagine deve avere estensione .jpg o .gif");
				return false;
			}
		}
		
		if ((immagine.value!="") || (step_modifica.value=="1")){
			action = "conferma_annuncio.asp";
		}else{
			action = "ok.asp";
			enctype="application/x-www-form-urlencoded" 
		}
		
		
	}
	return true;
}



function changeImage(imgObj){
	var altezza = 150;
	var larghezza = 235;
	
	//alert ("larghezza"  + imgObj.width + "\naltezza"  + imgObj.height);
	if (imgObj.width > larghezza || imgObj.height > altezza){
		
		if (imgObj.width > imgObj.height){
			imgObj.width = larghezza;
			//imgObj.height = (larghezza*imgObj.height)/imgObj.width;
		}else{
			imgObj.height = altezza;
			//imgObj.width = (altezza * imgObj.width)/ imgObj.height
		}
	
	}
	
	//alert ("larghezza"  + imgObj.width + "\naltezza"  + imgObj.height);
}


function switchAction(todo){
	
	if (todo == 'modifica'){
		document.annuncio.action="vendo2.asp";				
		document.annuncio.submit();
	}else{	
		document.annuncio.action="delete_image.asp";			
		document.annuncio.submit();
	
	}	
}

/*
*  questa funzione blocca/sblocca le checkbox dei sottotipi
* 
* 
*/
function lookCheckSottotipo(tipoName){

	if (tipoName == 'FOR' || tipoName == 'STA'){
	   	document.getElementById('sca1').disabled = false;
	   	document.getElementById('sca2').disabled = false;
		document.getElementById('sca3').disabled = false;
	}else{
	   	document.getElementById('sca1').disabled = true;
		document.getElementById('sca2').disabled = true;
		document.getElementById('sca3').disabled = true;
	}

}


function checkFormAnnunci(){


	var frase=""
	/*fax contiene il nome dell'azienda*/
	if (document.form.fax.value=="")
		frase+="- Il campo azienda e' obbligatorio \n"
	if (document.form.nome.value=="")
		frase+="- Il campo Contatto e' obbligatorio \n"
	if (document.form.citta.value=="")
		frase+="- Il campo Regione e' obbligatorio \n"
	if (document.form.numtel.value=="")
		frase+="- Il campo Telefono e' obbligatorio \n"
	if (document.form.mail.value=="")
		frase+="- Il campo mail e' obbligatorio \n"

	if(document.form.mail.value !="" && !isEmail(document.form.mail.value))
	{
		frase+="- L'indirizzo E-mail inserito non e' corretto\n";
	}		
	

	if (document.form.textfield2.value=="")
		frase+="- Inserire il testo dell'annuncio \n"

	if (!document.form.radiobutton[0].checked)
			frase+="- E necessario fornire il proprio consenso al trattamento dei dati \n"	
	
	if (frase!=""){ 
		alert(frase)
		return false
		}
	else
		document.form.submit()	
}



function isEmail (emailStr) {
	// Formato user@domain e separazione di reg_username e dominio
	var emailPat=/^(.+)@(.+)$/
	// Pattern per ritrovare i caratteri speciali (non consentiti)
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	// Caratteri consentiti in reg_username o domainname
	var validChars="\[^\\s" + specialChars + "\]"
	// reg_username contenente spazi
	var quotedUser="(\"[^\"]*\")"
	// domainName costituito da un indirizzo IP
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	// Unità 'atomo' ovvero una serie di caratteri non speciali
	var atom=validChars + '+'
	// Una 'word' dell'reg_username. L'reg_username può essere costituito da più 'word'
	// separate da .
	var word="(" + atom + "|" + quotedUser + ")"
	// Struttura dell'reg_username
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	// Dominio sombolico
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

	// Controllo della sintassi reg_username@domain e separazione dell'reg_username dal
	// domain
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		//alert("Email address seems incorrect (check @ and .'s)")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]

	// User valido.
	if (user.match(userPat)==null) {
	    //alert("The reg_username doesn't seem to be valid.")
		 return false
	}

	// Indirizzo IP valido (nel caso in cui il domain sia un IP.
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		  for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
		        //alert("Destination IP address is invalid!")
			return false
		    }
	    }
	    return true
	}

	// Domain è un nome simbolico
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		//alert("The domain name doesn't seem to be valid.")
	    return false
	}


	// controllo sulla parte terminale del domain.

	// Spezzo il domain in 'atomi'
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if ((domArr[domArr.length-1].length<2) ||
	    (domArr[domArr.length-1].length>3)) {
	   // alert("The address must end in a three-letter domain, or two letter country.")
	   return false
	}

	// Parte terminale del dominio preceduta da un host name.
	if (len<2) {
	   //var errStr="This address is missing a hostname!"
	   //alert(errStr)
	   return false
	}

	return true;
}


