var newwin = new Array ();  // this array stores all the references to the new windows opened by this module
var auxwin;                 // this variable is set global to be passed on to the setTimeout command

function _window (name, reference) {
	this.name = name;
	this.ref  = reference;
}

function _numofwindows () {
	return (newwin.length);
}

function _getwinrefbyindex (index) {
	return (newwin[index].ref);
}

function _getwinrefbyname (winname) {
	var index;
	index = _getwinindex (winname);
	if (index != -1) { return (newwin[index].ref); }
	return 0;
}

function _getwinnamebyindex (index) {
	return (newwin[index].name);
}

function _getwinindex (winname) {
	var i;
	for (i = 0; i < newwin.length; i++) {
		if (newwin[i].name == winname) { return i; }
	}
	return -1;
}


function _browserFixQ822925(urlStr){
	var posStr=-1;
	var posAuxStr=-1;
	var auxStr="";

	//if(top.j_location == null){
		top.j_location = String(top.location.protocol) + "//" + String(top.location.hostname) + "/sian/";
	//}
	if(urlStr.indexOf('://') == -1) urlStr = top.j_location + urlStr;
	
	posStr = urlStr.indexOf('/'); // http:_/servername/sian/directoryName
	posStr = urlStr.indexOf('/', posStr+1); // http:/_servername/sian/directoryName
	posStr = urlStr.indexOf('/', posStr+1); // http://servername_sian/directoryName
	posStr = urlStr.indexOf('/', posStr+1); // http://servername/sian_directoryName
	posAuxStr = urlStr.indexOf('/', posStr+1); // http://servername/sian/directoryName_
	if (posStr != -1 && posAuxStr != -1) auxStr = urlStr.substring(posStr+1, posAuxStr);
	posStr = urlStr.indexOf('/', posAuxStr+1); // http://servername/directoryName/directoryName_
	
	
	if(posStr != -1 && posAuxStr != -1 && urlStr.substring(posAuxStr+1, posStr) == auxStr)
		urlStr = urlStr.substring(0, posAuxStr) + urlStr.substring(posStr, urlStr.length);
	
	return urlStr;

}


function _launchwin (winurl, winname, winfeatures, position) {
	var index;
	
	winurl = _browserFixQ822925(winurl);
	
	if (position != "") {

		var width, height, wintop, winleft;
		var a, b;
		var redigits = new RegExp("[^0-9]");
		var aux;

		a = winfeatures.indexOf ("width");
		for (a = a + 1; redigits.test (winfeatures.charAt (a)); a++);
		for (b = a; ((b < winfeatures.length) && (!redigits.test (winfeatures.charAt (b)))); b++);
		width = winfeatures.substring (a,b);

		a = winfeatures.indexOf ("height");
		for (a = a + 1; redigits.test (winfeatures.charAt (a)); a++);
		for (b = a; ((b < winfeatures.length) && (!redigits.test (winfeatures.charAt (b)))); b++);
		height = winfeatures.substring (a,b);

		if (position == "center") {
			winleft = (screen.width - width) / 2.0;
			wintop = (screen.height - height) / 2.0;
		} else if (position == "supesq") {
			winleft = 40;
			wintop = 40;
		} else if (position == "supdir") {
			winleft = (screen.width - width) - 40;
			wintop = 40;
		}

		aux = ",top=" + wintop + ",left=" + winleft;
		winfeatures = winfeatures.concat (aux);
	}

	index = _getwinindex (winname);
	auxwin = window.open (winurl, winname, winfeatures);
	if (index == -1) { // window does not exist
		newwin[newwin.length] = new _window (winname, auxwin);
	} else {
		newwin[index].ref = auxwin;
	}

	if (javascript_version > 1.0) {
		setTimeout("auxwin.focus();", 250);
	}
}

function _closewin (winname) {
	var index;
	if (winname == "all") {
		for (index = 0; index < newwin.length; index++) {
			if (newwin[index].ref && !newwin[index].ref.closed) {
				newwin[index].ref.close();
			}
		}
	} else {
		index = _getwinindex(winname);
		if (index != -1) {
			if (newwin[index].ref && !newwin[index].ref.closed) {
				newwin[index].ref.close();
			}
		}
	}
}

function _isopened (winname) {
	var index;
	index = _getwinindex(winname);
	if (index != -1) {
		if (newwin[index].ref && !newwin[index].ref.closed) {
			return 1;
		}
	}
	return 0;
}

// This function calls recursively the parent window so that it gets the highest level window
// The highest level window recursively targets its descendents until the desired window is 
// reached and focused 
function _focuswin (winname) {
	if (window.name == winname) {
		this.focus ();
	} else if (window.opener) {
		window.opener._focuswin (winname);
	} else {
		_focus (winname);
	}
}	

function _focus (winname) {
	var index;
	index = _getwinindex (winname);
	if (index != -1) {
		if (newwin[index].ref && !newwin[index].ref.closed) {
			newwin[index].ref.focus();
		}
	} else {
		for (index = 0; index < newwin.length; index++) {
			if (newwin[index].ref && !newwin[index].ref.closed) {
				newwin[index].ref._focus (winname);
			}
		}
	}
}
