﻿var PopUpWindow;
if (PopUpWindow)
{
    window.onunload = function() {    
    }
}
//This will disable Background and Create Warning Message Div.
function DisableBackground(){                        
    blDisabledBackground = true;
    if(document.getElementById("blurDiv")){ //Div Already Created
        document.getElementById("blurDiv").style.display = "";
    }
    else{
        var blurDiv = document.createElement("div");
        blurDiv.id = "blurDiv";
        blurDiv.style.cssText = "position:absolute; top:0; left:0; background-color: #ffffff; opacity:0.8; filter:alpha(opacity=80); -moz-opacity:0.8;  z-index:4;"
        document.getElementsByTagName("body")[0].appendChild(blurDiv);
        CreateWarnBackground();
    }
    document.getElementById('blurDiv').style.height = document.body.clientHeight + parseInt(15) + 'px';
    document.getElementById('blurDiv').style.width = document.body.clientWidth + parseInt(15) + 'px';
    
    //Page Setting
    window.scrollTo(0,0);
    KillMySession(0); //Ajax Call.
    document.documentElement.style.overflow='hidden';
    
    //Remove Autosave Interval If Present.
    if(IsAutosave == true){    clearInterval(autoInerval);  isAutosaveFlag = false;    }
}

//This is Warning message Div & after clicking OK setTimeout will get reset.
function CreateWarnBackground(){
    if(document.getElementById("blurDivWarn")){ //Div Already Created
        document.getElementById("blurDivWarn").style.display = "";
    }
    else{
        var blurDivWarn = document.createElement("div");
        blurDivWarn.id = "blurDivWarn";
        blurDivWarn.style.cssText = "position:absolute; width:352px; z-index:5; left: 300px; top: 170px; border:3px solid " + BorderColor + "; padding:15px; background-color: White";
        //blurDivWarn.innerHTML = "<font size='2' face=" + FontFamily + " color=" + FontColor + ">For security reasons your Online Survey session will time out in a few minutes if there is no additional activity.</font>"
        //blurDivWarn.innerHTML = blurDivWarn.innerHTML + "<font size='2' face=" + FontFamily + " color=" + FontColor + ">&nbsp;If you are still working on your survey and would like the session not to expire, simply click OK.<br/><br/></font>"
        blurDivWarn.innerHTML = "<font size='2' face=" + FontFamily + " color=" + FontColor + ">It appears that you have been on this page for an extended period.<br /></font>"
        blurDivWarn.innerHTML = blurDivWarn.innerHTML + "<font size='2' face=" + FontFamily + " color=" + FontColor + ">If you require additional time, please click &quot;OK&quot;.<br />Failure to do so will result in loss of your responses.<br/><br/></font>"
        blurDivWarn.innerHTML = blurDivWarn.innerHTML + "<div align='center'><a id='lnkOK' href='#' OnClick='EnableBackGround();'><font size='2' face=" + FontFamily + " color=" + FontColor + "><b>OK</b></font></a></div>";
        document.getElementsByTagName("body")[0].appendChild(blurDivWarn);
    }
    
    DisableDD();
    DisableNEXTBACKButton();
    ResetTimeOut(setTimeOutWarning1);
    setTimeOutWarning2 = setTimeout("CreateCloseBackground();", parseInt(getTimeWarning2));
	document.getElementById('lnkOK').focus();
	document.onkeydown = checkKeycode;
}

var keycode;
function checkKeycode(e) {
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    if(keycode == 32) {
        if(document.getElementById("blurDivWarn")) {
            if(document.getElementById("blurDivWarn").style.display == "")  return false;
        }
    }
}

//This is Final Div after this user can only close Broswer Tab.
function CreateCloseBackground(){
    DisableLinks();
    document.getElementById("blurDivWarn").style.display = "none";
    if(document.getElementById("blurDivNew")){ //Div Already Created
        document.getElementById("blurDivNew").style.display = "";
    }
    else{
        var blurDivNew = document.createElement("div");
        blurDivNew.id = "blurDivNew";
        blurDivNew.style.cssText = "position:absolute; width:352px; z-index:5; left: 300px; top: 170px; border:3px solid " + BorderColor + "; padding:15px; background-color: White";
        blurDivNew.innerHTML = "<font size='2' face=" + FontFamily + " color=" + FontColor + ">For security reasons your Online Survey session has been timed out due to inactivity.</font>";
        blurDivNew.innerHTML = blurDivNew.innerHTML + "<font size='2' face=" + FontFamily + " color=" + FontColor + ">&nbsp;You can click on the participation link again to complete this survey.<br/><br/></font>";
        if(DisableBrowser != 'true') {
            blurDivNew.innerHTML = blurDivNew.innerHTML + "<div align='center'><a href='#' OnClick='javascript:self.close();'><font size='2' face=" + FontFamily + " color=" + FontColor + "><b>Close</b></font></a></div>";
        }
        document.getElementsByTagName("body")[0].appendChild(blurDivNew);
    }
    window.scrollTo(0,0);

    DisableDD();
    DisableNEXTBACKButton();
    ResetTimeOut(setTimeOutWarning2);
    KillMySession(2); //Ajax Call.
}

//This will Enable the Background and RESET already created Timeout.
function EnableBackGround(){
    blDisabledBackground = false;
    var blurDiv = document.getElementById("blurDiv");
    blurDiv.parentNode.removeChild(blurDiv);
    document.getElementById("blurDivWarn").style.display = "none";
    //RestartTimeOut(setTimeOutWarning2);
	RestartTimeOut_New(setTimeOutWarning2);
    
    //Page Setting
    window.scrollTo(0,0);
    document.documentElement.style.overflow = '';
 
    EnableDD();
    EnableNEXTBACKButton();
    KillMySession(1); //Ajax Call.
	document.onkeydown = '';
}

//Call to RESET TimeOut.
function ResetTimeOut(objTimeOut){  clearTimeout(objTimeOut);   }

//Call On Body OnClick Event and in EnableBackGround function.
function RestartTimeOut(objTimeOut){
    //ResetTimeOut(objTimeOut);
    //setTimeOutWarning1 = setTimeout("DisableBackground();", parseInt(getTimeWarning1));
}

//Call On Body OnClick Event and in EnableBackGround function.
function RestartTimeOut_New(objTimeOut){
    ResetTimeOut(objTimeOut);
    setTimeOutWarning1 = setTimeout("DisableBackground();", parseInt(getTimeWarning1));
}

//Disable NEXT/BACK BUTTON
function DisableNEXTBACKButton() {
    var IE_Prv ;
    var IE_Last ;
    
    if(document.getElementsByName("BUTTON_BACK")) {
        IE_Prv = document.getElementsByName("BUTTON_BACK");
    }
    
    if(document.getElementsByName("BUTTON_SUBMIT")) {
        IE_Last = document.getElementsByName("BUTTON_SUBMIT");
    }
    
    for(var i=0; i < IE_Prv.length; i++) {
        IE_Prv[i].disabled = true;
    }
    
    for(var j=0; j < IE_Last.length; j++) {
        IE_Last[j].disabled = true;
    }
}

//Enable NEXT/BACK BUTTON
function EnableNEXTBACKButton() {
    var IE_Prv ;
    var IE_Last ;
    
    if(document.getElementsByName("BUTTON_BACK")) {
        IE_Prv = document.getElementsByName("BUTTON_BACK");
    }
    
    if(document.getElementsByName("BUTTON_SUBMIT")) {
        IE_Last = document.getElementsByName("BUTTON_SUBMIT");
    }
    
    for(var i=0; i < IE_Prv.length; i++) {
        IE_Prv[i].disabled = false;
    }
    
    for(var j=0; j < IE_Last.length; j++) {
        IE_Last[j].disabled = false;
    }
}

//Disable ALL Links.
function DisableLinks() {
    obj = document.getElementsByTagName("a");
    var i=0, intCount=obj.length;
    for (i=0; i<intCount; i++){
        obj[i].setAttribute('onclick', 'javascript:return false;')
        obj[i].setAttribute('target','')
        obj[i].setAttribute('href','#')
    }
}

//Disable DropDowns IE 6 Issue.
function DisableDD() {
    obj = document.getElementsByTagName("select");
    var i=0, intCount=obj.length;
    for (i=0; i<intCount; i++){
        obj[i].setAttribute('disabled', 'disable');
    }
}

//Enable DropDowns IE 6 Issue.
function EnableDD() {
    obj = document.getElementsByTagName("select");
    var i=0, intCount=obj.length;
    for (i=0; i<intCount; i++){
        obj[i].removeAttribute('disabled');
    }
}

//Ajax Script to Kill Session.
function createRequestObject() {
    var req;
    if(window.XMLHttpRequest){
        req = new XMLHttpRequest(); //Firefox, Safari, Opera.
    } else if(window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP"); //Internet Explorer 5+
    } else {    // There is an error creating the object, just as an old browser is being used.
    }
        return req;
}

//Create Ajax request.
function CreateRequest(){ this.ajaxObject = createRequestObject(); }

//Serverside Call.
function KillMySession(MessageType){
    var clsAjax = new CreateRequest();
    var status = false;
    var contentType = "application/x-www-form-urlencoded; charset=UTF-8";

    clsAjax.ajaxObject.onreadystatechange=function (){
        if (clsAjax.ajaxObject.readyState == 4) {
        //Do Nothing.
        }
    }
    clsAjax.ajaxObject.open("GET", "../../clients/KillSession.aspx?RecNo=" + RecNo + "&CorpNo=" + CorpNo + "&SurveyNo=" + SurveyNo + "&Email=" + Email + "&MsgType=" + MessageType + "&UID=" + UID, true);
    clsAjax.ajaxObject.send(null);
}
