// globally set up the different possible color combos - all "on" states refer to this to see what color they should be in, and the stylesheet that gets served up depends on this info too

colors = new Array('darkBlue','green','lightBlue','orange');
hexColors =  new Array('004d7a','79a32f','6796a1','cb7d2a');
//set a random number for use in determining a stylesheet/color to serve up
rn = Math.floor((Math.random() * colors.length));

//check to see if they've already got a color set, if not, give them a color based on random number and set the cookie, if so, give them the color they have
var x = readCookie('colorCookie')

if (!x) {
	yourColor = colors[rn];
	createCookie('colorCookie',yourColor,1);
	hi = rn;
}
if (x) {
	yourColor = x;
	for(i=0;i<=colors.length;i++) {
		if(x==colors[i]) {hi=i};
	}
}
//enact appropriate color css, set some variables for "on" states used later on in the 'page' function
setActiveStyleSheet(yourColor);
hex = hexColors[hi];
hex = '#' + hex;
//bgImg = 'url(/williamsmarketing.oaktree.com/images/activeNav' + colors[hi] + '.gif)';

//functions for creating, reading, and deleting cookie - we don't really use the delete one at the moment
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=/";
}

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 eraseCookie(name) {
	createCookie(name,"",-1);
}

//function to swap stylesheets, used by color scripting above

function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
}

//this function determines what page/section and turns "on" the appropriate items

function page() {

	//get pagename
	l = document.location.href;
	l.toString();
	b = l.lastIndexOf('/')+1;
	dl = l.lastIndexOf('.');
	dl = l.length-dl;
	e = l.length-dl;
	l = l.substring(b,e);
	
	//current directory	stuff
	c = 0;	
	cs=new Array();
	
	//change style of hyperlinks to current page anywhere on the page
	
	//the next two lines set up what we're checking here: if it's IE we can just run through all the objects in the document and check out their properties.  If it is something standards based, like Firefox, we have to use document.anchors, and also we have to make sure to name our links so we can find them (even thought we aren't USING the name to find them (argh!).  In other words, the script will work in ie with or without named links, but needs the names for the others
	
	if (document.all) {itot = document.all.length} else {itot = document.anchors.length};
	if (document.all) {al = document.all} else {al = document.anchors};
	
	
	
	for (i=0;i<itot;i++) {
		dis = (al[i].id);
		if(al[i].href) {
			lin = al[i].href;
			if(lin==document.location.href) {
				if(dis.indexOf('subNav')>=0) {
					al[i].className = 'subNavOn';
					// have to force the link color - something to do with css rules/overrules
					al[i].style.color = hex;
				};
				if(dis.indexOf('TwoRows')>=0) {
					al[i].className = 'subNavOnTwoRows';
				};
			}
		}
		}
		
		
		var sectionClass = section.toLowerCase() + 'On';
		section = 'nav' + section;
		sectionNavItem = document.getElementById(section);
		sectionNavItem.className = sectionClass;
		
		//run google maps function for contact page - this has to run now, and becasuse the bottom section of the page needs to be constant through the site (Dreamweaver) we'll run a check to see if it is that page and run the function in that case only
		
		u = document.location.href;
		if (u.indexOf('/about-us/contact.aspx') > 0) {
			loadMap();
		}
		// end google maps thing
		
}
	
//catch function to manually turn something on - this is a temporary solution for third level pages that are currently in 2nd level directories.  Eventually we'll add the ability for the script to recognize both the section and the subsection via directories the current page is in
	
function manual(whichLink) {

	document.getElementById(whichLink).style.color = '#FFA500';
	
}

// preselect functions for contact forms

	function getQueryVariable(variable) {
	  var query = window.location.search.substring(1);
	  var vars = query.split("&");
	  for (var i=0;i<vars.length;i++) {
	    var pair = vars[i].split("=");
	    if (pair[0] == variable) {
	      return pair[1];
	    }
	  } 
	  return null;
	 }

	
	function preselect() {
		
		whichOne = getQueryVariable("property");
		
		if (whichOne != null) {
			p=whichOne;
			while( p.indexOf('%20' )+1) p=p.replace(/%20/,' ');
			
			box = document.form1.ddlProperties;
			
			for(i=0; i<box.length; i++) {
				if (box.options[i].value == p) {
					//alert(box.options[i].value);
					box.options[i].selected = true;
				}
			}
		}
	}