var isDOM=document.getElementById?true:false;
var isOpera=window.opera&&isDOM&&document.readyState;
var isIE=document.all&&document.all.item&&!isOpera;


function compileParamString (str, strParamName, value)
{
	if (str != "") str += ",";
	return str + strParamName + "=" + value;
}
//---------------------------------------------------------------------------
function showPopup (moduleID, title, left, top, width, height)//left=-1, right=-1, width=-1, height=-1) 
{		
	var str = "";
	if (left != -1) str = compileParamString (str, "left", left);
	if (top != -1) str = compileParamString (str, "top", top);
	if (width != -1) str = compileParamString (str, "width", width);
	if (height != -1) str = compileParamString (str, "height", height);	
	if (str != "") str += ",";
	str += "hotkeys=0,scrollbars=1,resizable=yes,menubar=0";			
	var msgWindow = window.open("index.php?sid=" + moduleID, title, str);	
	if (msgWindow != null) {
		try {
		    msgWindow.offscreenBuffering = true; 
		    msgWindow.focus ();
		    msgWindow.scrollBy (0, 0); 
		} catch (e) {;}
	}
}
//---------------------------------------------------------------------------
function showConfirm (text)
{
	return confirm (text + getJeannieSignString ());
}
//---------------------------------------------------------------------------
function showAlert (text)
{	
	alert (text + getJeannieSignString ());
}
//---------------------------------------------------------------------------
function getJeannieSignString () {
	return "\n\n----------------------------------------------------\n                                                        Jeannie";
}
//---------------------------------------------------------------------------
function applyRecords (strTemplate, arr, index) {
	var i = 0;
	var str = strTemplate;
	for (i=0;i<arr[0].length;i++) {
		str = replaceVar(str,arr[0][i],arr[index][i]);
	}
	return str;
}
//---------------------------------------------------------------------------
function getRecordsValue (arr, index, varName) {

	var toRet = '';
	var i = 0;
	for (i=0;i<arr[0].length;i++) {
		if (arr [0][i]==varName) {
			toRet=arr[index][i];
			break;
		}
	}
	return toRet;
}
//---------------------------------------------------------------------------
function replaceVar (str, varName, varValue) {

	return str.replace('{'+varName+'}',varValue).replace('{'+varName+'}',varValue).replace('{'+varName+'}',varValue);
}
//---------------------------------------------------------------------------
function indexOf (arr, str)
{
	var toRet = -1;
	var i = 0;
	for (i = 0; i < arr.length; i++)
		if (arr [i] == str) {
			toRet = i;
			break;
		}
	return toRet;
}
//---------------------------------------------------------------------------
function makeSorting (arr, indecies, str, bAscending)
{
	var columnIndex = indexOf (arr [0], str);	
	var i = 0;
	for (i = 0; i < arr.length - 1; i++)
		indecies [i] = i + 1;
	if (columnIndex != -1) {				
		for (j = 0; j < indecies.length; j++) {
			var bChanged = false;
			for (i = 0; i < indecies.length - 1; i++) {
				var val1 = arr [indecies [i]][columnIndex];
				var val2 = arr [indecies [i + 1]][columnIndex];
				if ((bAscending && val2 < val1) || (!bAscending && val2 > val1)) {
					bChanged = true;
					var oldIndex = indecies [i];
					indecies [i] = indecies [i + 1];
					indecies [i + 1] = oldIndex;
				}
			}
			if (!bChanged) break;
		}		
	}
}
//---------------------------------------------------------------------------
function onTextFocus (theControl)
{
	theControl.select();
}
//---------------------------------------------------------------------------

function getElementByAttrValue(element,attr,value)
{
var els = self.document.getElementsByTagName(element);
for(i=0;i<els.length;i++)
	{
	if (els[i].getAttribute(attr)==value && !els[i].disabled) return els[i];
	}
return false;
}

function resultAdvance(first,next,e)
{
var evt = (e) ? e : ((window.event) ? event : null);
if (evt)
	{
	if (evt.keyCode==9)
		{
		if (elem = getElementByAttrValue('input','name','PST_CTRL_'+next))
			{
			elem.focus();
			}
		else
			{
			getElementByAttrValue('input','name','PST_CTRL_'+first).focus();;
			}
		return false;		
		}
	}
}

function resultAdvanceE(first,next,e) { if (window.isIE) return resultAdvance(first,next,e); }

function onSequencedTabClicked (theControl, theTraversal, theTraversalAlt)
{
	if (event.keyCode==9) {
		if (!theTraversal.disabled)
			theTraversal.focus ();
		else if (theTraversalAlt != undefined)
			theTraversalAlt.focus ();
      	event.returnValue = false;
   	}
}

function xhr(url,options){
	this.initialize(url,options);
}

function _dummy(){};

xhr.prototype={

	initialize:function(url, options){
		this.transport = this.getTransport();
		this.postBody = options.postBody || '';
		if (options.post) {
			var str=[];
			for(var name in options.post){
				str.push(name+'='+encodeURIComponent(options.post[name]));
			}
			this.postBody=str.join('&');
			options.post=null;
		}
		this.method = options.method || 'post';
		this.onComplete = options.onComplete || null;
		this.update = options.update || '';
		this.request(url);
	},

	request:function(url){
		this.transport.open(this.method,url,true);
		this.transport.onreadystatechange=this.onStateChange.bind(this);
		if (this.method == 'post') {
			this.transport.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			if (this.transport.overrideMimeType) this.transport.setRequestHeader('Connection', 'close');
		}
		this.transport.send(this.postBody);
	},

	onStateChange:function(){
		if (this.transport.readyState == 4 && this.transport.status == 200) {
			if (this.onComplete)
				setTimeout(function(){this.onComplete(this.transport);}.bind(this), 10);
			if (this.update)
				setTimeout(function(){this.update.innerHTML = this.transport.responseText;}.bind(this), 10);
			this.transport.onreadystatechange = _dummy;
		}
	},

	getTransport:function(){
		if (window.ActiveXObject) return new ActiveXObject('Microsoft.XMLHTTP');
		else if (window.XMLHttpRequest) return new XMLHttpRequest();
		else return false;
	}
};

