﻿/* COBScripts.js - Holds the Sitewide Custom Javascript 

Last Modified: July 21, 2009 - Vijay

*/



/* FUNCTION: SiteDirRedirect(siteURL)
   Desc: Redirects the User to the Given Site URL
   Param:
   		siteURL : String - Valid URL of a Subsite (Absolute path starting with /)
*/
function SiteDirRedirect(siteURL)
{
	if(siteURL=='0')
	{
		return;		// This is the First Item - Do Nothing
	} 
	else 
	{
		if(siteURL=='') 
		{
			alert('Site not available yet - please contact the Website Administrator');
			return;
		}
		else
		{
			// TODO: (Good to have) - Validate the URL to find if it's really a valid site
			
			window.location.href = siteURL;		// NAVIGATE TO GIVEN URL
		}
	}
}   

/* Book Mark For COB */

function bookmark_us(url, title)
{
	
	if (window.sidebar) // firefox
    	window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print)
	{ // opera
    	var elem = document.createElement('a');
    	elem.setAttribute('href',url);
    	elem.setAttribute('title',title);
    	elem.setAttribute('rel','sidebar');
   		elem.click();
	} 
	else if(document.all)// ie
    	window.external.AddFavorite(url, title);
}

/* Fonts selection based on Style */


/* setActiveStyleSheet() disables all 3 of the font size stylesheets, then enables the one that the user selected.
   the parameter title is passed from the link and matches the title attribute of the css reference.	
   setActiveStyleSheet() also turns off all of the text size icons, and then turns on the current selection
*/
function setActiveStyleSheet(title) {

  var linkTags = document.getElementsByTagName("link");
  
  for(var i=0;i<linkTags.length;i++)
  {
  	if(linkTags[i].getAttribute("rel").indexOf("stylesheet") && linkTags[i].getAttribute("title"))
	{
		linkTags[i].disabled = true;
		var myTitle = linkTags[i].getAttribute('title');
		if(linkTags[i].getAttribute("title") == title)
		{
			linkTags[i].disabled = false;
		}
	}
  }
}

/* getActiveStyleSheet() is used to find the currently enabled stylesheet, the name of that stylesheet is stored using a cookie
   and retrieved the next time a page is loaded to maintain the correct font-size.
*/
function getActiveStyleSheet() {
  var linkTags = document.getElementsByTagName("link");
  
  for(var i=0; i<linkTags.length; i++) {
  	if(linkTags[i].getAttribute("rel").indexOf("stylesheet") != -1
		&& linkTags[i].getAttribute("title") && !linkTags[i].disabled)
		return linkTags[i].getAttribute("title");
  }
  return null;
}

/* increaseFontSizeStyleSheet() switches to the next-large stylesheet.
*/
function increaseFontSizeStyleSheet() {
  switch( getActiveStyleSheet() ) {
    case 'COBMainLarge':
      setActiveStyleSheet('COBMainMedium');
      break;
    case 'COBMainMedium':
      setActiveStyleSheet('COBMain');
      break;
    case 'COBMain':
      break;
  }
  return null;
}

/* decreaseFontSizeStyleSheet() switches to the next-smaller stylesheet.
   added for the text-size buttons in the topnav bar June 2, 2009
*/
function decreaseFontSizeStyleSheet() {
  switch( getActiveStyleSheet() ) {
    case 'COBMain':
      setActiveStyleSheet('COBMainMedium');
      break;
    case 'COBMainMedium':
      setActiveStyleSheet('COBMainLarge');
      break;
    case 'COBMainLarge':
      break;
  }
  return null;
}

/* getPreferredStyleSheet() is used when no cookie is found to load the default/standard stylesheet
*/
function getPreferredStyleSheet() {
  var linkTags = document.getElementsByTagName("link");
  
  for(var i=0; i<linkTags.length; i++) {
    if(linkTags[i].getAttribute("rel").indexOf("style") != -1
       && linkTags[i].getAttribute("rel").indexOf("alt") == -1
       && linkTags[i].getAttribute("title"))
	   return linkTags[i].getAttribute("title");
  }
  return null;
}

/* setCookie() creates a cookie of name "style" with the value matching the user's current stylesheet selection
*/
function setCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

/* getCookie() retrieves a cookie of name "style", parses the value and returns xsmall, small or medium based on the user's previous stylesheet selection
*/
function getCookie(name) {
  var cookieName = name + "=";
  var cookieSplit = document.cookie.split(';');
  for(var i=0;i < cookieSplit.length;i++) {
    var c = cookieSplit[i];
    while(c.charAt(0)==' ')
	{
		c = c.substring(1,c.length);
	}
    if(c.indexOf(cookieName) == 0)
	{
		return c.substring(cookieName.length,c.length);
	}
  }
  return null;
}


/* This onload event retrieves the style cookie when a page loads 
*/
window.onload = function(e) {
  var cookie = getCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

/* This unonload event sets the style cookie when a page unloads 
*/
window.onunload = function(e) {
  var title = getActiveStyleSheet();
  setCookie("style", title, 365);
}
	
/* Email Related */
function GotoEmailPage() 
{
	var title = window.document.title;
	var url = window.document.URL;
	
	window.document.location = "/pages/EmailPageLink.aspx?title=" + title + "&link=" + url;
}		
	
