var req;
var addrReq;
var addrListReq;
var showReq;
var ns4 = (document.layers) ? true : false;
var ie4 = (document.all && !document.getElementById) ? true : false;
var ie5 = (document.all && document.getElementById) ? true : false;
var ns6 = (!document.all && document.getElementById) ? true : false;

function callPage(pageUrl, divElementId, loadinglMessage, pageErrorMessage) {
     document.getElementById(divElementId).innerHTML = loadinglMessage;
     req=GetXmlHttpObject();
 
     req.onreadystatechange = function() {responsefromServer(divElementId, pageErrorMessage);};
     req.open("GET",pageUrl,true);
     req.send(null);
  }
function responsefromServer(divElementId, pageErrorMessage) {
   var output = '';
   if(req.readyState == 4) {
      if(req.status == 200) {
         output = req.responseText;
         document.getElementById(divElementId).innerHTML = output;
         } else {
         document.getElementById(divElementId).innerHTML = pageErrorMessage+"\n"+output;
         }
      }
  }

function callPageWReq(pageURL, divElementID, loadingMessage, pageErrorMessage, theReq) {
    document.getElementById(divElementID).innerHTML=loadingMessage;
    theReq=GetXmlHttpObject();
    theReq.onreadystatechange=function() {responsefromServerWReq(divElementID,pageErrorMessage,theReq)}
    theReq.open("GET",pageURL,true)
    theReq.send(null);
}
function responsefromServerWReq(divElementId, pageErrorMessage, theReq) {
   var output = '';
   if(theReq.readyState == 4) {      
      if(theReq.status == 200) {
         output = theReq.responseText;      
         document.getElementById(divElementId).innerHTML = output;
       } else {
         document.getElementById(divElementId).innerHTML = pageErrorMessage+"\n"+output;
       }
   }
}

/* this function is for tab panels */
function activeTab(tab,destpage,destdiv)
{   	

	document.getElementById("tab"+tab).className = "active";
	if(destdiv=='content')
	{
		show(false,'tabdiv');
		show(true,'content');
	}else{
		show(true,'tabdiv');
		show(false,'content');
	}	
	callPage(destpage, destdiv, '<img src=\"imgs/loading.gif\" /> content is loading, please wait...', 'error in loading page <img src=\"imgs/error_caution.gif\" />');		
}

function aPostForm(formName,divElementId) {
    var pageErrorMessage='Error in Loading page';
    req=GetXmlHttpObject();
    pagename="login1.aspx";
    passData='&dowhat=Login';
    if (null == eval("document."+formName+".UserName.value")) {
        frmuser="";
    }else{
        frmuser=eval("document."+formName+".UserName.value");
    }    
    if (null == eval("document."+formName+".Password.value")) {
        frmpassword="";   
    }else{
        frmpassword=eval("document."+formName+".Password.value");
    }   
    
    passData='dowhat=Login';
    passData=passData+'&Password='+escape(frmpassword);
    passData=passData+'&UserName='+escape(frmuser);

    req.open("POST",pagename,true); 
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    req.onreadystatechange = function() {responsefromServer(divElementId, pageErrorMessage);}; 
    req.send(passData);
}

function aQuerystringForm(formName,divElementId){
    var pageErrorMessage='Error in Loading page';
    req=GetXmlHttpObject();
    pagename="survey.aspx";  
    frmmember=eval("document."+formName+".member.value");
    frmsurveyid=eval("document."+formName+".surveyid.value");
    for (var i=0; i < document.frmSurvey.answer.length; i++)
    {
    if (document.frmSurvey.answer[i].checked)
      {
      var rad_val = document.frmSurvey.answer[i].value;
      }
    }
    frmanswer = rad_val;
    passData='member='+escape(frmmember);
    passData=passData+'&answer='+escape(frmanswer);
    passData=passData+'&surveyid='+escape(frmsurveyid);
    passData=passData+'&dowhat=save';
    
    req.open("POST",pagename,true); 
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    req.onreadystatechange = function() {responsefromServer(divElementId, pageErrorMessage);}; 
    req.send(passData);
}
function singleCall(destpage,destdiv) {
    callPage(destpage,destdiv,'<img src=\"imgs/loading.gif\" /> content is loading, please wait...', 'error in loading page');
}

function submitform(formname,idnum){
    thisform = document.getElementById(formname)
    thisform.CourseID.value = idnum
    thisform.submit()
}
function getChoices(QuestionID) {
    pageURL="adminQuizChoices.aspx?QuestionID=" + QuestionID    
    callPage(pageURL,"choices","Loading","Unable to Locate Page")
}
function getNews(storyid) {
    if (storyid > 0) {
        pageURL="newsstory.aspx?storyid=" + storyid
    }else{
        pageURL="newsstory.aspx"
    }
    callPageWReq(pageURL,"news","Loading...","No News isn't good News.",req)
}
function getNewsList(all) {
    if (all > "") {
        pageURL="newslist.aspx?all=" + all
    }else{
        pageURL="newslist.aspx"
    }
    callPageWReq(pageURL,"newslist","Loading...","No News circulation at this time.",showReq)
}
function showline(lineid,all){
    clearlines(all)
    linediv="line" + lineid
    document.getElementById(linediv).className = "show"
}
function clearlines(all) {
    if (all > "") {
        for (x=1;x<=all;x++) {
           linediv="line" + x
           document.getElementById(linediv).className = "noshow"
        }
    }
}

function nothingfunction(){
}
function GetXmlHttpObject() {
   var xmlHttp=null;
   try  { // Firefox, Opera 8.0+, Safari
     xmlHttp=new XMLHttpRequest();
   }
   catch (e) {  // Internet Explorer
      try {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e) {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
   }
   return xmlHttp;
}

function deleteline(index) {
    if(ns6){
        window.location="store/cart.aspx?dowhat=delete&index=" + index
    }
        window.location="cart.aspx?dowhat=delete&index=" + index
    
}
function showme(divname){
	showdis=document.getElementById(divname).innerHTML
	document.getElementById('showmetext').value=showdis
}
function calPickDay(theday,themonth,theyear) {
    destpage="quickCalendar.aspx?monum=" + themonth + "&daynum=" + theday + "&yearnum=" + theyear
    callPage(destpage, 'calspan', '<img src=\"imgs/loading.gif\" /> content is loading, please wait...', 'error in loading page <img src=\"imgs/error_caution.gif\" />');		

}
function popupme() {
    alert("Got Here");
}
 function EditLine(formName,divElementId,pagename,editID) {
    var pageErrorMessage='Error in Loading page';
    req=GetXmlHttpObject();    
    passData='dowhat=Edit';
    passData=passData+'&editID='+editID;
    req.open("POST",pagename,true); 
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    req.onreadystatechange = function() {responsefromServer(divElementId, pageErrorMessage);}; 
    req.send(passData);
}
function savePwEdit(fieldName) {
    editVal=document.getElementById(fieldName).value;
    pagename="adminStudents.aspx";
    pagename=pagename+"?dw=save&sidstr="+fieldName;
    pagename=pagename+"&saveval="+editVal;
    parent.location = pagename
}
function gotoPage(pagename)  {
    parent.location = pagename
}
function gotoStorePage(pagename)  {   
    if(ns6){
        pagename="store/" + pagename
    }
    parent.location = pagename
}
function gotoPageDeac(pagename)  {
    var answer = confirm("Deactivate this Member?")
	if (answer){
		alert("Member Deactivated")
		parent.location = pagename
	}
	else{
		alert("Cancelled")
	}

    
}

function show(sw,obj) {
	// show/hide the divisions
	var brow=((navigator.appName)+(parseInt(navigator.appVersion)));
	alert(brow);
	if (sw && (ie4 || ie5) ) document.all[obj].style.visibility = 'visible';
	if (!sw && (ie4 || ie5) ) document.all[obj].style.visibility = 'hidden';
	if (sw && ns4) document.layers[obj].visibility = 'visible';
	if (!sw && ns4) document.layers[obj].visibility = 'hidden';
	if (sw && brow=="Netscape5") document.all[obj].style.visibility = 'visible';
	if (!sw && brow=="Netscape5") document.all[obj].style.visibility = 'hidden';
}
function newshow(sw,obj,obj2) {
	// show/hide the divisions
	// var brow=((navigator.appName)+(parseInt(navigator.appVersion)));
	// alert(brow);
      var myWidth = 0, myHeight = 0;
      if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
      } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
      } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
      }	
	  var boxwidth=document.getElementById(obj2).style.width;
	  boxwidth=boxwidth.substr(0,boxwidth.length-2);
	  // alert("boxwidth is "+boxwidth);
	  var newleft=(myWidth/2)-(boxwidth/2);
	  // alert(newleft);
	  document.getElementById(obj).style.left=newleft+"px";
	if (sw) document.getElementById(obj).style.visibility = 'visible';
	if (!sw) document.getElementById(obj).style.visibility = 'hidden';
}
function updshow(sw,obj,obj2,evt) {
	// show/hide the divisions
	// var brow=((navigator.appName)+(parseInt(navigator.appVersion)));
	// alert(brow);
	  //alert("clientX value: " + evt.clientX + "\n"+"clientY value: " + evt.clientY + "\n");
      var myWidth = 0, myHeight = 0;
      if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
      } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
      } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
      }	
      var mainHgt=document.getElementById('maintbl').style.height;
      //alert("main table height"+mainHgt);
      //alert("client hgt"+myHeight);
      var boxlocation = evt.clientY;
      //alert(boxlocation);
      if(boxlocation<200){
        boxlocation=parseFloat(boxlocation)+parseFloat(150)
      }
	  var boxwidth=document.getElementById(obj2).style.width;
	  boxwidth=boxwidth.substr(0,boxwidth.length-2);
	  // alert("boxwidth is "+boxwidth);
	  var newleft=(myWidth/2)-(boxwidth/2);
	  // alert(newleft);
	  document.getElementById(obj).style.left=newleft+"px";
	  document.getElementById(obj).style.top=boxlocation+"px";
	if (sw) document.getElementById(obj).style.visibility = 'visible';
	if (!sw) document.getElementById(obj).style.visibility = 'hidden';
}
function openeditX(HT,WT,sbars) {
	var winX = (document.all)?window.screenLeft:window.screenX;
	var winY = (document.all)?window.screenTop:window.screenY;
	theX=parseFloat(winX)+parseFloat(100)
	theY=parseFloat(winY)+parseFloat(50)
	var W = "height="+HT+",width="+WT+",scrollbars="+sbars+",left="+theX+",top="+theY
	editwinX=open("blank.html","editwinX",W)
	return true
}

function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );
}
function showCoords(evt){
  alert(
    "clientX value: " + evt.clientX + "\n" +
    "clientY value: " + evt.clientY + "\n"
  );
}
function clearDiv(obj) {
        document.getElementById(obj).innerHTML="";
        document.getElementById(obj).style.height="0px";
}
function saveQuestion(divElementId,pagename){
    var pageErrorMessage='Error in Loading page';
    req=GetXmlHttpObject();
    // pagename="editQuiz.aspx";
    questText=document.getElementById("questEditTxt").value;
    questOrder=document.getElementById("questEditOrd").value;
    questID=document.getElementById("QuestionEditID").value;
    objID=document.getElementById("ObjectiveEditID").value;
    correctID=document.getElementById("questCorrectChoice").value;
    CourseID=document.getElementById("CourseEditID").value;
    
    passData='dowhat=updateEQ';
    passData=passData+'&questTxt='+escape(questText);
    passData=passData+'&questOrd='+escape(questOrder);
    passData=passData+'&QuestionID='+escape(questID);
    passData=passData+'&ObjectiveID='+escape(objID);
    passData=passData+'&questCorrectChoice='+escape(correctID);
    passData=passData+'&CourseID='+escape(CourseID);
    //  alert(passData);  
    req.open("POST",pagename,true); 
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    req.onreadystatechange = function() {responsefromServer(divElementId, pageErrorMessage);}; 
    req.send(passData);
} 
function saveChoice(divElementId,pagename){
    var pageErrorMessage='Error in Loading page';
    req=GetXmlHttpObject();
    // pagename="addChoice.aspx";
    
    choiceText=document.getElementById("choiceAddTxt").value;
    choiceOrder=document.getElementById("choiceOrd").value;
    questID=document.getElementById("QuestionEditID").value;
    choiceID=document.getElementById("ChoiceEditID").value;
    objID=document.getElementById("ObjectiveEditID").value;
    CourseID=document.getElementById("CourseEditID").value;
    
    passData='dowhat=updateAC';
    passData=passData+'&choiceTxt='+escape(choiceText);
    passData=passData+'&choiceOrd='+escape(choiceOrder);
    passData=passData+'&choiceID='+escape(choiceID);
    passData=passData+'&QuestionID='+escape(questID);
    passData=passData+'&ObjectiveID='+escape(objID);
    passData=passData+'&CourseID='+escape(CourseID);
        
    // alert(passData);  
    req.open("POST",pagename,true); 
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    req.onreadystatechange = function() {responsefromServer(divElementId, pageErrorMessage);}; 
    req.send(passData);
}        
function setToEdit(boxname) {
    document.getElementById(boxname).className = "savebox2";
}
function setDoneEdit(boxname) {
    document.getElementById(boxname).className = "savebox1";
}
function setEntToEdit(boxname,theclass) {
    document.getElementById(boxname).className = theclass;
}

function updatecart(linecnt) {
    qtystr=""
    for (x=0;x<linecnt;x++) {
       qtystr=qtystr + "&qty" + x + "=" + document.getElementById("qty" + x).value
    }
    if(ns6) {
        window.location="store/cart.aspx?dowhat=update" + qtystr
    }else{
        window.location="cart.aspx?dowhat=update" + qtystr
    }
}
function setEntDoneEdit(boxname,theclass) {
    document.getElementById(boxname).className = theclass;
}
function saveAField(boxname,memid) {
    setDoneEdit(boxname);
	changevalue=eval("document.memHome."+boxname+".value");
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert("Your Browser Does Not Support AJAX!")
		return
	}
	url="savememfield.aspx"
	passData="thefield=" + boxname +"&thevalue="+changevalue+"&memid="+memid;
	
	xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");		
	xmlHttp.onreadystatechange=UNameresults;
	xmlHttp.send(passData);
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   }
function IsLetter(sText){
   var ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var IsLetter=true;
   var Char;

   for (i = 0; i < sText.length && IsLetter == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsLetter = false;
         }
      }
   return IsLetter;
}
function checkLetter(fieldName){
    checkVal=document.getElementById(fieldName).value;
    if (!IsLetter(checkVal) == true){
        alert("The Answer Letter, must be a Letter");
        document.getElementById(fieldName).value="";
        }
    
}

function AddEditCourse(courseID) {
    url="editcourse.aspx?courseID=" + courseID
    document.getElementById("addCourse").style.visibility="visible"
    callPage(url,"addCourse","Loading","I hate when this happens...")
}  

function checkNumber(fieldName){
    checkVal=document.getElementById(fieldName).value;
    checkField=document.getElementById(fieldName)
    if (!IsNumeric(checkVal) == true){
        alert("The Order Must be Represented by a Number");
        document.getElementById(fieldName).value="";
        setTimeout("checkField.focus()", 50);
        }
    
}

