
var address  = 'http://tsearch-dev.telemat.it/get_result.php';
var address2 = 'http://tsearch-dev.telemat.it/get_mail.php';

if( location.host.substr(0,6) != "test2." ) {
	address  = 'http://tsearch.telemat.it/get_result.php';
	address2 = 'http://tsearch.telemat.it/get_mail.php';
}

if( typeof(telemat_publisher) == 'undefined' || !telemat_publisher ) var telemat_publisher = '';

//alert( telemat_publisher );

//1.1: risolve problema di Internet Explorer (http://support.microsoft.com/kb/884534/en-us/)


//		      GESTIONE SCRIPT DINAMICI
//	
//	utilizzato per gestire caricamento funzioni javascript-ajax
//	 			
//
//

// Constructor -- pass a REST request URL to the constructor
//
function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl; 
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
//
JSONscriptRequest.prototype.buildScriptTag = function () {

    // Create the script tag
    this.scriptObj = document.createElement("script");
    
    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
// removeScriptTag method
// 
JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);  
}

// addScriptTag method
//
JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
}


//
//
//	GESTIONE DEI DATI
//
//

//Callback per getForm
function getForm(jsonData) {     

	elem = document.getElementById('TelematForm');
	elem.innerHTML=jsonData;

	bObj.removeScriptTag(); 

}

//Callback per getFormAvanzato
function getFormAvanzato(jsonData) {     

	elem = document.getElementById('TelematForm');
	elem.innerHTML=jsonData;
	
	//visualizza la cartina in flash

	
	bObj.removeScriptTag(); 

}

//Callback per getResult
function getResult(jsonData) {     

	elem = document.getElementById('TelematResult');
	elem.innerHTML=jsonData;

	bObj.removeScriptTag(); 

}

//Callback per getAdvancedResult
function getAdvancedResult(jsonData) {     

	getResult(jsonData);

}

//Callback per getInfo
function getInfo(jsonData){

	elem = document.getElementById('TelematResult');
	elem.innerHTML=jsonData;

	bObj.removeScriptTag(); 

}

//Calback per mail inviata
function getSend(jsonData){

	elem = document.getElementById('TelematResult');
	elem.innerHTML=jsonData;

	bObj.removeScriptTag(); 
	
}

//per far funzionare Enter
function onEnter( evt, frm ) {

	

	var keyCode = null;

	if( evt.which ) {
		keyCode = evt.which;
	} else if( evt.keyCode ) {
		keyCode = evt.keyCode;
	}
	if( 13 == keyCode ) {
		loadResult();
		return false;
	}
	return true;

}

function onEnterAvanzato( evt2, frm2 ) {

	
	
	var keyCode2 = null;

	if( evt2.which ) {
		keyCode2 = evt2.which;
	} else if( evt2.keyCode ) {
		keyCode2 = evt2.keyCode;
	}
	if( 13 == keyCode2 ) {
		loadAdvancedResult();
		return false;
	}
	return true;

}


function loadForm(){ 
	// The web service call
	var req  = address+'?action=getForm';
	// Create a new request object
	bObj = new JSONscriptRequest(req); 
	// Build the dynamic script tag
	bObj.buildScriptTag(); 
	// Add the script tag to the page
	bObj.addScriptTag();
}

function loadFormAvanzato(){

	resetResult();
	document.getElementById('TelematResult').style.display = 'none'; 

	// The web service call
	var req  = address+'?action=getFormAvanzato';
	 
	// Create a new request object
	bObj = new JSONscriptRequest(req); 
	// Build the dynamic script tag
	bObj.buildScriptTag(); 
	// Add the script tag to the page
	bObj.addScriptTag();

}

function loadFormSemplice(){

	resetResult();
	document.getElementById('TelematResult').style.display = 'none'; 

	var req  = address+'?action=getForm';
	bObj = new JSONscriptRequest(req);
	bObj.buildScriptTag(); 
	bObj.addScriptTag();

}


function loadInfo(){

	var req  = address+'?action=getInfo';
	bObj = new JSONscriptRequest(req);
	bObj.buildScriptTag(); 
	bObj.addScriptTag();

}

function loadResultCore(inviato, titolo, offset){ 

	document.getElementById('TelematResult').style.display = 'block'; 	
	

	if (offset == "noOffset"){
		var req  = address+'?action=getResult&from='+titolo+'&inviato='+inviato;
	}else{
		var req  = address+'?action=getResult&from='+titolo+'&inviato='+inviato+'&offset='+offset;
	}
	
	// Create a new request object
	bObj = new JSONscriptRequest(req); 
	// Build the dynamic script tag
	bObj.buildScriptTag(); 
	// Add the script tag to the page
	bObj.addScriptTag();
}

function resetResult(){

	elem = document.getElementById('TelematResult');
	elem.innerHTML="";

}

function loadResult(){ 
	inviato = document.telematSearch.inviato.value;

	if (document.telematSearch.titolo.value != "<Inserisci le parole da cercare>"){
		titolo = document.telematSearch.titolo.value;
	} else {
		document.telematSearch.titolo.value = "";
		titolo = "";
	}
	offset = "noOffset";
	loadResultCore(inviato, titolo, offset);
}


function loadAdvancedResult(){

	inviato = "2";

	DalGiorno = document.telemat_adv_search.DalGiorno.value;
	DalMese = document.telemat_adv_search.DalMese.value;
	DalAnno = document.telemat_adv_search.DalAnno.value;
	AGiorno = document.telemat_adv_search.AGiorno.value;
	AMese = document.telemat_adv_search.AMese.value;
	AAnno = document.telemat_adv_search.AAnno.value;
	DaEuro = document.telemat_adv_search.DaEuro.value;
	AEuro = document.telemat_adv_search.AEuro.value;

	regione = "";
	for (i=0; i<document.telemat_adv_search.regioni.length; i++){
		if (document.telemat_adv_search.regioni[i].checked==true)
			regione = document.telemat_adv_search.regioni[i].value+";"+regione
	}


	if (document.telemat_adv_search.par.value != "<Inserisci le parole da cercare>"){
		from = document.telemat_adv_search.par.value;
	} else {
		document.telemat_adv_search.par.value = "";
		from = "";
	}
	
	document.getElementById('TelematResult').style.display = 'block'; 


	if ( document.telemat_adv_search.parG.checked ){
		t = "g";
	}else{
		t = "e";
	} 
	
	offset = "noOffset";

	var req  = address+'?action=getAdvancedResult&inviato='+inviato+'&from='+from+'&DalGiorno='+DalGiorno+'&DalMese='+DalMese+'&DalAnno='+DalAnno+'&AGiorno='+AGiorno+'&AMese='+AMese+'&AAnno='+AAnno+'&DaEuro='+DaEuro+'&AEuro='+AEuro+'&t='+t+'&regione='+regione;
	

	
	// Create a new request object
	bObj = new JSONscriptRequest(req); 
	// Build the dynamic script tag
	bObj.buildScriptTag(); 
	// Add the script tag to the page
	bObj.addScriptTag();
}


//GESTIONE NAVIGATOR


function pagMove(inviato, titolo, offset){

	loadResultCore(inviato, titolo, offset);

}

function pagAdvancedMove(offset){

	inviato = "2";
	from = document.telemat_adv_search.par.value;
	DalGiorno = document.telemat_adv_search.DalGiorno.value;
	DalMese = document.telemat_adv_search.DalMese.value;
	DalAnno = document.telemat_adv_search.DalAnno.value;
	AGiorno = document.telemat_adv_search.AGiorno.value;
	AMese = document.telemat_adv_search.AMese.value;
	AAnno = document.telemat_adv_search.AAnno.value;
	DaEuro = document.telemat_adv_search.DaEuro.value;
	AEuro = document.telemat_adv_search.AEuro.value;
	
	regione = "";
	for (i=0; i<document.telemat_adv_search.regioni.length; i++){
		if (document.telemat_adv_search.regioni[i].checked==true)
			regione = document.telemat_adv_search.regioni[i].value+";"+regione
	}



	var req  = address+'?action=getAdvancedResult&inviato='+inviato+'&from='+from+'&DalGiorno='+DalGiorno+'&DalMese='+DalMese+'&DalAnno='+DalAnno+'&AGiorno='+AGiorno+'&AMese='+AMese+'&AAnno='+AAnno+'&DaEuro='+DaEuro+'&AEuro='+AEuro+'&t='+t+'&offset='+offset+'&regione='+regione;

	// Create a new request object
	bObj = new JSONscriptRequest(req); 
	// Build the dynamic script tag
	bObj.buildScriptTag(); 
	// Add the script tag to the page
	bObj.addScriptTag();

}

function infoInviaForm(){

	check = 0;

	

	if (document.richiediInfo.ragione.value == "" ){
		document.getElementById('infoErroreRagione').style.display = 'block'; 
		check = 1;
	}else{
		document.getElementById('infoErroreRagione').style.display = 'none';
		ragione = document.richiediInfo.ragione.value;
	}


	if (document.richiediInfo.indirizzo.value == "" ){
		document.getElementById('infoErroreIndirizzo').style.display = 'block'; 
		check = 1;
	}else{
		document.getElementById('infoErroreIndirizzo').style.display = 'none';
		indirizzo = document.richiediInfo.indirizzo.value;
	}


	if (document.richiediInfo.CAP.value == "" ){
		document.getElementById('infoErroreCAP').style.display = 'block'; 
		check = 1;
	}else{
		if (false){
			document.getElementById('infoErroreCAP2').style.display = 'block'; 
			check = 1;
		}else{
			document.getElementById('infoErroreCAP').style.display = 'none';
			document.getElementById('infoErroreCAP2').style.display = 'none'; 
			CAP = document.richiediInfo.CAP.value;
		}
	}
	


	if (document.richiediInfo.citta.value == "" ){
		document.getElementById('infoErroreCitta').style.display = 'block'; 
		check = 1;
	}else{
		document.getElementById('infoErroreCitta').style.display = 'none';
		citta = document.richiediInfo.citta.value;
	}
	
	if (document.richiediInfo.email.value == "" ){
		document.getElementById('infoErroreEmail').style.display = 'block'; 
		check = 1;
	}else{
		document.getElementById('infoErroreEmail').style.display = 'none';
		email = document.richiediInfo.email.value;
	}
	

	if (document.richiediInfo.nome.value == "" ){
		document.getElementById('infoErroreNome').style.display = 'block'; 
		check = 1;
	}else{
		document.getElementById('infoErroreNome').style.display = 'none';
		nome = document.richiediInfo.nome.value;
	}


	if (document.richiediInfo.cognome.value == "" ){
		document.getElementById('infoErroreCognome').style.display = 'block'; 
		check = 1;
	}else{
		document.getElementById('infoErroreCognome').style.display = 'none';
		cognome = document.richiediInfo.cognome.value;
	}

 	if (document.richiediInfo.telefono.value == "" ){
		document.getElementById('infoErroreTelefono').style.display = 'block'; 
		check = 1;
	}else{
		if (false){
			document.getElementById('infoErroreTelefono2').style.display = 'block'; 
			check = 1;
		}else{
			document.getElementById('infoErroreTelefono').style.display = 'none';
			document.getElementById('infoErroreTelefono2').style.display = 'none';
			telefono = document.richiediInfo.telefono.value;
		}

	}


 	if (document.richiediInfo.provincia.value == "" ){
		document.getElementById('infoErroreProvincia').style.display = 'block'; 
		check = 1;
	}else{
		document.getElementById('infoErroreProvincia').style.display = 'none';
		provincia = document.richiediInfo.provincia.value;
	}

	if (document.richiediInfo.privacy.checked == false ){
		document.getElementById('infoErrorePrivacy').style.display = 'block'; 
		privacy = 0;
		check = 1;
	}else{
		document.getElementById('infoErrorePrivacy').style.display = 'none';
		privacy = 1;
	}



	if (check == 0){

		settore = document.richiediInfo.settore.value;
		fax = document.richiediInfo.fax.value;
		cellulare = document.richiediInfo.cellulare.value;
		att_attivita = document.richiediInfo.att_attivita.value;
		sito = document.richiediInfo.sito.value;
		note = document.richiediInfo.note.value;

		var req  = address2+'?action=sendInfo&ragione='+ragione+'&indirizzo='+indirizzo+'&CAP='+CAP+'&citta='+citta+'&email='+email+'&nome='+nome+'&cognome='+cognome+'&telefono='+telefono+'&PR='+provincia+'&settore='+settore+'&fax='+fax+'&cellulare='+cellulare+'&att_attivita='+att_attivita+'&sito='+sito+'&note='+note+'&privacy='+privacy+'&codice_richiesta_informazioni=539026174&publisher='+telemat_publisher;

		// Create a new request object
		bObj = new JSONscriptRequest(req); 
		// Build the dynamic script tag
		bObj.buildScriptTag(); 
		// Add the script tag to the page
		bObj.addScriptTag();

	}
	
	
}



//document.getElementsByTagName("TelematSearch").innerHTML = '<div id="TelematForm"></div><div id="TelematResult"></div>';
loadForm();


