/*######################*/

/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Base64 = {
 
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = Base64._utf8_encode(input);
 
		while (i < input.length) {
 
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
 
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 
		}
 
		return output;
	},
 
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
		while (i < input.length) {
 
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			output = output + String.fromCharCode(chr1);
 
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
 
		}
 
		output = Base64._utf8_decode(output);
 
		return output;
 
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}





function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function segnalazione(testo) {
	
	var stringa = Base64.encode(testo);
	//var miovalore = (document.FormInserzionisti.inserzionisti_tipologia_inserzione.selectedIndex + 1);
	//alert(miovalore);
	http.open('get', 'js/segnalazione.php?stringa='+stringa);
    http.onreadystatechange = function () {
									if(http.readyState == 4){
										 //response = 'gestione campo form da qui<br><input type="file" name="pippo">';
										
										var response = http.responseText;

											//document.getElementById(update[0]).innerHTML = update[1];
											//document.getElementById(''+div_id+'').innerHTML = eval(response);

									}
								}
    http.send(null);
}

/*#############################################*/


function verificaForm(nomeForm) {
	switch (nomeForm) {

		case 'FormInfo':
			if (document.forms[nomeForm].nome_richiedente.value=='') {
				alert('Inserire il nome!');
				return false;
			}

			if (document.forms[nomeForm].email_richiedente.value=='' && document.forms[nomeForm].telefono_richiedente.value=='') {
				alert('Inserire almeno un recapito (indirizzo email o numero di telefono)!');
				return false;
			}


		break;
		
		case "formAR":
		
			if (document.forms[nomeForm].username.value=='') {
				alert('Inserire lo username!');
				return false;
			}
			if (document.forms[nomeForm].password.value=='') {
				alert('Inserire la password!');
				return false;
			}
		break;
		
		case "fCerca":
		
		var regex = /script/i;
		if( regex.test(document.forms[nomeForm].stringa.value) ){
			
			segnalazione(document.forms[nomeForm].stringa.value);
			
			alert('sono stati immessi uno o più caratteri non consentiti');
			document.forms[nomeForm].stringa.value = '';
			return false;
		}
		

		
		
			if (document.forms[nomeForm].stringa.value=='' || document.forms[nomeForm].stringa.value=='Cerca nel sito...' || document.forms[nomeForm].stringa.value=='Find...') {
				
				if (lingua=='en') alert('Please, insert somethig to search!');
				else alert('Per favore, inserire una parola da cercare!');
				return false;
			}
			
		break;

		case "CV1":
		
			if (document.forms[nomeForm].nome_cognome.value=='') {
				if (lingua=='en') alert('Please, insert your name and surname!');
				else alert('Per favore, inserire nome e cognome!');
				return false;
			}


			if (document.forms[nomeForm].indirizzo.value=='') {
				if (lingua=='en') alert('Please, insert your address!');
				else alert('Per favore, inserire l\'indirizzo!');
				return false;
			}

			if (document.forms[nomeForm].telefono.value=='') {
				if (lingua=='en') alert('Please, insert your telephone number!');
				else alert('Per favore, inserire il numero di telefono!');
				return false;
			}

			if (document.forms[nomeForm].cittadinanza.value=='') {
				if (lingua=='en') alert('Please, insert cittadinanza!');
				else alert('Per favore, inserire la cittadinanza!');
				return false;
			}

			if (document.forms[nomeForm].data_di_nascita.value=='') {
				if (lingua=='en') alert('Please, insert your date of birth!');
				else alert('Per favore, inserire la data di nascita!');
				return false;
			}


		break;

		case "CV2":
		
			
		break;

		case "CV3":
		
			if (document.forms[nomeForm].formazione_da_a.value=='') {
				if (lingua=='en') alert('Please, insert the dates!');
				else alert('Per favore, inserire le date!');
				return false;
			}

			if (document.forms[nomeForm].nome_istituto1.value=='') {
				if (lingua=='en') alert('Please, insert the school!');
				else alert('Per favore, inserire l\'istituto!');
				return false;
			}

			if (document.forms[nomeForm].materie_abilita1.value=='') {
				if (lingua=='en') alert('Please, insert the materie e abilità professionali!');
				else alert('Per favore, inserire le materie e abilità professionali!');
				return false;
			}

			if (document.forms[nomeForm].qualifica1.value=='') {
				if (lingua=='en') alert('Please, insert the qualifica!');
				else alert('Per favore, inserire la qualifica!');
				return false;
			}

		break;

		case "CV4":

			if (document.forms[nomeForm].madrelingua.value=='') {
				if (lingua=='en') alert('Please, insert the madrelingua!');
				else alert('Per favore, inserire la madrelingua!');
				return false;
			}

		break;

		case "CV5":

			if (!document.forms[nomeForm].ok_privacy.checked) {
				if (lingua=='en') alert('Please, check the privacy!');
				else alert('E\' necessario accettare il trattamento dei dati personali!');
				return false;
			}
			
		break;




		
	}
	return true;
}

function apri_mappa(nomeMappa) {

	window.open('mappa.php?mappa='+nomeMappa, 'Mappa', 'width=600, height=500, scrollbars=no, resizable=yes');
}
