// JavaScript Document
// Note that cookieList[accessIndex] must be a string for switch statement 
// v1.aa: Site license: Aug 18, 2004.
// working as of 4-24-10
// site checkbox.
var footnote; 
var lastName=ThermoCookieList[lastNameIndex];
var message='<div class="messagebox">Visit "My Account" to extend/upgrade your license. It expire(s/ed) on  '+getExpiryDate()+'.</div>';
//alert("mdeia access expiry are: "+ThermoCookieList[mediaIndex]+" "+ThermoCookieList[accessIndex]+" "+ThermoCookieList[expiryIndex])
if(ThermoCookieList[mediaIndex]=="WEB") {
    message=new String('<div class="messagebox">Each session lasts for 2 hours or until you log out. You can extend/upgrade your license beyond '+getExpiryDate()+'  by visiting the My Account page or by writing directly to prof.bhattacharjee@gmail.com if you are an educator or a site license holder. </div>');

}

//check login input...
function handleEnter(e){
    var evt = e || window.event;
    var keyPressed = evt.which || evt.keyCode;
    //alert('hello' +keyPressed);
    if (keyPressed == 13 )handleLogInClick();
}
//check login input...
function handleLogInClick(){
    var bSite = document.getElementById('SITEBOX').checked;
    var sID = document.getElementById('userid').value;
    if (sID == '')bSite = true;
    //if(sID.trim() == '') bSite = true;
    var sPW = document.getElementById('password').value;
    var sPattern = '^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$';
    var orPattern  =  new RegExp(sPattern,"gi");
    var bEmail = sID.search(orPattern) != -1;
    if(!bSite && !bEmail){
            alert("The login id must be a valid email address, which was used during account creation. To log in from inside your campus, check the Site License box and click Log In.  ");
            return;
    }
    if(bSite){
        document.getElementById('userid').value = '';
        document.getElementById('password').value = '';
    }
    //alert('click'+document.getElementById('SITEBOX').checked+sID +sPW+' '+bEmail);
    //alert('calling php'+loginURL);
    //return true;
    document.loginForm.submit();
}

// display login form of various types (expired etc.)
// more info on form submission in http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml
function loginForm(){
    var modeColor=" #d07500 "; var width=" width=80% ";
    var testCookie = GetTestCookie('testCookie');

    // guest access on the web
    //
    var modeColorWidth, secondString="My Account", firstString="", accountURL=myaccountURL;
    if(ThermoCookieList[mediaIndex]=="LOCAL") accountURL=tomyaccountURL;
    //alert('in homejs '+loginURL);
   if(ThermoCookieList[accessIndex] == 0 && ThermoCookieList[mediaIndex]=="WEB" && lastName == "Guest" && testCookie == 1 ) {
//   var b = true;
//   if(b){
       // document.write('<form name="loginForm" id="loginForm" method="post" action="javascript:handleLogInClick()"'+' >');
        document.write('<form class="loginForm" name="loginForm" id="loginForm" method="post" action='+loginURL+' >');
        //document.write('<form name="loginForm"   >');
        document.write("<input type=hidden name=operation value=\"login\">");
        document.write("<table width=\"100%\" bgcolor=\"dddddd\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
	
        document.write("<tr>");
        document.write("  <td width=\"2\">&nbsp;</td>");
        document.write('  <td height=20 colspan=1><font face=verdana size=-1> Log in or <a href='+signupURL+' >create an account</a></font></td>');

        document.write("  <td width=\"2\">&nbsp;</td>");
        document.write('  <td height=20  colspan=3>[ <A href='+forgotURL+' ><font face=arial size=-1>Forgot password?</font></A>&nbsp;&nbsp;&nbsp;<A href='+changeURL+' ><font face=arial size=-1>Change password</font></A>&nbsp;&nbsp;&nbsp;<A href='+myaccountURL+' target="_top"><font face=arial size=-1>My Account</font></A> ]</td>');
        document.write("</tr>");
	
        document.write("<tr>");
        document.write("  <td width=\"2\">&nbsp;</td>");
        document.write("  <td width=\"190\" height=\"5\" valign=\"middle\"><font face=\"verdana\" size=\"-2\">Login id (e-mail address):</font></td>");
        document.write("  <td width=\"2\">&nbsp;</td>");
        document.write("  <td  height=\"5\" colspan=\"3\" valign=\"middle\"><font face=\"verdana\" size=\"-2\">Password: (No id or password is necessary with site license.)</font></td>");
        document.write("</tr>");
	
        document.write("<tr>");
        document.write("  <td width=\"2\">&nbsp;</td>");
        document.write("  <td width=\"190\" height=\"5\" valign=\"top\"><font face=\"verdana\" size=\"-1\"><INPUT name=userid onkeypress = 'handleEnter(event)'  type = text id=\"userid\" size=\"30\" maxLength=\"40\" tabindex=\"1\"></font></td>");
        //document.write("  <td width=\"190\" height=\"5\" valign=\"top\"><font face=\"verdana\" size=\"-1\"><INPUT name=userid   type = text id=\"userid\" size=\"30\" maxLength=\"40\" tabindex=\"1\"></font></td>");
        document.write("  <td width=\"2\">&nbsp;</td>");
        //document.write("  <td  height=\"5\" valign=\"top\"><font face=\"verdana\" size=\"-1\"><INPUT  name=password type=password id=\"password\" size=\"15\" maxLength=\"15\" tabindex=\"2\"></font></td>");
        document.write("  <td  width = 100 height=\"5\" valign=\"top\"><font face=\"verdana\" size=\"-1\"><INPUT onkeypress = 'handleEnter(event)' name=password type=password id=\"password\" size=\"15\" maxLength=\"15\" tabindex=\"2\"></font></td>");
        today = new Date();
        ts = today.getTime();
        document.write("  <td  colspan=2  ><INPUT type=hidden value=\"" + ts + "\" name=\"clientTime\">");
        //document.write("  <INPUT  type=submit value=\"Log In\" name=log > <INPUT type=checkbox onclick = 'handleLogInClick' value=NO name=SITEBOX id=SITEBOX tabindex=\"4\"><font face=\"verdana\" size=\"-1\">Site License</font></td>");
        document.write("  <INPUT  type=button onclick = 'handleLogInClick()' value=\"Log In\" name=log > <INPUT type=checkbox onclick = 'handleLogInClick' value=NO name=SITEBOX id=SITEBOX tabindex=\"4\"><font face=\"verdana\" size=\"-1\">Site License</font></td>");
        //document.write("  <INPUT onclick = 'handleLogInClick()' type=submit value=\"Log In\" name=log > <INPUT type=checkbox onclick = 'handleLogInClick' value=NO name=SITEBOX id=SITEBOX tabindex=\"4\"><font face=\"verdana\" size=\"-1\">Site License</font></td>");
        //document.write("  <INPUT onclick = 'handleLogInClick'  value=\"Log In\" name=log > <INPUT type=checkbox CHECKED value=NO name=SITEBOX id=SITEBOX tabindex=\"4\"><font face=\"verdana\" size=\"-1\">Site License</font></td>");

        document.write("</tr>");
	
        document.write("<tr>");
        document.write("  <td width=\"2\">&nbsp;</td>");
        document.write("  <td colspan=\"5\"><INPUT type=checkbox CHECKED value=YES name=SAVEOPTION tabindex=\"4\">");
        document.write("  <font face=\"verdana\" size=\"-1\">Remember my e-mail address and password on this computer.&nbsp;&nbsp;&nbsp;</font></td>");
        document.write("</tr>");
	
        document.write("</table>");
        document.write("</form>");
    }
    else {  
        // web expired, web active, and all local accounts
        //
        var salute=ThermoCookieList[saluteIndex];

        //alert("salutation is: "+salute+" lastname "+lastName);
        if (salute=="Site") {
            lastName="Licensed User";
        //accountURL=tomyaccountURL; // site licensed users must log in to see the account page.
        }
	
        if(ThermoCookieList[accessIndex] == 0 && ThermoCookieList[mediaIndex]=="WEB") {

            modeColor=" #d07500 "; width=" width=60% ";

            if( testCookie == 0 ) {

                document.write('<table align=center width=100% bgcolor=dddddd border=0 cellpadding=5 cellspacing=0 height=20><tr><td BGCOLOR=ffff55'+width+' align=left><font color=#ff0000 face=verdana size=-1><strong>Log In Warning: Try to reload the page. If you still see this message, it means your browser is not allowing cookies to be created. Lower the privacy to Low from the Tools>Internet Options..>Privacy tab. Read the Getting Started section of the Tutorial to make TEST work in your browser. &nbsp;</strong></td></tr></table>');
                return;
            }

            firstString="License Alert*" + testCookie;
            secondString="My Account";
        }
        else if(ThermoCookieList[accessIndex] == 0 && ThermoCookieList[mediaIndex]=="LOCAL") {
            modeColor=" #d07500 "; width=" width=80% ";
            firstString="Activate TEST before use.";
            secondString="My Account";
        }
        else if(ThermoCookieList[accessIndex] ==5 ) {
            modeColor="#4aa02c"; width=" width=60% ";
        }
        else if(ThermoCookieList[accessIndex] ==10) {
            modeColor="#5555aa"; width=" width=60% ";
        }
        else if(ThermoCookieList[accessIndex] ==15) {
            modeColor="#d07500";
            width=" width=60% ";
        }

        var thirdString="";

        if(ThermoCookieList[mediaIndex]=="WEB")
            thirdString="Log Out,";

        //document.write('<table align=center width=100% bgcolor=dddddd border=0 cellpadding=0 cellspacing=0 height=20><tr><td BGCOLOR='+modeColor+width+' align=center><font color=#ffffff face=verdana size=-1><strong>&nbsp;Greetings, ' + ThermoCookieList[saluteIndex] + '&nbsp;' + lastName +  '! </strong>'+firstString+'<font></td><td align=center><a href='+logoutURL+'><font color='+modeColor+' face=verdana size=-1 width=200>'+thirdString+'</a></font>&nbsp&nbsp&nbsp<a href= '+accountURL+' ><font color='+modeColor+' face=verdana size=-1 width=200>'+secondString+'</font></a></td></tr></table>');

        document.write('<table align=center width=100% bgcolor=dddddd border=0 cellpadding=0 cellspacing=0 height=20 ><tr><td BGCOLOR='+modeColor+width+' align=center><font color=#ffffff face=verdana size=-1><strong>&nbsp;Greetings, ' + ThermoCookieList[saluteIndex] + '&nbsp;' + lastName +  '! </strong>'+firstString+'<font></td><td align=center><a href='+logoutURL+'><font color='+modeColor+' face=verdana size=-1 width=200>'+thirdString+'</a></font>&nbsp&nbsp&nbsp<a href= '+accountURL+' target="_top"><font color='+modeColor+' face=verdana size=-1 width=200>'+secondString+'</font></a></td></tr></table>');

    }
}
// called by home.html it writes graphic header
function writeHomeHeader(){
    //defalut declaration: Guest Access
    //var headerPart1="headerPart1Guest", headerPart2="headerPart2Guest";
    var headerPart1, headerPart2;
    if(getExpiryStatus()=="true") ThermoCookieList[accessIndex]="0";
    //alert("In case, media, and lastname, "+ThermoCookieList[accessIndex]+actualMedia+ThermoCookieList[lastNameIndex]+message);

    switch(ThermoCookieList[accessIndex]) {
        case "0": // either not registered or expired license, distinguish the two through last name. "Guest" cannot be anyone's lastname

            headerPart1="headerPart1Guest"; headerPart2="headerPart2Guest";
            footnote=new String("Guest Mode/WEB: This is the default mode with very limited access. You may browse the Tutorial or the Slide Show pages to learn about TEST, but you must log in in order to use TEST effectively. ");
            message=new String('<br><table width=100%  boder=2 cellpadding=2 cellspacing=0><tr><td><font face=\"verdana\" size=\"-1\" color=blue><hr>Activation Procedure: You must activate this locally installed distribution of TEST. Activation means downloading a license file and save it in a particular folder. The following instructions assume that you have already copied the entire content of the CD into a local folder named "myTEST" (in your C drive or desktop) and bookmarked this local page in your favorite browser (Firefox/IE/Safari). More detailed instructions can be found in the README.txt file in the CD.<ol><li>If you are not connected to the Internet, go to step-2. Click on the "My Account" link on the Task Bar (or next to the greetings  above). It will take you to the TEST registration site, where you must log in to access your account. If you do not have an account, you can create one in minutes following the "create an account" link. In case you forgot your password it will be emailed to you as long as you remember your login id (the email address you use during registration). <p>After you log in, you can see your account status in the "My Account" page. There you will see a button  "Send CDROM License" prominantly displayed (among other options). Click on it to receive the license file "license.txt" as an email attachment (should take less than a minute; make sure you check your spam folder just in case the machine generated mail is treated as spam by your email application). The license file contains the expiration date of your subscription and the type of subscription (educational vs. professional). Log out of "My Account" and check your email. After the "license.txt" file arrives as an attachment, save it in the "testcenter" folder, located inside the "myTEST" folder (where you copied the content of the CD). The new file replaces an old  file by the same name ("license.txt") copied from the CD. <p>Launch TEST again (using your local bookmark or clicking the index.html file in the "myTEST" folder).  A personalized greetings on the home page will indicate that TEST has been successfully activated. Also, this instruction block will disappear and appear again only after your license will have expired.<p><li>If your computer does not have a network connection, you have to go to a different networked computer and visit www.thermofluids.net, click on myAccount link, and get the "license.txt" file as described in Step-1. After the file arrives, copy it into a floppy disk or some other moveable media, and transfer it to the "myTEST/testcenter" folder in your non-networked computer.  <p><li>Once activated, the local edition of TEST does not require any network connection (except for accessing the solution manual). After your license period expires, these instructions will reappear on this page. To extend your license, go to "My Account" page, renew your license,  obtain a fresh "license.txt" file to update/upgrade your existing license. Users who renew license before the expiration date receive a substantial discount. <p><li>If you received the cd from a colleague, follow the same procedure starting at Step-1. Of course, you have to create an account first by registering (click on "Register Now" in the "My Account" page). </ol><hr></font></td></tr></table>');


            //if(actualMedia=="WEB"&& lastName=="Guest") message='<div class="messagebox"><span class = "news" >GOT A TECHNICAL QUESTION OR COMMENT? VISIT THE FORUM* AND JOIN THE THERMODYNAMIC CONVERSATION! </span><p>To test if your institution has a site license, simply click the Log In button above. Academic site license is free and can be activated in minutes for your institution. An educator can request a site license by simply creating a personal account and then sending the first two nodes of the IP address of your campus to prof.bhattacharjee@gmail.com.  </div>';
            if(actualMedia=="WEB"&& lastName=="Guest") message='<div class="messagebox"><div class = "news" >FORUM ACCESS BECOMES FASTER AS YOU ACCESS IT MORE OFTEN. IF YOU ARE A CHROME USER, YOU HAVE TO CLICK THE FORUM LINK TWICE TO LAUNCH IT. </div>To test if your institution has a site license, simply click the Log In button above. Academic site license is free and can be activated in minutes for your campus. If you are an educator, create a personal account (in seconds) and then send an email to Prof. Bhattacharjee (prof.bhattacharjee@gmail.com) to upgrade your account. Also, to set up the free site license, include the first two nodes of the IP address of your campus.   </div>';
            //if(actualMedia=="WEB"&& lastName=="Guest") message='<div class="messagebox">You must log in to use the thermodynamic solvers called daemons. Creating a TEST account takes seconds; for more information, read the Getting Started section of the Tutorial linked from the task bar below.  <p>To test if your institution has a site license, simply click the Log In button above. Academic site license is free and can be activated in minutes for your institution. An educator can request a site license by simply creating a personal account and then sending the first two nodes of the IP address of his/her campus to prof.bhattacharjee@gmail.com.  </div>';
		
            if(actualMedia=="WEB" && getExpiryStatus()=="true"){ //account expired, web user
                message=new String('Dear '+ThermoCookieList[saluteIndex] + ' ' + lastName +',\n\nYour license to use TEST needs to be renewed, which apparently expired on '+getExpiryDate()+'. Close this \nwindow and go to \"My Account\" page, where you can renew/upgrade your license in minutes. If you are an educator, please write directly to prof.bhattacharjee@gmail.com for a permanent academic license.\n\nThank you for your support! \n\nS. Bhattacharjee, Author of TEST\n');
                alert(message);
                message= '<div class="messageboxred">Your license to use TEST needs to be renewed, which apparently expired on '+getExpiryDate()+'. Go to My Account page, where you can renew/upgrade your license in minutes. <p>If you are an educator, please write directly to prof.bhattacharjee@gmail.com to extend/upgrade your academic license.';
            }

            if(actualMedia=="LOCAL"){
                headerPart1="headerPart1GuestLocal";
                footnote=new String("Guest Mode/Local Edition: This is the default mode with very limited access. You must activate this local edition in order to use TEST effectively.");

            }
			

            break;
        case "5":
            headerPart1="headerPart1Evaluation"; headerPart2="headerPart2Evaluation";
            footnote=new String("Evaluation Mode: This mode gives you almost as much access as the Academic Mode for a limited period of time. In the Evaluation Mode run a few daemons to make sure that everything works before upgrading your license to Academic or Professional Mode.");

            if(actualMedia=="LOCAL"){
                headerPart1="headerPart1EvaluationLocal";
            }
            break;
        case "10":
            headerPart1="headerPart1Academic"; headerPart2="headerPart2Academic";
            footnote=new String("Academic Mode/Web Edition: This mode gives you access to an adequate set of working substances used in thermodynamics education.  Web Edition means you are accessing the latest version of TEST. The access speed is, however, limited by the speed of your connection. If you would like to run TEST locally to enjoy its native performance, order a TEST-CD. The Professional Edition of TEST contains a much larger data base and comes with a CD for local installation. ");

            if(actualMedia=="LOCAL"){
                headerPart1="headerPart1AcademicLocal";
                footnote=new String("Academic Mode/Local Edition: This mode gives you access to an adequate set of working substances used in thermodynamics education. Local installation means access speed is at its fastest and no Internet connection is necessary. The Professional Edition of TEST contains a much larger data base. ");

            }
            break;
        case "15":
            headerPart1="headerPart1Professional"; headerPart2="headerPart2Professional";
            footnote=new String("Professional Mode/Web Edition: This mode gives you complete access to the growing list of working substances available in the TEST database. Web Edition means you are accessing the latest version of TEST. The access speed is, however, limited by the speed of your connection. For best access, obtain a license with a CD so that you can run TEST locally for speed and access the Web Edition to use the latest version from anywhere. ");

            if(actualMedia=="LOCAL"){
                headerPart1="headerPart1ProfessionalLocal";
                footnote=new String("Professional Mode/Local Edition: This mode gives you complete access to the growing list of working substances available in the TEST database. Local installation means access speed is at its fastest. Occasionally access TEST on line to see if the on line version has improved sufficiently to justify an upgrade.");

            //alert('got it');
                            
            }
            break;
        default:
            break;
    }
    document.write('<tr><td bgcolor=#ffffff valign=top align=center width=20%><img src="testcentericons/testLogoEd.jpg" Alt="TEST Logo" nosave border=0 width=100 height=90 usemap="#m_testLogoEd"><map name="m_testLogoEd"><area shape="rect" coords="17,0,24,6" href='+adminURL+'><area shape="rect" coords="25,0,31,6" href="http://energy.sdsu.edu/awstats/awstats.pl?config=energy.sdsu.edu"></map></td><td width=75% valign=top align=middle ><a target="_top" href="http://www.thermofluids.net"><img src="testcentericons/'+headerPart1+'.jpg" nosave border=0 width=590 height=90></a></td><td bgcolor=#ffffff valign=top align=middle width=5%><img src="testcentericons/'+headerPart2+'.jpg" width=40 height=90  alt="About TEST-Pro" border=0></td></tr>');


}

// right under login message, do camtasia video
function writeVideoClip(){
    var iRandom = Math.floor(Math.random()*3);
    var asFeature = [];
    asFeature[0] = 'features/slides/featureA.html';
    asFeature[1] = 'features/slides/featureB.html';
    asFeature[2] = 'features/slides/featureC.html';
    if(getExpiryStatus()=="true") ThermoCookieList[accessIndex]="0";
    if(actualMedia=="WEB"  && ThermoCookieList[accessIndex] != 0) {
    }
    else{
            
        //var s = "<tr><td valign=center align=left><iframe frameborder='0' scrolling='no' valign = 'top' align = 'left' width='580' height = '335' marginheight='0px' marginwidth='0px' src='features/slides/featureA.html'></iframe></td></tr>";
        var s = "<tr><td valign=center align=left><iframe frameborder='0' scrolling='no' valign = 'top' align = 'left' width='580' height = '335' marginheight='0px' marginwidth='0px' src='"+asFeature[iRandom]+"'></iframe></td></tr>";
 

        document.write(s);
    }
}

function writeHomeMessage(){
    document.write(message);
}

function writeHomeFootnote(){
    document.write('<font color=999999>'+footnote+'</font>');
}
//image for Web Edition vs. Web Mirror etc.
function writeMirror(){
    var mirrorFile="webEdition.jpg";
    if(actualMedia=="WEB") mirrorFile="webMirror.jpg";
    document.write('<img src=testcentericons/'+mirrorFile+'>');
}
function writeHomeCount(){	
    document.write('<td bgcolor=dddddd></td><td bgcolor=#eeeeee valign=middle><img src=testcentericons/footer.jpg valign=top NOSAVE></td>');

/*	if(ThermoCookieList[mediaIndex]=="WEB") {
		//document.write('<img src=testcentericons/counter.jpg valign=top NOSAVE><img valign=top  src="http://thermo.sdsu.edu/stats/total.php">');
		document.write('<td bgcolor=#eeeeee valign=middle    height=34 colspan=2><table width=100% border=0 cellpadding=0 cellspacing=0 align=center><tr><td align=center colspan=3><font face=verdana size="-2">Registered Users</font></td><td align=center><font face=verdana size="-2">Pages Browsed (since 2004)</font></td></tr><td align=center ><img valign=top   align=center src='+studentCount+' ></td><td align=center><img valign=top  src='+eduCount+'></td><td align=center><img valign=top   src='+proCount+'></td><td align=center><img valign=top  src='+pageCount+'></td></tr></table></td>');
		
		


	}
	else {
	}*/
}

function openPopUpWindow(winURL, winWidth, winHeight, winFeatures, winLeft, winTop){
    var defaultPixelLeft = 200;
    var defaultPixelTop = 20;
    windowName = "popUpWindow" + windowCount++; //unique name for each pop-up window
    closePopUpWindow();
    if (openPopUpWindow.arguments.length >= 4)
        winFeatures = "," + winFeatures;
    else
        winFeatures = "";
    if (openPopUpWindow.arguments.length == 6)
        winFeatures += getLocation(winWidth, winHeight, winLeft, winTop);
		 
    else
        winFeatures += getLocation(winWidth, winHeight, defaultPixelLeft, defaultPixelTop);
  
    popUpWindow = window.open(winURL, windowName, "width=" + winWidth
        + ",height=" + winHeight +  winFeatures);
}
