/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject() {
	var request_type;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		request_type = new ActiveXObject("Microsoft.XMLHTTP");
		}else{
		request_type = new XMLHttpRequest();
	}
	return request_type;
}

var http = createObject();

/* -------------------------- */
/* LOGIN */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;
function login() {
	// Optional: Show a waiting message in the layer with ID ajax_response
	document.getElementById('errore-login').innerHTML = "Attendere Prego..."
	// Required: verify that all fileds is not empty. Use encodeURIComponent() to solve some issues about character encoding.
	var username = encodeURIComponent(document.getElementById('username').value);
	var psw = encodeURIComponent(document.getElementById('psw').value);
	// Set te random number to add to URL request
	nocache = Math.random();
	// Pass the login variables like URL variable
	//username = username.replace("&", "amp;");
	//alert (username);
	http.open('get', 'login.php?username=' + username + '&psw=' + psw + '&nocache = '+nocache);
	http.onreadystatechange = loginReply;
	http.send(null);
}
function loginReply() {
	if(http.readyState == 4){
		var response = http.responseText;
	
		if(response == "0"){
			// if login fails
			document.getElementById('errore-login').innerHTML = 'Login fallito! Verifica utente e password';
			// else if login is ok show a message: "Welcome + the user name".
		} else if(response == "1aTy29!qz"){
			location.replace("http://www.isistore.it/registrazione.php?");
		} else {
			document.getElementById('errore-login').innerHTML = 'Accesso ad ISIStore in corso...';
			location.replace("http://www.isistore.it/_login_isieng.php?cliente="+response);
		}
	}
}
