


//***************************
// *** FUNZIONI SUPERIORI ***
//***************************
function Log(paramErr)
{
	var CurrFile = location.pathname.split('/');
	try
	{
		var okLog = SendByGet(SetLogParam(paramErr), 'http://www.catastoinrete.it/includes/LogJsError.asp')		
	    if (okLog != true)
	    {
			SendMail(paramErr);
	    }
    }
    catch(err)
    {
    	var paramErr = new Array('javascript', '2', '['+ sessId + '] :: Log() :: ' + err.message, err.description, CurrFile[CurrFile.length - 1]);		
	    SendMail(paramErr);
    }
}

function SendMail(paramErr)
{
	var CurrFile = location.pathname.split('/');
	try
	{
		var okSendMail = SendByPost(SetMailParam(paramErr), 'http://www.catastoinrete.it/includes/SendJsMail.asp');
		if (okSendMail != true)
	    {	
			//TODO : scrivere in un file...
		}
	}
    catch(err)
    {
    	var paramErr = new Array('javascript', '2', '['+ sessId + '] :: Log() :: ' + err.message, err.description, CurrFile[CurrFile.length - 1]);		
	    //TODO : scrivere in un file...
    }
}


//***********************************
// *** FUNZIONI PREPARA PARAMETRI ***
//***********************************
function SetLogParam(parameters)
{
	//alert(parameters[2] + '  ***  ' + parameters[3]);
	var ret = '';
	try
	{
		var appoParam2 = parameters[2];
		if (parameters[2] == undefined)
		{
			appoParam2 = 'undefined';
		}
		else
		{
			if (parameters[2].indexOf('\'') > 0)
			{
				appoParam2 = appoParam2.replace('\'', '\'\'');
			}
			if (parameters[2].indexOf('&') > 0)
			{
				appoParam2 = appoParam2.replace('&', '');
			}
		}
		
		var appoParam3 = parameters[3];
		if (parameters[3] == undefined)
		{
			appoParam3 = 'undefined';
		}
		else
		{
			if (parameters[3].indexOf('\'') > 0)
			{
				appoParam3 = appoParam3.replace('\'', '\'\'');
			}
			if (parameters[3].indexOf('&') > 0)
			{
				appoParam3 = appoParam3.replace('&', '');
			}
		}
		
		//ret = "'" + encodeURI(parameters[0]) + "', " + encodeURI(parameters[1]) + ", '" + encodeURI(appoParam2) + "', '" + encodeURI(appoParam3) + "', '" + encodeURI(parameters[4]) + "'";
		ret = "'" + parameters[0] + "', " + parameters[1] + ", '" + appoParam2 + "', '" + appoParam3 + "', '" + parameters[4] + "'";
	}
	catch(err)
    {
        err.message = 'SetLogParam(' + parameters[0] + "', " + parameters[1] + ", '" + parameters[2] + "', '" + parameters[3] + "', '" + parameters[4] + ')  :: ' + err.message;
        throw(err);
        
        return '';
    }
    
	return ret;
}
function SetMailParam(parameters)
{
	var ret = '';
	try
	{
		var appoParam2 = parameters[2];
		if (parameters[2] == undefined)
		{
			appoParam2 = 'undefined';
		}
		else
		{
			if (parameters[2].indexOf('&') > 0)
			{
				appoParam2 = appoParam2.replace('&', '');
			}
			if (parameters[2].indexOf('<') > 0)
			{
				appoParam2 = appoParam2.replace('<', '');
			}
		}
		
		var appoParam3 = parameters[3];
		if (parameters[3] == undefined)
		{
			appoParam3 = 'undefined';
		}
		else
		{
			if (parameters[3].indexOf('&') > 0)
			{
				appoParam3 = appoParam3.replace('&', '');
			}
			if (parameters[3].indexOf('<') > 0)
			{
				appoParam3 = appoParam3.replace('<', '');
			}
		}
		
		var mailParams = new Array();
		mailParams[0] = 'marino.cenni@infocomas.it;daniele.fabbri@infocomas.it;matteo.distort@infocomas.it';
		mailParams[1] = 'mail@catastoinrete.it';
		mailParams[2] = 'ERRORE INSERT INTO WebLog';
		mailParams[3] = 'ERRORE INSERT INTO WebLog - db ServiziOnLine' + '<br/>Tipo: ' + parameters[0] + '<br/>Livello: ' + parameters[1] + '<br/>Messaggio: ' + appoParam2 + '<br/>Descrizione: ' + appoParam3 + '<br/>Provenienza: ' + parameters[4] + '<br/>Data: ' + Date();
		
		ret = 'To=' + encodeURI(mailParams[0]) + '&From=' + encodeURI(mailParams[1]) + '&Sbj=' + encodeURI(mailParams[2]) + '&Msg=' + encodeURI(mailParams[3]);
	}
	catch(err)
    {
        err.message = 'SetMailParam(' + parameters[0] + "', " + parameters[1] + ", '" + parameters[2] + "', '" + parameters[3] + "', '" + parameters[4] + ')  :: ' + err.message;
        throw(err);
        
        return '';
    }
		
	return ret;	
}


//*************************************
// *** FUNZIONI INVIO GET/POST AJAX ***
//*************************************
function SendByGet(param, urlDestination)
{
    var ris = true;
    
    try
    {
	    if (param == '')
	    	return false;
	
	    if (window.XMLHttpRequest)
	    {   
	        // code for IE7+, Firefox, Chrome, Opera, Safari
	        xmlhttp = new XMLHttpRequest();
	    }
	    else
	    {   
			// code for IE6, IE5
			//xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			try
	    	{
				//code for IE6, IE5
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e) 
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
	    }
	    
	    if (!xmlhttp)
		{
			jAlertWrapper('Cannot create XMLHTTP instance', 'Attention');
			return false;
		}
	    
	    xmlhttp.onreadystatechange = function()
	    {
	        if (xmlhttp.readyState == 4)
	        {
	        	//alert('responseText: ' + xmlhttp.responseText);
	            if (xmlhttp.responseText != 'ok')
	            {
	                ris = false;
	                return ris;
	            }
	        }
	    }
	
	    xmlhttp.open("GET", urlDestination + "?param=" + param , true);
	    xmlhttp.send(null);
    
    }
    catch(err)
    {
    	ris = false;
    	
        err.message = 'SendByGet(' + param + ')  :: ' + err.message;
        throw(err);
    }
    
    return ris;
}



/*
*** REMMATO DA DANIELE 
function SendByGetAndBack(param, urlDestination)
{
    try
    {
    	var xmlDoc;
    	var xmlhttp;
    	
    	
    	
	    if (param == '' || param == null)
	    {
			//KO.. Converto da stringa a xml
    		if (window.ActiveXObject)
    		{
    		  //IExplore
    		  xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
              xmlDoc.async = 'false';
              xmlDoc.loadXML('<?xml version="1.0" encoding="UTF-8"?><res>ko</res>');
            }
            else
            {
              //Firefox e altri
              //var parser = new DOMParser();
              //xmlDoc = parser.parseFromString('<?xml version="1.0" encoding="UTF-8"?><res>ko</res>', 'text/xml');
			  xmlDoc = document.implementation.createDocument("","",null);
			  xmlDoc.async = false;
			  xmlDoc.load('<?xml version="1.0" encoding="UTF-8"?><res>ko</res>');			  
            }
            return xmlDoc;
		}
				
		
		
	    if (window.XMLHttpRequest)
	    {   
	        // code for IE7+, Firefox, Chrome, Opera, Safari
	        xmlhttp = new XMLHttpRequest();
	        
	    }
	    else
	    {   
	    	try
	    	{
				//code for IE6, IE5
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e) 
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
	    }
	   		   
	    if (xmlhttp == null)
		{
			//KO..
			err.message = 'SendByGetAndBack(' + param + ', ' + urlDestination + ')  :: unable to create xmlhttp object :: ' + err.message;
        	throw(err);        	
		}
		
		var req = '';
		for (i = 0; i < param.length; i++)
		{
			req = req + '&param' + i + '=' + param[i];
		}
		req = '?' + req.substring(1, req.length)
		
		//codice per Mozilla, etc.
		
		
		
		/*
	    if (window.XMLHttpRequest) {
	        xmlhttp.open("GET", urlDestination + req, true);
			//xmlhttp.setRequestHeader("content-type", "application/x-www-form-urlencoded");
			xmlhttp.send(null);								
		}
		//codice per IExplore		
		else if (window.ActiveXObject) {			
			if (xmlhttp) {
				xmlhttp.open("GET", urlDestination + req, true);
				xmlhttp.setRequestHeader("content-type", "application/x-www-form-urlencoded");
				xmlhttp.send();
			}
		} 
		
		*/
		
/*   
	if (xmlhttp!=null)
	{
		xmlhttp.onreadystatechange=function()
											{ 
												if (xmlhttp.readyState == 4)
													if (xmlhttp.status == 200)
														// alert(xmlhttp.status);
														return caricadati(xmlhttp);
											}
		
		xmlhttp.open("GET", urlDestination + req, true);
		xmlhttp.send(null);	
		   	
    }
    
    }
    
    catch(err)
    {
    	
        err.message = 'SendByGetAndBack(' + param + ', ' + urlDestination + ') :: ' + err.message;
        //throw(err);
         
        //Converto da stringa a xml
		if (window.ActiveXObject)
		{
		  //IExplore
          xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
          xmlDoc.async = 'false';
          xmlDoc.loadXML('<?xml version="1.0" encoding="UTF-8"?><res>ko</res>');
        }
        else
        {
          //Firefox e altri
          //var parser = new DOMParser();
          //xmlDoc = parser.parseFromString('<?xml version="1.0" encoding="UTF-8"?><res>ko</res>', 'text/xml');
          xmlDoc = document.implementation.createDocument("","",null);
		  xmlDoc.async = false;
		  xmlDoc.load('<?xml version="1.0" encoding="UTF-8"?><res>ko</res>');
        }
        return xmlDoc;
    }

}

*** FINE REMMATURA
*/



function SendByGetAndBack(param, urlDestination)
{
    try
    {
    	var xmlDoc;
    	
	    if (param == '' || param == null)
	    {
			//KO.. Converto da stringa a xml
    		if (window.ActiveXObject)
    		{
              xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
              xmlDoc.async = 'false';
              xmlDoc.loadXML('<?xml version="1.0" encoding="UTF-8"?><res>ko</res>');
            }
            else
            {
              var parser = new DOMParser();
              xmlDoc = parser.parseFromString('<?xml version="1.0" encoding="UTF-8"?><res>ko</res>', 'text/xml');
            }
            return xmlDoc;
		}
		
	
	    if (window.XMLHttpRequest)
	    {   
	        // code for IE7+, Firefox, Chrome, Opera, Safari
	        xmlhttp = new XMLHttpRequest();
	    }
	    else
	    {   
	    	try
	    	{
				//code for IE6, IE5
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e) 
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
	    }
	    
	    if (!xmlhttp)
		{
			//KO..
			err.message = 'SendByGetAndBack(' + param + ', ' + urlDestination + ')  :: unable to create xmlhttp object :: ' + err.message;
        	throw(err);
		}
		
		var req = '';
		for (i = 0; i < param.length; i++)
		{
			req = req + '&param' + i + '=' + param[i];
		}
		req = '?' + req.substring(1, req.length)

	    xmlhttp.open("GET", urlDestination + req, false);
	    xmlhttp.setRequestHeader("content-type", "application/x-www-form-urlencoded"); 
	    xmlhttp.send(null);
	    
	    if(xmlhttp.status == 200)
	    {
	    	var ret;
	    	
	    	//OK.. Converto da stringa a xml
    		if (window.ActiveXObject)
    		{
            	xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
              	xmlDoc.async = 'false';
              	xmlDoc.loadXML(xmlhttp.responseText);
              
              	ret = xmlDoc;
            }
            else
            {
              	var parser = new DOMParser();
              	xmlDoc = parser.parseFromString(xmlhttp.responseText,'text/xml');
              
              	ret = xmlDoc;
            }

	        return ret;
	    }
	    else
	    {
	        //KO.. Converto da stringa a xml
    		if (window.ActiveXObject)
    		{
              xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
              xmlDoc.async = 'false';
              xmlDoc.loadXML('<?xml version="1.0" encoding="UTF-8"?><res>ko</res>');
            }
            else
            {
              var parser = new DOMParser();
              xmlDoc = parser.parseFromString('<?xml version="1.0" encoding="UTF-8"?><res>ko</res>', 'text/xml');
            }
            return xmlDoc;
	    }
    	
    }
    catch(err)
    {
    	//var CurrFile = location.pathname.split('/');
        err.message = 'SendByGetAndBack(' + param + ', ' + urlDestination + ') :: ' + err.message;
        
        //var paramErr = new Array('javascript', '2', '['+ sessId + '] :: ' + err.message, err.description, CurrFile[CurrFile.length - 1]);
        //Log(paramErr)
        //throw(err);
        
        //Converto da stringa a xml
		if (window.ActiveXObject)
		{
          xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
          xmlDoc.async = 'false';
          xmlDoc.loadXML('<?xml version="1.0" encoding="UTF-8"?><res>ko</res>');
        }
        else
        {
          var parser = new DOMParser();
          xmlDoc = parser.parseFromString('<?xml version="1.0" encoding="UTF-8"?><res>ko</res>', 'text/xml');
        }
        return xmlDoc;
    }
    
}




function caricadati(xmlhttp)
{
	var ret;	    	
	
	//OK.. Converto da stringa a xml
	if (window.ActiveXObject)
	{
	  //IExplore
      xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
      xmlDoc.async = 'false';
      xmlDoc.loadXML(xmlhttp.responseText);             
      ret = xmlDoc;

    }
    else
    {
      //Firefox e altri
      //var parser = new DOMParser();
      //xmlDoc = parser.parseFromString(xmlhttp.responseText,'text/xml');             		                
      xmlDoc = document.implementation.createDocument("","",null);
	  xmlDoc.async = false;
	  xmlDoc.load(xmlhttp.responseText);              
      ret = xmlDoc;
    }
	
    return ret;			        
} 

function SendByPost(param, urlDestination)
{
    var ris = true;
    
    if (param == '')
    	return false;
    
    http_request = false;
  
    if (window.XMLHttpRequest)
    {   
        // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        
        if (http_request.overrideMimeType)
        {
            http_request.overrideMimeType('text/html');
        }
      }
      else if (window.ActiveXObject)
      { 
        // IE
        try
        {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {}
         }
      }
      
      if (!http_request)
      {
         jAlertWrapper('Cannot create XMLHTTP instance', 'Attention');
         return false;
      }
      
      http_request.onreadystatechange = function()
      {
      	if (http_request.readyState == 4)
		{
			//alert('responseText: ' + http_request.responseText);
			if (http_request.responseText != 'ok')
			{
			    ris = false;
			}
		}
      }
      http_request.open('POST', urlDestination, true, "", "");
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", param.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(param);
      
      return ris;
}


