//editable
user_name="Student";
if(document.cookie!=""){
  myCookie = "";
  myCookie = document.cookie;
  the_info = new Array();
	// load the cookie into a variable and unescape it
	var the_cookie = document.cookie;
	var the_cookie = unescape(the_cookie);
	// get rid of fandmMute array

	// break each name:value pair into an array
	var separated_values = the_cookie.split("; ");
	// loop through the list of name:values and load
	// up the associate array
	var property_value = "";

	for (var loop = 0; loop < separated_values.length; loop++){
		if(separated_values[loop]!=""){
			property_value = separated_values[loop];
			var broken_info = property_value.split("=");
			var the_property = broken_info[0];
			if(typeof(broken_info[1])=='undefined') continue;
			ender = (typeof(broken_info[1])!='undefined')?(broken_info[1].indexOf(";")!=-1)?broken_info[1].indexOf(";"):broken_info[1].length:0;
			var the_value = broken_info[1].substring(0,ender);
			the_info[the_property] = the_value;
			}
		}

	}
ipclk = -1;
	strButtonText = 'Save Answer';
	strErrorMsgBlankField = 'You didn\'t type in an answer.';
	strErrorMsgNotAllFieldsFilledOut = "You haven't finalized all the questions";
	strErrorMsgNotAllFieldsInGroupFilledOut = "You must enter a value for each field in this group";
	intWindowOpened=0;
	//iterate through all elements - if the element is a button then set its value, if it's a question, it sets that string value in the questions array.
	intFormsCount = document.forms.length;
function getUserName(){
	sun = '<form name="username" action="javascript:setUserName()">';
	sun += 'Please log in <br><input type="text" name="val">';
	sun += '<br>';
	sun += '<input type="button" value="Go" onClick="setUserName();">';
	sun += '</form>';
	document.write("<div id=\"uname\" style=\"top:100px;left:300px;\"><center>"+sun+"</center></div>");
	}
function setUserName(){
	if(document.username.val.value!=""){
		//myCookie = unescape(document.cookie);
		myCookie = document.username.val.value;

	  	var exp = new Date();
		var oneYearFromNow = exp.getTime() + (365*24*60*60*1000)
		exp.setTime(oneYearFromNow)
	  	document.cookie ="username="+ escape(myCookie) + ";expires="+exp.toGMTString()+";path=/";
	  	user_name = document.username.val.value;
	  	document.getElementById("uname").style.visibility="hidden";
	  //	init();
	  	document.getElementById("welcome").innerHTML="Welcome, "+user_name+"<br>If this is not you, please <input type='button' style='background:black;color:orange;border:1px solid orange;vertical-align:middle;font-size:9pt;' onClick='logout()' value='log out'>";

		document.getElementById("welcome").style.color="Orange";
	  	}
	}

function logout(){
	var kill_date = new Date("January 1, 1970");
	var kill_string = "username=stub;expires=" + kill_date+";path=/";
	document.cookie = kill_string;
	window.history.go(0)
	}

strButtonText = 'Save Answer';
strErrorMsgBlankField = 'You didn\'t type in an answer.';
strErrorMsgNotAllFieldsFilledOut = "You haven't finalized all the questions";
strErrorMsgNotAllFieldsInGroupFilledOut = "You must enter a value for each field in this group";
intWindowOpened=0;
//iterate through all elements - if the element is a button then set its value, if it's a question, it sets that string value in the questions array.

function init(){
intFormsCount = document.forms.length;

	for (p=0; p<intFormsCount; p++){
		intElementCount = document.forms[p].elements.length;
		if (document.forms[p].name != "submit"){
			for (i=0; i<intElementCount ;i++ ){
				if(typeof(the_info)!='undefined'){
					if(the_info[document.forms[p].elements[i].name]){
						document.forms[p].elements[i].value = the_info[document.forms[p].elements[i].name];
						}
					}
				if(document.forms[p].elements[i].name.indexOf('open') == -1){
					if (document.forms[p].elements[i].type=='button'){
						document.forms[p].elements[i].value = strButtonText;
						}

					}
				}
			}
		}
	}


function submitVal(el){
	elName = el.name;
	isError = false;
	var exp = new Date();
	var oneWeekFromNow = exp.getTime() + (30*60*1000);
	exp.setTime(oneWeekFromNow)

	for(i = 0; i<intFormsCount; i++){
		intElementsCount = document.forms[i].elements.length;
		for(j = 0; j<intElementsCount; j++){
			if(document.forms[i].elements[j].name == elName){
				check = document.forms[i].elements[j-1].name;
				if(check.indexOf("group")!= -1){
					groupName = check.substring(0,6);
					intElementCount = document.forms[i].elements.length;
					for (c=0; c<intElementCount ;c++ ){
						checkElName = document.forms[i].elements[c].name;
						if(checkElName.substring(0,6)==groupName){
							if(document.forms[i].elements[c].value==''){
								isError = true;
								break;
								}
							}
						}
					if (isError==true){
						return;
						}
					else{
						intElementCount = document.forms[i].elements.length;
						for (g=0; g<intElementCount ;g++ ){
							if(document.forms[i].elements[g].name.substring(0,6)==groupName){
								myCookie = document.forms[i].elements[g].name + "=" + escape(document.forms[i].elements[g].value);
								document.cookie =myCookie + ";expires="+exp.toGMTString();

								}
							}
						}
					}
				else{
					myCookie = document.forms[i].elements[j-1].name + "=" +escape(document.forms[i].elements[j-1].value);
					document.cookie =myCookie + ";expires="+exp.toGMTString();

					}
				}
			}
		}




	}




function getLocation(){


	loc = document.location.toString();
	loc = loc.substring(8,loc.length).replace('\\','/');
	loc = loc.substring(0,loc.lastIndexOf('/'));


	return loc;
	}
function submitLab(){


	//check to see if each each element has been finalized

	strFieldArray = "";
	for (j = 0; j>intFormsCount; j++){
		intElementCount = document.forms[j].elements.length;
			for (i=0; i<intElementCount; i++)

			{
			strFieldArray += "," + document.forms[j].elements[i].name;
			}
		}

	if (strFieldArray.substring(0,1) == ','){
	strFieldArray = strFieldArray.substring(1,strFieldArray.length);
	}
	buildResultsPage();
	}


function buildResultsPage(){

	document.submit.elements['submitbutton'].value = "Processing...";
	document.submit.elements['submitbutton'].style.backgroundColor = '#FF0000';


	var h = "";


	h += "<HTML>\r\n";
	h += "<HEAD>\r\n";
	h += "<TITLE>\r\n";
	h += user_name+"'s Results\r\n";
	h += "</TITLE>\r\n";
	h += "<link rel=\"stylesheet\" href=\"../css/labStyle.css\">\r\n";
	h += "</HEAD>\r\n";
	h += "<BODY bgcolor=\"#FFFFFF\">\r\n";
	var a = "";
	n = 1;
	g = 1;
	gn = 1;
	row = 1;
	number = 1;
	lastNumber = 0;
	done = 0;
	intFormsCount = document.forms.length;
	for (loop=0; loop<intFormsCount-1; loop++){
	e = document.forms[loop].elements
	var kill_date = new Date("January 1, 1970");



	//h += "<h2>"+ document.forms[loop].name +"</h2>\r\n";


	intElementCount = document.forms[loop].elements.length;

for (m = 0; m < intElementCount; m++){
	if(e[m].value.charAt(0)==' '){
		e[m].value = e[m].value.substr(1);
		}
	if(e[m].value.replace(/\s/g,'') == ''){
		e[m].value = e[m].value.replace(/\s/g,'');
		}
	}
for (m = 0; m < intElementCount; m++){
strElementName = e[m].name;

if(e[m].value.replace(/\s/g,'') == ''){
	e[m].value = e[m].value.replace(/\s/g,'');
	}

if(strElementName.indexOf('open') > -1 || strElementName.indexOf('button') > -1){
	continue;
	}
else{
	if(e[m].type=='hidden') h += "<h2>"+ e[m].value +"</h2>\r\n";
	else if (number > lastNumber && document.getElementById("qst"+number)){
		myQuestion = document.getElementById("qst"+number)
		question = myQuestion.innerHTML;
		q = escape(question);
		h += "<p><font color=\"#FF0000\">" + q + "</font>\r\n";
		lastNumber = number;
		}
	if (strElementName.indexOf("group") != -1){
		replacer = new Array();
		someHtml = "<input type=\"hidden\" /><span class=\"answer\">"+e[m].value+"</span>";
		replaceMe = document.getElementById(strElementName);
		replacer[replacer.length] = someHtml;
		inps = /<input ([\D]+=['"]?.+['"]? )?name=['"]*(group([\d]+)_field([\d]+))['"]*( )?([\w]+=['"]?.+['"]?)?(\/)?>/im;
		vals = /value=(['"]?[\S]+['"]? )/gim;
		lastGroupDelim = strElementName.indexOf("_");
		currentGroup = strElementName.substring(0,lastGroupDelim);
		groupFind = document.getElementById(currentGroup);
		group = new String(groupFind.innerHTML);
		group = group.replace(vals,'');
		while(inps.test(group)){
			var b = group.match(inps);
			var studans = (e[m].value!='')?e[m].value:'Answer not submitted';
			group = group.replace(b[0],studans);
			myCookie = e[m].name + "=asdf";
			document.cookie = myCookie + ";expires="+kill_date;
			m++
			}
		m--;

		a += "<table cellspacing=\"0\" border=\"1\">";
		a += group;
		a +="</table>";
		done = 0;
		number += 1;
		a = escape(a);
		h += a;
		a = "";
		}

	else if (strElementName.indexOf("field") != -1){
		myCookie = e[m].name + "=asdf";
		document.cookie =myCookie + ";expires="+kill_date;

		if(e[m].value != ''){
			h += "<blockquote>" + e[m].value + "</blockquote>\r\n";
			}
			else{
				h += ' Answer not submitted ';
			}
		number +=1;
		}
	}
}


		}
	h += "</body>\r\n";
	h += "</html>\r\n";

	if(typeof(win) != 'undefined' && !win.closed){
		win.close();
		}
	win = window.open('','interactive');
	win.document.write(unescape(h));
	win.document.close();
	document.submit.elements['submitbutton'].value = "Submit Lab";
	document.submit.elements['submitbutton'].style.backgroundColor = '';

	var kill_string = "username=stub;expires=" + kill_date+";path=/";
	document.cookie = kill_string;

	}
agt=navigator.userAgent.toLowerCase();
SF = ((agt.indexOf('safari')!=-1)&&(agt.indexOf('mac')!=-1))?true:false;
FF = (agt.indexOf('gecko')!=-1 && !SF);
if(SF){
		audio='<div ><embed name="preloader" src="../media/stub.mov" id="preloader" backgroundColor="#FFFFFF" width="30" controller="true" height="1"  pluginspace="http://www.apple.com/quicktime/download/" autoplay="false" enablejavascript="true" hidden="hidden">';
		audio+='</embed></div>';
		document.write(audio);
	}
function checkres(ht){
	scrht = self.screen.availHeight;
	return (scrht <= ht && (FF||SF));
	}
function popRes(){
	wide = 760;
	var vert = (screen.height - 300) / 2;
	var horiz = (screen.width - 760) / 2;
	var extras = 'height=300,width='+wide+',top='+vert+',left='+horiz+',scrollbars=no,toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no';
	var wblocation = (FF)?'../scripts/resolution.html':'../scripts/resolutionmac.html';
	var name = 'reswindow';
	win = window.open(wblocation, name, extras)
	if (parseInt(navigator.appVersion) >= 4) { win.focus(); }
	}


function interactiveWindow(whichProject,title,width,height){
	if(ipclk < 0){ ipclk = setTimeout('ipclk=-1',500); }
	else{ return; }

	if(checkres(600)){
		popRes();
		return;
		}

	java = (whichProject.substr(whichProject.length-1) == 'j')?true:false;

	var vert = (screen.height - height) / 2;
	var horiz = (screen.width - width) / 2;

	if (screen.width > 800){ //jd
	var extras = 'height='+height+',width='+width+',top='+vert+',left='+horiz+',scrollbars=no,toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no,dependent';
	} else { // jd added fullscreen
 	 var extras = 'height='+height+',width='+width+',top='+vert+',left='+horiz+',scrollbars=no,toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no, fullscreen=yes,dependent';
	}
	var projfile='scripts/interactives.html?titleOP='+title+'&wid='+width+'&hgt='+height+'&fname='+whichProject.substring(0,whichProject.length-1);
	if((typeof(win)!='undefined')&&(!win.closed)){
		if(win.document.location.href.toString().indexOf(projfile)==-1){
			win.close();
			win = window.open('../'+projfile,'interactive'+whichProject,extras);
			}
		else {win.window.focus();}
		}
	else{win = window.open('../'+projfile,'interactive'+whichProject,extras);}

	}
function closejavawins(){
	if(typeof(win)!='undefined'&&(!win.closed)){win.close()}
	}

IE = navigator.appName.indexOf("Internet Explorer")>-1?true:false;

function NewWindow(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
	}

function MM_openBrWindow(theURL,winName,features) {
	win = window.open(theURL,winName,features);
	if (parseInt(navigator.appVersion) >= 4) { win.focus(); }
	}

function popup(term){
	layerToPop =  document.getElementById(term);
	layerToPop.style.visibility = "visible";
	if(SF) layerToPop.onclick = function(){hidelayer(term);}
	}

function hidelayer(lName){
	layerToHide = document.getElementById(lName).style;
	layerToHide.visibility = "hidden";

	}
function showInfo(){}
 function closeInfo(){}

