
// TODO: Funzione di prova, da rimuovere prima possibile!!!
function tryRemote(contextPath)
{
	var url = "/" + contextPath + "/login?cmd=cmdRemoteTest";
	var req = createXMLHttpRequest();
	
	if(req) {
		req.open("GET", url, false);
		req.send("");
	    // only if req shows "loaded"
	    if (req.readyState == 4) {
	        // only if "OK"
	        if (req.status == 200) {
	            // ...processing statements go here...
	            window.alert(req.responseText);
	            return true;
	        } else {
	            alert("There was a problem retrieving the XML data:\n" +
	                req.statusText);
	            return false;
	        }
	    }
	}
        
        return false;
}

function f_changePassword(contextPath, username, old, new1, new2, errorUsernameId, errorNewPasswordId){
	var errorUsernameHtml = document.getElementById(errorUsernameId)
	var errorNewPasswordHtml = document.getElementById(errorNewPasswordId)
	if (new1 != new2) {
		errorNewPasswordHtml.innerHTML = "Le due password non coincidono."
		return false;
	}

	var url = contextPath + "/login?cmd=cmdChangePassword&username=" + username + "&oldpassword=" + old  + "&newpassword=" + new1;
	var req = createXMLHttpRequest();
	
	if(req) {
		req.open("GET", url, false);
		req.send("");
	    // only if req shows "loaded"
	    if (req.readyState == 4) {
	        // only if "OK"
	        if (req.status == 200) {
	        	var result = req.responseText.substr(0,2);
	            if (result=="ok") {
		            return true;
		        } else {
		        	errorUsernameHtml.innerHTML = "Non è stato possibile cambiare le password.";
		        	return false;
		        }
	        } else {
	        	errorUsernameHtml.innerHTML = "Errore di comunicazione.";
	        }
	    }
	}
	
	return false;
}



// estratto da http://developer.apple.com/internet/webcontent/xmlhttpreq.html
// dovrebbe supportare tutti i browser più comuni.
// TODO: Anche se scritto da rinomati programmatori della Apple,
// il codice andrebbe sistemato.

var req;



function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            // ...processing statements go here...
            window.alert(req.responseText);
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    }
}

function loadXMLDoc(url) {
    req = false;
    
	// branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send("");
	}
}

// Crea un oggetto XMLHttpRequest standard.
function createXMLHttpRequest() {
    var xhreq = null;
    
	// branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    	try {
			xhreq = new XMLHttpRequest();
        } catch(e) {
			xhreq = null;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	xhreq = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		xhreq = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		xhreq = null;
        	}
		}
    }
    
    return xhreq;
}

