// this variable will hold the window obect
// we only allow one pop-up at a time
var popUpWnd = null;
var modalWnd = null;


/*/
 *  PURPOSE:
 * 		To create and center a pop-up window.
 * 
 *  COMMENTS:
 * 		It will replace to old pop-up if called
 *  	without calling DestroyPopUpWnd() first..
/*/

function CreatePopUpWnd (url, width, height, resize, name)
{
	var doCenter = false;

	if(!name)
	{
		name = "ATT_POPUP";
	}
	
	if((popUpWnd == null) || popUpWnd.closed)
	{
		attribs = "";

		/*/ there's no popUpWnd displayed /*/

		// assemble some params
		if(resize) size = "yes"; else size = "no";

		/*/
		 *  We want to center the pop-up; however, to do this we need to know the
		 *  screen size.  The screen object is only available in JavaScript 1.2 and
		 *  later (w/o Java and/or CGI helping), so we must check for the existance
		 *  of it in the window object to determine if we can get the screen size.
		 * 
		 *  It is safe to assume the window object exists because it was implemented
		 *  in the very first version of JavaScript (that's 1.0).
		/*/
		for(var item in window)
			{ if(item == "screen") { doCenter = true; break; } }

		if(doCenter)
		{	/*/ center the window /*/

			// if the screen is smaller than the window, override the resize setting
			if(screen.width <= width || screen.height <= height) size = "yes";

			WndTop  = (screen.height - height) / 2;
			WndLeft = (screen.width  - width)  / 2;

			// collect the attributes
			attribs = "width=" + width + ",height=" + height + ",resizable=" + size + ",scrollbars=" + size + "," + 
			"status=no,toolbar=no,directories=no,menubar=no,location=no,top=" + WndTop + ",left=" + WndLeft;
		}
		else
		{
			/*/
			 *  There is still one last thing we can do for JavaScrpt 1.1
			 *  users in Netscape.  Using the AWT in Java we can pull the
			 *  information we need, provided it is enabled.
			/*/
			if(navigator.appName=="Netscape" && navigator.javaEnabled())
			{	/*/ center the window /*/

				var toolkit = java.awt.Toolkit.getDefaultToolkit();
				var screen_size = toolkit.getScreenSize();

				// if the screen is smaller than the window, override the resize setting
				if(screen_size.width <= width || screen_size.height <= height) size = "yes";

				WndTop  = (screen_size.height - height) / 2;
				WndLeft = (screen_size.width  - width)  / 2;

				// collect the attributes
				attribs = "width=" + width + ",height=" + height + ",resizable=" + size + ",scrollbars=" + size + "," + 
				"status=no,toolbar=no,directories=no,menubar=no,location=no,top=" + WndTop + ",left=" + WndLeft;
			}
			else
			{	/*/ use the default window position /*/

				// override the resize setting
				size = "yes";

				// collect the attributes
				attribs = "width=" + width + ",height=" + height + ",resizable=" + size + ",scrollbars=" + size + "," + 
				"status=no,toolbar=no,directories=no,menubar=no,location=no";
			}
		}

		// create the window
		popUpWnd = open(url, name, attribs);
	}
	else
	{
		// destory the current window
		DestroyPopUpWnd();
		// recurse, just once, to display the new window
		CreatePopUpWnd(url, width, height, resize, name);
	}
}

/*/
 *  PURPOSE:
 * 		To destroy the pop-up window.
 * 
 *  COMMENTS:
 * 		This is available if wish to destroy
 *  	the pop-up window manually.
/*/

function DestroyPopUpWnd ()
{
	// close the current window
	if(popUpWnd != null)
	{
		popUpWnd.close();
		popUpWnd = null;
	}
}

function GetCheckBoxValue(checkBoxId)
{
	var checkBox; 
	checkBox = document.getElementById(checkBoxId);
	if(null != checkBox)
	{
		return checkBox.checked;
	}
	else
	{
		return false;
	}
}

function GetRadioButtonListSelectedValue(radioButtonListId)
{
	for(i=0;;i++)
	{
		if(document.getElementById(radioButtonListId + '_' + i))
		{
			if(document.getElementById(radioButtonListId + '_' + i).checked)
			{
				return document.getElementById(radioButtonListId + '_' + i).value;
			}
		}
		else
		{
			return null;
		}
	}
}

function SetControlVisibility(controlId, isVisible)
{
	var ctl = document.getElementById(controlId);
	if(!ctl)return;

	if(isVisible)
	{
		if(controlId == 'TDLeft')
		{
			// ctl.style.display='auto'; 
		}
		else
		{
			ctl.style.display='block'; 
		}
			
		ctl.style.visibility='visible';
	}
	else
	{
		ctl.style.display='none';
		ctl.style.visibility='hidden';
	}
}

function SetControlState(controlId, isEnabled)
{
	var ctl = document.getElementById(controlId);
	if(!ctl)return;

	ctl.disabled = !isEnabled;
}

function GetControlValue(controlId)
{
	var ctl = document.getElementById(controlId);
	if(!ctl)return null;
	
	return ctl.value;
}


function igtbl_getNumberOfSelectedRows(gridName)
{
	var selCount = 0;
	var oGrid = igtbl_getGridById(gridName);
	if(null == oGrid)return;

	for(i=0;i<oGrid.Rows.length;i++)
	{
		if(oGrid.Rows.getRow(i).getSelected())selCount++;
	}
	return selCount;
}

function IgnoreEvents(e)
{
	return false;
}

function ShowModalDialogBox(url, title, width, height, scroll, resize)
{
	var attributes;
	if (window.showModalDialog)
	{
		attributes = 'center:yes;status:no;dialogHide:true;help:no;';
		if(scroll)
		{
			attributes+='scroll:yes;';
		}
		else
		{
			attributes+='scroll:no;';
		}

		if(resize)
		{
			attributes+='resizable:yes;';
		}
		else
		{
			attributes+='resizable:no;';
		}
		
		attributes+= 'dialogWidth:' + width + 'px;';
		attributes+= 'dialogHeight:' + height + 'px;';
		window.showModalDialog(url,title,attributes);
		return window.returnValue;
	}
	else
	{
		window.top.captureEvents(Event.CLICK|Event.FOCUS);
		window.top.onclick=IgnoreEvents;
		window.top.onfocus=HandleFocus; 
		attributes = 'dependent=yes,'
		attributes+= 'width=' + width + 'px,';
		attributes+= 'height=' + height + 'px,';
		modalWnd = window.open(url,title,attributes);
		modalWnd.focus();
	}
}

function HandleFocus()
{
	if (modalWnd)
	{
		if (!modalWnd.closed)
		{
			modalWnd.focus();
		}
		else
		{
			window.top.releaseEvents (Event.CLICK|Event.FOCUS);
			window.top.onclick = '';
		}
	}
return false;
}

function ReloadFrame(frameName)
{
	var targetFrame = top.frames[frameName];
	if(null != targetFrame)targetFrame.location.reload(true);
}

function PrintFrame(frameName)
{
	var targetFrame = top.frames[frameName];
/*	if(window.print)
	{
//		alert("window.print");
		window.print();
	}
*/
	if(null != targetFrame)
	{
//		alert("targetFrame.Print");
		targetFrame.focus();
		targetFrame.print();
	}
}

function PrintIFrame(frameName)
{
	var targetFrame = window.frames[frameName];
	if(null != targetFrame)
	{
//		alert("targetFrame.Print");
		targetFrame.focus();
		targetFrame.print();
	}
}

function DefaultButtonHandler(ctlId)
{
	var btn = document.getElementById(ctlId);
	if(!btn)return;
	if(!btn.click)return;
	
	if(window.event != undefined)
	{	
		// process only the Enter key
		if (event.keyCode == 13)
		{
				// cancel the default submit
				event.returnValue=false;
				event.cancel = true;
				// submit the form by programmatically clicking the specified button
				btn.click();
		}
	}
}

function saveId(id)
{
    var idpre = id.split("_")[0] +"_";
    var hdSelPos=document.getElementById (idpre + "hdSelPos");
    var dlst = document.getElementById (idpre + "dlstPosition");
    var nd;
    var chk
    if ((hdSelPos !=null) && (dlst !=null))
    {
        hdSelPos.value ="";
        for (i=0;i<dlst.rows.length;i++)
        {
            nd = dlst.rows[i];
            for (j=0;j < nd.cells.length;j++)
            {
                chk = nd.cells[j];
                
                if (chk.childNodes.length>0)
                {
                    if (navigator.appName !="Microsoft Internet Explorer")
                    {
                        if (chk.childNodes[1].checked == true)
                        {
                            hdSelPos.value +=chk.childNodes[6].value+",";
                        }
                    }
                    else
                    {
                        if (chk.childNodes[0].checked == true)
                        {
                            hdSelPos.value +=chk.childNodes[5].value+",";
                        }
                    }
                }
            }        
        }
    }
}

// This function is used to resolve time-zone problem of AJAX Calendar Extention: CalendarExtender
// Add code OnClientHiding="fixDate" to CalendarExtender
function fixDate(sender)
{
    if (sender._selectedDate && sender._visibleDate && sender._element)
    {
        if (sender._selectedDate.getDate() != sender._visibleDate.getDate()) 
        {
            var fixedDate = sender._visibleDate.localeFormat(sender._format);
            sender._selectedDate = sender._visibleDate;
            sender._element.value = fixedDate;
        }
    }
}

function RegisterEventDateControls(
    calFrom, 
    txtFromHour,
    txtFromMinute,
    ddlFromMeridian,
    calTo, 
    txtToHour,
    txtToMinute,
    ddlToMeridian,
    chkUntilFurtherNotice,
    chkAllDay)
{
    // ZK: Disabled for ticket #1693
    return;

    var setToDateFunction = function() { 
        SetToDate(
            calFrom, 
            calTo, 
            chkUntilFurtherNotice == null ? true : !chkUntilFurtherNotice.checked); 
        /*    
        if(chkUntilFurtherNotice != null)
        {
            ClearTimes(
                txtFromHour, 
                txtToHour, 
                txtFromMinute, 
                txtToMinute, 
                ddlFromMeridian, 
                ddlToMeridian);
        }
        */
    };
    
    var setToTimeFunction = function() {        
        SetToTime(
            calFrom, 
            calTo, 
            txtFromHour, 
            txtToHour, 
            txtFromMinute, 
            txtToMinute, 
            ddlFromMeridian, 
            ddlToMeridian, 
            chkUntilFurtherNotice == null ? true : !chkUntilFurtherNotice.checked, 
            !chkAllDay.checked);
    };            

    calFrom.onchange = setToDateFunction;
    txtFromHour.onchange = setToTimeFunction;
    txtFromMinute.onchange = setToTimeFunction;
    ddlFromMeridian.onchange = setToTimeFunction;
}

// ----------
// Ensure that the "To" Date is never before the "From" date. Also populates the "To" Date
// if it is empty when the "From" date is set.
//
// Call this on the From Date text box's onblur and onchange events.
//
function SetToDate(calFrom, calTo, enabled) {
//    if (enabled && calFrom.value > calTo.value) {
        calTo.value = calFrom.value;
//    }
}

function ClearTimes(hourFrom, hourTo, minuteFrom, minuteTo, meridianFrom, meridianTo)
{
    hourFrom.value = "";
    minuteFrom.value = "";
    meridianFrom.selectedIndex = 0;
    
    hourTo.value = "";
    minuteTo.value = "";
    meridianTo.selectedIndex = 0;
}

function SetToTime(calFrom, calTo, hourFrom, hourTo, minuteFrom, minuteTo, meridianFrom, meridianTo, dateEnabled, timeEnabled) {
    if (timeEnabled && (!dateEnabled || calFrom.value == calTo.value)) {
        var fromMilitaryHour = GetMilitaryHour(hourFrom, meridianFrom);
        var toMilitaryHour = GetMilitaryHour(hourTo, meridianTo);

        if (minuteFrom.value == "") {
            minuteFrom.value = "00";
        }
        
        if (meridianFrom.selectedIndex <= 0) {
            meridianFrom.selectedIndex = 1;
        }

        if (minuteTo.value == "") {
            minuteTo.value = "00";
        }
        
        if (meridianTo.selectedIndex <= 0) {
            meridianTo.selectedIndex = 1;
        }

        /*
        if (fromMilitaryHour < 23 && fromMilitaryHour > toMilitaryHour) {
            // Set the To time to an hour from now
            SetHourAndMeridian(hourTo, meridianTo, parseInt(fromMilitaryHour) + 1);
            SetHourAndMeridian(hourTo, meridianTo, parseInt(fromMilitaryHour));
            minuteTo.value = minuteFrom.value;
        } else if (fromMilitaryHour < 23 && fromMilitaryHour == toMilitaryHour) {
            if (parseInt(minuteFrom.value) > parseInt(minuteTo.value)) {
                SetHourAndMeridian(hourTo, meridianTo, parseInt(fromMilitaryHour) + 1);
                minuteTo.value = minuteFrom.value;                
            }
        } else if (fromMilitaryHour == 23 && fromMilitaryHour > toMilitaryHour) {
            // Set the To time to 23h59
            SetHourAndMeridian(hourTo, meridianTo, 23);
            minuteTo.value = "59";    
        }
        */
        
        SetHourAndMeridian(hourTo, meridianTo, parseInt(fromMilitaryHour));
        minuteTo.value = minuteFrom.value;                
    }
}

function GetMilitaryHour(hour, meridian) {
    var militaryHour = parseInt(hour.value);

    if (isNaN(militaryHour)) {
        militaryHour = 0;
    }
    
    if (meridian[meridian.selectedIndex].value == "PM") {
        militaryHour += 12;
    }
    
    if (militaryHour == 12 || militaryHour == 24) {
        militaryHour -= 12;
    }

    return militaryHour;
}

function SetHourAndMeridian(hour, meridian, militaryHour) {
    if (militaryHour >= 12) {
        hour.value = militaryHour - 12;
        meridian.selectedIndex = 2;
    } else {
        hour.value = militaryHour;
        meridian.selectedIndex = 1;
    }

    if (hour.value == 0) {
        hour.value = 12;
    }
}

