function URLEncode( plaintext )
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};

function URLDecode( encoded )
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while

   return plaintext;
};

/**
 * funciï¿½ que genera el peu de qualsevol diï¿½leg
 * @param {Object} path Ruta base per la plantilla (template)
 */
function _dialog_footer(color) {
	if(typeof(color)=='undefined') color='black';
	
	win  = '</div>';
	win += '</td>';
	win += '<td class="dialog_'+color+'_e"></td>';
	win += '</tr>';
	win += '<tr>';
	win += '<td class="dialog_'+color+'_sw"></td>';
	win += '<td class="dialog_'+color+'_s"></td>';
	win += '<td class="dialog_'+color+'_se"></td>';
	win += '</tr>';
	win += '</table>';		
	win += '</div>';
	
	return win;
}

/**
 * Funciï¿½ que genera l'encapï¿½alament de qualsevol diï¿½leg
 * @param (Object) path Ruta base per la plantilla (template)
 * @param (Object) title Tï¿½tol que tindrï¿½ la finestra
 * @param {Object} jsTop
 * @param {Object} jsLeft
 * @param {Object} jsWidth
 * @param {Object} jsHeight
 */
function _dialog_header(title, jsTop,jsLeft,jsWidth,jsHeight,color,dv,showclose) {
	if (typeof(color) == 'undefined') {
		color = 'black';
	} else {
		if(color=='') color='black';
	}
	
	if (typeof(dv) == 'undefined') {
		dv = 'divJSDialogContent';
	} else {
		if(dv=='') dv = 'divJSDialogContent';
	}
	
	if(typeof(showclose)=='undefined') {
		showclose = true;
	}
	mtw = jsWidth - 60;
	tl  = mtw / 6;
	title = URLDecode(title);
	if (title.length > tl) {
		title = title.substr(0, tl - 1) + '...';
	}
	win  = '<div style="position: absolute; left: '+jsLeft+'px; top: '+jsTop+'px" id="jsWindow">';
	win += '<table class="text textBlanc" style="width:'+jsWidth+'px;height:'+jsHeight+'px;" cellpadding="0" cellspacing="0">';
	win += '<tr style="cursor:pointer;" onmousedown="dragStart(event, \'jsWindow\')">';
	win += ' <td class="dialog_'+color+'_nw"></td>';
	win += ' <td class="dialog_'+color+'_n3">';
	win += '    <div style="float:left;" class="dialog_'+color+'_n1"><p style="margin-top:20px;">&nbsp;'+title+'</p></div>';
	win += '    <div style="float:left;" class="dialog_'+color+'_n2">&nbsp;</div>';
	if(showclose) win += '    <div class="dialog_'+color+'_close_down" id="btnCloseDialog" onclick="closeDialog();" onMouseOver="rollover(this);" onMouseOut="rollout(this);"></div>';
	win += ' </td>';
	win += ' <td class="dialog_'+color+'_ne"></td>';
	win += '</tr>';
	win += '<tr>';
	win += ' <td class="dialog_'+color+'_w"></td>';
	win += ' <td style="vertical-align: top;text-align:center;height:'+(jsHeight - 20)+'px">';
	win += '   <div id="'+dv+'" style="background:#ffffff;color:#000000;width:'+(jsWidth-36)+'px;height:100%;">';
	
	return win;
}

/**
 * Tanca el diï¿½leg
 */
function closeDialog() {
	fadeOutScreen();
	el = document.getElementById( 'jsWindow' );
	js = document.getElementById( 'jsDialog' );
	if( el ) el.parentNode.removeChild(el);
	if( js ) js.parentNode.removeChild(js);
}

function getPageSizeWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll;
}

/**
 * Enfosqueix la finestra
 */
function fadeInScreen() {
	ps = getPageSizeWithScroll();
	div = "<div class=\"fade_screen\" style=\"width:"+ps[0]+"px;height:"+ps[1]+"px\" id=\"_fade_div\">&nbsp;</div>";
	var newcontent = document.createElement('div'); 
	newcontent.innerHTML = div; 
	document.getElementsByTagName("body")[0].appendChild(newcontent);
	newcontent.style.top = '0px';
	newcontent.style.left = '0px';
}

/**
 * Aclareix la finestra
 */
function fadeOutScreen() {
	el = document.getElementById( '_fade_div' );
	if( el ) {
		el.parentNode.removeChild(el);
	}
}

/**
 * Obre un cuadre de diï¿½leg mostrant un contingut obtingut via AJAX
 */
function openAjaxDialog(param) {
	dw = 460;
	dh = 430;
	
	if(typeof(param.width)!='undefined'){
		dw = param.width;		
	}
	
	if(typeof(param.height)!='undefined'){
		dh = param.height;		
	}
	
	var callback = {
		success: function(o) {
			el = document.getElementById('divJSDialogContent');
			el.innerHTML = o.responseText;
		},
		upload: function(o) {
			this.success(o);
		}
	}
	
	if(typeof(param.showclose)=='undefined') param.showclose = true;
	
	openDialog( param.title, '<div style="left:'+(dw-20)+'px;top:'+(((dh-50)/2)-15)+'px;"><img src="/templates/bambulectorv1/img/loading.gif" width="41" height="39" /></div>', dw, dh, param.color, param.showclose );
	var cObj = YAHOO.util.Connect.asyncRequest('GET', param.url, callback );
}

/**
 * Obre un cuadre de diï¿½leg mostrant un contingut obtingut via AJAX
 */
/*function openAjaxDialog(title, url, color, showclose) {
	dw = 460;
	dh = 430;
	var callback = {
		success: function(o) {
			el = document.getElementById('divJSDialogContent');
			el.innerHTML = o.responseText;
		},
		upload: function(o) {
			this.success(o);
		}
	}
	openDialog( title, '<div style="left:'+(dw-20)+'px;top:'+(((dh-50)/2)-15)+'px;"><img src="/templates/bambulectorv1/img/loading.gif" width="41" height="39" /></div>', dw, dh, color, showclose );
	var cObj = YAHOO.util.Connect.asyncRequest('GET', url, callback );
}*/

/**
 * Obre un cuadre de diï¿½leg mostrant una alerta
 */
function openAlert(msg) {
	dw = 350;
	dh = 150;
	cntnt = '<table style="width:100%;height:100%"><tr><td style="text-align:center"><img src="/templates/bambulectorv1/img/ico/ico_alerta.gif"></td></tr><tr><td class="pContentText" style="text-align:center">'+msg+'</td></tr></table>';
	openDialog( 'Alerta', cntnt, dw, dh, 'black' );
}

/**
 * Obre una finestra modal
 * @param (Object) path Ruta base per la plantilla (template)
 * @param {Object} title
 * @param {Object} content
 * @param {Object} width
 * @param {Object} height
 */
function openDialog( title, content, jsWidth, jsHeight, color, showclose ) {
	//Obtenim la posiciï¿½ per a centrar el diï¿½leg a la pantalla
    var ua = navigator.userAgent.toLowerCase();
    var msie = ua.indexOf("msie ")
    if ( msie > 0 ) {      // If Internet Explorer, return version number
       var msieVersion = parseInt (ua.substring (msie+5, ua.indexOf (".", msie )));
    } else {                 // If another browser, return 0
       var msieVersion = 0;
    }
       
	if (msie > 0) {
 		winW = document.body.offsetWidth;
  		winH = document.documentElement.clientHeight;  //IE6+ Strict
  		if(winH==0) winH = document.body.offsetHeight; //IE5-7 no strict
 	} else {
 		winW = window.innerWidth;
  		winH = window.innerHeight;
 	}
 
	jsLeft = (winW - parseInt(jsWidth)) / 2;
	if( (msieVersion==0) || (msieVersion >= 7) ) {
		jsTop = ( (winH - parseInt(jsHeight)) / 2 ) + document.documentElement.scrollTop;
	} else {
		jsTop = ( (winH - parseInt(jsHeight)) / 2 ) + document.documentElement.scrollTop;
	}
	
	//Dibuixem el diàleg
	if (typeof(color) == 'undefined') {
		color = 'black';
	}
	else {
		if ((color == null) || (color == '')) 
			color = 'black';
	}
	
	win = _dialog_header(title, jsTop, jsLeft, jsWidth, jsHeight, color, '', showclose );
	win += unescape(content);
	win += _dialog_footer(color);
	
	fadeInScreen();
	var newcontent = document.createElement('div'); 
	newcontent.innerHTML = win; 
	document.getElementsByTagName("body")[0].appendChild(newcontent);
	newcontent.style.top = jsTop + 'px';
	newcontent.style.left = jsLeft + 'px';		
}

function openExtVideoDialog(title,width,height,embed,codrecurs){
	incStats(codrecurs);
	
	if( (width==0) || (width=='') ) width=800;
	if( (height==0)|| (height=='')) height=600;
	
	openDialog( title, URLDecode(embed), parseInt(width)+36, parseInt(height) );
}
/**
 * Obre un cuadre de diï¿½leg per a mostrar el reproductor de Vï¿½deo
 * @param {Object} title
 * @param {Object} file
 */
function openFLVDialog(title, file, codrecurs) {
	incStats(codrecurs);
	
	content = '<object type="application/x-shockwave-flash" data="/flash/player_video.swf?video='+file+'&titol='+title+'" width="395" height="360" id="RVideo" style="border:0px;margin:0px">';
	content+= '	<param name="movie" value="/flash/player_video.swf?video='+file+'&titol='+title+'" />';
	content+= '	<param name="wmode" value="transparent" />';
	content+= '</object>';	
	
	openDialog( title, content, 425, 380 );
}

/**
 * Obre un cuadre de diï¿½leg per a mostrar una imatge
 * @param {Object} title
 * @param {Object} image
 */
function openIMGDialog( title, image, codrecurs ) {
	incStats(codrecurs);

	margin_top =72;
	margin_left=36;
	
	//mides
	width=800;
	height=600;
	
	if (navigator.appName.indexOf("Microsoft")!=-1) {
	  maxH = document.body.offsetHeight - 20 /*- (margin_top*2)*/;
	} else {
	  maxH = window.innerHeight - 16 /*- (margin_top*2)*/;
	}	
	
	if (height > maxH) {
		ratio  = maxH/height;
		height = maxH;
		width  = Math.round(width*ratio);
	}
		
	content = '<div height="'+height+'"><img src="'+image+'" alt="'+title+'" style="max-width:'+width+'px;width:expression(this.width>'+width+' ? \''+width+'px\' : true);max-height:'+height+'px;height:expression(this.height>'+height+'?\''+height+'px\':true);" /></div>';
	openDialog( title, content, parseInt(width)+margin_left, parseInt(height)+margin_top );
}

/**
 * Obre un cuadre de diï¿½leg per a mostrar el reproductor MP3
 * @param {Object} title
 * @param {Object} file
 */
function openMP3Dialog(title, file, codrecurs) {
	//incStats(codrecurs);
	
	content = '<object data="/flash/webplayer/webplayer.swf" type="application/x-shockwave-flash" width="240" height="64">';
	content+= '<param name="movie" value="/flash/webplayer/webplayer.swf"></param>';
	content+= '<param name="menu" value="false"></param>';
	content+= '<param name="scale" value="noscale"></param>';
	content+= '<param name="bgcolor" value="#171717"></param>';
	content+= '<param name="flashvars" value="src='+file+'&amp;autostart=yes&amp;loop=no&amp;debug=no" />';
	content+= '<embed src="/flash/webplayer/webplayer.swf" quality="high" type="application/x-shockwave-flash" width="240" height="64" flashvars="src='+file+'&amp;autostart=yes&amp;loop=no&amp;debug=no"></embed>';
	content+= '</object>';
	openDialog( title, content, 276, 64, 'black' );
	return false;
}

/**
 * Obre un diï¿½leg per a mostrar un SWF
 * @param {Object} file
 */
function openSWFDialog(title, file, width, height, base_path, codrecurs) {
	incStats(codrecurs);
	
	lastSlash = file.lastIndexOf("/");
	base_path = file.substring(0,lastSlash);
	
	margin_top =50;
	margin_left=36;
	
	//mides
	if( (width==0) || (width=='') ) width=800;
	if( (height==0)|| (height=='')) height=600;
	
	if (navigator.appName.indexOf("Microsoft")!=-1) {
	  maxH = document.body.offsetHeight - 20 - (margin_top*2);
	} else {
	  maxH = window.innerHeight - 16 - (margin_top*2);
	}	
	
	if (height > maxH) {
		ratio  = maxH/height;
		height = maxH;
		width  = Math.round(width*ratio);
	}
	
	content = '<div>';
	content +=  '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0"  width="'+width+'" height="'+height+'">';
    content += '<param name="movie" value="'+file+'">';
    content += '<param name="quality" value="high">';
    content += '<param name="base" value="'+base_path+'">';
    content += '<embed src="'+file+'" quality="high" base="'+base_path+'" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+width+'" height="'+height+'"></embed>';
	content += '</object>';
	content += '</div>';
	content += '<div style="text-align:center;margin-top:10px;"><a href="#" onclick="window.open(\''+file+'\');return false;">'+strNovaFinestra+'</a></div>';
	
	openDialog( title, content, parseInt(width)+margin_left, parseInt(height)+margin_top );
}

/**
 * Obre l'enllaï¿½ indicat en una nova finestra
 * @param {Object} url
 * @param {Object} codrecurs
 */
function openLink( url, codrecurs ) {
	incStats(codrecurs);
	window.open(url,"_blank");
}
