/* #@VER=1@# */
//Es redefineix la classe String afegint la funció encodeHTML() //es substitueixen els carácters especials de l'HTML per els seus codis per a què es mostrin correctament 
String.prototype.encodeHTML = function() {
	var s = this;
	
	// & ampersand
	s = s.replace(/&/gi, "&amp;");
	// " double quotation
	s = s.replace(/"/gi, "&quot;");
	// ' single quotation
	s = s.replace(/'/gi, "&#39;");
	// < less-than
	s = s.replace(/</gi, "&lt;");
	// > greater-than
	s = s.replace(/>/gi, "&gt;");
	
	return s;
}

// Funciones genericas
function includeCss( sFtx ){
	document.write('<link href="' +sFtx +'" type="text/css" rel="stylesheet">');
}

function includeJS( sFtx ){
	document.write(unescape('%3Cscript language="JavaScript1.2" src="' +sFtx +'" type="text/javascript"%3E%3C/script%3E'));
}

function replaceCodes( sLabel) {
    var tmp;
    tmp = new RegExp("\\* [A-Z]+\\[[0-9]+\\] \\*","gi");
		sLabel = sLabel.replace(tmp,function(str,n){var str=str.substring(2,str.length-2);return eval('msg.'+str);});
		document.write(sLabel);
}

function replaceCodes2( sLabel) {
    var tmp;
    tmp = new RegExp("\\* [A-Z]+\\[[0-9]+\\] \\*","gi");
		sLabel = sLabel.replace(tmp,function(str,n){var str=str.substring(2,str.length-2);return eval('msg.'+str);});
		return sLabel;
}

function replaceAppCodes(){
	// Inicio gestión cambio literales
	var tmp,contentBody;
	contentBody = document.body.innerHTML;
	// reemplazar literales
	if(msg instanceof Array){
		tmp = new RegExp("\\* [0-9]+ \\*","gi");
		contentBody = contentBody.replace(tmp,function(str,n){var num=str.substring(2,str.length-2);return msg[num];});
	}
	else {
		tmp = new RegExp("\\* [A-Z]+\\[[0-9]+\\] \\*","gi");
		contentBody = contentBody.replace(tmp,function(str,n){var str=str.substring(2,str.length-2);return eval('msg.'+str);});
	}
	//cambiar directorios
	tmp = new RegExp("\\* (base|img|css|xsl|js) \\*","gi");
	contentBody = contentBody.replace(tmp,function(str,n){var str=str.substring(2,str.length-2);return eval('app.'+str);});	
	//
	var qs=window.top.location.href;
  var pos=qs.indexOf("task=edit",0);
	if(pos==-1){
   document.body.innerHTML = contentBody;
	}	
	document.body.style.display = "inline";
	
	resizeContent();	
}

function resizeContent(){
	var oContent = document.getElementById("workarea");
	var oHeader = document.getElementById("header");
	var oButtonbar = document.getElementById("buttonBar");
	
	if(oContent){
		hTotal = document.body.clientHeight;
		if(oHeader){
			if(!oHeader.style.height){
				oHeader.style.height = 70;
			}
			hTotal -= parseInt(oHeader.style.height);
		}
		if(oButtonbar){
			if(!oButtonbar.style.height){
				oButtonbar.style.height = 30;
			}
			hTotal -= parseInt(oButtonbar.style.height);
		}
		
		if(navigator.appName.indexOf("Microsoft") != -1){
			oContent.style.height = (hTotal) + "px";
			oContent.style.width = document.body.clientWidth + "px";
		}
		else {
			if(oContent.style.padding){
				padding = 2* parseInt(oContent.style.padding);
				oContent.style.height = (hTotal - padding)+ "px";
				oContent.style.width = (document.body.clientWidth - padding) + "px";
			}
		}
				
		oContent.style.overflow = "auto";
		oContent.className = "scrollable";
	}  	
}

function loadHeader(oContainer,sXml,sXsl) {
		// ****************** CABECERA
		// supone existe el objeto ses
				try {
				  var objXML = new ActiveXObject("Microsoft.XMLDOM");
					var objXSL = new ActiveXObject("Microsoft.XMLDOM");
					objXML.async = "false";
					objXML.load( sXml );
					setAtributo(objXML,"IDIOMA",ses.idi);
					setAtributo(objXML,"UserName",ses.user);
					showWindowTitle( objXML );
					objXSL.async = "false";
					objXSL.load( sXsl );
					oContainer.innerHTML = objXML.transformNode(objXSL);
					delete objXML;
					delete objXSL;
				}
				catch(e) {
					oContainer.innerHTML = e.description;
				}
}

function getQS() {
	var Pairs, OnePair;
	var Hash = new Array();
	var direccion = document.location.href.split('#')[0];
	Pairs = direccion.split('?');
	if (Pairs.length>=2) {
		Pairs = Pairs[1].split('&');
		for(i=0; i<Pairs.length; i++) {
			OnePair = Pairs[i].split('=');
			Hash[OnePair[0]] = OnePair[1];  
		}
	}
	return Hash;
}

function setLabelValue(obj,val){
	obj.innerHTML = val;
	obj.title = val;
}

function openWin(url,name,type,w,h,extra) {
	var wingen,adjust=false,bExtra=false;
	var hProteo = 52, hDefecto = 29, hLocation = 26, hMenubar = 45, hStatus = 20, hToolbar=26, hResto = 0, hTaskbar=28, nExtra=0;

	if(typeof(extra) != "undefined"){
		bExtra = true;
		upperExtra = extra.toUpperCase();
		aExtra = upperExtra.split(",");
		for(var i=0;i<aExtra.length;i++){
			var aTmp = aExtra[i].split("=");
			if(aTmp[1] == "1"){
				nExtra++;
				switch(aTmp[0]){
					case "LOCATION":
						hResto += hLocation;
						break;
					case "MENUBAR":
						hResto += hMenubar;
						break;
					case "STATUS":
						hResto += hStatus;
						break;
					case "TOOLBAR":
						hResto += hToolbar;
						break;						
				}
			}
		}
		hResto -= nExtra;
	}
	
	if(type=="HELP" || type=="PROTEO"){
		if(h > screen.availHeight - hProteo - hDefecto - hResto){
			h = screen.availHeight - hProteo - hDefecto - hResto;
			adjust = true;
			type = (type == "HELP" ? type : "PROTEO");
		}
		if(w > screen.availWidth - 10){
			w = screen.availWidth - 10;
			adjust = true;
			type = (type == "HELP" ? type : "PROTEO");
		}
		
		if(w == 0){
			// Ajustamos tamaño width
			w = screen.availWidth - 10;
			type = (type == "HELP" ? type : "PROTEO");
		}
		if(h == 0){
			// Ajustamos tamaño height
			h = screen.availHeight - hProteo - hDefecto - hResto;
			type = (type == "HELP" ? type : "PROTEO");
		}
	}
	
	var topCenter = (screen.height - h)/2;
	var leftCenter = (screen.width - w)/2;
	
	switch(type){
		case "ORI":
			wingen = window.open(url,name,'top=0px,left=0px,width='+w+',height='+h + (bExtra ? ',' + extra : ''));
			break;
		case "CEN":
			wingen = window.open(url,name,'top=' + topCenter + ',left=' + leftCenter + ',width='+w+',height='+h + (bExtra ? ',' + extra : ''));
			break;
		case "PROTEO":
			//wingen = window.open(url,name,'top=' + (screen.availHeight - h - hDefecto - hResto) + ',left=0,width='+w+',height='+h+ (adjust ? ',scrollbars=1' : '') + (bExtra ? ',' + extra : ''));			
			wingen = window.open(url,name,'top=' + (screen.height - h - hDefecto - hResto - hTaskbar) + ',left=0,width='+w+',height='+h+ (adjust ? ',scrollbars=1' : '') + (bExtra ? ',' + extra : ''));			
			break;
		case "HELP":
			//wingen = window.open(url,name,'top=' + (screen.availHeight - h - hDefecto - hResto) + ',left=0px,width='+ w +',height='+ h + ',resizable=yes,scrollbars=yes' + (bExtra ? ',' + extra : ''));
			wingen = window.open(url,name,'top=' + (screen.height - h - hDefecto - hResto - hTaskbar) + ',left=0px,width='+ w +',height='+ h + ',resizable=yes,scrollbars=yes' + (bExtra ? ',' + extra : ''));
			break;
	}
	
	wingen.focus();
	
	return wingen;
}

// Apertura ventana visualización mensajes informativos al usuario
var winLoading = null;
var numLoading = 0;
var arrLoading = new Array();

function viewLoading(msg){
	var strMsg = "";
	var w = 75, h = 75;

	if(typeof(msg)=='undefined') {
		msg = '<img src="/loading.gif">';alert(msg);
	}

  if(numLoading == 0) {
		alert("aqui");
		var topCenter = window.screenTop + (window.document.documentElement.scrollHeight - h)/2;
		var leftCenter = window.screenLeft + (window.document.documentElement.scrollWidth - w)/2;
		
		winLoading = window.open('about:blank','Loading','top=' + topCenter + ',left=' + leftCenter + ',width='+w+',height='+h);
		
    strMsg += '<html><head><link href="" type="text/css" rel="stylesheet"><title>...............</title></head><body><table cellpadding="0" cellspacing="0" width="100%" height="100%">';
	  strMsg += '<tr><td align="center"></td></tr>';
	  strMsg += '</table></body></html>';
		
	winLoading.document.write(strMsg);
	winLoading.focus();
	} else {
		if( arrLoading[numLoading-1] != msg ) {
			winLoading.document.all("messageText").innerHTML = msg;
			winLoading.focus();
		}
  }
  
  arrLoading[numLoading] = msg;
  numLoading++;
}

function hideLoading( force ){
	if( typeof(force)!="undefined" && force ) {
		if(winLoading!=null) {
			winLoading.close();
		}
		winLoading = null;
		numLoading = 0;
		arrLoading = new Array();		
	} else {
	  numLoading--;
  	if(numLoading == 0) {
	  	winLoading.close();
	  	winLoading = null;
			arrLoading = new Array();		
		} else {
			if( arrLoading[numLoading] != arrLoading[numLoading-1] ) {
				winLoading.document.all("messageText").innerHTML = arrLoading[numLoading-1];	  
			}
		}
	}
}

function openDlgSelection( id ) {
	var urlDlg, sUrl;

	var w = 700;
	var h = 400;
	var posx = ((screen.width-w)/2);
	var posy = (screen.height-h)/2;
	if(typeof(app) == "undefined"){
		// No existe llamada a appcontext.js
		urlDlg = "/CDlgSelection-v2.html";
		sUrl = urlDlg + '?openpage&obj=' + id + '&lang=es';
	}
	else {
		urlDlg = app.html + "/CDlgSelection-v2.html";
		sUrl = urlDlg + '?openpage&obj=' + id + '&lang=' + ses.lang;
	}
		
	var windowDlg = window.open(sUrl,'CDlgSelection','width='+w+',height='+h+',left='+posx+',top='+posy+',scrollbars=auto,resizable=no');
	windowDlg.focus();
}

function close_startpage(){
	if(window.opener!=null) {
		window.close();
	}
}


