/* AutoComplete TextBox

History
15 Sep 2006	ES	Created
08 FEB 2007 Reddy Karri Modified to set focus to the control while error validation summary
*/
// collection of client-side object that requires
// initialization after page load
var clientObjects = new Array();

// initialize page
function initPage() {
	if (pane=document.getElementById('LeftPane')) {
		if (pane.innerText == '') {
			if (toggler = document.getElementById('LeftPaneToggler'))
				toggler.style.display = 'none';
		}
	}
	if (pane=document.getElementById('RightPane')) {
		if (pane.innerText == '') {
			if (toggler = document.getElementById('RightPaneToggler'))
				toggler.style.display = 'none';
		}
	}
	
	// initialize
	for(var i=0; i<clientObjects.length; i++) {
		if (clientObjects[i] != null && typeof clientObjects[i].init == 'function')
			clientObjects[i].init();
	}
}

// redirect page to specified url
function redirectPage(url, paramsName, paramsValue)
{
	if (typeof(buildURL) == 'function' &&
		typeof(paramsName) != 'undefined' &&
		typeof(paramsValue) != 'undefined')
		url = buildURL(url, paramsName, paramsValue);

	window.location.href=url;
}


// safe way to close a popup window
function closeWindow() {
	if (window.opener && !window.opener.closed)
		window.close();
}
/*
function focusControl(id)
{
	//TODO: Optimize this code
	var obj;
	var focused = false;
	try	{
		obj = document.getElementById(id);
		
		//If control is not input control, check for the childrens
		//Set first children input control to focus
		if (obj.tagName!="INPUT" && obj.tagName!="SELECT" && obj.children.length>0)
		{
			for (var index=0;index<obj.children.length; index++)
			{
				var childObj=obj.children[index];
				if (childObj.tagName=="INPUT" || childObj.tagName=="SELECT")
				{
					childObj.focus();
					break;
				}
			}
		}
		else
			obj.focus();
			
		focused = true;
	}
	catch(e){ }
	
	if (!focused) {
		if(typeof(igtab_all) != 'object') {
			try	{
				obj = igedit_getById(id);
				obj.focus();
				focused = true;
			}
			catch(e){}
		}
		else {
			if(typeof(igtab_all) == 'object') {
				for(tabID in igtab_all) {
					if (oWebTab = igtab_getTabById(tabID)) {
						for(var i=0; i<oWebTab.Tabs.length; i++) {
							oTab = oWebTab.Tabs[i];
							if (oTab.findControl(id)) {
								oWebTab.setSelectedTab(oTab);

								try	{
									obj = document.getElementById(id);
									obj.focus();
									focused = true;
								}
								catch(e){}		
								if (!focused) {
									try {
										obj = igedit_getById(id);
										obj.focus();
										focused = true;
									}
									catch(e){}
								}
							}
						}
					}
				}
			}
		}
	}
}
*/
// focus control of specified id
//08 FEB 2007 Reddy Karri Modified to set focus to the control while error validation summary
function focusControl(id)
{
	var focused = false;
	if(typeof(igtab_all) != 'object') {
		//looking for other than infragistics control
		focused = setFocusToControlFromVS(id);
	}
	if (!focused) {		
		if(typeof(igtab_all) == 'object') {
			for(tabID in igtab_all) {
				if (oWebTab = igtab_getTabById(tabID)) {
					for(var i=0; i<oWebTab.Tabs.length; i++) {
						oTab = oWebTab.Tabs[i];
						if (oTab.findControl(id)) {
							oWebTab.setSelectedTab(oTab);
							setFocusToControlFromVS(id);
						}
					}
				}
			}
		}		
	}
}

//Created by Reddy Karri to set the focus to the control
function setFocusToControlFromVS(id)
{
	var obj;
	var focused = false;
	var lfocus =false;
	try	{		
		if(typeof(igedit_all) == 'object') {
			obj = igedit_getById(id);			
		}
		if(obj == null && typeof(igdrp_all) == 'object') {
			obj = igdrp_getComboById(id);			
		}		
		if(obj == null){
			obj = document.getElementById(id);			
			if (obj.tagName!="INPUT" && obj.tagName!="SELECT" && obj.children.length>0)
			{
				//recursively looping containar controls
				focused = recursiveFocus(obj);
			}
			else
			{lfocus = true;}
		}
		else
		{lfocus = true;}
				
		if(lfocus == true)
		{	obj.focus();
			focused = true;
		}
	}
	catch(e){}
	return focused;
}
function recursiveFocus(obj)
{
	var focused = false;
	for (var index=0;index<obj.children.length; index++)
	{
		var childObj=obj.children[index];
		if(childObj.children.length>0)
		{
			focused = recursiveFocus(childObj);
			break;
		}		
		if ((childObj.tagName=="INPUT" && childObj.type!="hidden") || childObj.tagName=="SELECT" || childObj.tagName=="TEXTAREA")
		{						
			childObj.focus();
			focused = true;	
			break;
		}
	}
	return focused;
}
/*----------------------------------------------------------------------
iRegion - collapsible region / pane
------------------------------------------------------------------------*/
var irContent = '';
var irWidth = 10;
var irIncr = 50;
var irTimer = 5;
var irClicked = 0;
var irRegionType = '';
var irInitWidth;

function toggle_iRegion(button, regionId, regionType)
{
	if (!irClicked)
	{
		irClicked = 1;
				
		var btn_show_src;
		var btn_hide_src;
		irRegionType = regionType;
		if (irRegionType=='right')
		{
			btn_show_src = 'icons/pane_left.gif';
			btn_hide_src = 'icons/pane_right.gif';
		}
		else
		{
			btn_show_src = 'icons/pane_right.gif';
			btn_hide_src = 'icons/pane_left.gif';
		}
	
		irContent = document.getElementById(regionId);
		if (irContent.style.display=='')
		{
			//collapse
			irContent.style.overflow = 'hidden';
			irInitWidth = irContent.offsetWidth;
			irContent.style.pixelWidth = irContent.offsetWidth;
			iRegion_Collapse();
			button.src = btn_show_src;
		}
		else
		{
			//expand
			irWidth = irIncr;
			irContent.style.filter = 'progid:DXImageTransform.Microsoft.Alpha( Opacity=100, FinishOpacity=0, Style=0, StartX=0,  FinishX=100, StartY=0, FinishY=100)';
			irContent.style.display='';
			irContent.style.overflow = 'hidden';
			irContent.style.width = irWidth + "px";
			iRegion_Expand();
			button.src = btn_hide_src;
		}
		
		irClicked = 0;
	}
}

function iRegion_Expand()
{
	if (irContent.offsetWidth < (irContent.scrollWidth-irIncr))
	{
		irWidth += irIncr;
		irContent.style.width = irWidth + "px";
		setTimeout("iRegion_Expand()",irTimer);
	}
	else
	{
		irContent.style.width="auto";
		irContent.style.overflow="visible";
		irWidth = irIncr;
	}
}

function iRegion_Collapse()
{
	if (irContent.style.pixelWidth > irIncr)
	{
		irContent.style.pixelWidth -= irIncr;
		irContent.style.filter = 'progid:DXImageTransform.Microsoft.Alpha( Opacity=' + Math.round(irContent.style.pixelWidth/irInitWidth*100) + ', FinishOpacity=0, Style=0, StartX=0,  FinishX=100, StartY=0, FinishY=100)';
		setTimeout("iRegion_Collapse()",irTimer);
	}
	else
	{
		irContent.style.display='none';	
	}
}


/*----------------------------------------------------------------------
iPanel - collapsible panel
------------------------------------------------------------------------*/
var ipContent = '';
var ipHeight = 10;
var ipIncr = 50;
var ipTimer = 5;
var ipClicked = 0;
var ipInitHeight;

function toggle_iPanel(button, contentId, imgCollapse, imgExpand) {
	if(!ipClicked) {
		ipClicked=1;
		ipContent = document.getElementById(contentId);
		
		if (ipContent.style.display=='') {
			//collapse
			ipContent.style.overflow = 'hidden';
			ipInitHeight = ipContent.offsetHeight;
			ipContent.style.pixelHeight = ipContent.offsetHeight;
			iPanel_Collapse();
			button.src = (typeof(imgExpand)=='undefined') ? 'icons/panel_plus.gif' : imgExpand;
			button.alt = 'Click to expand';
		}
		else {
			//expand
			ipContent.style.filter = 'progid:DXImageTransform.Microsoft.Alpha( Opacity=100, FinishOpacity=0, Style=0, StartX=0,  FinishX=100, StartY=0, FinishY=100)';
			ipContent.style.display='';
			ipContent.style.overflow = 'hidden';
			ipContent.style.height = ipHeight + "px";
			iPanel_Expand();
			button.src = (typeof(imgCollapse)=='undefined') ? 'icons/panel_minus.gif' : imgCollapse;
			button.alt = "Click to collapse";
		}
		ipClicked=0;
	}
}

function iPanel_Expand(){
	if (ipContent.offsetHeight < (ipContent.scrollHeight-ipIncr))
	{
		ipHeight += ipIncr;
		ipContent.style.height = ipHeight + "px";
		setTimeout("iPanel_Expand()",ipTimer);
	}
	else
	{
		ipContent.style.height="auto";
		ipContent.style.overflow="visible"
		ipHeight = 10;
	}
}

function iPanel_Collapse()
{
	if (ipContent.style.pixelHeight > ipIncr)
	{
		ipContent.style.pixelHeight -= ipIncr;
		ipContent.style.filter = 'progid:DXImageTransform.Microsoft.Alpha( Opacity=' + Math.round(ipContent.style.pixelHeight/ipInitHeight*100) + ', FinishOpacity=0, Style=0, StartX=0,  FinishX=100, StartY=0, FinishY=100)';
		setTimeout("iPanel_Collapse()",ipTimer);
	}
	else
	{
		ipContent.style.display='none';	
	}
}

function PageQuery(q) 
{
	if(q.length > 1) this.q = q.substring(1, q.length);
	else 
	this.q = null;
	
	this.keyValuePairs = new Array();
	if(q) {
		for(var i=0; i < this.q.split("&").length; i++) 
		{
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	
	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++) 
		{
			if(this.keyValuePairs[j].split("=")[0] == s)
			return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}
	this.getParameters = function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) 
		{
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; } 
}


function queryString(key)
{
	var page = new PageQuery(window.location.search); 
	return unescape(page.getValue(key)); 
}
function SetToggleExpansion(contentid,imgid,displayflag)
{	var arContentId = contentid.split(";");
	var arImageId = imgid.split(";");
	if(arContentId.length !=arImageId.length)
	{ alert("Content and Image parameter items not equal"); return false;}
	
	var part_content=0;
	var delayflag =false; var durtime =0;
	while (part_content < arContentId.length)
	{
		var eachcnt = document.getElementById(arContentId[part_content]);						
		if ((eachcnt.style.display!="" && displayflag=="E")|| (eachcnt.style.display=="" && displayflag=="C") )
		{	if(delayflag==true)	
			{	window.setTimeout("fnToggleGroups('"+arImageId[part_content]+"','"+arContentId[part_content]+"')",durtime);}
			else
			{	fnToggleGroups(arImageId[part_content],arContentId[part_content]);	}
			
			delayflag=true;
			durtime+=500;
		}
		part_content+=1;
	}	
}	
function fnToggleGroups(imgid,divid){		
	var imgCtl = document.getElementById(imgid);
	toggle_iPanel(imgCtl,divid);
}	
//return current date and time 
//using for refresh reports to pass the current date and time
function getCurrentDateTime() {
	var currentTime = new Date();
	var month = currentTime.getMonth() + 1;
	var day = currentTime.getDate();
	var year = currentTime.getFullYear();
	var hours = currentTime.getHours();
	var minutes = currentTime.getMinutes();
	var seconds = currentTime.getSeconds();
	return  day + "/" + month + "/" + year + " " + hours + ":" + minutes + ":" +  seconds;
}

/* not recommanded as if it is called, the login will keep forever and harm security
function RenewSession() {
	if (typeof(AjaxEngine)=='function') {
		var ajax = new AjaxEngine("Handlers/UserHandler.ashx");
		ajax.DoRemoteQuery(['a'],['s'], setSessionTimer, true, 'POST');
	}
}

function setSessionTimer(obj, ctx) {
	try { 
		eval('var rs='+obj); 
		if (typeof(rs) != 'undefined' && typeof(rs.Tag) != 'undefined'){
			// rs.Tag store milliseconds of timeout, renew it 30 seconds before
			window.setTimeout(RenewSession, parseInt(rs.Tag)-30000);
		} 
	}
	catch(e) {}
}
*/

function RenewLogin(message) {
	if (typeof(AjaxEngine)=='function' && window.confirm(message)) {
		var ajax = new AjaxEngine("Handlers/UserHandler.ashx");
		ajax.DoRemoteQuery(['a'],['l'], setLoginTimer, true, 'POST');
	}
}

function setLoginTimer(obj, ctx) {
	try { 
		eval('var rs='+obj); 
		if (typeof(rs) != 'undefined' && typeof(rs.Code) != 'undefined'){
			if (rs.Code == '0' && typeof(rs.Tag) != 'undefined') {
				// rs.Tag store milliseconds of prompt
				window.setTimeout("RenewLogin('" + rs.Message + "')" , parseInt(rs.Tag));
			}
		} 
	}
	catch(e) {}
}

/*@cc_on @*/
/*@if (@_win32 && @_jscript_version>=5)

function window.confirm(str)
{
    try { 
        execScript('n = msgbox("'+str+'","4132")', "vbscript");
        return(n == 6);
    }
	catch(e) {
	    return false;
	}
}

function confirm(str)
{
    try { 
        execScript('n = msgbox("'+str+'","4132")', "vbscript");
        return(n == 6);
    }
	catch(e) {
	    return false;
	}
}
@end @*/
