// the XmlHTTPRequest object
var xhr;


// get the XHR object from system
function getXHR() {
	
	try {
		xhr = new XMLHttpRequest();
	} 
	catch (e) {
		
		try {
			var xhr = new ActiveXObject('Msxml2.XMLHTTP');
		}
		catch (e) {
		
			try {
				var xhr = new ActiveXObject('Microsoft.XMLHTTP');
			} 
			catch (e) {
			
				document.write('XMLHttpRequest not supported'); 
			}
		}
	}
	
	return xhr;
}


// writes the content of the specified URL into the specified div
function putAjaxHtml(div, url) {

		// gets the XHR object 
		var xhr = getXHR();
		
		// calls a function when the state changes
		xhr.onreadystatechange = function() {
			
			// if the XmlHTTPRequest has ceen completed
			if (xhr.readyState == 4){
	
				// if the HTTP resposne status is OK
				if (xhr.status == 200){
		    
		    		// writes the content of the HTTP response into the specified div
		     		target = document.getElementById(div);
		     		target.innerHTML = xhr.responseText;
		     	}
		     	else {
					// MODDATA
					// ELIMINARE "return false;" e scommentare l'alert se non dovesse andare
					return false;
					//alert("HTTP Response code = " + xhr.status);
		    	}
		  	}
	 	}
	 	
		// creates the request
		xhr.open('POST', url ,true);
		
		// and sends it
		xhr.send(null);
}

function putAjaxHtmlMulti(name, url) {

		// gets the XHR object 
		var xhr = getXHR();
		
		// calls a function when the state changes
		xhr.onreadystatechange = function() {
			
			// if the XmlHTTPRequest has ceen completed
			if (xhr.readyState == 4){
	
				// if the HTTP resposne status is OK
				if (xhr.status == 200){
		    
		    		// writes the content of the HTTP response into the specified div
		     		target = document.getElementsByName(name);
					for (var i = 0; i < target.length; i++) {
						target.item(i).innerHTML = xhr.responseText;
						target.item(i).style.display="block";
					}
		     	}
		     	else {
					// MODDATA
					// ELIMINARE "return false;" E DECOMMENTARE L'ALERT SE NON DOVESSE ANDARE
					return false;
					//alert("HTTP Response code = " + xhr.status);
		    	}
		  	}
	 	}
	 	
		// creates the request
		xhr.open('POST', url ,true);
		
		// and sends it
		xhr.send(null);
}



// sends an email using the send_mail.jsp page
function sendMail(recipient, sender, subject, body) {
		
		// creates the param
		var params = "to=" + encodeURI(recipient) + "&from=" + encodeURI(sender) + "&subject=" + encodeURI(subject) + "&body=" + encodeURI(body);
		
		// retrieves the Xml HTTP request		
		var xhr = getXHR();
	
		// creates the call		
		xhr.open('POST', 'SYS_send_mail.jsp', true);
		                  		
		// sends the proper header information (for POST method) along with the request
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

		// now sends the request
		xhr.send(params);
}



function confirmDelete(url) {

	if (window.confirm("Are you sure you want to delete this item?")) {
		
		window.location = url;
	}
}

	
function openPopupWindow(url) {
	
	var style = 'width=700, status=no, menubar=no, toolbar=no, scrollbars=yes';
	window.open(url, 'new_window', style);
}

function openPopupWindow4(url, width){
	var style = 'status=no, menubar=no, toolbar=no, scrollbars=yes, '+'width='+width;
	window.open(url, 'new_window', style);
}


function openPopupWindow2(url, style) {
	
	window.open(url, 'new_window', style);
}


function openPopupWindow3(url, style, name) {
	
	window.open(url, name, style);
}


function setVisible(id) {

	if (document.getElementById) document.getElementById(id).style.visibility="visible";
    else if (document.all) document.all[id].style.visibility="visible";
	else document.layers[id].visibility="show";
}

function setHidden(id) {

	if (document.getElementById) document.getElementById(id).style.visibility="hidden";
	else if (document.all) document.all[id].style.visibility="hidden";
	else document.layers[id].visibility="hide";
}

function checkRegExp(exp, value, field_name) {
	
	var e = new RegExp(exp);

	if (!e.test(value)) {
		if (value == '') return 63;
		else return 62;
	}
	else return 0;	

}

function validateField(exp, value, field_name, nullable) {
	
	// if the value is nullable and its value is '', it's ok
	if (nullable && value=='') return 0;
	
	var validation;

	// checks the value	
	if (exp == 'C.F.') validation = checkCodiceFiscale(value);
	else if (exp == 'P.IVA') validation = checkPIVA(value);
	else validation = checkRegExp(exp, value, field_name);
	
	// if isn't valid, show an alert to the user
	//if (validation != "") alert(validation);
	
	// and returns true if there's no error msg
	return validation;	
}


function checkCodiceFiscale(cf) {
	var validi, i, s, set1, set2, setpari, setdisp;
	//if (cf == '')  return '';
	cf = cf.toUpperCase();
	if( cf.length != 16 ) return 64;
	validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	for( i = 0; i < 16; i++ ){
		if( validi.indexOf( cf.charAt(i) ) == -1 ) return 65;
	}
	set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
	s = 0;
	for( i = 1; i <= 13; i += 2 )	s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	for( i = 0; i <= 14; i += 2 )	s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) ) return 66;
	return 0;
}


function checkPIVA(pi) {
	if( pi.length != 11 ) return 67;
	validi = "0123456789";
	for( i = 0; i < 11; i++ ) {
		if( validi.indexOf( pi.charAt(i) ) == -1) return 68;
	}
	s = 0;
	for( i = 0; i <= 9; i += 2 ) s += pi.charCodeAt(i) - '0'.charCodeAt(0);
	for( i = 1; i <= 9; i += 2 ) {
		c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
		if( c > 9 )  c = c - 9;
		s += c;
	}
	if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) ) return 69;
	
	return 0;
}

function getUrlParam(name) { 
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); 
	var regexS = "[\\?&]"+name+"=([^&#]*)"; 
	var regex = new RegExp( regexS ); 
	var results = regex.exec( window.location.href ); 
	if( results == null )    return " "; 
	else return results[1];
}

function getXMLHTTP() { 
	var xmlhttp=false;      
    try{
    	xmlhttp=new XMLHttpRequest();
    }catch(e){               
		try{                    
        	xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
        }catch(e){
        	try{
            	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }catch(e1){
            	xmlhttp=false;
            }
         }
	}
	return xmlhttp;
}


// check per form registrazione
//


//fuction to return the xml http object
function getXMLHTTP() { 
                var xmlhttp=false;      
                try{
                        xmlhttp=new XMLHttpRequest();
                }
                catch(e)        {               
                        try{                    
                                xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
                        }
                        catch(e){
                                try{
                                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                                }
                                catch(e1){
                                        xmlhttp=false;
                                }
                        }
                }
                        
                return xmlhttp;
    }


function getLogin(email, url) {              
	var strURL="http://"+url+"/_sys/getLogin.jsp?username="+email;
    var req = getXMLHTTP();                
        if (req) {
        	req.onreadystatechange = function() {
	        if (req.readyState == 4) {
            	// only if "OK"
                if (req.status == 200) {                                                
                	setVisible('div_email');
					document.getElementById('div_email').innerHTML=req.responseText;                                          
                } else {
                	alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                }
            }                               
        	}                       
        req.open("GET", strURL, true);
        req.send(null);
        }
}

function getNick(nickname, url) {              
	var strURL="http://"+url+"/_sys/getNick.jsp?nickuser="+nickname;
        var req = getXMLHTTP();    
        if (req) {
                req.onreadystatechange = function() {
	        if (req.readyState == 4) {
                // only if "OK"
                       if (req.status == 200) {                                                
                                        setVisible('div_forum_nickname');
					document.getElementById('div_forum_nickname').innerHTML=req.responseText;                                          
                                } else {
                                        alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                                }
                        }                               
                }                       
                req.open("GET", strURL, true);
                req.send(null);
        }
}

/*******************************************************************/
/***                                                             ***/
/***   Tokenizer.js - JavaScript String Tokenizer Function       ***/
/***                                                             ***/
/***   Version   : 0.2                                           ***/
/***   Date      : 01.05.2005                                    ***/
/***   Copyright : 2005 Adrian Zentner                           ***/
/***   Website   : http://www.adrian.zentner.name/               ***/
/***                                                             ***/
/***   This library is free software. It can be freely used as   ***/
/***   long as this this copyright notice is not removed.        ***/
/***                                                             ***/
/*******************************************************************/

String.prototype.tokenize = tokenize;

function tokenize()
  {
     var input             = "";
     var separator         = " ";
     var trim              = "";
     var ignoreEmptyTokens = true;

     try {
       String(this.toLowerCase());
     }
     catch(e) {
       window.alert("Tokenizer Usage: string myTokens[] = myString.tokenize(string separator, string trim, boolean ignoreEmptyTokens);");
       return;
     }

     if(typeof(this) != "undefined")
       {
          input = String(this);
       }

     if(typeof(tokenize.arguments[0]) != "undefined")
       {
          separator = String(tokenize.arguments[0]);
       }

     if(typeof(tokenize.arguments[1]) != "undefined")
       {
          trim = String(tokenize.arguments[1]);
       }

     if(typeof(tokenize.arguments[2]) != "undefined")
       {
          if(!tokenize.arguments[2])
            ignoreEmptyTokens = false;
       }

     var array = input.split(separator);

     if(trim)
       for(var i=0; i<array.length; i++)
         {
           while(array[i].slice(0, trim.length) == trim)
             array[i] = array[i].slice(trim.length);
           while(array[i].slice(array[i].length-trim.length) == trim)
             array[i] = array[i].slice(0, array[i].length-trim.length);
         }

     var token = new Array();
     if(ignoreEmptyTokens)
       {
          for(var i=0; i<array.length; i++)
            if(array[i] != "")
              token.push(array[i]);
       }
     else
       {
          token = array;
       }

     return token;
  }




