/****** Popup window in center ******/
function PopupPage(pageUrl, pageWidth, pageHeight,windowName)
{
	//Code to display in the center goes here
	var left = (screen.width - pageWidth)/2;
	var top = (screen.height - pageHeight)/2;
	if (isNaN(left))
		left = 0;
	if (isNaN(top))
		top = 0;
	if (null == windowName	 )
		windowName = "_blank";
	var newPopup = window.open(pageUrl, windowName , "dependent=yes, directories=no, location=no, menubar=no, personalbar=no, resizable=no, scrollbars=no, status=no, toolbar=no, height=" + pageHeight + ", width=" + pageWidth + ", left=" + left + ", top=" + top + ", dialog=yes");
	return newPopup;
}
function PopupScrollPage(pageUrl, pageWidth, pageHeight, windowName)
{
	//Code to display in the center goes here
	var left = (screen.width - pageWidth)/2;
	var top = (screen.height - pageHeight)/2;
	if (isNaN(left))
		left = 0;
	if (isNaN(top))
		top = 0;
	if (null == windowName	 )
		windowName = "_blank";
	var newPopup = window.open(pageUrl, windowName , "dependent=yes, directories=no, location=no, menubar=no, personalbar=no, resizable=no, scrollbars=yes, status=no, toolbar=no, height=" + pageHeight + ", width=" + pageWidth + ", left=" + left + ", top=" + top + ", dialog=yes");
	return newPopup;
}
/****** Preload Images ******/
var gImages = new Array();
function preloadImages(imageSrc)
{
	var newIdx = gImages.length;
	gImages[newIdx] = new Image();
	gImages[newIdx].src = imageSrc;
}

/****** Common String functions ******/
String.prototype.trim = trim;
String.prototype.replaceAll = replaceAll;
function trim()
{
	if (this.length > 0)
	{
		var retstr = this.replace(/^\s+/,"");
		retstr = retstr.replace(/\s+$/,"");
		return retstr;
	}
	else return this;
}
function replaceAll(str1, str2)
{
	var idx1 = 0;
	var idx2 = this.indexOf(str1);
	var retStr = "";
	while (idx2 >= 0)
	{
		retStr += this.substring(idx1,idx2) + str2;
		idx1 = idx2 + str1.length;
		idx2 = this.indexOf(str1,idx1);
	}
	retStr += this.substring(idx1);
	return retStr;

}

/****** Common Array functions ******/
Array.prototype.remove = removeFromArray;
Array.prototype.contains = containsElement;
function removeFromArray(index)
{
	if (index < this.length)
	{
		var newLength = this.length-1;
		for (var i=index; i<newLength; i++)
		{
			this[i] = this[i+1];
		}
		this.length = newLength;
	}
}
function containsElement(element)
{
	for (var idx=0; idx<this.length; idx++)
	{
		if (this[idx] == element)
			return true;
	}
	return false;
}

/****** Element Movements within a single list / between 2 lists ******/
// Check whether the object is a valid object
function isValid(oObject)
{
	if (oObject == null) return false;
	if (typeof(oObject) == "undefined") return false;
	return true;
}

// Check whether the object is of type select (dropdown / list)
function isValidSelect(oObject)
{
	if (!isValid(oObject) || typeof(oObject.options) == "undefined") return false;
	return true;
}

// Unselects the options of the given select object
function unselect(oObject)
{
	// Remove the selection from all the options
	for (var i = 0; i < oObject.length; i++)
	{
		oObject.options[i].selected = false;
	}
}

function move(oFrom, oTo, maxlimit, maxLimitMessage)
{
	// If the parameter is an object and is of type select or select-multiple
	if (!isValidSelect(oFrom) || !isValidSelect(oTo))
	{
		return;
	}

	var access = 1;
	var removelist = new Array();
	var rlcount = 0;
	var refreshthis = 0;
	if (maxlimit==null)
		maxlimit = 0;
	if (maxLimitMessage==null)
		maxLimitMessage = "";
	var messageDisplayed = false;

	unselect(oTo);
	for (var i = 0; i < oFrom.length; i++)
	{
		if (oFrom.options[i].selected)
		{
			if (maxlimit == 0 || oTo.options.length < maxlimit)
			{
				var selText	= oFrom.options[i].text;
				var selValue= oFrom.options[i].value;
				oTo.options[oTo.options.length] = new Option(selText,selValue);
				removelist[rlcount] = i;
				rlcount++;
			}
			else
			{
				if (!messageDisplayed)
				{
					alert(maxLimitMessage);
					messageDisplayed = true;
				}
			}
		}
	}

	unselect(oFrom);
	for (var i = removelist.length; i>0;  i--)
	{
		oFrom.options[removelist[i-1]]= null;
	}
}

function clearList(obj)
{
	obj.options.length = 0;
}

function moveUp(obj)
{
	if (!isValidSelect(obj)) return;
	if (0 >= obj.selectedIndex) return;

	var selIdx = obj.selectedIndex;
	var movedOption = new Option(obj.options[selIdx-1].text, obj.options[selIdx-1].value);

	unselect(obj);
	obj.options[selIdx-1] = new Option(obj.options[selIdx].text, obj.options[selIdx].value);
	obj.options[selIdx-1].selected = true;
	obj.options[selIdx] = movedOption;
}

function moveDown(obj)
{
	if (!isValidSelect(obj)) return;
	if ((obj.options.length-1) <= obj.selectedIndex) return;

	var selIdx = obj.selectedIndex;
	var movedOption = new Option(obj.options[selIdx+1].text, obj.options[selIdx+1].value);

	unselect(obj);
	obj.options[selIdx+1] = new Option(obj.options[selIdx].text, obj.options[selIdx].value);
	obj.options[selIdx+1].selected = true;
	obj.options[selIdx] = movedOption;
}
/*******************************/

/****** Display Properties ******/
function displayProperties(obj)
{
	var str = "";
	var prop;
	if (typeof(obj.length) != "undefined" && typeof(obj[0]) != "undefined")
	{
		for (prop = 0;prop < obj.length;prop++)
		{
			str += "\n" + prop + "=" + obj[prop];
		}
	}
	else
	{
		for (prop in obj)
		{
			str += "\n" + prop + "=" + eval("obj." + prop);
		}
	}
	alert(str);
}

function displayPropertiesExpHTML(obj)
{
	var str = "";
	var prop;
	if (typeof(obj.length) != "undefined" && typeof(obj[0]) != "undefined")
	{
		for (prop = 0;prop < obj.length;prop++)
		{
			str += "\n" + prop + "=" + obj[prop];
		}
	}
	else
	{
		for (prop in obj)
		{
			if ((prop == "innerHTML") || (prop == "outerHTML") || (prop == "innerText") || (prop == "outerText")) continue;
			str += "\n" + prop + "=" + eval("obj." + prop);
		}
	}
	alert(str);
}
function displayPropertiesBrk(obj)
{
	var str = "";
	var prop;
	var ct=0;
	if (typeof(obj.length) != "undefined" && typeof(obj[0]) != "undefined")
	{
		for (prop = 0;prop < obj.length;prop++)
		{
			str += "\n" + prop + "=" + obj[prop];
			if (ct > 30)
			{
				alert(str);
				str="";
				ct = 0;
			}
			ct++;
		}
	}
	else
	{
		for (prop in obj)
		{
			str += "\n" + prop + "=" + eval("obj." + prop);
			if (ct > 30)
			{
				alert(str);
				str="";
				ct = 0;
			}
			ct++;
		}
	}
	alert(str);
}
