var systemGuid = '';
var systemTheme = '';
var g_objPaymentLinkID = '';
var CLIENT_MESSAGE_EPAYMENT_NAVIGATE = "You are about to be transferred to the Netbanx e-Payment site;<br>during this process please do not refresh your browser or use the browser's back button.<br>After payment, click 'Back to Merchant' to return to the portal home page.<br><br>Please click Ok to proceed.";
var CLIENT_MESSAGE_EPAYMENT_NAVIGATE_YES_NO = "<b>Clicking 'Yes' will transfer you to the Netbanx e-Payment site</b>;<br>during this process please do not refresh your browser or use the browser's back button.<br>After payment, click 'Back to Merchant' to return to the portal home page.<br/><br/>";

function SaveLastLinkID() {
    g_objPaymentLinkID = tttb_getLastLinkClicked();
}
function PostBackToClientWithSavedLinkID() {
    //use the g_strLinkID saved earlier with SaveLastLinkID
    tttb_raisePostBack(g_objPaymentLinkID);

}
var popupSucceeded = false;
function EBSLP_OFP(uioId, standardFee) 
{
	var hPopupInfo = new ttlayout_PopupInfo();
	var action = "OpenLPFeeDetails";

	var ss1 = new ttsearchString();
	var ss2 = new ttsearchString();

	ss1.Add("UIO_ID", OperatorType_equal, uioId, UIControlType_InputNumeric);
	ss2.Add("STANDARD_FEE", OperatorType_equal, standardFee, UIControlType_InputNumeric);
	

	hPopupInfo.lngWidth = 951;
	if (hPopupInfo.lngWidth >= screen.width)
	    hPopupInfo.lngWidth = screen.width;
	hPopupInfo.lngHeight = 735;
	if (hPopupInfo.lngHeight >= screen.height)
	    hPopupInfo.lngHeight = screen.height;

    var randSeed = Math.random()*100;

	hPopupInfo.strDisplayPage = "default.aspx";
	hPopupInfo.additionalQueryString = "&ng=LPPopup&na=" + action + "&m=41&lid=68&r=" + randSeed;
	hPopupInfo.additionalQueryString += "&SS0=" + ss1.ToString();
	hPopupInfo.additionalQueryString += "&SS1=" + ss2.ToString();

    hPopupInfo.showModalDialog = true;

	//Open popup as normal, passing reference to our return data object
    ttlayout_openPopup(hPopupInfo);

}

function EBSLP_WDIPLD(linkContainerId, costsContainerId, displayMode) 
{
    var costs = document.getElementById(costsContainerId);
    var links = document.getElementById(linkContainerId);

	if (displayMode == "0") 
	{
		links.style.display = "none";
	}
	if (displayMode == "1") 
	{
		costs.style.display = "none";
	}

}

function AttachPaymentPopupWindow(guid) {
    //reset the popup detection 
    popupSucceeded = false;
    systemGuid = guid;
    //attempt to automatically show popup.
    ttlayout_AttachEvent(window.top, "load", showPaymentPopup);
}

function DoNothing() {
}

function showPaymentPopup(userInitiated) {
    //if modal dialogs are supported 
    //attempt to automatically open a modal dialog; this may be prevented - we will only know after the fact
    if (userInitiated == true || 
        window.showModalDialog) 
    {
        var hPopupInfo = new ttlayout_PopupInfo();
        var action = "OpenEpayment";

        hPopupInfo.lngWidth = 951;
        if (hPopupInfo.lngWidth >= screen.width)
            hPopupInfo.lngWidth = screen.width;
        hPopupInfo.lngHeight = 735;
        if (hPopupInfo.lngHeight >= screen.height)
            hPopupInfo.lngHeight = screen.height;

        var randSeed = Math.random() * 100;

        hPopupInfo.Context = "WPopupPayment";
        hPopupInfo.strContext = "WPopupPayment";
        hPopupInfo.strDisplayPage = document.location.href;
        hPopupInfo.additionalQueryString = "&ng=PublicEnrolment&na=" + action + "&m=41&p=" + systemGuid + "&r=" + randSeed;
        //don't use popuphandler with embedded IFrames.
        hPopupInfo.useExternalPopupHandler = false;
        hPopupInfo.showModalDialog = true;
        //Open popup as normal, passing reference to our return data object - should set success during the payments process only if popup rendered 
        ttlayout_openPopup(hPopupInfo, null, window);
    }
    //refresh the mainwindow after modal dialog closes
    if (popupSucceeded == true) 
    {
        ttlayout_Refresh();
    }
    else 
    {
        tterror_showOk("You are about to initiate the e-Payment process to take payment for your enrolment.<br/>During this process we will open a popup that displays the NetBanx screen.</br></br/>Please click <b>OK</b> to continue.","showPaymentPopup(true)");
    }
}

function ShowNavigateMessage(showNetbanx, showPreReq, showPreReqYesNo, requisitesWarningHeader, requisitesWarningBody, coReqMessage, apostropheToken, userAnswer) 
{
    var returnVal = false;
    if (userAnswer == undefined) {
        SaveLastLinkID();

        var messageToDisplay = "";
        if (showPreReq) {
            var header = QUESTIONROW.replace("{0}", requisitesWarningHeader);
            var body = QUESTIONROW.replace("{0}", requisitesWarningBody);

            messageToDisplay = header +  coReqMessage + body;
        }
        if (showNetbanx) {
            if (showPreReq) {
                if (showPreReqYesNo) {
                    messageToDisplay += CLIENT_MESSAGE_EPAYMENT_NAVIGATE_YES_NO;
                }
                else {
                    //cannot navigate so don't display the netbanx message
                }
            }
            else {
                messageToDisplay += CLIENT_MESSAGE_EPAYMENT_NAVIGATE;
            }
        }

        var regExp = new RegExp(apostropheToken,"g");
        messageToDisplay = messageToDisplay.replace(regExp, "'");
        if (showPreReqYesNo) 
        {
            tterror_showYesNo(messageToDisplay, "ShowNavigateMessage(" + showNetbanx + "," + showPreReq + "," + showPreReqYesNo + ",'" + requisitesWarningHeader + "','" + requisitesWarningBody + "','" + coReqMessage + "','" + apostropheToken + "', true)", "ShowNavigateMessage(" + showNetbanx + "," + showPreReq + "," + showPreReqYesNo + ",'" + requisitesWarningHeader + "','" + requisitesWarningBody + "','" + coReqMessage + "','" + apostropheToken + "', false)");
        }
        else 
        {
            tterror_showOk(messageToDisplay, "ShowNavigateMessage(" + showNetbanx + "," + showPreReq + "," + showPreReqYesNo + ",'" + requisitesWarningHeader + "','" + requisitesWarningBody + "','" + coReqMessage + "','" + apostropheToken + "', " + (showPreReqYesNo == false && showPreReq ? false : true) + ")");
        }
   }
    else {
        if (userAnswer == true) {
            PostBackToClientWithSavedLinkID();
        }
        returnVal = userAnswer;
    }
    return returnVal;
}

function ShowEpaymentNavigateMessage(requisitesWarningHeader, requisitesWarningBody, coreqs, answer) {
    var returnVal = false;
    if (answer == undefined) 
    {
        SaveLastLinkID();

        var header = REQUISITESWARNINGHEADER.replace("{0}", requisitesWarningHeader);
        var footer = REQUISITESWARNINGBODY.replace("{0}", requisitesWarningBody);

        tterror_showOk(header + coreqs + CLIENT_MESSAGE_EPAYMENT_NAVIGATE, "ShowEpaymentNavigateMessage('', true)");
    }
    else 
    {
        if (answer == true) 
        {
            PostBackToClientWithSavedLinkID();
        }
        returnVal = answer;
    }
    return returnVal;
}

function SaveLastLinkID() {
    g_objLinkID = tttb_getLastLinkClicked();
}
function PostBackToClientWithSavedLinkID() {
    //use the g_strLinkID saved earlier with SaveLastLinkID
    tttb_raisePostBack(g_objLinkID);

}




