function submitlogin()
{
var username=(document.loginform.username.value);
var usernamelength=username.length;
if (usernamelength<1)
{alert ("please enter a username");
return;}
var password=(document.loginform.password.value);
var passwordlength=password.length;
if (passwordlength<1)
{alert ("please enter a password");
return;}

location.href="javascript:document.loginform.submit()";

}
function navigate(x)
{
location.href=x;
}
function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
  {alert(alerttxt);return false;}
else {return true}
}
}

function validate_email(field,alerttxt)
{
with (field)
{
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
if (apos<1||dotpos-apos<2)
  {alert(alerttxt);return false;}
else {return true;}
}
}
function validate_form(thisform)
{
with (thisform)
{
if (validate_required(name,"please enter your name")==false)
  {email.focus();return false;}
if (validate_email(email,"please enter a valid email address")==false)
  {email.focus();return false;}
if (validate_required(subject,"please enter a subject")==false)
  {email.focus();return false;}
if (validate_required(message,"please enter a message")==false)
  {email.focus();return false;}
}
}

function loginProblemSubmit()
{

var email=(document.emailform.email.value);

var emaillength=email.length;
if (emaillength<1)
{alert ("please enter an email address");
return;}
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

if(reg.test(email) == false) {
      alert('Please enter a valid email address');
      return false;}

location.href="javascript:document.emailform.submit()";

}


//define some global variables used when selecting study level, subject, test type, etc;
var studyLevel;
var subject;
var etime=new Array();
var eamount=new Array();
function addSubjectSelection(x){
    
    
    var options;
    if(x==''||x!=studyLevel){
        options='<select name="subjectSelect" id="subjectSelect" onchange="addTestTypeOptions(this.value)" style="margin-left:2em; width:18em;"><option value=""></option></select>';
        addTestTypeOptions('');
    }
    studyLevel=x;
    if(x=='atpla'){
    //set up the mock time and amount variables:
    etime['pof']="1 hour";
eamount['pof']=44;
etime['pofh']="1 hour";
eamount['pofh']=44;
etime['afs']="2 hours";
eamount['afs']=76;
etime['mb']="1 hour";
eamount['mb']=22;
etime['perf']="1 hour";
eamount['perf']=34;
etime['inst']="1 hour 30 minutes";
eamount['inst']=56;
etime['rnav']="1 hour 30 minutes";
eamount['rnav']=59;
etime['ops']="1 hour 20 minutes";
eamount['ops']=50;
etime['fplan']="3 hours";
eamount['fplan']=56;
etime['nav']="2 hours";
eamount['nav']=54;
etime['met']="2 hours 30 minutes";
eamount['met']=90;
etime['law']="1 hour 40 minutes";
eamount['law']=75;
etime['hpl']="1 hour";
eamount['hpl']=47;
etime['ifrcoms']="30 minutes";
eamount['ifrcoms']=23;
etime['vfrcoms']="30 minutes";
eamount['vfrcoms']=23;
    options='<select name="subjectSelect" id="subjectSelect" onchange="addTestTypeOptions(this.value)" style="margin-left:2em; width:18em;"><option value="">choose from drop-down:</option><option value="pof">Principles of Flight(A)</option><option value="afs">Airframes and Systems, <br/>Electrics and Powerplant</option><option value="mb">Mass and Balance</option><option value="perf">Performance</option><option value="inst">Instrumentation</option><option value="rnav">Radio Navigation</option><option value="ops">Operational Procedures</option><option value="fplan">Flight Planning and Monitoring</option><option value="nav">General Navigation</option><option value="met">Meteorology</option><option value="law">Air Law</option><option value="hpl">Human Performance and Limitations</option><option value="ifrcoms">IFR Communications</option><option value="vfrcoms">VFR Communications</option></select>';
    }
    if(x=='cpla'){
       etime['pof']="45 minutes";
eamount['pof']=33;
etime['afs']="2 hours 30 minutes";
eamount['afs']=98;
etime['mb']="1 hour";
eamount['mb']=22;
etime['perf']="1 hour";
eamount['perf']=34;
etime['inst']="1 hour 30 minutes";
eamount['inst']=56;
etime['ops']="45 minutes";
eamount['ops']=33;
etime['perfplan']="3 hours";
eamount['perfplan']=77;
etime['nav']="1 hour 30 minutes";
eamount['nav']=45;
etime['met']="1 hour 30 minutes";
eamount['met']=54;
etime['law']="45 minutes";
eamount['law']=34;
etime['hpl']="30 minutes";
eamount['hpl']=24;
etime['vfrcoms']="30 minutes";
eamount['vfrcoms']=23;
       options='<select name="subjectSelect" id="subjectSelect" onchange="addTestTypeOptions(this.value)" style="margin-left:2em; width:18em;"><option value="">choose from drop-down:</option><option value="pof">Principles of Flight(A)</option><option value="afs">Aircraft General Knowledge</option><option value="ops">Operational Procedures</option><option value="perfplan">Flight Performance and Planning</option><option value="nav">Navigation</option><option value="met">Meteorology</option><option value="law">Air Law</option><option value="hpl">Human Performance and Limitations</option><option value="vfrcoms">VFR Communications</option></select>';
        }
    if(x=='ira'){
        etime['afs']="1 hour 15 minutes";
eamount['afs']=48;
etime['lawops']="45 minutes";
eamount['lawops']=47;
etime['perfplan']="2 hours";
eamount['perfplan']=42;
etime['nav']="2 hours";
eamount['nav']=74;
etime['met']="1 hour 30 minutes";
eamount['met']=54;
etime['hpl']="30 minutes";
eamount['hpl']=24;
etime['ifrcoms']="30 minutes";
eamount['ifrcoms']=23;
       options='<select name="subjectSelect" id="subjectSelect" onchange="addTestTypeOptions(this.value)" style="margin-left:2em; width:18em;"><option value="">choose from drop-down:</option><option value="afs">Aircraft General Knowledge</option><option value="perfplan">Flight Performance and Planning</option><option value="nav">Navigation</option><option value="met">Meteorology</option><option value="lawops">Air Law / Operational Procedures</option><option value="hpl">Human Performance and Limitations</option><option value="ifrcoms">IFR Communications</option></select>';
        }
    if(x=='atplh'){
        etime['pof']="1 hour";
eamount['pof']=44;
etime['pofh']="1 hour";
eamount['pofh']=44;
etime['afs']="2 hours";
eamount['afs']=76;
etime['mb']="1 hour";
eamount['mb']=22;
etime['perf']="1 hour";
eamount['perf']=34;
etime['inst']="1 hour 30 minutes";
eamount['inst']=56;
etime['rnav']="1 hour 30 minutes";
eamount['rnav']=59;
etime['ops']="1 hour 20 minutes";
eamount['ops']=50;
etime['fplan']="3 hours";
eamount['fplan']=56;
etime['nav']="2 hours";
eamount['nav']=54;
etime['met']="2 hours 30 minutes";
eamount['met']=90;
etime['law']="1 hour 40 minutes";
eamount['law']=75;
etime['hpl']="1 hour";
eamount['hpl']=47;
etime['ifrcoms']="30 minutes";
eamount['ifrcoms']=23;
etime['vfrcoms']="30 minutes";
eamount['vfrcoms']=23;
    options='<select name="subjectSelect" id="subjectSelect" onchange="addTestTypeOptions(this.value)" style="margin-left:2em; width:18em;"><option value="">choose from drop-down:</option><option value="pofh">Principles of Flight(H)</option><option value="afs">Airframes and Systems,<br/> Electrics and Powerplant</option><option value="mb">Mass and Balance</option><option value="inst">Instrumentation</option><option value="=rnav">Radio Navigation</option><option value="=ops">Operational Procedures</option><option value="=fplan">Flight Planning and Monitoring</option><option value="=nav">General Navigation</option><option value="met">Meteorology</option><option value="law">Air Law</option><option value="hpl">Human Performance and Limitations</option><option value="ifrcoms">IFR Communications</option><option value="vfrcoms">VFR Communications</option></select>';
        }
    if(x=='cplh'){
        etime['pofh']="1 hour";
eamount['pofh']=44;

etime['afs']="2 hours 30 minutes";
eamount['afs']=98;
etime['mb']="1 hour";
eamount['mb']=22;
etime['perfplan']="1 hour";
eamount['perf']=34;
etime['inst']="1 hour 30 minutes";
eamount['inst']=56;

etime['ops']="1 hour 20 minutes";
eamount['ops']=50;
etime['perfplan']="3 hours 30 minutes";
eamount['perfplan']=85;
etime['nav']="1 hour 30 minutes";
eamount['nav']=45;
etime['met']="1 hour";
eamount['met']=36;
etime['law']="1 hour";
eamount['law']=40;
etime['hpl']="30 minutes";
eamount['hpl']=24;

etime['vfrcoms']="30 minutes";
eamount['vfrcoms']=23;
       options='<select name="subjectSelect" id="subjectSelect" onchange="addTestTypeOptions(this.value)" style="margin-left:2em; width:18em;"><option value="">choose from drop-down:</option><option value="pofh">Principles of Flight(H)</option><option value="afs">Aircraft General Knowledge</option><option value="ops">Operational Procedures</option><option value="perfplan">Flight Performance and Planning</option><option value="nav">Navigation</option><option value="met">Meteorology</option><option value="law">Air Law</option><option value="hpl">Human Performance and Limitations</option><option value="vfrcoms">VFR Communications</option></select>';
        }
    if(x=='irh'){
        etime['afs']="1 hour 15 minutes";
eamount['afs']=48;
etime['lawops']="1 hour";
eamount['lawops']=40;
etime['perfplan']="2 hours";
eamount['perfplan']=42;
etime['nav']="2 hours";
eamount['nav']=74;
etime['met']="1 hour 30 minutes";
eamount['met']=54;

etime['hpl']="30 minutes";
eamount['hpl']=24;

etime['ifrcoms']="30 minutes";
eamount['ifrcoms']=23;
       options='<select name="subjectSelect" id="subjectSelect" onchange="addTestTypeOptions(this.value)" style="margin-left:2em; width:18em;"><option value="">choose from drop-down:</option><option value="afs">Aircraft General Knowledge</option><option value="perfplan">Flight Performance and Planning</option><option value="nav">Navigation</option><option value="met">Meteorology</option><option value="lawops">Air Law / Operational Procedures</option><option value="hpl">Human Performance and Limitations</option><option value="ifrcoms">IFR Communications</option></select>';
        }
    document.getElementById('subjectSpan').innerHTML=options;
    }
function addTestTypeOptions(x){
    if(x!=subject)
        testoptions('');
    subject=x;
    
    
    var options;
    if(x=='') {
        options='<select name="testTypeSelect" id="testTypeSelect" onchange="testoptions(this.value)" style="margin-left:2em; width:18em;"><option value=""></option></select>';
        testoptions('');
        document.getElementById('typeSpan').innerHTML=options;
        return;
    }
        else{
        options='<select name="testTypeSelect" id="testTypeSelect" onchange="testoptions(this.value)" style="margin-left:2em; width:18em;"><option value="learningobj">Learning objectives:</option><option value="mock">Take a mock exam of '+eamount[subject]+' questions:</option><option value="search">Search questions:</option><option value="specific">Recall questions by ID:</option></select>';
        }
    document.getElementById('typeSpan').innerHTML=options;
    testoptions('learningobj');
    }
function testoptions(x) {
    if(x=='')
        document.getElementById("typeoptions").innerHTML='';
    if(x=="search")
    {document.getElementById("typeoptions").innerHTML='<form name="searchform" action="javascript:getsearchtest()" ><label for="searchterm">Please enter a search term: </label><input type="text" name="searchterm" id="searchterm" style="width: 10em;"/><br/><br/><input type="button" class="submit" value="search" onclick="getsearchtest()" onmouseover="this.style.cursor=\'pointer\'"/>';

    }
    if(x=="specific")
    {alert("this feature is disabled on the demonstration");
     return;
    }
    if(x=="mock")
    {alert("this feature is disabled on the demonstration");
    }
    if(x=="learningobj") {/*code to get the LO form via ajax, and then stick it into the correct place:*/
        document.getElementById("typeoptions").innerHTML='<h3>Loading data, please wait</h3>';
        var xmlhttp=getHttpRequestObject();
        var date=new Date();
        var time=date.getTime();
        xmlhttp.open("GET","getLoForm.php?subject="+subject+"&coursetype="+studyLevel+"&t="+time,true);//false makes it sort out the request b4 doing anything else, if it was true it would continue excuting the next scripts, which fooks up whole thing.
        xmlhttp.onreadystatechange=function()
		{
		if (xmlhttp.readyState==4)
			{
			loForm=xmlhttp.responseText;
            document.getElementById("typeoptions").innerHTML=loForm;
            
			}
		}
    
	xmlhttp.send(null);

    }
}
/*function makeAjaxRequest(x){
    //first set up the HTTPrequest object:
    var xmlhttp;
    xmlhttp=null;
    if (window.XMLHttpRequest) {// code for Firefox, Opera, IE7, etc.
        xmlhttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    var date=new Date();
    var time=date.getTime();
    
    xmlhttp.open("GET", x+"&t="+time,true);//false makes it sort out the request b4 doing anything else, if it was true it would continue excuting the next scripts, which fooks up whole thing.
	xmlhttp.onreadystatechange=function()
		{
		if (xmlhttp.readyState==4)
			{
			returnVal=xmlhttp.responseText;
            whenReady(returnVal);
			}
		}
    
	xmlhttp.send(null);

    return returnVal;
}*/

function getsearchtest(){
    
    var dong=(document.searchform.searchterm.value);// ie dong is the value of the searchterm element of the searchform of this document
    
    dong=trim(dong);
    if(dong=="")
    {alert("please enter a search term");
     return;
    }
    var phpurlextension="&subject="+subject+"&coursetype="+studyLevel+"&testtype=search&searchterm="+dong;
    location.href="demo.php?page=demogetquestions"+phpurlextension;
}



//Define trim function
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function getHttpRequestObject(){
    var xmlhttp;
    xmlhttp=null;
    if (window.XMLHttpRequest) {// code for Firefox, Opera, IE7, etc.
        xmlhttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    return xmlhttp;
}


//////////////FUNCTIONS TDW THE LO FORM///////////////////////////////////////
function changeall(field){

    var dong=document.lotable.allofsub.checked;
    if (dong==false) {
        for (i = 0; i < field.length; i++)
            field[i].checked = false ;
    }
    if (dong==true) {
        for (i = 0; i < field.length; i++)
            field[i].checked = true ;
    }
}


function changeclass(x,y) {

    var qqq=document.lotable.list[y].checked;


    if(qqq==true) {
        var field=document.lotable.list;
        for (i = 0; i < field.length; i++) {
            if(document.lotable.list[i].className==x)
                field[i].checked = true ;
        }
    }
    else {
        var field=document.lotable.list;
        for (i = 0; i < field.length; i++) {
            if(document.lotable.list[i].className==x)
                field[i].checked =false ;
        }
    }

}

var boxid=new Array();
function getlotest() {
  //first job, need to get the boxid array from php:
  var xmlhttp=getHttpRequestObject();
  var date=new Date();
  var time=date.getTime();
  xmlhttp.open("GET","getBoxIdArray.php?t="+time,true);//false makes it sort out the request b4 doing anything else, if it was true it would continue excuting the next scripts, which fooks up whole thing.
        xmlhttp.onreadystatechange=function()
		{
		if (xmlhttp.readyState==4){
			returnString=xmlhttp.responseText;
            eval(returnString);

            getlotestPart2();
			}
		}

	xmlhttp.send(null);
    
  
}

function getlotestPart2(){
    var url_extension='&testtype=lo&selection=';
    var field=document.lotable.list;
    var x=0;

    for (i = 0; i < field.length; i++) {
        if (field[i].checked==true&&field[i].value<2)
        {url_extension=url_extension+boxid[i].substring(3)+",";
         x=1;}



    }
    
    if(x==1)
        location.href="demo.php?page=demogetquestions"+url_extension;
    else
    {alert("Please select at least one learning objective. If you have selected a main learning objective, you must select at least one of its sub learning objectives.");
     return;}
}





function backapage(){
history.go(-1);}

