// 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 {
					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 openPopupWindow2(url, style) {
	
	window.open(url, 'new_window', style);
}


function openPopupWindow2(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 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+"/nick/_sys/getMail.jsp?email="+email;
    var req = getXMLHTTP();                
        if (req) {
        	req.onreadystatechange = function() {
	        if (req.readyState == 4) {
            	// only if "OK"
                if (req.status == 200) {                                                
                	setVisible('error_email');
					document.getElementById('error_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+"/nick/_sys/getNick.jsp?Login="+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);
        }
}
