//////////////////////////
//For message
/////////////////////////
var commMsg = "Processing, please wait...";
var commImgPath = "../images/busy05.gif";
var loginMsg = "Operator is logging in...";
var loginImgPath = "images/busy01.gif";

var XMLHTTP_ActiveX = "Microsoft.XMLHTTP";
var ServerTime;
var windows = new Array();

/****************************************************************************************
* function PopupPicker:   open new dialog which contains calendar (ie. DatePicker.aspz)
*                         bind the passed in textbox value to the returned handle
*                         In:   Id of the textbox
*****************************************************************************************/
function PopupPicker(objName)
{ 
    settings='dialogHeight:240px;dialogWidth=235px;status:no;scroll:yes;resizable:yes;';
    var date = window.showModalDialog("../DatePicker.aspx", "", settings);
    
    if (date)
    {
        document.getElementById(objName).value = date;
    }        
}

/****************************************************************************************
* function disableCtrlKey:  Cancel the response when User press Ctr Key from
*                           DatePicker.aspx, Report.aspx, RightFrame.aspx
*****************************************************************************************/    
function disableCtrlKey() 
{
    if (window.event.ctrlKey) 
    {
        window.event.returnValue = false;
        window.event.cancelBubble = true;
        return false;
    }
}
/*********************************************************************************
*	clickTheSetBtn function:    simulate the passed in button click event
*                               when Enter Key press on corresponding dropdownlist
**********************************************************************************/
function clickTheSetBtn(e, buttonid)
{
	var bt = document.getElementById(buttonid);
	if (typeof bt == 'object')
	{
		if (navigator.appName.indexOf("Netscape")>(-1))
		{
			if(e.keyCode == 13)
			{
				bt.click();
				return false;
			}
		}
		if (navigator.appName.indexOf("Microsoft Internet Explorer")>(-1))
		{
			if (event.keyCode == 13)
			{
				bt.click();
				return false;
			}
		}
	}
}
/*****************************************************************************
*   SetDate function:   set the dateValue to return handle of calendar window
*                       close the calendar window
*******************************************************************************/
function SetDate(dateValue)
{
    window.returnValue = dateValue;
    
    window.close();
}
/*******************************************************************************************************
*   checkAndSetDate function:   make sure TemporaryAccess End date not before TemporaryAccess From date
********************************************************************************************************/	
function checkAndSetDate(obj)
{
    var date3 = document.getElementById("tbFALDateFrom").value;
    var date4 = document.getElementById("tbFALDateEnd").value;

    if (date3 == null || date4 == null || (Date.parse(date3) > Date.parse(date4)) == true)
    {
        event.cancelBubble = true;
        event.returnValue = false;
    }    
}
/*******************************************************************************************************
*   captureKeyPressEvent function:   disable F5 key (using on CardDetails.aspx, CardDetails_LeftFrame.aspx
*                                    CardDetails_MiddleFrame.aspx, CardDetails_RightFrame.aspx, 
*                                    CardDetails_TopFrame.aspx, Header.aspx
********************************************************************************************************/
function captureKeyPressEvent()
{
    if (event.keyCode == 116) //F5 key is pressed
    {
        event.returnValue = false;
        event.keyCode = 0;
    }
}
/*********************************************
*   deContext function:   disable ContextMenu
**********************************************/
function deContext()
{
    event.cancelBubble = true;
    event.returnValue = false;
}

/**************************************************************************************************************
*   doBeforeClose function:   try to call removing a card if any from session["currentopencard"] in sync mode
*                             ie. the script waits for a response (last line in the function) before continuing
*                             (used in CardDetails.aspx)
***************************************************************************************************************/
function doBeforeClose()
{
    var control = window.frames(1).document.getElementById("CardId");
    if (control ==null)  
    {
        alert("CardId control is null");
        return;         
    }
    
    cardId = control.value;
    if (cardId == null || cardId == "")
    {
        alert("CardId is empty");
        event.cancelBubble = true;
        event.returnValue = false;
        
        return;
    }
    
    var req = new ActiveXObject(XMLHTTP_ActiveX);
    req.open("POST", "SessionListener.aspx?&task=deleteCardFromList&cardId=" + cardId, false);
    req.send();
    
    var ret = req.responseText;  
}

/************************************************************************************
*   showProgressMsg function:   displaying progress message (only used in Login.aspx)
*************************************************************************************/
function showProgressMsg1(msg, imgPath)
{
    
    var frame = document.getElementById("dataProcMsg");
    var doc;
    if (frame.contentWindow)
        doc = frame.contentWindow.document;
    else
        doc = frame.document;
        
    var top = document.body.clientHeight/2 - frame.style.pixelHeight/2;
    var left = document.body.clientWidth/2 - frame.style.pixelWidth/2;
    
    if (top < 0)
        top = 0;
    if (left < 0)
        left = 0;
    
    frame.style.pixelLeft = left;
    frame.style.pixelTop = top;
    
    var htmlText = "<html><body style='Margin: 0px;background-color:#99ccff;border-width: 1px;";
    htmlText += "border-color:black'><form><table width=100% height=100%><tr><td><img src='" + imgPath +"' /></td><td align=center>";
    htmlText += "<Font color=black>" + msg + "</Font></td></tr></table></form></body></html>";
    
    doc.write(htmlText);
    frame.style.display = "block";
    doc.body.setCapture();
}

/**********************************************************************************************
*   preventDateInputOnFocus function:   remove focus on input control passed in when leftclick
***********************************************************************************************/
function preventDateInputOnFocus(id)
{
    // client-side script that pretend setting the specified
    // input control's readOnly status
    var elem = document.getElementById(id);
    if (elem != null)
    {
        // assumption: this element is enabled, so disable it!
        elem.onfocus = function preventFocus(e) { this.blur(); };
    }
}

/*********************************************************************************
*	displayOperatorInfo function:    display OperatorInfo and GMS Web Server time
*                                    in the status bar
**********************************************************************************/
function displayOperatorInfo(msgOpInfo)
{
    window.defaultStatus  = msgOpInfo;
    document.displayOpInfo_return = true;
}

/**************************************************************************************
*	InitServerTimeVariable function:    get the real current datetime of GMS Web server
***************************************************************************************/
function InitServerTimeVariable()
{
    var timecollection = document.getElementById("GMSTimeCollection");
    
    if (timecollection == null)
        return;
        
    //Note : the array includes year,month,day,hour,minute,second in order.
    var timeArray = timecollection.value.split(",");
    
    var year = timeArray[0];
    var month = timeArray[1];
    var day = timeArray[2];
    var hour = timeArray[3];
    var minute = timeArray[4];
    var second = timeArray[5];
    
    ServerTime = new Date(year, month - 1, day, hour, minute, second);    
}

/**************************************************************************************************
*	displayServerTime function:    set the real current datetime of GMS Web server to browser page
***************************************************************************************************/
function displayServerTime()
{
    var GMSTime = document.getElementById("GMSTime");
    if (GMSTime == null)    //not in login page
    {
        GMSTime = document.getElementById("GMSTimeCollection");
        if (GMSTime == null)
            return;
    }

    ServerTime.setSeconds(ServerTime.getSeconds() + 1);
    
    if (top.frames.length < 3)  //in login page
    {
        GMSTime.innerText = ServerTime.toLocaleString();
        return;
    }
    
    top.defaultStatus = document.getElementById("OperatorInfo").value + "    GMS Server Time : " + ServerTime.toLocaleString();
}

/**********************************************************************
*	releaseServerTimeObj function:  release memory used for ServerTime
***********************************************************************/
function releaseServerTimeObj()
{
    delete ServerTime;
}

/**********************************************************************
*	Show cascaded windows for rightframe.aspx which is base for all carddetails.aspx window 
***********************************************************************/
function showCascadedWindow(url)
{
    var array = new Array();
    for (i = 0; i < windows.length; i++)
    {
        if (!windows[i].closed) array.push(windows[i]);
    }
    
    windows = array;
    var newWindow = window.open(url, "_blank", "top=" + 50 * windows.length + "; left=" + 50 * windows.length + "; height=700; width=900, status=yes, resizable=yes ");
    windows.push(newWindow);
}

function closeCardReportPreview()
{
    if (reportViewerWin != null && !reportViewerWin.closed)
        reportViewerWin.close();
}

/**********************************************************************
*	Disable backspace key to avoid refreshing page and cause wrong gui presentation
***********************************************************************/
function handleBackspaceKey()
{
    if (event.keyCode == 8 && !(event.srcElement.tagName == "INPUT" && event.srcElement.type == "text"))
    {
        event.cancelBubble = false;
        event.returnValue = false;
    }
}