/* ---------------------------- */
/* 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('error').style.display = "block"
	document.getElementById('error').innerHTML = "Loading..."
	
	// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
	var username = encodeURI(document.getElementById('username').value);
	var password = encodeURI(document.getElementById('password').value);
	
	// Set te random number to add to URL request
	nocache = Math.random();
	
	// Pass the login variables like URL variable
	http.open('get', '../admin/auth.php?username='+username+'&password='+password+'&nocache='+nocache+'&mode=login');
	http.onreadystatechange = loginReply;
	http.send(null);
	
}

function loginReply() {
		
	if(http.readyState == 4){
		
		var response = http.responseText;
		
		if(response == 0){
			
			// if login fails
			document.getElementById('error').innerHTML = 'Login failed! Verify user and password';
			// else if login is ok show a message: "Welcome + the user name".
		
		} else {
			
			closelogin();
		
		}
	
	}
}

function showArticle(id) {
	
	// Optional: Show a waiting message in the layer with ID ajax_response
	document.getElementById('loading').style.display = "block";
	document.getElementById('loading').innerHTML = "Loading...";
	
	// Set te random number to add to URL request
	nocache = Math.random();
	// Pass the login variables like URL variable
	http.open('get', '../modules/getArticle.php?id='+id);
	http.onreadystatechange = Article;
	http.send(null);
	
}

function Article() {
		
	if(http.readyState == 4){
		
		var response = http.responseText;
		
		if(response == 0){
			
			// if get article fails
			document.getElementById('loading').innerHTML = 'There was an error. Please try again';
		
		} else {
			// else if get article is ok show a message: Article.
			
			document.getElementById('article').innerHTML = response;
			document.getElementById('loading').style.display = "none";
			var acc7c = new Spry.Widget.Accordion("article", { useFixedPanelHeights: false, defaultPanel: -1 });
		
		}
	
	}
}

