// JavaScript Document

store = "";
var exc, mod, sct;
myPage = new Object();
myPage.cont=0;
myPage.dir=0;
myObject = new Object();
myObject.studid="hello";
myObject.exc = "1";
myObject.mod = "0";
myObject.modNxt = 0;
myObject.modid = 0;
myObject.dotid = 0;
myObject.sct = "0";
myObject.assgn = "";
myObject.nuName = "";

window.onbeforeunload = function() {
  qs();
  if(qsParm['func']=="content"||qsParm['func']=="cases"){
    //alert("The Back Button will not respond as expected on this website. Please use the available links provided in the content.");
  }
}

function openTxtBox(id){
  document.getElementById(id).innerHTML = "<form name=\"pteForm\" action=\"pteSaveTxt.php\" method=\"post\"><textarea name=\"texttobesaved\" style=\"width: 636px; height: 100px;\" ></textarea><br /><span style=\"cursor: pointer; color: blue;\" onclick=\"javascript:document.pteForm.submit();\">Save Text</span></form>";
}

function savePTE(id){
  var browser=navigator.appName;
  var b_version=navigator.appVersion;
  var version=parseFloat(b_version);
  
  alert("Browser name: "+ browser+" Browser version: "+ version);
  //document.execCommand('SaveAs');
  //alert(browser.toLowerCase().search("explorer"));
  if(browser.toLowerCase().search("explorer")!=-1){  
       var win = window.open('', '_blank', 'top=10000');
       win.document.open('text/html', '_blank');
       win.document.charset="iso-8859-1";
       win.document.write(document.getElementById('txt_'+id).innerHTML);
       win.document.execCommand('SaveAs',true,'txt_'+id+'.txt');
       win.close();
  }else{
  
  }
}

function showSubmittedWork(key,value){
  thisElement = document.getElementById(key)?document.getElementById(key):document.getElementById(value);  
  thisKey = document.getElementById(key)?key:value;
  //document.write(key+":"+value+":"+document.getElementById(thisKey).type+"<br />");
  if(thisElement){
    switch(document.getElementById(thisKey).type){
      case "radio":
        document.getElementById(value).checked=true;
        break;
      case "select-one":        
        document.getElementById(key).value=value;
        break;  
    }
  }      
}

function checkPasswordsMatch(){
    if(document.getElementById('password').value==document.getElementById('confpass').value){
      alert("Congatulations! Settings have changed. You will now be redirected to the home page.");
      document.forms[0].submit();
    }else{
      alert("Passwords do not match."); 
    } 
}

function init(catnum,modid,contentid){
  //alert(catnum+":"+modid+":"+contentid);
  qs();  
  myObject.offering = qsParm['offering'];
  //alert(qsParm['skin']);
  myObject.catnum = catnum;
  myObject.modid = modid;
  var url = urls.plugins+"functions_http.php?func="+qsParm['func']+"&skin="+qsParm['skin']+"&menu=modinit&catnum="+catnum+"&offering="+myObject.offering;
  var modreq=getHTTPObject(); 
  modreq.open("GET", url, true);  
  modreq.onreadystatechange=function(){
    if (modreq.readyState==4) {
      if (modreq.status==200) {
        //alert(modreq.responseText);
        document.getElementById('wholePage').innerHTML = modreq.responseText;
        openSpecial(contentid,myObject.modid,myObject.exc);
        //classContent(contentid,myObject.modid,myObject.exc);
      }
    }
  }
  modreq.send(null);   
}

function makeIframe(tableName,sectid,catnum,contentid){

/*
  iFrameTxt="<iframe name=\"myIframeUpload\" src=\"http://208.48.48.137/home/uploadSilo.php?tableName="+tableName+"&catnum="+catnum+"&contentid="+contentid+"\" title=\"UpLoad Tool\" id=\"myIframeUpload\">";
  iFrameTxt+="<!-- Alternate content for non-supporting browsers -->";
  iFrameTxt+="<p>browser must support iframes</p>";
  iFrameTxt+="</iframe>";
  document.getElementById("myIframe").innerHTML = iFrameTxt;
*/
}

function uploadAssgn(id,sect,catnum){
  //alert(id+":"+sect+":"+catnum);
  document.getElementById("wholePage").innerHTML = "";
  var url = urls.plugins+"functions_http.php?menu=uploadAssgnForm&catnum="+catnum+"&itemgradebookid="+id+"&offering="+myObject.offering;
  var modreq=getHTTPObject(); 
  modreq.open("GET", url, true);  
  modreq.onreadystatechange=function(){
    if (modreq.readyState==4) {
      if (modreq.status==200) {
        document.getElementById("wholePage").innerHTML = modreq.responseText;
      }
    }
  }
  modreq.send(null);
}

function loadAssgn(catnum,itemgradebookid){
  document.getElementById("wholePage").innerHTML = "";
  var url = urls.plugins+"functions_http.php?menu=processAssgn&process=createAssgn&catnum="+catnum+"&id="+itemgradebookid;
  var modreq=getHTTPObject(); 
  modreq.open("GET", url, true);    
  modreq.onreadystatechange=function(){ 
  if(ie6){ 
      if(!modreq.getResponseHeader("Date")){
        var cached = modreq;
        modreq = (typeof(XMLHttpRequest) != "undefined") ? new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");
        var ifModifiedSince = cached.getResponseHeader("Last-Modified");
        ifModifiedSince = (ifModifiedSince) ? ifModifiedSince : new Date(0); // January 1, 1970
        modreq.open("GET", url, false);
        modreq.setRequestHeader("If-Modified-Since", ifModifiedSince);
        modreq.send("");
        if(modreq.status == 304)
        {
          modreq = cached;
        }
      }
    }
    if (modreq.readyState==4) {
      if (modreq.status==200) {        
        document.getElementById("wholePage").innerHTML = modreq.responseText;        
      }
    }
  }
  modreq.send(null);
  
}

function return2Content(contentid){
  init(myObject.catnum,myObject.modid,contentid);
}

function myAssgnSubmit(myform,catnum,id){
//alert(myform+":"+catnum+":"+id);
//alert(document.getElementById("wholePage").innerHTML);
  myObject.catnum = catnum;
  myObject.id = id;
  var tempObj; 
  var varstring = encodeURI(myform[0].name+"="+myform[0].value+"&assgnkey=assgnkey");  
  modreq = false;
  var tempTxt="";
  url = urls.plugins+"functions_http.php?menu=getAssgnType&id="+id;
  var modreq=getHTTPObject();
  modreq.open('POST', url, true);
  modreq.onreadystatechange=function(){
    if (modreq.readyState == 4) {
      if (modreq.status == 200) {        
        assgnvar = modreq.responseText;
        var assgnvar = assgnvar.split(":");
        assgntype = assgnvar[0];
        numofitems = assgnvar[1];        
        switch(assgntype){
          case "survey":      
            for(i=1; i<=numofitems; i++){
              tempObj=eval("document.forms[0].q" + i);
              for(j=0; j<tempObj.length;j++){
                if(tempObj[j].checked){
                  tempTxt = "ok";
                }
              }
              if(tempTxt==""){
                alert("Please respond to all items");
                return false;
              }
              tempTxt="";       
            }
            if (confirm ("You are allowed to submit this only once. Click Cancel if you are not ready to Submit.")) makePostCall(myform);      
            break;
          //case "wpfeed": //whole page feedback
            //if (confirm ("You are allowed to submit this only once. Click Cancel if you are not ready to Submit.")) makePostCall(myform); 
            //break;
          default:
            if (confirm ("You are allowed to submit this only once. Click Cancel if you are not ready to Submit.")) makePostCall(myform);            
            break;
        }                   
      } else {
        alert('There was a problem with makeVARRequest.');
      }
      
    }  
  }  
  modreq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  modreq.setRequestHeader("Content-length", varstring.length);
  modreq.setRequestHeader("Connection", "close");
  modreq.send(varstring);  
}

function makePostCallResubmit(myform,catnum,id){  
  //var poststr = encodeURI("myStudId="+myObject.studid+"&");
  var poststr = encodeURI("resubmit=true&");  
  for(i=0;i<myform.length;i++){
    if(myform[i].checked) poststr += encodeURI(myform[i].name+"="+myform[i].id+"&");
    if(myform[i].type=="hidden") poststr += encodeURI(myform[i].name+"="+myform[i].value+"&");
    if(myform[i].type=="textarea") poststr += encodeURI(myform[i].name+"="+myform[i].value+"&");
    if(myform[i].type=="select-one") poststr += encodeURI(myform[i].name+"="+myform[i].value+"&");
  } 
  httpPost = false;
  //alert("catnum="+myObject.catnum+"&id="+myObject.id+"&offering="+myObject.offering);
  url = urls.plugins+"functions_http.php?menu=processAssgn&process=postAssgn&catnum="+catnum+"&id="+id;
  var httpPost=getHTTPObject();
  httpPost.open('POST', url, true);
  httpPost.onreadystatechange=function(){
    if (httpPost.readyState == 4) {
      if (httpPost.status == 200) {
        result = httpPost.responseText;
        document.getElementById('wholePage').innerHTML = result;            
      } else {
        alert('There was a problem with makePOSTRequest.');
      }
    }  
  }  
  httpPost.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  httpPost.setRequestHeader("Content-length", poststr.length);
  httpPost.setRequestHeader("Connection", "close");
  httpPost.send(poststr);
} 

function doThisAction(myform){
  //alert(myForm['myAction'].value);
  var url = urls.plugins+"functions_http.php?menu=doAction&type="+myform['myAction'].value;
  var poststr = '';  
  for(i=0;i<myform['actionArray'].length;i++){
    if(myform[i].checked) poststr += encodeURI(myform[i].name+"="+myform[i].value+"&");
  }
  poststr += encodeURI(myform['esubject'].name+"="+myform['esubject'].value+"&"); 
  poststr += encodeURI(myform['ebody'].name+"="+myform['ebody'].value+"&"); 
  var httpPost=getHTTPObject(); 
  httpPost.open('POST', url, true);  
  httpPost.onreadystatechange=function(){
    if (httpPost.readyState==4) {
      if (httpPost.status==200) {
        //document.getElementById("wholePage").innerHTML = modreq.responseText;
        alert(httpPost.responseText);
      }
    }
  }
  httpPost.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  httpPost.setRequestHeader("Content-length", poststr.length);
  httpPost.setRequestHeader("Connection", "close");
  httpPost.send(poststr);
}

function makePostCall(myform){
  //var poststr = encodeURI("myStudId="+myObject.studid+"&");
  var poststr = encodeURI(myform[0].name+"="+myform[0].value+"&");
  for(i=0;i<myform.length;i++){   
    if(myform[i].checked) poststr += encodeURI(myform[i].name+"="+myform[i].id+"&");    
    if(myform[i].type=="textarea") poststr += encodeURI(myform[i].name+"="+myform[i].value+"&");
    if(myform[i].type=="select-one") poststr += encodeURI(myform[i].name+"="+myform[i].value+"&");
  } 
  makePOSTRequest(poststr);
}        

function makePOSTRequest(poststr) {
  httpPost = false;
  //alert(myObject.offering);
  //alert("catnum="+myObject.catnum+"&id="+myObject.id+"&offering="+myObject.offering);
  url = urls.plugins+"functions_http.php?menu=processAssgn&process=postAssgn&catnum="+myObject.catnum+"&id="+myObject.id+"&offering="+myObject.offering;
  var httpPost=getHTTPObject();
  httpPost.open('POST', url, true);
  httpPost.onreadystatechange=function(){
    if (httpPost.readyState == 4) {
      if (httpPost.status == 200) {
        result = httpPost.responseText;        
        document.getElementById('wholePage').innerHTML = result;            
      } else {
        alert('There was a problem with makePOSTRequest.');
      }
    }  
  }  
  httpPost.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  httpPost.setRequestHeader("Content-length", poststr.length);
  httpPost.setRequestHeader("Connection", "close");
  httpPost.send(poststr);
}

function getPage(contentid,modid,exc){
  if(exc==""){
    classContent(contentid,modid,exc);     
  }else{
    innerPiece(contentid,modid,exc); 
  }
}

function excol(exc) {
if(document.getElementById(exc)) myObject.nuName=="displayNone"?document.getElementById(exc).innerHTML= "(click to expand)":document.getElementById(exc).innerHTML= "(click to collapse)";
}

function openSpecial(contentid, modid, exc) { 
  //alert(contentid+":"+modid+":"+exc);
  qs();
  var url = urls.plugins+"functions_http.php?menu=thecontent&offering="+qsParm['offering']+"&catnum="+myObject.catnum+"&id="+contentid+"&exc="+exc+"&mod="+modid;
  var modreq=getHTTPObject(); 
  modreq.open("GET", url, true);  
  modreq.onreadystatechange=function(){
    if (modreq.readyState==4) {
      if (modreq.status==200) {
        //alert(document.getElementById("courseContent").innerHTML);
        //alert(modreq.responseText);
        document.getElementById("courseContent").innerHTML = modreq.responseText;      
        //myObject.nuName = (document.getElementById("mod"+modid).className=='displayNone')?'displayBlock':'displayNone';
        //document.getElementById("mod"+modid).className = myObject.nuName;        
        //excol("excol"+exc);
      }
    }
  }
  modreq.send(null);
  myObject.modid = modid;
  myObject.exc = exc;
}

function change2Green4IE() {
  document.getElementById("imaIEgreen").className="nextPageIE";
}

function change2Blue4IE() {
  document.getElementById("imaIEgreen").className="nextPage";
}

function classContent(contentid, modid, exc, usescroll, gotop) {   
  //alert("whirled peas");
  qs();   
  var dotid =contentid+""+modid; 
  if(document.getElementById("dd"+dotid)){
    if(document.getElementById("dd"+myObject.dotid)) document.getElementById("dd"+myObject.dotid+"_dd").className = document.getElementById("mod"+modid).className=='displayNone'?"beGone":"beGreen";
    if(document.getElementById("mod"+modid).className=='displayNone') document.getElementById("dd"+dotid+"_dd").className = "beGreen";
    if(document.getElementById("mod"+modid).className=='displayNone') myObject.dotid = dotid;
    //myObject.dotid = dotid;
  }

  //if(document.getElementById("mod"+modid).className=='displayNone'&&document.getElementById("dd"+myObject.dotid)) document.getElementById("dd"+myObject.dotid).className = "beGone";
  var url = urls.plugins+"functions_http.php?menu=thecontent&offering="+qsParm['offering']+"&catnum="+myObject.catnum+"&id="+contentid+"&exc="+exc+"&mod="+modid;
  var modreq=getHTTPObject(); 
  modreq.open("GET", url, true);  
  modreq.onreadystatechange=function(){
    if (modreq.readyState==4) {
      if (modreq.status==200) {
        if(!document.getElementById("excol"+exc)){
          document.getElementById("courseContent").innerHTML = modreq.responseText;
        }else{  
        //if(modid != myObject.modNxt && document.getElementById("excol"+exc).innerHTML=="(click to expand)") document.getElementById("courseContent").innerHTML = modreq.responseText;
          if(document.getElementById("excol"+exc).innerHTML=="(click to expand)") document.getElementById("courseContent").innerHTML = modreq.responseText;
        }
        
        //alert(document.getElementById("excol"+exc).innerHTML);
        //alert(contentid+":"+modid+":"+exc);
        myObject.modNxt = modid;        
        myObject.nuName = (document.getElementById("mod"+modid).className=='displayNone')?'displayBlock':'displayNone';
        
        document.getElementById("mod"+modid).className = myObject.nuName; 
        //alert(contentid+":"+modid+":"+exc);
        if(document.getElementById("suptop")) document.getElementById("suptop").innerHTML = "<a href=\"#\" onclick=\"classContent("+contentid+","+modid+",1,0);\" style=\"text-decoration: none;\">top</a>";
        var IE6 = (document.documentElement && document.documentElement.scrollTop);
        var NN = (window.innerHeight)
        pageTop = NN ? window.pageYOffset : (IE6 ? document.documentElement.scrollTop : document.body.scrollTop);
        bigTop = pageTop>219?pageTop:219;
        //alert(bigTop);
        if(usescroll==1&&qsParm['skin']=="pte"){
          document.getElementById("content").style.position="absolute";
          //bigTop = pageYOffset>230?pageYOffset:230;
          //alert(document.body.clientHeight); 
          //bigTop = IE6 ? 219 : bigTop; 
          if(gotop==1) bigTop = 219;        
          document.getElementById("content").style.top=bigTop+"px";
          document.getElementById("content").style.left="179px"; 
          excol("excol"+exc);
        }else{
          document.getElementById("content").style.position="absolute";
          //bigTop = pageYOffset>230?pageYOffset:230;
          //alert(document.body.clientHeight);          
          document.getElementById("content").style.top="219px";
          document.getElementById("content").style.left="179px"; 
        }         
      }
    }
  }
  modreq.send(null);
  myObject.modid = modid;  
  myObject.exc = exc;
}

function innerPiece(contentid,modid,exc,usescroll) { 
  //alert("inner peas");
  qs();
  //document.getElementById("content").scrollHeight;
  //  if(document.getElementById("dd"+contentid)) alert("dd"+contentid);
  var dotid =contentid+""+modid; 
  if(document.getElementById("dd"+dotid)){
    if(document.getElementById("dd"+myObject.dotid)) document.getElementById("dd"+myObject.dotid+"_dd").className = "beGone";
    document.getElementById("dd"+dotid+"_dd").className = "beGreen";
    myObject.dotid = dotid;
  }
  var url = urls.plugins+"functions_http.php?menu=thecontent&offering="+qsParm['offering']+"&catnum="+myObject.catnum+"&id="+contentid+"&exc="+exc+"&mod="+modid+"&rand="+Math.random();
  var modreq=getHTTPObject(); 
  modreq.open("GET", url, true);  
  modreq.onreadystatechange=function(){
    if (modreq.readyState==4) {
      if (modreq.status==200) {
        if(document.getElementById("courseContent").innerHTML != modreq.responseText)document.getElementById("courseContent").innerHTML = modreq.responseText;
        document.getElementById("suptop").innerHTML = "<a href=\"#\" onclick=\"innerPiece("+contentid+","+modid+",1,0);\" style=\"text-decoration: none;\">top</a>";
        var IE6 = (document.documentElement && document.documentElement.scrollTop);
        var NN = (window.innerHeight)
        pageTop = NN ? window.pageYOffset : (IE6 ? document.documentElement.scrollTop : document.body.scrollTop);
        bigTop = pageTop>219?pageTop:219;
        if(usescroll==1&&qsParm['skin']=="pte"){
          document.getElementById("content").style.position="absolute";
          //bigTop = pageYOffset>230?pageYOffset:230;
          //alert(document.body.clientHeight);          
          document.getElementById("content").style.top=bigTop+"px";
          document.getElementById("content").style.left="179px"; 
          excol("excol"+exc);
        }else{
          document.getElementById("content").style.position="absolute";
          //bigTop = pageYOffset>230?pageYOffset:230;
          //alert(document.body.clientHeight);          
          document.getElementById("content").style.top="219px";
          document.getElementById("content").style.left="179px"; 
        } 
        document.getElementById("mod"+modid).className = "displayBlock";  
      }
    }
  }
  modreq.send(null);
  myObject.exc = exc;
  myObject.sct = sct;
}

function initPage(){//deals with new page layout in mentor 2
  if(document.getElementById("p1")){ 
    document.getElementById("p1").className = 'displayNone';    
    myPage.dir = (myPage.cont==1)?(myPage.dir+1):1;
    if(myPage.dir>myPage.total) myPage.dir=myPage.total;
    document.getElementById("p" + myPage.dir).className = 'displayBlock';
    if(myPage.cont==1){
      if(myPage.dir==myPage.total) document.getElementById("pageNav").innerHTML = "<a href=\"#\" onclick=\"quizFunct('prev',"+myPage.total+");\"><< Go Back</a> | Page "+myPage.dir+" out of "+myPage.total+" | Continue >>";
      else if(myPage.dir==1) document.getElementById("pageNav").innerHTML = "<< Go Back | Page "+myPage.dir+" out of "+myPage.total+" | <a href=\"#\" onclick=\"quizFunct('next',"+myPage.total+");\">Continue >></a>";
      else document.getElementById("pageNav").innerHTML = "<a href=\"#\" onclick=\"quizFunct('prev',"+myPage.total+");\"><< Go Back</a> | Page "+myPage.dir+" out of "+myPage.total+" | <a href=\"#\" onclick=\"quizFunct('next',"+myPage.total+");\">Continue >></a>";
    }
    myPage.cont=0;
    myPage.total=1;
  }  
}

function quizFunct(dir,total){//deals with new page layout in mentor 2
  myPage.total = total; 
  document.getElementById("p" + myPage.dir).className = 'displayNone';   
  (dir == "prev")?(myPage.dir--):(myPage.dir++);
  if(myPage.dir<=1) myPage.dir="1";  
  if(myPage.dir>=total) myPage.dir=total;
  document.getElementById("p" + myPage.dir).className = 'displayBlock';
  if(myPage.dir==myPage.total) document.getElementById("pageNav").innerHTML = "<a href=\"#\" onclick=\"quizFunct('prev',"+myPage.total+");\"><< Go Back</a> | Page "+myPage.dir+" out of "+myPage.total+" | Continue ";
  else if(myPage.dir==1) document.getElementById("pageNav").innerHTML = "<< Go Back | Page "+myPage.dir+" out of "+myPage.total+" | <a href=\"#\" onclick=\"quizFunct('next',"+myPage.total+");\">Continue </a>";
  else document.getElementById("pageNav").innerHTML = "<a href=\"#\" onclick=\"quizFunct('prev',"+myPage.total+");\"><< Go Back</a> | Page "+myPage.dir+" out of "+myPage.total+" | <a href=\"#\" onclick=\"quizFunct('next',"+myPage.total+");\">Continue </a>";
}