function f_getcookieval(offset){
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) {
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
}

function f_getcookie(name){
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) {
			return f_getcookieval (j);
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) {
			break;
		}
	}
	return '';
}

function f_setcookie (name, value, expires){
	document.cookie = name + "=" + escape (value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString()));
}

function test4Cookies(){
	if (test4CookieInt()){
		document.getElementById("cookietest").style.visibility='hidden';
	}
}

function test4CookieInt(){
	f_setcookie("testcook","1",null)
	if (f_getcookie("testcook")=="1"){
		return true;
	}else{
		return false;
	}
}

function clearNew(){
	//clear the 'new' values which are used for page navigation
	//(this protects against problems occuring when the user clicks the
	//browser 'back' button)

	//&newvalues=1 flags the beginning of any 'new' state values that get set for navigation
	//Clear out any existing before setting.
	document.getElementById("frm").action=removeFromUrl(document.getElementById("frm").action,'newvalues');
}

function activatemycollection(colname,colid){
	clearNew();
	var sourcef = document.getElementById("frm");
	sourcef.emu_action.value = 'mycollectionactivate'
	if (window.encodeURIComponent){
		sourcef.action = 'emuseum.asp?emu_action=mycollectionactivate&mycollection=' + colid + '&mycollectionname=' + encodeURIComponent(colname) + '&currentrecord=1';
	}else{
		sourcef.action = 'emuseum.asp?emu_action=mycollectionactivate&mycollection=' + colid + '&mycollectionname=' + escape(colname) + '&currentrecord=1';
	}
  	sourcef.submit();
  	//window.history.back(-1);
}

function addobjects2mycoll(byuser){
	var oidtext='';
	var oids='';
	var otexts='';
	var surl='';
	var objids='';
	
	var sourceuc = document.getElementById("usercoll");
	var sources4 = document.getElementById("selected4mycoll");
	
	if (sourceuc!=null){
		
		var pagesize=sourceuc.pagesize.value;
		var colid=sourceuc.currentusercollid.value;
		var colname=sourceuc.currentusercollid[sourceuc.currentusercollid.selectedIndex].text;
		var modid=sourceuc.moduleid.value;
		var lang=sourceuc.lang.value;
		var cachedIDs=f_getcookie("cachedmycollitems");
		var cachedTexts=f_getcookie("cachedmycollitemstext");
		var aCachedIDs=new Array();
		var aCachedTexts=new Array();
		if (cachedIDs.length>0){
			aCachedIDs=cachedIDs.split(",");
			aCachedTexts=cachedTexts.split("/,/");
		}
		if (colid=='' && byuser=='1'){
			alert("Please select a my Collection from the drop down list.");
		}else{
			for (var i=0;i<sources4.elements.length;i++) {
				if (sources4.elements[i].name=='add2mycoll'){
					oidtext=sources4.elements[i].value;
					var j = oidtext.indexOf(":::",0);
					var oid = 0;
					var otext = "";
					if (j>-1){
						oid = oidtext.substring(0,j);
						otext = oidtext.substring(j+3);
					}
					if (oid!=0){
						if(sources4.elements[i].checked==1){
							if (!isInCachedMyColls(aCachedIDs,modid,oid)){
								aCachedIDs.push(modid+'^'+oid);
								aCachedTexts.push(otext);
							}
							if (byuser=='1'){
								sources4.elements[i].checked=0;
							}
						}else{
							//need to remove it
							for (var k=0;k<aCachedIDs.length;k++){
								var singleCached=aCachedIDs[k];
								var l=singleCached.indexOf("^",0);
								if (l>-1){
									var smodid=singleCached.substring(0,l);
									var soid=singleCached.substring(l+1);
									if (modid==smodid && soid==oid){
										aCachedIDs.splice(k,1);
										aCachedTexts.splice(k,1);
									}
								}
							}
						}
					}
				}
			}
			oids=aCachedIDs.join(",");
			otexts=aCachedTexts.join("/,/");
			if (byuser=='1'){
				if (oids.length>0){
					if (window.encodeURIComponent){
						surl='AddObjToColl.asp?byuser=1&lang='+lang+'&objectid=' + oids + '&mycollectionid=' + colid + '&objtext=' + encodeURIComponent(otexts)+'&mycollectionname='+encodeURIComponent(colname);	
					}else{
						surl='AddObjToColl.asp?byuser=1&lang='+lang+'&objectid=' + oids + '&mycollectionid=' + colid + '&objtext=' + escape(otexts)+'&mycollectionname='+escape(colname);	
					}

					openWindow(surl,'Confirmation','width=400,height=300,scrollbars=yes,resizable=no,screenx=200,screeny=200,left=200,top=200,titlebar=0');
					f_setcookie("cachedmycollitems","",null); 
					f_setcookie("cachedmycollitemstext","",null); 
				}
			}else{
				if (oids.length>0){
					f_setcookie("cachedmycollitems",oids,null); 
					f_setcookie("cachedmycollitemstext",otexts,null); 
				}
			}
		}
	}
}

function checkForSelectedMyColls(){
	addobjects2mycoll(0);
}

function checkCachedMyColls(){
	var sources4 = document.getElementById("selected4mycoll");
	if (document.getElementById("usercoll")!=null){
		var modid=document.getElementById("usercoll").moduleid.value;
		aCachedIDs=f_getcookie("cachedmycollitems").split(",");
	
		for (var i=0;i<sources4.elements.length;i++) {
			var current=sources4.elements[i].value;
			var j = current.indexOf(":::",0);
			if (j>-1){
				var oid=current.substring(0,j);
				if (isInCachedMyColls(aCachedIDs,modid,oid)){
					sources4.elements[i].checked=1;
				}
			}

		}
	}
}

function isInCachedMyColls(aCachedIDs,modid,oid){
	var to_return=false;
	for (var k=0;k<aCachedIDs.length;k++){
		var singleCached=aCachedIDs[k];
		var l=singleCached.indexOf("^",0);
		if (l>-1){
			var smodid=singleCached.substring(0,l);
			var soid=singleCached.substring(l+1);
			if (modid==smodid && soid==oid){
				to_return= true;
			}
		}
	}
	return to_return;
}

function addQSToURL(){
	checkForSelectedMyColls();
	clearNew();
	var sourcef = document.getElementById("frm");
	sourcef.action=appendOrReplace2Url(sourcef.action,'quicksearch',sourcef.quicksearch.value);
}

function appendOrReplace2Url(sUrl,sKey,sValue){
	sUrl=removeFromUrl(sUrl,sKey);
	if (sValue.length>0){
		var cS;
		if (sUrl.indexOf('?')==-1){
			cS='?';
		}else{
			cS='&';
		}
		return sUrl+cS+sKey+'='+sValue;
	}else{
		return sUrl;
	}
}

function removeFromUrl(sUrl,sKey){
	var i = sUrl.indexOf('?'+sKey+'=');
	if (i==-1){
		i = sUrl.indexOf('&'+sKey+'=');
	}
	if (i>-1){
		var j=sUrl.indexOf('&',i+1);
		if (j==-1){
			return sUrl.substr(0,i);
		}else{
			return sUrl.substr(0,i+1) + sUrl.substr(j+1);
		}
	}else{
		return sUrl;
	}
}

function openWindow(winURL,winName,winFlags){
    var myWindow=window.open(winURL,winName,winFlags);
    if (myWindow!=null){
    	myWindow.focus();
    }
}

function acrobatTest(){
	window.focus();
	var source = document.getElementById("reports");
	if (source!=null){
		if(source.exportformat.value=="pdf"){
			if (!acrobat.installed){
				document.getElementById('ppreview').innerHTML='<a href="http://www.adobe.com/products/acrobat/readstep2.html"><img src="../images/get_adobe_reader.gif" alt="get acrobat reader"/></a>';
			}
		}
	}
}

function setMedia(filename,caption,title,lang,zoomify){
	//selects a particular media record for enlarging
	var sourcem = document.getElementById("media");
	sourcem.filename.value = filename;
	sourcem.caption.value = caption;
	sourcem.title.value = title;
	sourcem.zoomify.value = zoomify;
	spawn('../html/media_enlarged_' + lang + '.html','popup','635','725');
}

function setMedia_people(filename,caption,title,lang,zoomify){
	//selects a particular media record for enlarging
	var sourcem = document.getElementById("media");
	sourcem.filename.value = filename;
	sourcem.caption.value = caption;
	sourcem.title.value = title;
	sourcem.zoomify.value = zoomify;
	spawn('../html/media_enlarged_person_' + lang + '.html','popup','635','725');
}

function spawn(url, nameW, w, h){
  	if (navigator.appVersion.indexOf('4') != -1) {
		// Vars for centering the new window on Version 4 Browsers
		x4 = screen.width/2 - (w/2);
		// 60 subtracted from the calculated y position so that the window is positioned higher up the screen!
		y4 = screen.height/2 - (h/2) - 60;
		window.open(url, nameW, 'height='+h+',width='+w+',scrollbars=1,resizable=1,menubar=0,toolbar=0,status=0,location=0,directories=0,left=' + x4 + ',top=' + y4 + '');
  	}else{
    	window.open(url, nameW, 'height='+h+',width='+w+',scrollbars=1,resizable=1,menubar=0,toolbar=0,status=0,location=0,directories=0,left=150,top=200');
	}
}

function loadXMLDoc(fname)
	{
	  var xmlDoc;
	  // code for IE
	  if (window.ActiveXObject)
	  {
		xmlDoc=new ActiveXObject("MSXML2.FreeThreadedDomDocument");
	  }
	  // code for Mozilla, Firefox, Opera, etc.
	  else if (document.implementation
	  && document.implementation.createDocument)
	  {
		xmlDoc=document.implementation.createDocument("","",null);
	   }
	  else
	  {
		alert('Your browser cannot handle this script');
	  }
	xmlDoc.async=false;
	xmlDoc.load(fname);
	return(xmlDoc);
	}
	
function displayResult()
	{
	var grade = document.LPSelect.grade.value;
	var subject = document.LPSelect.subject.value;
	xml=loadXMLDoc("../lesson_plans.xml");
	xsl=loadXMLDoc("../lesson_plans.xsl");
	// code for IE
	if (window.ActiveXObject)
	  {
		var XSLTCompiled = new ActiveXObject("MSXML2.XSLTemplate");
		//Add the stylesheet information
		XSLTCompiled.stylesheet = xsl.documentElement;
		
		//Create the XSLT processor
		var XSLTProcessor1 = XSLTCompiled.createProcessor();
		 XSLTProcessor1.input = xml;
		//Set the parameters
		if (grade != "none")
			{
				XSLTProcessor1.addParameter("param1", grade);
			}
		else
			{
				XSLTProcessor1.addParameter("param1", "");
			}
		if (subject != "none")
			{
				XSLTProcessor1.addParameter("param2", subject);
			}
		else
			{
				XSLTProcessor1.addParameter("param2", "");
			}
		 
		//Perform the transform
		XSLTProcessor1.transform();
		ex=XSLTProcessor1.output;
		//ex=xml.transformNode(xsl);
//				document.getElementById("gradetest").innerHTML = grade;
//		document.getElementById("subjecttest").innerHTML = subject;
		document.getElementById("LP").innerHTML=ex;
	  }
	  // code for Mozilla, Firefox, Opera, etc.
	  else if (document.implementation
	  && document.implementation.createDocument)
	  {
		xsltProc = new XSLTProcessor();
		xsltProc.importStylesheet(xsl);
		if (grade != "none")
			{
				xsltProc.setParameter(null, 'param1', grade);
			}
		else
			{
				xsltProc.setParameter(null, 'param1', '');
			}
		if (subject != "none")
			{
			xsltProc.setParameter(null, 'param2', subject);
			}
		else
			{
				xsltProc.setParameter(null, 'param2', '');
			}
// 		xsltProc.setParameter(null, 'param2',subject);
		resultDocument = xsltProc.transformToFragment(xml,document);
//		document.getElementById("gradetest").innerHTML = grade;
//		document.getElementById("subjecttest").innerHTML = subject;
		document.getElementById("LP").innerHTML = "";
		document.getElementById("LP").appendChild(resultDocument);
	  }
	}
