/*
* 		Extens solucions S.L.
*		JS File common.js
*/

var toolimg=new Array();
var extoolbar = new Array();
var extoolbarp = new Array();
var _lang = null;
var _sec = 1;
var _loading = 0;
var _history = 0;

function init(lang){

	if(lang != "es" && lang != "ca")
		_lang = "ca";
	else
		_lang = lang;

	toolimg[0]=new Image(9,8);
	toolimg[0].src= "../_img/quadrat.gif";
	toolimg[1]=new Image(9,8);
	toolimg[1].src= "../_img/fletxa_fort.jpg";
	toolimg[2]=new Image(9,8);
	toolimg[2].src= "../_img/fletxa_fluix.png";

	extoolbar[0]=new Image(138,19);
	extoolbar[0].src= "../_img/_entorn/barra/"  + _lang + "/1.jpg";
	extoolbar[1]=new Image(133,19);
	extoolbar[1].src= "../_img/_entorn/barra/"  + _lang + "/2.jpg";
	extoolbar[2]=new Image(108,19);
	extoolbar[2].src= "../_img/_entorn/barra/"  + _lang + "/3.jpg";
	extoolbar[3]=new Image(82,19);
	extoolbar[3].src= "../_img/_entorn/barra/"  + _lang + "/4.jpg";
	extoolbar[4]=new Image(143,19);
	extoolbar[4].src= "../_img/_entorn/barra/"  + _lang + "/5.jpg";
	extoolbar[5]=new Image(102,19);
	extoolbar[5].src= "../_img/_entorn/barra/"  + _lang + "/6.jpg";
	
	extoolbarp[0]=new Image(138,19);
	extoolbarp[0].src= "../_img/_entorn/barra/"  + _lang + "/1p.jpg";
	extoolbarp[1]=new Image(133,19);
	extoolbarp[1].src= "../_img/_entorn/barra/"  + _lang + "/2p.jpg";
	extoolbarp[2]=new Image(108,19);
	extoolbarp[2].src= "../_img/_entorn/barra/"  + _lang + "/3p.jpg";
	extoolbarp[3]=new Image(82,19);
	extoolbarp[3].src= "../_img/_entorn/barra/"  + _lang + "/4p.jpg";
	extoolbarp[4]=new Image(143,19);
	extoolbarp[4].src= "../_img/_entorn/barra/"  + _lang + "/5p.jpg";
	extoolbarp[5]=new Image(102,19);
	extoolbarp[5].src= "../_img/_entorn/barra/"  + _lang + "/6p.jpg";

	var lloc = window.location.href.split("#");
	if(lloc[1]){
		section = lloc[1].split("_");
		chToSection(parseInt(section[0]), parseInt(section[1]), parseInt(section[2]));
	}else
		page('st');
}

function showLoading(show) {
	document.getElementById('tel').style.visibility = show ? "visible" : "hidden";
}

function page(){
	var args = page.arguments;
	var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP"): new XMLHttpRequest();
	switch (args[0]){
		case "lp":
			if (x){
				_loading++;
				x.onreadystatechange = function(){
					if(_loading != 0) showLoading(true);
					if (x.readyState == 4 && x.status == 200){
						el = document.getElementById(args[2]);
						el.innerHTML = x.responseText;
						_loading--;
						if(_loading == 0) showLoading(false);
					}
				}
				x.open("GET", args[1], true);
				x.send(null);
			}
		break;

		case "lsp":
			if (x){
				_loading++;
				if(_loading != 0) showLoading(true);
				x.onreadystatechange = function(){
					if (x.readyState == 4 && x.status == 200) {
						el = document.getElementById(args[2]);
						el.innerHTML = x.responseText;
						_loading--;
						page('lp', args[3], args[4]);
					}
				}
				x.open("GET", args[1], true);
				x.send(null);
			}
		break;

	case "st":
		page('lp', 'portada.php', 'inhalt');
		setItemBar(0);
		break;
	}
	_history++;
}

function setItemBar(x){
	for(i=1; i <=4; i++){
		if(x == i){
			document.getElementById('_'+i).src  = toolimg[1].src;
			document.getElementById('a'+i).className = 'mitem1';
		}else{
			document.getElementById('_'+i).src  = toolimg[0].src;
			document.getElementById('a'+i).className = 'mitem0';
		}
	}
}

function chTabEntorn(x){
	if(x != _sec){
		for(z=1;z<=6;z++){
			if(z == x) document.getElementById('_x'+z).src  = extoolbarp[z-1].src;
			if(z == _sec) document.getElementById('_x'+z).src  = extoolbar[z-1].src;
		}
		_sec = x;
		if(x == 1){
			page('lp', 'productes/_collaboratiu.php', 'subinhalt');
		}else if(x == 2) {
			page('lp', 'productes/_correu.php', 'subinhalt');
		}else if(x == 3) {
			page('lp', 'productes/_fax.php', 'subinhalt');
		}else if(x == 4) {
			page('lp', 'productes/_tallafocs.php', 'subinhalt');
		}else if(x == 5) {
			page('lp', 'productes/_domini.php', 'subinhalt');
		}else if(x == 6) {
			page('lp', 'productes/_vpn.php', 'subinhalt');
		}
	}
}

function chToSection(x, y, z){
	if(x == 1){
		setItemBar(x);
		page('lp', 'empresa/empresa.php', 'inhalt');
	}else if(x == 2){ 
		setItemBar(x);
		switch(y){
			case 1:
				_sec = z;
				if(z == 1){
					page('lsp', 'serveis/suport.php', 'inhalt', 'serveis/suport/re.php', 'subinhalt');
				}else if(z == 2){
					page('lsp', 'serveis/suport.php', 'inhalt', 'serveis/suport/xc.php', 'subinhalt');
				}else if(z == 3){
					page('lsp', 'serveis/suport.php', 'inhalt', 'serveis/suport/ce.php', 'subinhalt');
				}else if(z == 4){
					page('lsp', 'serveis/suport.php', 'inhalt', 'serveis/suport/st.php', 'subinhalt');
				}else if(z == 5){
					page('lsp', 'serveis/suport.php', 'inhalt', 'serveis/suport/ar.php', 'subinhalt');
				}else if(z == 6){
					page('lsp', 'serveis/suport.php', 'inhalt', 'serveis/suport/pm.php', 'subinhalt');
				}else{
					page('lsp', 'serveis/suport.php', 'inhalt', 'serveis/suport/re.php', 'subinhalt');
					_sec = 1;
				}
				break; 
			case 2:
				page('lp', 'serveis/formacio.php', 'inhalt');
				break; 
			case 3:
				page('lp', 'serveis/disseny.php', 'inhalt');
				break; 
			case 4:
				page('lp', 'serveis/pweb.php', 'inhalt');
				break; 
			default:
				page('lsp', 'serveis/suport.php', 'inhalt', 'serveis/suport/re.php', 'subinhalt');
		}
	}else if(x == 3){
		setItemBar(x);
		switch(y){
			case 1:
				page('lp', 'productes/equipspc.php', 'inhalt');
				break; 
			case 2:
				page('lp', 'productes/periferics.php', 'inhalt');
				break; 
			case 3:
				_sec = z;
				if(z == 1){
					page('lsp', 'productes/entornex.php?bar=1', 'inhalt', 'productes/_collaboratiu.php', 'subinhalt');
				}else if(z == 2){
					page('lsp', 'productes/entornex.php?bar=2', 'inhalt', 'productes/_correu.php', 'subinhalt');
				}else if(z == 3){
					page('lsp', 'productes/entornex.php?bar=3', 'inhalt', 'productes/_fax.php', 'subinhalt');
				}else if(z == 4){
					page('lsp', 'productes/entornex.php?bar=4', 'inhalt', 'productes/_tallafocs.php', 'subinhalt');
				}else if(z == 5){
					page('lsp', 'productes/entornex.php?bar=5', 'inhalt', 'productes/_domini.php', 'subinhalt');
				}else if(z == 6){
					page('lsp', 'productes/entornex.php?bar=6', 'inhalt', 'productes/_vpn.php', 'subinhalt');
				}else{
					page('lsp', 'productes/entornex.php?bar=1', 'inhalt', 'productes/_collaboratiu.php', 'subinhalt');
					_sec = 1;
				}
				break; 
			case 4:
				page('lp', 'productes/gnulinux.php', 'inhalt');
				break; 
			default:
				page('lp', 'productes/equipspc.php', 'inhalt');
		}
	}else if(x ==4){
		setItemBar(x);
		page('lp', 'contactar/contactar.php', 'inhalt');
	}else{
		page('st');
	}
}

function clean(){
	document.forms[0].nom.value = "";
	document.forms[0].mail.value = "";
	document.forms[0].missatge.value = "";
}

function sendData(){
	var f = document.forms[0];
	var url ="../_php/mail.php";
	var params =  "?nom=" +  encodeURIComponent(f.nom.value) + "&mail=" + encodeURIComponent(f.mail.value);
	params += "&missatge=" + encodeURIComponent(f.missatge.value);
	page('lp', url + params, 'inhalt');
}

function dopop(_file,_width,_height){
	var newWindow= open(_file,'newWin','toolbar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+_width+',height='+_height);
}

function doamp(_file,_width,_height,_title){
	newWindow=window.open(_file,'newWin','toolbar=no,scrollbars=no,resizable=no,copyhistory=no,width='+_width+',height='+_height);
	newWindow.document.write('<html><head><title>'+_title+'<\/title><\/head><body background="'+_file+'"><\/body><\/html>');
	newWindow.resizeBy(_width-newWindow.document.body.clientWidth, _height-newWindow.document.body.clientHeight);
	newWindow.focus()
}

function show(sh, id){
	document.getElementById(id).style.visibility = sh ? "visible" : "hidden";
}