//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.yale.edu/its/index.html";//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="<p><em>My customized QuickLinks</em>:</p><form name=\"quicklinks\" id=\"quicklinks\"><select name=\"urllink\" id=\"urllink\" onchange=\"loadQuickLinks()\"><option value='#'>Choose a QuickLink</option>";	
				while(i<cookieArray.length)
		{
		quick_links+='<option value="'+cookieArray[i+1]+'">'+cookieArray[i]+'</option>';
		i=i+2;
		}
		//quick_links=quick_links + "</select><input type=\"button\" onclick=\"loadQuickLinks()\" value=\"Submit\"></form>"; 
		quick_links=quick_links + "</select></form>"; 

	return(quick_links + '<a href="http://www.yale.edu/its/quicklinks.html">Customize your Links</a>');
	}
	else 
	{
		quick_links="<p><em>My customized QuickLinks:</em></p><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 + '<a href="http://www.yale.edu/its/quicklinks.html"><p><em>Customize your QuickLinks</a></em></p>');		
	}
}
else
{ 
// Default Quicklinks
//==============================
var quick_links;
quick_links="<p>Default link set:</p>";
//class added to ul to style the list (vv)
quick_links+='<ul class="altList">';
quick_links+='<li><a href="http://www.yale.edu/its/email/">Email services</a></li>';
quick_links+='<li><a href="http://www.yale.edu/its/email/transfer.html">File Transfer Facility</a></li>';
quick_links+='<li><a href="http://www.yale.edu/its/help/supportgroups.html">Identify your local support contact</a></li>';
quick_links+='<li><a href="http://www.yale.edu/its/help/its_a-z.html"> ITS A-Z Index </a></li>';
quick_links+='<li><a href="http://www.yale.edu/its/forms/">ITS forms</a></li>';
quick_links+='<li><a href="http://www.yale.edu/its/accounts/password.html">Passwords at Yale</a></li>';
quick_links+='<li><a href="http://www.yale.edu/its/policy/">Policies</a></li>';
quick_links+='<li><a href="http://www.yale.edu/its/software/">Software</a></li>';
quick_links+='<li><a href="http://www.yale.edu/its/email/webmail.html">Webmail</a></li>'; 
quick_links+='</ul>';
return(quick_links + '<p><em><a href="http://www.yale.edu/its/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;
}
}