/*
Onmouseover evidenzia il tab del menu e fa comparire il sottomenu
Onmouseout nasconde il sottomenu e resetta il tab
*/
var menu_selected = '';
function menu_submenu(tabnum, action){
	if(action == 'over'){
		if(menu_selected != tabnum) document.getElementById('tab' + tabnum).className = 'selected';
		document.getElementById('submenu_tab' + tabnum).className = 'showElement';
	}
	
	if(action == 'out'){
		if(menu_selected != tabnum) document.getElementById('tab' + tabnum).className = '';
		document.getElementById('submenu_tab' + tabnum).className = 'hideElement';
	}
}



function change_TAB(tabnum){
	num_tab = 3;
	//azzero gli stili di tutte le tab (num_tab)
	for(i=1;i<=num_tab;i++){
		if(document.getElementById('info_tab' + i)){
			document.getElementById('info_tab' + i).className = '';
			document.getElementById('box_info_tab' + i).className = 'hideElement';
			if(document.getElementById('box_info_tab' + i + '-in')) document.getElementById('box_info_tab' + i + '-in').className = 'hideElement';
		}
	}
	
	//seleziono tab cliccato
	document.getElementById('info_tab' + tabnum).className = 'selected';
	document.getElementById('box_info_tab' + tabnum).className = 'showElement';
	if(document.getElementById('box_info_tab' + tabnum + '-in')) document.getElementById('box_info_tab' + tabnum + '-in').className = 'showElement';
}



function changeClass(id, classe){
	document.getElementById(id).className = classe;
}


function setDisplay(id, display){
	document.getElementById(id).style.display = display;
}


/* CHANGE FONT SIZE */
var actual_css_variation = 0;
var max_css_var = 6;
var min_css_var = -2;
var styleID = 'css_genextra';
var pathstyle = 'styles/fontresize.php';

function stylesheetSet(variation){
	var l = document.getElementById(styleID); 
    l.href = pathstyle + "?vSIZE=" + variation;
}


function stylesheetChange(variation){
	if(actual_css_variation+variation <= max_css_var && actual_css_variation+variation >= min_css_var){
		css = pathstyle + "?vSIZE=" + (actual_css_variation+variation);
		var l = document.getElementById(styleID); 
        l.href = css;
		
		setCookie(styleID + '_cookie', (actual_css_variation+variation), 365);
		actual_css_variation = actual_css_variation+variation;
	}
}

function stylesheetReset(){
	css = pathstyle + "?vSIZE=0";
	var l = document.getElementById(styleID); 
      	l.href = css;
	setCookie(styleID + '_cookie', 0, 365);
	actual_css_variation = 0;
}


// Funzioni per settare/leggere i cookie
function setCookie(name, value, expdays) {   //memorizza il cookie
	var now = new Date();
	var exp = new Date(now.getTime() + (1000*60*60*24*expdays));
	document.cookie = name + "=" + escape(value) + ";" + "expires=" + exp.toGMTString() + ";" + "path=/";
}

function getCookie(name) {    //restituisce il cookie
	var cname = name + "=";
	var dc = document.cookie;
	if (dc.length > 0) {
	    var start = dc.indexOf(cname);
	    if (start != -1) {
	        start += cname.length;
	        var stop = dc.indexOf(";", start);
	        if (stop == -1) stop = dc.length;
	        return unescape(dc.substring(start,stop));
	    }
	}
	return null;
}
/* CHANGE FONT SIZE */




// crea l'oggetto per la comunicazione AJAX con il server
// compatibile con tutti i browser che supportano AJAX
function crea_http_req() {
	var req = false;
	if (typeof XMLHttpRequest != "undefined")
		req = new XMLHttpRequest();
	if (!req && typeof ActiveXObject != "undefined") {
		try {
			req=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				req=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				try {
					req=new ActiveXObject("Msxml2.XMLHTTP.4.0");
				} catch (e3) {
					req=null;
				}
			}
		}
	}

	if(!req && window.createRequest)
		req = window.createRequest();

	if (!req) alert("Your browser doesn't support AJAX");

	return req;
}



// invia i dati del form al server
function invia_dati() {
	var http_req = crea_http_req();
	
	var dati_post = "username=" +
					encodeURIComponent( document.getElementById("userid").value ) +
					"&password=" +
					encodeURIComponent( document.getElementById("psw").value );

	http_req.open('POST', 'ajax_check_login.php', true);
	http_req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	http_req.setRequestHeader("Content-length", dati_post.length);
	http_req.setRequestHeader("Connection", "close");
	http_req.send(dati_post);
	
	http_req.onreadystatechange = function() {
								  		gestisci_risposta(http_req);
									}
	
}

// recupero e gestisco la risposta inviata dal server
function gestisci_risposta(http_req) {
	if(http_req.readyState == 4) {
		var esito = http_req.responseText;
		var msg = document.getElementById('form_error');
		
		switch (esito) {
		  case '1':
		  	setDisplay('form_error', 'block');
			msg.innerHTML = 'Please enter field \'User ID\'<br />Please enter field \'Password\'';
		  break;

		 case '2':
		 	setDisplay('form_error', 'block');
			msg.innerHTML = 'Wrong \'User ID\' and/or \'Password\'';
		  break;

		  case '3':
			changeClass('login_customersarea', 'hideElement');
			changeClass('login_customerlogged', 'showElement');
		  break;

		  default:
		  	setDisplay('form_error', 'block');
			msg.innerHTML = 'Unrecognized server response: ' + esito;
		}
	}
}
