//Style Sheet Switcher version 1.1 Oct 10th, 2006
//Author: Dynamic Drive: http://www.dynamicdrive.com
//Usage terms: http://www.dynamicdrive.com/notice.htm

var manual_or_random="manual" //"manual" or "random"
var randomsetting="3 days" //"eachtime", "sessiononly", or "x days (replace x with desired integer)". Only applicable if mode is random.

//////No need to edit beyond here//////////////

function getCookie(Name) { 
var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
if (document.cookie.match(re)) //if cookie found
return document.cookie.match(re)[0].split("=")[1] //return its value
return null
}

function setCookie(name, value, days) {
var expireDate = new Date()
//set "expstring" to either future or past date, to set or delete cookie, respectively
var expstring=(typeof days!="undefined")? expireDate.setDate(expireDate.getDate()+parseInt(days)) : expireDate.setDate(expireDate.getDate()-5)
document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/";
}

function deleteCookie(name){
setCookie(name, "moot")
}


function setStylesheet(title, randomize){ //Main stylesheet switcher function. Second parameter if defined causes a random alternate stylesheet (including none) to be enabled
var i, cacheobj, altsheets=[""]
for(i=0; (cacheobj=document.getElementsByTagName("link")[i]); i++) {
if(cacheobj.getAttribute("rel").toLowerCase()=="alternate stylesheet" && cacheobj.getAttribute("title")) { //if this is an alternate stylesheet with title
cacheobj.disabled = true
altsheets.push(cacheobj) //store reference to alt stylesheets inside array
if(cacheobj.getAttribute("title") == title) //enable alternate stylesheet with title that matches parameter
cacheobj.disabled = false //enable chosen style sheet
}
}
if (typeof randomize!="undefined"){ //if second paramter is defined, randomly enable an alt style sheet (includes non)
var randomnumber=Math.floor(Math.random()*altsheets.length)
altsheets[randomnumber].disabled=false
}
return (typeof randomize!="undefined" && altsheets[randomnumber]!="")? altsheets[randomnumber].getAttribute("title") : "" //if in "random" mode, return "title" of randomly enabled alt stylesheet
}

function chooseStyle(styletitle, days){ //Interface function to switch style sheets plus save "title" attr of selected stylesheet to cookie
if (document.getElementById){
setStylesheet(styletitle)
setCookie("mysheet", styletitle, days)
}
}

function indicateSelected(element){ //Optional function that shows which style sheet is currently selected within group of radio buttons or select menu
if (selectedtitle!=null && (element.type==undefined || element.type=="select-one")){ //if element is a radio button or select menu
var element=(element.type=="select-one") ? element.options : element
for (var i=0; i<element.length; i++){
if (element[i].value==selectedtitle){ //if match found between form element value and cookie value
if (element[i].tagName=="OPTION") //if this is a select menu
element[i].selected=true
else //else if it's a radio button
element[i].checked=true
break
}
}
}
}

if (manual_or_random=="manual"){ //IF MANUAL MODE
var selectedtitle=getCookie("mysheet")
if (document.getElementById && selectedtitle!=null) //load user chosen style sheet from cookie if there is one stored
setStylesheet(selectedtitle)
}
else if (manual_or_random=="random"){ //IF AUTO RANDOM MODE
if (randomsetting=="eachtime")
setStylesheet("", "random")
else if (randomsetting=="sessiononly"){ //if "sessiononly" setting
if (getCookie("mysheet_s")==null) //if "mysheet_s" session cookie is empty
document.cookie="mysheet_s="+setStylesheet("", "random")+"; path=/" //activate random alt stylesheet while remembering its "title" value
else
setStylesheet(getCookie("mysheet_s")) //just activate random alt stylesheet stored in cookie
}
else if (randomsetting.search(/^[1-9]+ days/i)!=-1){ //if "x days" setting
if (getCookie("mysheet_r")==null || parseInt(getCookie("mysheet_r_days"))!=parseInt(randomsetting)){ //if "mysheet_r" cookie is empty or admin has changed number of days to persist in "x days" variable
setCookie("mysheet_r", setStylesheet("", "random"), parseInt(randomsetting)) //activate random alt stylesheet while remembering its "title" value
setCookie("mysheet_r_days", randomsetting, parseInt(randomsetting)) //Also remember the number of days to persist per the "x days" variable
}
else
setStylesheet(getCookie("mysheet_r")) //just activate random alt stylesheet stored in cookie
} 
}


// rotating image change for homepage
function changeQuote()
{
var quotes = new Array('<p>&quot;MIT OpenCourseWare is the best service for self-learning.&quot;</p><ul><li>-</li>	<li>Student<br />Iran</li></ul>', '<p>&quot;This site helped me get better grades and increased my knowledge level.&quot;</p><ul><li>-</li>	<li>Student<br />Israel</li></ul>', '<p>&quot;With OCW I can complement my studies and have a better education.&quot;</p><ul><li>-</li>	<li>Student<br />Mexico</li></ul>', '<p>&quot;If it wasn\'t for these OCW lectures, I would not have received my \'A\' in my algorithms course.&quot;</p><ul><li>-</li>	<li>Student<br />Norway</li></ul>', '<p>&quot;MIT OCW has been invaluable to my research by filling in gaps in my general understanding.&quot;</p><ul><li>-</li>	<li>Student<br />Denmark</li></ul>', '<p>&quot;OCW courses cover wide variety of interests. They are a great guide and source of learning to me.&quot;</p><ul><li>-</li>	<li>Student<br />United Kingdom</li></ul>', '<p>&quot;I check OCW whenever I start a course. I also push my professors to check what\'s being done here.&quot;</p><ul><li>-</li>	<li>Student<br />Turkey</li></ul>', '<p>&quot;Thanks to video lectures, I managed to get an A in physics this term.&quot;</p><ul><li>-</li>	<li>Student<br />Latvia</li></ul>', '<p>&quot;OCW is inspiring me to push harder and enjoy learning with the assurance that I\'m getting the best.&quot;</p><ul><li>-</li>	<li>Student<br />Nigeria</li></ul>', '<p>&quot;MIT OCW serves as an excellent tool to motivate my students.&quot;</p><ul><li>-</li>	<li>Educator<br />India</li></ul>', '<p>&quot;This is an excellent resource and it gives teachers in \'remote\' areas a window on the world.&quot;</p><ul><li>-</li>	<li>Educator<br />United Arab Emirates</li></ul>', '<p>&quot;I have improved my classes so much with your material.&quot;</p><ul><li>-</li>	<li>Educator<br />Brazil</li></ul>', '<p>&quot;I find MIT OCW of great aid in letting me compare and improve my materials, books and assignments.&quot;</p><ul><li>-</li>	<li>Educator<br />Mexico</li></ul>', '<p>&quot;OCW has allowed me to feel more confident in the way I present examples and teach lessons.&quot;</p><ul><li>-</li>	<li>Educator<br />Panama</li></ul>', '<p>&quot;As an educator, I find OCW a valuable resource which enhances my classroom activities.&quot;</p><ul><li>-</li>	<li>Educator<br />Switzerland</li></ul>', '<p>&quot;This fantastic teaching resource is very useful for physicists working in the developing world.&quot;</p><ul><li>-</li>	<li>Educator<br />South Africa</li></ul>', '<p>&quot;The material presented here is of great help in teaching Biology.&quot;</p><ul><li>-</li>	<li>Educator<br />Nigeria</li></ul>', '<p>&quot;This site has become my favorite for learning and upgrading my knowledge.&quot;</p><ul><li>-</li>	<li>Educator<br />Nepal</li></ul>');

var images = new Array ("/OcwWeb/images/wihomeimages/01.jpg", "/OcwWeb/images/wihomeimages/02.jpg", "/OcwWeb/images/wihomeimages/03.jpg", "/OcwWeb/images/wihomeimages/04.jpg", "/OcwWeb/images/wihomeimages/05.jpg", "/OcwWeb/images/wihomeimages/06.jpg", "/OcwWeb/images/wihomeimages/07.jpg", "/OcwWeb/images/wihomeimages/08.jpg", "/OcwWeb/images/wihomeimages/09.jpg", "/OcwWeb/images/wihomeimages/10.jpg", "/OcwWeb/images/wihomeimages/11.jpg", "/OcwWeb/images/wihomeimages/12.jpg", "/OcwWeb/images/wihomeimages/13.jpg", "/OcwWeb/images/wihomeimages/14.jpg", "/OcwWeb/images/wihomeimages/15.jpg", "/OcwWeb/images/wihomeimages/16.jpg", "/OcwWeb/images/wihomeimages/17.jpg");

var qrand = Math.floor(Math.random()*quotes.length);
var irand = Math.floor(Math.random()*images.length);
var txt='';
txt+='<ul><li class="email"><a href="javascript:emailPopUp()">Email this page<\/a><\/li><\/ul>';
document.getElementById("switchbutton").innerHTML=txt;
document.getElementById("EWebEditProPlaceholder1").style.backgroundImage = "url("+images[irand]+")";
document.getElementById("quote_main").innerHTML = quotes[qrand] + '<p><a href="/OcwWeb/web/about/impact/index.htm"  class="bullet">Read more  world impact. <\/a><\/p>';

}


// rotating image change ends here

window.onload=function(){
var txt='';
txt+='<ul><li class="email"><a href="javascript:emailPopUp()">Email this page<\/a><\/li><\/ul>';
document.getElementById("switchbutton").innerHTML=txt;
							  	   

}

function emailPopUp()
{
	var pageTitle = document.title;
	var pageUrl   = document.location;
	var URL = "http://ocw.mit.edu/OcwWeb/jsp/emailpage.jsp?title=" + pageTitle + "&referer=" + encodeURIComponent(pageUrl);
	window.open(URL,null,"height=470,width=530,status=yes,toolbar=no,menubar=no,location=no,resizable=yes");
}
function clearSearchBox()
{
	if(document.getElementById("terms").value == "Enter search keyword")
	{
		document.getElementById("terms").value = "";	
	}
	
}
function clearEmailBox()
{

	if(document.getElementById("txt_subscribe").value == "Enter email address")
	{
		document.getElementById("txt_subscribe").value = "";	
	}
	
}

function fillSearchBox()
{
	if(document.getElementById("terms").value == "")
	{
		document.getElementById("terms").value = "Enter search keyword";	
	}
}
function fillEmailBox()
{
	if(document.getElementById("txt_subscribe").value == "")
	{
		document.getElementById("txt_subscribe").value = "Enter email address";	
	}
}
/*
// Added by: Setu Garg
// artf472814 - Google search for HFH
// Date: 18-Jun-2007
// This function sets the value of the hidden variables in the search form
//	1. searchInOcwSite - enables or disables usage of ocw collection
//	2. siteSelectionIndex - Used to select the value in the drop down on the search results page
*/
function SearchOptionChanged()
{
	if (document.getElementById("searchSelection").selectedIndex!='0' && document.getElementById("searchSelection").selectedIndex!='1')
	{
		document.getElementById("searchInOcwSite").disabled = true;
		document.getElementById("siteSelectionIndex").value = document.getElementById("searchSelection").selectedIndex;
	}

	if (document.getElementById("searchSelection").selectedIndex=='0')
	{
		document.getElementById("searchInOcwSite").disabled = false;
		document.getElementById("searchInOcwSite").value = 'ocw';
		document.getElementById("siteSelectionIndex").value = document.getElementById("searchSelection").selectedIndex;
	}
	else
	if(document.getElementById("searchSelection").selectedIndex=='1')
	{
		document.getElementById("searchInOcwSite").disabled = false;
		document.getElementById("searchInOcwSite").value = 'ocwhs';
		document.getElementById("siteSelectionIndex").value = document.getElementById("searchSelection").selectedIndex;
	}

}
/*
// Added by: Setu Garg
// artf472814 - Google search for HFH
// Date: 18-Jun-2007
// This function sets the drop down on the search results page
*/
function setSiteSelection()
{
        var selectionIndex=getParameter ("siteSelectionIndex");
        var txt='';
        txt+='<ul><li class="email"><a href="javascript:emailPopUp()">Email this page<\/a><\/li><\/ul><div class="clear"><\/div>';
        document.getElementById("switchbutton").innerHTML=txt; 

	// fetch the value of siteSelectionIndex from URL
	var selectionIndex=getParameter ("siteSelectionIndex");	
  	document.getElementById("searchSelection").selectedIndex = selectionIndex;	
}

/*
// Added by: Setu Garg
// artf534446 - Google search for HFH
// Date: 2-Aug-2007
// This function fetched the parameter value from the URL.
*/
function getParameter (parameterName ) {
// Add "=" to the parameter name (i.e. parameterName=value)
var queryString = window.top.location.search.substring(1);

var parameterName = parameterName + "=";
if ( queryString.length > 0 ) {
// Find the beginning of the string
begin = queryString.indexOf ( parameterName );
// If the parameter name is not found, skip it, otherwise return the value
if ( begin != -1 ) {
// Add the length (integer) to the beginning
begin += parameterName.length;
// Multiple parameters are separated by the "&" sign
end = queryString.indexOf ( "&" , begin );
if ( end == -1 ) {
end = queryString.length
}
// Return the string
return unescape ( queryString.substring ( begin, end ) );
}
// Return "null" if no parameter has been found
return "null";
}
} 
