
//	Author:	Mikael Simonsson <mikael@bluemist.se>

//	Copyright (C) 2003-2008 Blue Mist, bluemist.se
//	This file may not be redistributed in whole or part. A license from Blue Mist
//	is required for each instance of the application which this file is a part of.


var blueUtil = {

	trimLeft: function (s) { return s.replace(/^\s+/, ''); },
	trimRight: function (s) { return s.replace(/\s+$/, ''); },
	trim: function (s) { return this.trimLeft(this.trimRight(s)); },

	openWindow: function (sURL, sName, iWidth, iHeight, sResizable, sStatus)
	{
		var sOptions = '';
		sOptions += 'toolbar=0,location=0,directories=0,menubar=0';
		sOptions += ',status=' + sStatus + ',scrollbars=' + sResizable +',resizable='  + sResizable;
		sOptions += ',width=' + iWidth + ',height=' + iHeight;

		var objWin = window.open(sURL, sName, sOptions);
		if (objWin != null)
		{
			objWin.moveTo((screen.width-iWidth)/2,(screen.height-iHeight)/2);
			objWin.focus();
		}
	},

	selectProduct: function (o)
	{
		if ( o.length > 0 && o.selectedIndex > 0 && '' == o.options[o.selectedIndex].value )
			o.selectedIndex = o.selectedIndex - 1;
	},

	toggleGroup: function (iId, sTemplatePath)
	{
		var oUl = 0;
		var oImg = 0;

		if ( document.getElementById )
		{
			oUl = document.getElementById('groupItem' + iId);
			oImg = document.getElementById('groupImage' + iId);
		}
		else if ( document.all )
		{
			oUl = document.all['groupItem' + iId];
			oImg = document.all['groupImage' + iId];
		}

		if ( oUl && oImg )
		{
			if ( oUl.style.display == 'none')
			{
				oUl.style.display = 'block';
				oImg.src = sTemplatePath + '/i/minus.gif';
			}
			else
			{
				oUl.style.display = 'none';
				oImg.src = sTemplatePath + '/i/plus.gif';
			}
		}

		return false;
	},

	toggleDisplay: function (iId)
	{
		var o = 0;

		if ( document.getElementById )
			o = document.getElementById(iId);
		else if ( document.all )
			o = document.all[iId];

		if ( o )
		{
			if ( o.style.display == 'none' )
				o.style.display = 'block';
			else
				o.style.display = 'none';
		}
	},

	selectGo: function (o, sPathRelativeToRoot)
	{
		if ( o.length > 0 && o.selectedIndex != -1 && o.options[o.selectedIndex].value != '' )
			self.location.href = sPathRelativeToRoot + o.options[o.selectedIndex].value;
	},

	decodeBase64: function (sInputRaw)
	{
		var sOutput = "";
		var sBase64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

		// Strip invalid chars
		var sInput = sInputRaw.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		var iInputLength = sInput.length;

		// We need at least 4 characters and the string must be in parts of 4
		if ( iInputLength >= 4 && iInputLength % 4 == 0 )
		{
			var i = 0;

			var c1, c2, c3 = "";
			var e1, e2, e3, e4 = "";

			do
			{
				e1 = sBase64Chars.indexOf(sInput.charAt(i++));
				e2 = sBase64Chars.indexOf(sInput.charAt(i++));
				e3 = sBase64Chars.indexOf(sInput.charAt(i++));
				e4 = sBase64Chars.indexOf(sInput.charAt(i++));

				c1 = ( e1 << 2 ) | ( e2 >> 4 );
				c2 = ( ( e2 & 15 ) << 4 ) | ( e3 >> 2 );
				c3 = ( ( e3 & 3 ) << 6 ) | e4;

				sOutput = sOutput + String.fromCharCode(c1);

				if ( e3 != 64 ) sOutput = sOutput + String.fromCharCode(c2);
				if ( e4 != 64 ) sOutput = sOutput + String.fromCharCode(c3);

			} while ( i < iInputLength );
		}

		return sOutput;
	},

	emProtection: function (sInput64, sStyle)
	{
		var sOutput = "";
		var sEm = this.decodeBase64(sInput64);

		sOutput += "<" + "a" + " h" + "ref=\"";
		sOutput += "m" + "ail" + "to" + ":";
		sOutput += sEm;
		sOutput += "\"";

		if ( sStyle.length > 0 )
			sOutput += " style=\"" + sStyle + "\"";

		sOutput += ">" + sEm;
		sOutput += "</" + "a>";

		return sOutput;
	},

	getArrayIndexFromValue: function (a, v)
	{
		var iIndex = null;
		var iLength = a.length;
		for ( var i = 0; i < iLength; ++i )
		{
			if ( a[i] == v )
			{
				iIndex = i;
				break;
			}
		}
		return iIndex;
	},

	getQueryString: function (aQk, aQv)
	{
		var aQs = [];

		var iLength = aQk.length;
		if ( aQv.length == iLength )
		{
			for ( var i = 0; i < iLength; ++i )
				aQs.push(aQk[i] + "=" + encodeURIComponent(aQv[i]).replace(/\'/g, '%27'));
		}

		return aQs.join("&");
	},

    escapeHtml: function (s) {
        return s.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    },

	addClassName: function(o, s)
	{
		if ( o.className )
		{
			if ( this.hasClassName(o, s) ) return;
			o.className += ( o.className ? " " : "" ) + s;
		}
		else
		{
			o.className = s;
		}
	},

	removeClassName: function(o, s)
	{
		if ( o.className )
		{
			var aClasses = o.className.split(' ');
			var iLength = aClasses.length;
			var sClassName = '';
			for ( var i = 0; i < iLength; ++i )
			{
				if ( aClasses[i] != s )
					sClassName += aClasses[i] + ' ';
			}
			o.className = this.trimRight(sClassName);
		}
	},

	hasClassName: function(o, s)
	{
		if ( o.className )
			return new RegExp("(^|\\s)" + s + "(\\s|$)").test(o.className);

		return false;
	},

	handleParaTab: function (iCurrentId, aAllIds)
	{
		var iLength = aAllIds.length;
		var i, iId, oP, oT;
		for ( i = 0; i < iLength; ++i )
		{
			iId = aAllIds[i];

			if ( document.getElementById )
			{
				oP = document.getElementById('para-' + iId);
				oT = document.getElementById('para-tab-' + iId);
			}
			else if ( document.all )
			{
				oP = document.all['para-' + iId];
				oT = document.all['para-tab-' + iId];
			}

			if ( oP && oT )
			{
				if ( aAllIds[i] != iCurrentId )
				{
					oP.style.display = 'none';
					this.removeClassName(oT, 'active');
				}
				else
				{
					oP.style.display = 'block';
					this.addClassName(oT, 'active');
				}
			}
		}
	}
};
