function externalLinks() { 
	if ( ! document.getElementsByTagName ) {
		return;
	}
	var anchors = document.getElementsByTagName( "a" );
	for ( var i = 0; i < anchors.length; i++ ) {
		var anchor = anchors[i];
		if ( anchor.getAttribute( "href" ) && anchor.getAttribute( "rel" ) == "external" ) {
			anchor.target = "_blank";
		}
	} 
}
window.onload = externalLinks;
function registrati( pageName ) { 
	win = window.open( pageName, "Account", "width=500,height=600,top=50,left=100,scrollbars=1");
	win.focus();
}
/*
function richiesta() { 
	win = window.open( "richiesta.html" , "Richiesta", "width=500,height=500,top=50,left=100,scrollbars=1");
	win.focus();
}
function registratiSubmit() {
	opener.name = "opener";
	document.forms[0].target = opener.name;
	self.close();
}
*/
function validateEmail( email ) {
	var emailReg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
	var emailReg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,6}|[0-9]{1,3})(\]?)$/;
	return ! emailReg1.test( email ) && emailReg2.test( email );
}
function checkAccount( form, type ) {
	errors = "";
	if ( form.shortDescription.value == "" ) {
		errors += "Inserire il nome dell'agenzia\n";
	}
	if ( form.username.value == "" ) {
		errors += "Inserire uno username valido\n";
	}
	if ( form.email.value == "" ) {
		errors += "Inserire l'indirizzo e-mail\n";
	} else	if ( ! validateEmail( form.email.value ) ) {
		errors += "L'inidirizzo email inserito non č corretto\n";
	}
	if ( type == "agente" ) {
		if ( form.referer.value == "" ) {
			errors += "Inserire il nome del legale rappresentante\n";
		}
	} else if ( type == "azienda" ) {
		if ( form.extra1.value == "" ) {
			errors += "Inserirs la partita IVA\n";
		}
		if ( form.address.value == "" ) {
			errors += "Inserirs l'indirizzo\n";
		}
		if ( form.town.value == "" ) {
			errors += "Inserirs la cittā\n";
		}
		if (	form.phone.value == ""
				&& form.fax.value == ""
				&& form.mobile.value == "" ) {
			errors += "Inserire un recapito telefonico o fax\n";
		}
	}
	if ( errors.length > 0 ) {
		alert( errors );
		return false;
	}
}
function selectValue( oggettoSelect ) {
	return oggettoSelect.options[oggettoSelect.selectedIndex].value;
}
function radioValue( oggettoRadio ) {
	for ( var i = 0; i < oggettoRadio.length; i++ ) {
		if ( oggettoRadio[i].checked ) {
			return oggettoRadio[i].value;
		}
	}
}
function convalidaDatiPersonali( form ) {
	errors = '';
	requiredFields = {	"shortDescription" : "RagioneSociale",
						"referer" : "Cognome e nome legale rappresentante",
						"address" : "Indirizzo",
						"zip" : "Cap",
						"town" : "Cittā",
						"extra1" : "Partita IVA",
						"extra2" : "Codice Fiscale",
						"strutturaOperativa" : "Tipo Struttura Operativa",
						"dataInizioAttivita" : "Data Inizio Attivita",
						"extra3" : "Settore Merceologico",
						"clientsTypology" : "Tipologia clientela" };
	for ( i in requiredFields ) {
		switch ( i ) {
			case "strutturaOperativa":
				test = false;
				for ( j = 0; j < form.elements.length; j++ ) {
					if ( form[j].name.indexOf( "strutturaOperativa" ) != - 1 && form[j].checked ) {
						if ( form[form[j].name.replace( "checkbox", "shortDescription" )] == undefined || form[form[j].name.replace( "checkbox", "shortDescription" )].value != "" ) {
							test = true;
						} else {
							test = false;
							errors += "Il tipo di struttura operativa selezionata prevede la descrizione della metratura\n";
							break;
						}
					}
				}
				if ( ! test ) {
					errors += "Selezionare almeno un tipo di struttura operativa\n";
				}
			break;
			case "clientsTypology":
				for ( j = 0; j < form.elements.length; j++ ) {
					clientsTypology = false;
					if ( form[j].name.indexOf( "clientsTypology" ) != - 1 && form[j].checked ) {
						clientsTypology = true;
						break;
					}
				}
				if ( ! clientsTypology ) {
					errors += "Selezionare almeno una tipologia di clientela\n";
				}
			break;
			default:
				if ( form[i].value == "" ) {
					errors += "Il campo " + requiredFields[i] + " č obbligatorio\n";
				}
			break;
		}
	}
	if ( errors != "" ) {
		alert( errors );
		return false;
	} else {
		return true;
	}
}
function enableSubmit( value ) {
	form = document.getElementById( "createAccountForm" );
	form.submitButton.disabled = ! value;
}
function submitAgencyForm() {
	if ( confirm( "Salvare le modifiche apportate ai dati dell'agenzia?" ) ) {
		form = document.getElementById( "agency" );
		if ( convalidaDatiPersonali( form ) ) {
			form.action = "dati-personali.html?reloadPage=marche-rappresentate.html";
			form.submit();
		}
	} else {
		location = "marche-rappresentate.html";
	}
}
function richiedi() {
	location = "mailto:walter.piacentini@ascom.ud.it";
}
function gallery( entityID ) {
	var win = window.open( "galleria-det.html?entityID=" + entityID, "GalleriaFotografica", "width=760,height560" );
	win.focus();
}
function spedisci( email ) {
	location = "mailto:" + email.replace( "[presso]", "@" );
}
function selectUserBrand( userBrandID, mode ) {
	win = window.open( "", "UserBrand", "width=500,height=500,top=50,left=100,scrollbars=1");
	win.focus();
	document.forms[0].target = "UserBrand";
	document.forms[0].userBrandID.value = userBrandID;
	document.forms[0].mode.value = mode;
	document.forms[0].submit();
}
function selectUserAnnouncement( userAnnouncementID, mode ) {
	win = window.open( "", "UserAnnouncement", "width=500,height=500,top=50,left=100,scrollbars=1");
	win.focus();
	document.forms[0].target = "UserAnnouncement";
	document.forms[0].userAnnouncementID.value = userAnnouncementID;
	document.forms[0].mode.value = mode;
	document.forms[0].submit();
}
function selezionaAgente( oggettoSelect ) {
	location = "index.html?identificatoreAgente=" + selectValue( oggettoSelect );
}
