//Ray Blackford for Yale ITS
// Edited 9-5-07 by Vincent Massaro

/* 
javascript dom cheat sheet
=============================================
document.forms[0].length=number of items in form..also can be called by name [document.formname.length]
document.forms[0].elements[0].value...get values...use a variable to loop document.forms[0].elements[i].value
document.options.elements[0].checked...returns true or false if checked
===============================================
*/

/*
instructions for adding new link to quicklinks default set
==========================================================
1. scroll down to where it says Default Quicklinks (line 125)
2. follow the same format as the other links when adding a new link
*/

//this function stores the choices of links after the user has submitted
function grabData()
{
//variables
var display;
var string = new Array();
var cookieValue;
var y;
y=0;

//--------------grab display value checked-------------//
	for(i=0;i<document.options.display.length;i++)
	{
		if(document.options.display[i].checked)
		{ 
		display=document.options.display[i].value;
		}
	}
	//--------------grab quicklink values checked-------------//
	for(j=2;j<document.options.length;j++)
	{
		if(document.options.elements[j].checked)
		{
		//--------place all checked values in an array---//
		
		string[y]="<--break-->"+document.options.elements[j].name+"<--break-->"+document.options.elements[j].value;
		y++;
		}
	}
	
	
		if(string.length==0)
		{
		alert("Please choose at least one quicklink");
		}
		else 
		{ 
		cookieValue=display+string.join("");
		createCookie('options',cookieValue,365); 
		}
}

//function creates cookie with customized link information
//http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	//document.cookie = name+"="+value+expires+"; path=/"; 
	document.cookie = name+"="+value+expires+"; path=/"; 

	//document.cookie = name+"="+value+expires+"; path=/";
	window.location.href="http://www.csun.edu/";//The url you want to redirect to goes here
}

//http://www.quirksmode.org/js/cookies.html
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}



//function outputs customized links
function createNav()
{
var x = readCookie('options')
var quick_links;

var i;
i=1;

if (x) 
{
	var cookieArray=x.split("<--break-->");
	if(cookieArray[0]=='Pull-Down Menu')
	{ 
		quick_links="<form name=\"quicklinks\" id=\"quicklinks\" action=\" \"><select name=\"urllink\" id=\"urllink\"><option value='' selected='selected'>My customized QuickLinks:</option>";	
				while(i<cookieArray.length)
		{
		quick_links+='<option value="'+cookieArray[i+1]+'">'+cookieArray[i]+'</option>';
		i=i+2;
		}
		quick_links=quick_links + "</select><label for=\"gobtn\" class=\"hide\">Go</label><input id=\"gobtn\" name=\"gobutton\" type=\"button\" value=\"Go\" onclick=\"document.location=document.quicklinks.urllink.options [document.quicklinks.urllink.selectedIndex].value\" onkeypress=\"document.location=document.quicklinks.urllink.options [document.quicklinks.menu.selectedIndex].value\" /></form>"; 
		
		//quick_links=quick_links + "</select></form>"; 
		

	return(quick_links + '<p><em><a href="http://www.csun.edu/js/quicklinks.html">Customize your Links</a></em></p>');

	}
	else 
	{
		quick_links="<h3>My Customized QuickLinks:</h3><ul class='altList'>"; //Added altList class to style customized quicklinks
		while(i<cookieArray.length)
		{
			
		quick_links+='<li><a href="'+cookieArray[i+1]+'">'+cookieArray[i]+'</a></li>';
		i=i+2;
		}
		quick_links=quick_links+"</ul>";
		return(quick_links + '<p><em><a href="http://www.csun.edu/js/quicklinks.html">Customize your QuickLinks</a></em></p>');		
	}
}
else
{ 
// Default Quicklinks
//==============================
var quick_links;
quick_links="<form name=\"quicklinks\" action=\"\"><label for=\"quicklinkslist\">Quick Links</label>";
//class added to ul to style the list (vv)
quick_links+='<select name="menu" id="quicklinkslist" size="1">';
quick_links+='<option value="" selected="selected"></option>';
quick_links+='<option value="http://www.csumentor.edu/AdmissionApp/">Admissions Application</option>';
quick_links+='<option value="http://www-admn.csun.edu/publicsafety/police/crimereport.htm">Campus Security Report</option>';
quick_links+='<option value="http://www.csun.edu/career/">Career Center</option>';
quick_links+='<option value="http://tinyurl.com/675ao9/">Class Search</option>';
quick_links+='<option value="http://www.csun.edu/senate/">Faculty Senate</option>';
quick_links+='<option value="http://www.csun.edu/maps/cm1.html">Maps/Parking</option>';
quick_links+='<option value="http://matador.bkstr.com">Matador Bookstore</option>';
quick_links+='<option value="http://www.csun.edu/anr/soc/">Registration Guide</option>';
quick_links+='<option value="http://www.csun.edu/anr/forms/">Student Forms</option>';
quick_links+='<option value="http://www.csun.edu/catalog/">University Catalog</option>'; 
quick_links+='</select><label for="gobtn" class="hide">Go</label><input id="gobtn" name="gobutton" type="button" value="Go" onclick="document.location=document.quicklinks.menu.options [document.quicklinks.menu.selectedIndex].value" onkeypress="document.location=document.quicklinks.menu.options [document.quicklinks.menu.selectedIndex].value" /></form>';
return(quick_links + '<p><em><a href="http://www.csun.edu/js/quicklinks.html">Customize your QuickLinks</a></em></p>');
}
}

//This function will check existing settings when the user returns to the form
function checkForm()
{
var x = readCookie('options')
var quick_links; 
if (x) 
{
var cookieArray=x.split("<--break-->");

	for(i=0;i<cookieArray.length;i++)
	{
		for(j=0;j<document.options.length;j++)
		{
			if(cookieArray[i]==document.options.elements[j].value)
			{
			document.options.elements[j].checked=true;	
			}
		
		}
	}
		
} 

}

function loadQuickLinks()
{
//This function redirects the user to the page they choose from their customized quicklinks


var url=document.quicklinks.urllink.options[document.quicklinks.urllink.selectedIndex].value;
if (url!='#')
{
window.location=url;
}
}