var xmlHttp = false;
var isBusy = false;
var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

if (checkIt('konqueror'))
{
	browser = "Konqueror";
	OS = "Linux";
}
else if (checkIt('safari')) browser = "Safari"
else if (checkIt('omniweb')) browser = "OmniWeb"
else if (checkIt('opera')) browser = "Opera"
else if (checkIt('webtv')) browser = "WebTV";
else if (checkIt('icab')) browser = "iCab"
else if (checkIt('msie')) browser = "Internet Explorer"
else if (!checkIt('compatible'))
{
	browser = "Netscape Navigator"
	version = detect.charAt(8);
}
else browser = "An unknown browser";

function checkIt(string){
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

/*
ho definito l'oggetto xmlHttp in cima al file
*/
if (browser=="Internet Explorer"){
	try {
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e2) {
			xmlHttp = false;
		}
	}
}else{
	if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
		xmlHttp = new XMLHttpRequest();
	}
}

function carica_cattolica(){
	var area = document.getElementById('id_area').options[document.getElementById('id_area').selectedIndex].value;
	
	if( (area==2) || (area==8) || (area==14) || (area==15) ){
		document.getElementById('ext_catt').style.display = 'block';
	}

	if(area==4){
		document.getElementById('ext_lui').style.display = 'block';
	}
}


function carica_comuni(caricamento, id_provincia, id_comune){
	
	reset_select(document.getElementById('id_comune'));
	if ( caricamento == 1 ){
		assign_value('cap', '');
		assign_value('telefono', '');
	}
	
	if ( id_provincia == null || id_provincia == "" || id_provincia == 0 || id_provincia.length == 0 ) return;
	
	var url = "/include/getData.php?opt=carica_comuni&id_provincia="+id_provincia+"&id_comune="+id_comune;

	if (isBusy){
		xmlHttp.onreadystatechange = null; // no go in IE
		xmlHttp.abort();
	}
	
	// Open a connection to the server
	xmlHttp.open("GET", url, true);
	
	isBusy = true;

	// Setup a function for the server to run when it's done
	xmlHttp.onreadystatechange = result_carica_comuni;

	// Send the request
	xmlHttp.send(null);

}

function result_carica_comuni(){
	if (xmlHttp.readyState != 4){
		return;
	}else if (xmlHttp.readyState == 4){
		isBusy = false;
		
		var response = xmlHttp.responseText;
		eval(response);
		return;
	}
}

function carica_cap_telefono(valore){
	
	assign_value('cap', '');
	assign_value('telefono', '');
	
	if ( valore == null || valore == "" || valore == 0 || valore.length == 0 ) return;
	
	var url = "/include/getData.php?opt=carica_cap_telefono&valore="+valore;

	if (isBusy){
		xmlHttp.onreadystatechange = null; // no go in IE
		xmlHttp.abort();
	}
	
	// Open a connection to the server
	xmlHttp.open("GET", url, true);
	
	isBusy = true;

	// Setup a function for the server to run when it's done
	xmlHttp.onreadystatechange = result_carica_cap_telefono;

	// Send the request
	xmlHttp.send(null);

}

function result_carica_cap_telefono(){
	if (xmlHttp.readyState != 4){
		return;
	}else if (xmlHttp.readyState == 4){
		isBusy = false;
		
		var response = xmlHttp.responseText;
		var risultati = response.split('::');

		var obj1 = 'cap';
		var obj2 = 'telefono';
		
		assign_value(obj1, risultati[0]);
		assign_value(obj2, risultati[1]);
		return;
	}
}

function apri_div(tipo, valore){
	
	if ( tipo == null || tipo == "" || tipo.length == 0 ) return;
	if ( valore == null || valore == "" || valore == 0 ) return;

	var url = "/include/getData.php?opt=apri_div&tipo="+tipo+"&valore="+valore;

	if (isBusy){
		xmlHttp.onreadystatechange = null; // no go in IE
		xmlHttp.abort();
	}
	
	// Open a connection to the server
	xmlHttp.open("GET", url, true);
	
	isBusy = true;

	// Setup a function for the server to run when it's done
	xmlHttp.onreadystatechange = result_apri_div;

	// Send the request
	xmlHttp.send(null);

}

function result_apri_div(){
	if (xmlHttp.readyState != 4){
		return;
	}else if (xmlHttp.readyState == 4){
		isBusy = false;
		
		var response = xmlHttp.responseText;
		var obj = document.getElementById('pop_news');
		obj.style.top = '310px';
		obj.style.left = '430px';
		obj.innerHTML = response;
		obj.style.display = '';
		return;
	}
}

function carica_aree(id_corso, id_area, id_sede){
	
	if ( id_corso == null || id_corso == "" || id_corso == 0 ) return;

	var url = "/include/getData.php?opt=carica_aree&id_corso="+id_corso+"&id_area="+id_area+"&id_sede="+id_sede;

	if (isBusy){
		xmlHttp.onreadystatechange = null; // no go in IE
		xmlHttp.abort();
	}
	
	// Open a connection to the server
	xmlHttp.open("GET", url, true);
	
	isBusy = true;

	// Setup a function for the server to run when it's done
	xmlHttp.onreadystatechange = result_carica_aree;

	// Send the request
	xmlHttp.send(null);

}

function result_carica_aree(){
	if (xmlHttp.readyState != 4){
		return;
	}else if (xmlHttp.readyState == 4){
		isBusy = false;
		
		var response = xmlHttp.responseText;
		eval(response);
		return;
	}
	
}

/*****************
 * FUNZIONI DATE *
 ****************/
function CheckDiffData() {
	
	var d = document.getElementById('giorno').options[document.getElementById('giorno').selectedIndex].text;
	var m = document.getElementById('mese').options[document.getElementById('mese').selectedIndex].text;
	var y = document.getElementById('anno').options[document.getElementById('anno').selectedIndex].text;
	var errore = "Data non valida!";

	if( parseInt( d ) == 31 ) {
		if( m=="04" || m=="06" || m=="09" || m=="11") {
			alert(errore);
			document.getElementById('giorno').options[30].selected = true;
		}
	}

	if(m=="02") {
		if( Isbisestile(y) ) {
			if( parseInt( d ) > 29 ) {
				alert( errore);
				document.getElementById('giorno').options[29].selected = true;
			}
		} else {
			if( parseInt( d )  > 28) {
				alert(errore);
				document.getElementById('giorno').options[28].selected = true;

			}
		}
	}
}

function Isbisestile(anno){
	var isBisestile = false;
	anno = parseInt( anno, 10 );
	// every fourth year is a leap year

	if (anno%4 == 0){
		isBisestile=true;

		if (anno%100 == 0){
			isBisestile=false;

			if (anno%400 == 0){
				isBisestile=true;
			}
		}
	 }
	return isBisestile;
}

/******************
 * FUNZIONI VARIE *
 *****************/
function check_form_scelta(theForm){
	
	var controllo_tipo = false;
	var controllo_metodo = false;
	
	var radio_tipo = theForm.tipo;
	var radio_metodo = theForm.metodo;
	
	var testo = "Confermi le scelte effettuate?";

	for(var i=0; i < radio_tipo.length; i++) {
		if( radio_tipo[i].checked ){
			controllo_tipo = true;
			break;
		}
	}

	for(var i=0; i < radio_metodo.length; i++) {
		if( radio_metodo[i].checked ){
			controllo_metodo = true;
			break;
		}
	}
	
	if ( !controllo_tipo ){
		alert("E' obbligatorio selezionare una soluzione di pagamento.");
		return false;
	}

	if ( !controllo_metodo ){
		alert("E' obbligatorio selezionare un metodo di pagamento.");
		return false;
	}
	
	if ( theForm.condizioni[0].checked == false ){
		alert("Per effettuare un pagamento e' obbligatorio\naccettare le condizioni contrattuali.");
		return false;
	}

	if ( confirm(testo) ){
		return true;
	}else{
		return false;
	}

}

//assegna ad un campo(id) un valore
function assign_value(id, valore){
	var obj = document.getElementById(id);
	if ( obj != null ){
		obj.value = valore;
	}
}

//recupera un valore da un campo(id)
function get_value(id){
	return document.getElementById(id).value;
}

//recupera un valore da un campo(id)
function get_html(id){
	return document.getElementById(id).innerHTML;
}

//imposta il focus su un campo(id)
function get_focus(id){
	document.getElementById(id).focus();
}

function seleziona_select(oggetto){
	if ( oggetto != null ){
		oggetto.options[0] = new Option('Seleziona...',0);
		oggetto.options[0].selected = true;
	}
}

function seleziona_prima_option(oggetto){
	if ( oggetto != null ){
		oggetto.options[0].selected = true;
	}
}

function reset_select(oggetto){
	if ( oggetto != null ){
		oggetto.options.length = 0;
	}
}

function reset_input(oggetto){
	if ( oggetto != null ){
		oggetto.value = "";
	}
}

function ms_nsc(oggetto){
	
	var obj = document.getElementById(oggetto);
	var obj2 = "";
	
	if ( obj != null ){
		
		obj.style.display = 'block';
		
		if ( oggetto == 'registrazione' ){
			obj2 = "login";
		}else{
			obj2 = "registrazione";
		}
		
		document.getElementById(obj2).style.display = 'none';

	}
}

function show_hide(oggetto){
	var obj = document.getElementById(oggetto);
	if ( obj != null ){
		var status = obj.style.display;
		if ( status == 'block' ){
			obj.style.display = 'none';
		}else{
			obj.style.display = 'block';
		}
	}
}

function conferma_pagamento(){

	var testo = "Confermi questo pagamento?";
	
	if ( confirm(testo) ){
		return true;
	}else{
		return false;
	}

}

function show_div_amici(){
	var obj = document.getElementById('div_check_amici');
	if ( document.getElementById('check_amici').checked == true ){
		obj.style.display = 'block';
	}else{
		obj.style.display = 'none';
	}
}

/********************
 * FUNZIONE CARTINA *
 *******************/
function ApriInfo(str){
	searchWin = window.open(str,'immagini','scrollbars=yes,resizable=yes,width=500,height=350,status=no,location=no,toolbar=no');
	//searchWin.refer = self;
}

function open_window_credits() {
	window.open("credits.htm", null, "height=200,width=300,scrollbars=no,resizable=no,status=no,toolbar=no,menubar=no,location=no");
}