// JavaScript Document

// constants
var LOGIN_PREFIX = 'lsfsdoafgadsfiasdfn.php?';
// variables
var http = new Asynchronous( ); // We create the HTTP Object

var loggedIn = false;
var user_name = '';
var messages = '';
var messageElement = '';
var location_message = '';

function logoff() {
	var http_logoff = new Asynchronous( ); // We create the HTTP Object
		http_logoff.call('GET', 'losdafsdfsdfsdgoff.php');
		http_logoff.complete = function(status, statusText, responseText, responseXML) {
			loggedIn = false;
			footer();
			admin_menu();
			left_menu();
			continuity_page();
		}
}

function createLink_login() {
		if(document.form_login.login.value != '' && document.form_login.password.value != ''){
		login_admin(document.form_login.login.value, document.form_login.password.value);} else {
		return(false);}
}

function createLink_item() {
		if(document.form_item.new_item.value != '' ){
		create(document.forms[0].name,document.form_item.new_item.id, document.form_item.new_item.value);} else {
		return(false);}
}

function image(state,message,use) {
//	var state; // State=ON --> put the waiting logo on // State=off --> put back the login button (case if wrong login...)
	if (state == 'on') {
		remove_imgs();
		i=document.createElement('img');
 		i.src='_images/indicator.gif';
 		i.alt='loading';
		i.style.border='none';
		i.height='16';
		i.width='16';
		location_message.appendChild(i);
		
	} else if (state == 'off') {
		remove_imgs();
		button_log = document.createElement('input');
		button_log.setAttribute('type','submit');
		
			button_log.name='submit_button';
			button_log.id='submit_button';
			button_log.value='   Create   ';		
			button_log.onclick = createLink_item;

		
		location_message.appendChild(button_log);
		
		var span = document.createElement('span');
		span.className = 'message';
		message = message +'   ';
		messageElement = document.createTextNode(message);
		span.appendChild(messageElement);
		location_message.insertBefore(span,button_log);

	}
}
// Function to remove 2 first childs of the element id='message' (it is supposed to have only 2 childs at the most)
function remove_imgs() {
	location_message = document.getElementById('message_thickbox');
	if (location_message) {
		if (location_message.hasChildNodes()) {location_message.removeChild(location_message.firstChild);}
		if (location_message.hasChildNodes()) {location_message.removeChild(location_message.lastChild); }
	}
}



function login_admin(login, password) {
	// ignore request if we are already logged in
	if (loggedIn)
		return;

	// ignore if either is empty
	if (login != '' && password  != '') {
		image('on','','login');
		http.call('GET', LOGIN_PREFIX+'login='+login+'&password='+password);
		http.complete = function(status, statusText, responseText, responseXML) {
			results = responseText.split('|');
			if (results[0] == 'true')
			{
				loggedIn = true;
				user_name = results[2];
				messages = '';
				login_rights = new login_privilege(results[3]);
			}
			else
			{
				loggedIn = false;
				messages = 'invalid login/password';
			}
				showLogin();
		} //end of function complete
	}
}

function footer() {
footer_txt = '| &copy;Infews |';
	
	// Part of script from the thickbox to reinitiate the link to the footer
			$("SPAN#login_footer").empty().append(footer_txt);
			$("a.thickbox").click(function(){
			  var t = this.title || this.innerHTML || this.href;
			  TB_show(t,this.href);
			  this.blur();
			  return false;
			});
}
function continuity_page() {
		if (doc_selected != '' &&  doc_selected != 0) {compose_document('view', doc_selected); return;}
		if (search_selected !='' ) {search_action(search_selected, 'left'); return;}
		if (doc_selected == '' && search_selected == '' && category_selected == '' && indexterm_selected == '') {compose_page('home.php','centrecontent');return;}
		// browsing continuity is made from the left() function
}

function showLogin() {
	if (messages =='') {
		// Successfull Login
		//load the left menu
		left_menu();
		admin_menu();
		footer();
		continuity_page();
		TB_remove();
		return(false);
		
	} else {
		image('off',messages, 'login');
	
	}
}
function login_privilege(obj) {
	var obj_array=new Array();
	obj_array = obj.split(',');
	
	this.document_privilege = obj_array[0];
	this.category_privilege = obj_array[1];
	this.indexterm_privilege = obj_array[2];
	this.author_privilege = obj_array[3];
}


