﻿function __disableRightClick(e)
{
    if(!document.rightClickDisabled)
    {
        if(document.layers) 
        {
            document.captureEvents(Event.MOUSEDOWN);
            document.onmousedown = disableRightClick;
        }
        else document.oncontextmenu = __disableRightClick;
        return document.rightClickDisabled = true;
    }
    if(document.layers || (document.getElementById && !document.all))
    {
        if (e.which==2||e.which==3) return false;
    } 
    else return false;
}

function __disableSelect()
{
    document.onselectstart = function () { return false; };
    document.onmousedown = function () { return false; };
    document.onclick = function() { return true; };
    
}

function selectallchk(chk)
{
	var chks=document.getElementsByTagName("input");
	for (var i=0;i<chks.length;i++)
	{
		if (chks[i].id.indexOf("chkSelect")>=0)
		{
			chks[i].checked = chk.checked;
		}
	}
}

function __initClientScript(sender, args)
{
	if (args && args.get_error() != undefined)
	{
        args.set_errorHandled(true);
		window.location.reload(false);        
	}

	//button control initialization
	var ins=document.getElementsByTagName("input");
	var inp;
	for (var i=0;i<ins.length;i++)
	{
	    inp = ins[i];
	    if (inp.className == "button" || inp.className == "obutton")
	    {
	        //inp.style.filter = "progid:DXImageTransform.Microsoft.Shadow(color= '#999999' , Direction=135, Strength=3)";
	        //inp.onmouseover = function () { this.style.filter = "progid:DXImageTransform.Microsoft.Shadow(color= '#555555' , Direction=135, Strength=3)"; };
	        //inp.onmouseout = function () { this.style.filter = "progid:DXImageTransform.Microsoft.Shadow(color= '#999999' , Direction=135, Strength=3)"; };
	    } 
	    else if (inp.id.indexOf("chkAll")>=0)
		{
			inp.onclick = function () { selectallchk(this) };
		}		
	}
	
	//data grid initialization
	var trs=document.getElementsByTagName("tr");
	var tr;
	for (var i=0;i<trs.length;i++)
	{
		tr = trs[i];
		if (tr.className=="normalitem" || tr.className=="alternatingitem" || tr.className=="footer")
		{
			tr.onmouseover = function () { this.style.backgroundColor="#fbffb8"; };
			tr.onmouseout = function () { this.style.backgroundColor=""; };
		}
	    else if (tr.className=="headertab-background")
	    {
	        var tds=tr.getElementsByTagName("td");   
		    for (var j=0;j<tds.length;j++)
		    {
		        td = tds[j];
		        if (td.className == "tabmenu")
		        {
                    td.onmouseover = function() 
                    {
                        //this.style.filter = "progid:DXImageTransform.Microsoft.Fade(duration=0.3)"; 
                        this.className = "tabmenu-highlight";
                    }
                    
                    td.onmouseout = function()
                    {                                                
                        //if (this.filters) this.filters[0].Apply();
                        this.className = "tabmenu";
                        //if (this.filters) this.filters[0].Play();                        
                    }
    		            
	                td.onclick = function()
	                {
                        var lnk = this.getElementsByTagName("a")[0];
                        window.location = lnk.href;
	                }
	            }	            
		    }	        
	    }		
	}

	__displayProgress(false);	
	//__disableSelect();
}

function __displayProgress(show, args)
{
    try 
    {
        if (show) 
            __showProgress(); 
        else 
            __hideProgress(); 
    } catch (e) { }
    
    return true;
}

function __hideProgress()
{
    document.getElementById("__loader").style.display="none";
}

function __showProgress()
{
    document.getElementById("__loader").style.display="";
    setTimeout("__hideProgress()", 10000);    
}

function __initGlobal()
{
    if (window.addEventListener) 
        window.addEventListener("onbeforeunload", function () { return __displayProgress(true) }, false);            
    else if (window.attachEvent) 
        window.attachEvent("onbeforeunload", function () { return __displayProgress(true) });            
    else if (document.getElementById)
        window.onbeforeunload = window.attachEvent("onbeforeunload", function () { return __displayProgress(true) });
    
    //__disableRightClick();	
    if(typeof(Sys)=="object") 
    {
        Sys.Application.notifyScriptLoaded();
        Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(__displayProgress);    
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(__initClientScript);
    }
}

function jobmouseover(tbl)
{   
    tbl.style.cursor = "pointer";
    tbl.getElementsByTagName("TD")[0].style.textDecoration = "underline";
    tbl.style.backgroundImage = "url(images/careerbg.jpg)";
    tbl.style.color = "white";
}

function jobmouseout(tbl)
{
    tbl.style.cursor = "";
    tbl.getElementsByTagName("TD")[0].style.textDecoration = "";
    tbl.style.backgroundImage = "";
    tbl.style.color = "";
}

function jobclick(tbl, jid)
{
        wopen("opening.aspx?id=" + jid, "", 800, 600);
}

function wopen(url, name, w, h)
{
      w += 32;
      h += 96;
      wleft = (screen.width - w) / 2;
      wtop = (screen.height - h) / 2;
      if (wleft < 0) {
        w = screen.width;
        wleft = 0;
      }
      if (wtop < 0) {
        h = screen.height;
        wtop = 0;
      }
      var win = window.open(url,
        name,
        'width=' + w + ',height=' + h + ',' +
        'left=' + wleft + ',top=' + wtop + ',' +
        'location=no,menubar=no,' +
        'status=no,toolbar=no,scrollbars=yes,resizable=no');
      win.moveTo(wleft, wtop);
      win.resizeTo(w, h);
      win.focus();
      return false;
}

function redirectMain(page)
{
    if (window.opener)
    {    
        window.opener.location.href = page;
        window.opener.focus();
    }
    window.close();
    return false;
}

__initGlobal();
__initClientScript();

function previewQuestion(qid, lid)
{
    openWindow("../../questionintro.aspx?preview=1&qid=" + qid);    
}

function previewQuestionnaire(qid)
{
    openWindow("../questionintro.aspx?preview=1&qid=" + qid );    
}

function previewResQuestionnaire(qid,qiid)
{
    openWindow("../questionstart.aspx?&preview=1&res=1&qid=" + qid + "&qiid=" + qiid);    
}

function openQuestion(qid, aid, iid)
{
    openWindow("../../questionintro.aspx?preview=1&qid=" + qid + "&aid=" + aid + "&iid=" + iid);    
}

function previewExam(qid, lid)
{
    openWindow("../../examintro.aspx?preview=1&qid=" + qid);    
}


function openWindow(url)
{   
    var newWindow = window.open(url, "Question", "scrollbars");
    newWindow.focus();
    return newWindow;
}


function keepSessionAlive()
{
    var img = new Image(1,1);
    img.src = "ka.aspx?r=" + Math.random();
    window.setTimeout("keepSessionAlive()", 300000);
}   



function disableSelect()
{
    window.document.onselectstart = function () { return false; };
    window.document.onmousedown = function () { return false; };
    window.document.onclick = function() { return true; };
}

function disableRightClick(e)
{
    if(!window.document.rightClickDisabled)
    {
        if(window.document.layers) 
        {
            window.document.captureEvents(Event.MOUSEDOWN);
            window.document.onmousedown = disableRightClick;
        }
        else window.document.oncontextmenu = disableRightClick;
            
        return window.document.rightClickDisabled = true;
    }
    if(window.document.layers || (window.document.getElementById && !window.document.all))
    {
        if (e.which==2||e.which==3) return false;
    } 
    else return false;
}


function questionOnBeforeUnload(evt) 
{
    var preview = window.document.getElementById("questionpreview");
    
    if (preview.value == "0")
    {
        window.onbeforeunload = function (evt) 
        {     
              var message = 'Your answers for this session will not be saved if you close this window.';
              if (typeof evt == 'undefined') evt = window.event;
              if (evt) evt.returnValue = message;
              return message;
         }
    }
}

function validateExam()
{
    window.onbeforeunload = null;
    return false;
}

function validateQuestion(btn)
{    
    var result = true;   
    var error = window.document.getElementById("sectionerror");
    var preview = window.document.getElementById("questionpreview");
    var isCustomQuestion = window.document.getElementById("IsCustomQuestion");
    var trs = window.document.getElementsByTagName("tr");
    var tr = null;
    var tr1 = null;
    
    if (btn.disabled || preview.value == "1") return true;
    
    if (isCustomQuestion.value == '1')
    {
        result = false;
        for (var i=0; i < trs.length; i++)
        {
            tr = trs[i];
            if (tr.className == "question")
            {
                if (tr.isAnswered != null && tr.isQOAnswered != null)
                {
                    result = true;
                    break;
                }
             }
        }
        if (!result)
        {
            error.innerText = "Atleast one question must be answered.";
            error.style.display = "";
            alert(error.innerText);
        }  
        else
        {
            error.style.display = "none";
            window.onbeforeunload = null;
        }
        return result;
    }
    else
    {
        for (var i=0; i < trs.length; i++)
        {
            tr = trs[i];
            if (tr.className == "question")
            {
                if (tr.isAnswered == null || tr.isQOAnswered == null)
                {
                    if (tr1 == null) tr1 = tr;
                    tr.style.color = "red";
                    tr.cells[0].innerText = "*";
                    result = false;
                }
                else
                {            
                    tr.style.color = "";
                    tr.cells[0].innerText = "";   
                }
             }
        }
        
        if (!result)
        {
            error.innerText = "Question(s) marked in red must be answered.";
            error.style.display = "";        
            tr1.scrollIntoView();
            alert(error.innerText);
        }    
        else
        {
            error.style.display = "none";
            window.onbeforeunload = null;
        }
        return result;
    }
}



function mouseOver(tr)
{
    tr.style.backgroundColor = "#f5ffcc";
}

function mouseOut(tr)
{
    tr.style.backgroundColor = "";
}

function matrixClick(rad)
{
    var tr = rad.parentElement.parentElement;
    tr.isAnswered = 1;
}

function rankChange(sel)
{
    var tr = sel.parentElement.parentElement;
    var sls = tr.parentElement.getElementsByTagName("select");
    for (var i = 0; i < sls.length; i++)
    {
        if (sls[i].name != sel.name && sls[i].value == sel.value) 
        {
            sls[i].value = "";
            sls[i].parentElement.parentElement.isAnswered = null;
        }
    }
        
    if (trim(sel.value) != "") tr.isAnswered = 1; else tr.isAnswered = null;
}

function singleChoiceClick(rad)
{
    var tr = GetTRParent(rad);
    while (tr.tagName.toUpperCase() != "TR")
    {
        if (tr.parentElement == 'undefined')
            break;
        tr = GetTRParent(tr);
    }
    //var tr = rad.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement;    
    tr.isAnswered = 1; // set isAnswered property of question<TR> to 1
    
    computeScore(tr, rad);
}

function computeScore(tr, rad)
{
    //preview score for online interview assessment
    var assessment = window.document.getElementById("isAssessment");    
    if (assessment && assessment.value == '1')
    {
        var qlist = window.document.getElementById('questionList');
        var slist = window.document.getElementById('scoreList');
        var mlist = window.document.getElementById('maxScoreList');
        if (tr.isAnswered && tr.isQOAnswered && rad && qlist.value.indexOf(rad.name + ',') == -1)
        {
            qlist.value = qlist.value + rad.name + ',';
            slist.value = slist.value + rad.score + ',';
            mlist.value = mlist.value + rad.maxScore + ',';
        }
        else
        {
            qarr = qlist.value.split(',');
            sarr = slist.value.split(',');
            marr = mlist.value.split(',');
            qlist.value = '';
            slist.value = '';
            mlist.value = '';
            for (i = 0; i < qarr.length; i++)
            {
                if (qarr[i] != "")
                {
                    if (rad && rad.name == qarr[i])
                    {
                        sarr[i] = rad.score;
                    }
                    if (rad && rad.name == qarr[i] && (!tr.isAnswered || !tr.isQOAnswered))
                    {
                        
                    }
                    else
                    {                    
                        qlist.value = qlist.value + qarr[i] + ',';
                        slist.value = slist.value + sarr[i] + ',';
                        mlist.value = mlist.value + marr[i] + ',';
                    }
                }
            }
        }
        displayScore();        
    }
}

function displayScore()
{
    var assessment = window.document.getElementById("isAssessment");    
    if (assessment && assessment.value == '1')
    {
        var score = 0;
        var maxscore = 0;            
        var divscoreTop = window.document.getElementById('divScoreTop');
        var divscoreBottom = window.document.getElementById('divScoreBottom');
        var slist = window.document.getElementById('scoreList');
        var mlist = window.document.getElementById('maxScoreList');
        var customquestion = window.document.getElementById('isCustomQuestion');
        var sarr = slist.value.split(',');
        var marr = mlist.value.split(',');
                
        for (i = 0; i < sarr.length; i++)
        {
            if (sarr[i] != "") 
            {
                score = score + new Number(sarr[i]);
                maxscore = maxscore + new Number(marr[i]);
            }
        }
        
        if (customquestion.value == '0')
        {
            maxscore =  window.document.getElementById('maxScore').value;
        }
        
        if (maxscore > 0)
        {
            var current = ((score / maxscore) * 100).toFixed(2);
            divScoreTop.innerText = "Current Score: " + current;
            divScoreBottom.innerText = divScoreTop.innerText;
        }
        else
        {
            divScoreTop.innerText = "";
            divScoreBottom.innerText = "";
        }
    }
}

function questionTextChange(txt)
{
    var tr = GetTRParent(txt);
    var rad = null;
        
    while (tr.tagName.toUpperCase() != "TR")
    {
        if (tr.parentElement == 'undefined')
            break;
        tr = GetTRParent(tr);
    }    
    if (trim(txt.value) == "") tr.isQOAnswered = null; else tr.isQOAnswered = 1;  
    
    var inps = tr.getElementsByTagName("input");
    for (var i=0; i<inps.length; i++)
    {
        if (inps[i].type == "radio" && inps[i].checked)
        {
            rad = inps[i];
        }
    }
    
    computeScore(tr, rad);
}

function singleChoiceOptionClick(rad)
{
    //               <DIV>         <DIV>         <DIV>         <TD>          <TR>
    var tr = GetTRParent(rad);
    while (tr.tagName.toUpperCase() != "TR")
    {
        if (tr.parentElement == 'undefined')
            break;
        tr = GetTRParent(tr);
    }
    //var tr = rad.parentElement.parentElement.parentElement.parentElement.parentElement;    
    tr.isQOAnswered = 1; // set isAnswered property of question<TR> to 1
    
    var rad1 = null;
    var inps = tr.getElementsByTagName("input");
    for (var i=0; i<inps.length; i++)
    {
        if (inps[i].type == "radio" && inps[i].checked)
        {
            rad1 = inps[i];
        }
    }
    
    computeScore(tr, rad1);    
}

function GetTRParent(pe)
{
    return pe.parentElement;
}


function singleChoiceOtherClick(rad)
{
    var div = rad.parentElement;
    var tr = rad.parentElement.parentElement.parentElement.parentElement;
    var txt = div.getElementsByTagName("input")[1];
    if (trim(txt.value) == "") tr.isAnswered = null; else tr.isAnswered = 1;
    txt.focus();
}

function otherChoiceChange(txt)
{
    var div = txt.parentElement;
    var rad = div.getElementsByTagName("input")[0];
    var tr = rad.parentElement.parentElement.parentElement.parentElement;
    if (trim(txt.value) != "") 
    {
        rad.value = txt.value;
        rad.checked = true;        
        tr.isAnswered = 1;
    }
}

function shortTextChange(txt)
{
    var tbl = txt.parentElement.parentElement.parentElement;
    var tr = tbl.parentElement.parentElement.parentElement;
    var txs = tbl.getElementsByTagName("input");
    var result = "";
    var str;
    for (var i = 0; i < txs.length; i++)
    {
        str = trim(txs[i].value);
        if (str == "") 
        {
            result = "";
            break;
        }
        else if (result == "")
        {
            result = str;
        }
        else
        {
            result = result + "|" + str;
        }
    }

    if (result == "") tr.isAnswered = null; else tr.isAnswered = 1;
    document.getElementById(txt.textID).value = result;    
    
}

function longTextChange(txt)
{
    var tr = txt.parentElement.parentElement;
    if (trim(txt.value) == "") tr.isAnswered = null; else tr.isAnswered = 1;
}

function trim(str)
{
    return str.replace(/\s*/, "");
}

function navigatePrevious()
{
    window.onbeforeunload = null;
    return true;
}

function sourceChange(ddl, refValue, otherValue, spnReferrerTitle, tdName, tdContact, tdRelationship)
{
   if (ddl.value == refValue)   
   {
        document.getElementById(spnReferrerTitle).innerText = "Referrer Name";
        document.getElementById(tdName).style.visibility = "";
        document.getElementById(tdContact).style.visibility = "";
        document.getElementById(tdRelationship).style.visibility = "";
   }
   else if (ddl.value == otherValue)
   {
        document.getElementById(spnReferrerTitle).innerText = "Specify Other Source";
        document.getElementById(tdName).style.visibility = "";
        document.getElementById(tdContact).style.visibility = "hidden";
        document.getElementById(tdRelationship).style.visibility = "hidden";
   }
   else
   {
        document.getElementById(tdName).style.visibility = "hidden";
        document.getElementById(tdContact).style.visibility = "hidden";
        document.getElementById(tdRelationship).style.visibility = "hidden";
   }
}

function PreviewChart(url)
{
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var string_length = 8;
    var randomstring = '';
    for (var i=0; i<string_length; i++) {
	    var rnum = Math.floor(Math.random() * chars.length);
	    randomstring += chars.substring(rnum,rnum+1);
    }
    window.open(url,randomstring,'height=450px,width=650px,resizable=yes,scrollbars=yes,status=yes,toolbar=no,menubar=no,location=center');
    return false;
}