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 iscrizione( tipo ) {
	win = window.open( "./iscrizione.html?tipo=" + tipo, "Iscrizione", "width=600,height=550,top=50,left=100,scrollbars=1" );
	win.focus();
}
function giovani() { 
	win = window.open( "giovani.html", "Giovani", "width=900,height=550,top=50,left=100,scrollbars=1" );
	win.focus();
}
function video() { 
	win = window.open( "video.html", "video", "width=430,height=330,top=50,left=100,scrollbars=1" );
	win.focus();
}
function Transfer( sino ) {
	obj = document.getElementById("registrationForm")["atleti[Transfer]"];
	sino.form["atleti[NumeroPersone]"].disabled = ( radioValue( obj ) == "N" );
	sino.form["atleti[DataArrivo]"].disabled = ( radioValue( obj ) == "N" );
	sino.form["atleti[DataPartenza]"].disabled = ( radioValue( obj ) == "N" );
	sino.form["atleti[OraArrivo]"].disabled = ( radioValue( obj ) == "N" );
	sino.form["atleti[OraPartenza]"].disabled = ( radioValue( obj ) == "N" );
	sino.form["atleti[NumVoloArrivo]"].disabled = ( radioValue( obj ) == "N" );
	sino.form["atleti[NumVoloPartenza]"].disabled = ( radioValue( obj ) == "N" );
	sino.form["atleti[AereoportoPartenza]"].disabled = ( radioValue( obj ) == "N" );
	sino.form["atleti[AereoportoArrivo]"].disabled = ( radioValue( obj ) == "N" );
}
function radioValue( radioObj ) {
	for ( var i = 0; i < radioObj.length; i++ ) {
	if ( radioObj[i].checked ) {
			return radioObj[i].value;
		}
	}
	return false;
}
function convalidaIscrizione( form ) {
	errors = "";
	for ( var i = 0; i < requiredFields.length; i++ ) {
		switch ( requiredFields[i] ) {
			case "DataNascita":
				if ( ! checkDate( form["atleti[" + requiredFields[i] + "]"].value ) ) {
					errors += "Compilare correttamente il campo '" + labels[i] + "'\n";
				}
			break;
			case "Sesso":
				if ( radioValue( form["atleti[" + requiredFields[i] + "]"] ) === false ) {
					errors += "Il campo '" + labels[i] + "' è obbligatorio\n";
				}
			break;
			case "Email":
				if ( form["atleti[Email]"].value == "" || ! validateEmail( form["atleti[Email]"].value ) ) {
					errors += "Il campo 'Email' non è stato compilato correttamente\n";
				}
			break;
			case "IdentificatoreCategoria":
				if ( ! form["atleti[" + requiredFields[i] + "]"].selectedIndex ) {
					errors += "Il campo '" + labels[i] + "' è obbligatorio\n";
				}
			break;
			default:
				if ( form["atleti[" + requiredFields[i] + "]"].value == "" ) {
					errors += "Il campo '" + labels[i] + "' è obbligatorio\n";
				}
			break;
		}
	}
	if ( ! form.privacy.checked ) {
		errors += "Senza il consenso al trattamento dei dati non è possibile iscriversi";
	}
	if ( errors == "" ) {
		return true;
	} else {
		alert( errors );
		return false;
	}
}
function checkDate( date ) {
	if ( date.length == 0 ) {
		return false;
	}
	pieces = date.split( "/" );
	return ( pieces.length == 3 ) && ( d = new Date( pieces[2], pieces[1] - 1, pieces[0] ) ) && ( d.getDate() == pieces[0] ) && ( d.getMonth() == pieces[1] - 1 ) && ( d.getFullYear() == pieces[2] );
}
function validateEmail( email ) {
	var emailReg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
	var emailReg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,6}|[0-9]{1,3})(\]?)$/; // valid
	if ( ! ( ! emailReg1.test( email ) && emailReg2.test( email ) ) ) {
		return false;
	} else {
		return true;
	}
} 
function spedisci( email ) {
	location = "mailto:" + email.replace( "[presso]", "@" );
}