// rev 012010a - added pop-up window function 
// rev 060208a - added intergate domain to get_ref function
// rev 041408a - initial library


// All things cookie - I'm using these functions are to store
// collapsable div state for the left hand vertical menu.


// get referring domain

function get_ref() {

	var the_url = location.href;
	var lead_slashes = the_url.indexOf("//");
	var domain_start = lead_slashes + 2;
	var without_resource = the_url.substring(domain_start, the_url.length);
	var next_slash = without_resource.indexOf("/");
	var domain = without_resource.substring(0, next_slash);
	
	//alert (domain);
	
	if ((domain == 'www.cccoe.k12.ca.us') || (domain == 'www.cocoschools.org') || (domain == 'cocoschools.org') || (domain == 'intergate.cccoe.k12.ca.us')) {		
		
		//alert ("The referring domain: " + domain);
		return domain;
		
	} else {
		
		return null;
	
	}

}

// Generic set cookie function

var expdays=365;

function set_cookie(name, value, expires, path, domain, secure) {
	
	var path='/';
	
	var domain = get_ref();
	
	if ((domain == 'www.cccoe.k12.ca.us') || (domain == 'intergate.cccoe.k12.ca.us')) {		
		
		//alert ("The referring domain: " + domain);
		var domain = 'cccoe.k12.ca.us';
		
	}
	
	if ((domain == 'www.cocoschools.org') || (domain == 'cocoschools.org')) {		
		
		//alert ("The referring domain: " + domain);
		var domain = 'cocoschools.org';
		
	}
	
	//alert(domain);
	
	//alert ("setcookie functioncall: " + domain);
	
	//var domain='www.cccoe.k12.ca.us';
	
	//alert ("in the set cookie function: expdays: " + expdays);
	//alert ("set cookie function: path: " + path);
	//alert ("set cookie function: domain: " + domain);
	
	if (!expires) {
		expires = new Date()
	}
	document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : "; expires=" + expires.toGMTString()) + ((path == null) ? "" : "; path=" + path) + ((domain == null) ? "" : "; domain=" + domain) + ((secure == null) ? "" : "; secure");
	
	//alert("DEBUG The cookie itself: " + document.cookie);
	
}


// This function actually writes the particular menu group state to cookie onclick
// This function supports up to the 4 vertical menu groups ("The CCCOE",
// "Divisions", "Departments & Programs" and "Information") currently in
// defined in the html template. This functions will need to be modified
// if more main menu groups (and resulting divs) are added.

function saving_cookie(obj, n) {
	
	var key = obj;
	var value = n;
	var expdate = new Date ();
	expdate.setTime (expdate.getTime() + (expdays*24*60*60*1000));
	var group1 = 0;
	var group2 = 0;
	var group3 = 0;
	var group4 = 0;
	
	if (key == 'item1') {
		var data = n;
		//alert ("Group1 data: " + data);
		var cookie_name = "mg1";
		set_cookie(cookie_name,data,expdate);
	}
	
	if (key == 'item2') {
		var data = n;		
		//alert ("Group2 data: " + data);
		var cookie_name = "mg2";	
		set_cookie(cookie_name,data,expdate);
	}
	
	if (key == 'item3') {
		var data = n;		
		//alert ("Group3 data: " + data);
		var cookie_name = "mg3";
		set_cookie(cookie_name,data,expdate);		
	}
	
	if (key == 'item4') {
		var data = n;		
		//alert ("Group4 data: " + data);
		var cookie_name = "mg4";	
		set_cookie(cookie_name,data,expdate);		
	}	
	
}


// This function is called onload and initializes the menu state based upon
// cookie vals.
// This function supports up to the 4 vertical menu groups ("The CCCOE",
// "Divisions", "Departments & Programs" and "Information") currently in
// defined in the html template. This functions will need to be modified
// if more main menu groups (and resulting divs) are added.

function init_menu() {

		//alert ("c: " + c);
		
		var group = new Array(4);
		
		group[0] = "mg1";
		group[1] = "mg2";
		group[2] = "mg3";
		group[3] = "mg4";
	
	//new idea
		var result1 = readCookie(group[0]);
		var result2 = readCookie(group[1]);
		var result3 = readCookie(group[2]);
		var result4 = readCookie(group[3]);
		
	
		if (result1 == 1) {
		//alert ("result 1: " + result1);
			var div1 = 'item1';
			var abra3 = document.getElementById(div1).style;
			abra3.display = "block";
			var div2 = 'item1a';
			var abra4 = document.getElementById(div2).style;
			abra4.display = "none";	
			
		}
		
		if (result2 == 1) {		
		//alert ("result 2: " + result2);
			var div3 = 'item2';
			var abra5 = document.getElementById(div3).style;
			abra5.display = "block";
			var div4 = 'item2a';
			var abra6 = document.getElementById(div4).style;
			abra6.display = "none";	
		}
		
		if (result3 == 1) {					
		//alert ("result 3: " + result3);
			var div5 = 'item3';
			var abra7 = document.getElementById(div5).style;
			abra7.display = "block";
			var div6 = 'item3a';
			var abra8 = document.getElementById(div6).style;
			abra8.display = "none";	
		}		
		
		if (result4 == 1) { 			
		//alert ("result 4: " + result4);
			var div7 = 'item4';
			var abra9 = document.getElementById(div7).style;
			abra9.display = "block";
			var div8 = 'item4a';
			var abra10 = document.getElementById(div8).style;
			abra10.display = "none";		
		}	
	
}

// As the function name implies, this reads the cookie for menu state
// name - value pairs.

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) {	
		//alert (c.substring(nameEQ.length,c.length));
		return c.substring(nameEQ.length,c.length);
		}
	}
	return null;
}

// This is the functions that's called to write the menu
// group state to cookie upon the requisite onclick event

function idMenuState (obj1, obj2) {
	
	var el1 = document.getElementById(obj1);
	var el2 = document.getElementById(obj2);
	
		if (el1.style.display != 'none') {
			saving_cookie(obj1, 1);
		} else {
			saving_cookie(obj1, 0);
		}
}

// End all things cookie


// The collapsing / expanding div script used for the left
// vertical menu

function kadabra(zap, zap2) {

// switch menu block on and off - click event
if (document.getElementById) {
	
	var abra = document.getElementById(zap).style;
	
	if (abra.display == "block") {
	abra.display = "none";
	} else {
	abra.display = "block";
	}
		
	var abra2 = document.getElementById(zap2).style;
	
	if (abra.display == "block") {
	abra2.display = "none";
	} else {
	abra2.display = "block";
	}	
	
	// record menu state to cookie
	idMenuState(zap, zap2);
	
	return false; 
    
	} else {
	
	return true;
	
	}
	 
}

// All things breadcrumb

function breadCrumbs(base,delStr,defp,cStyle,tStyle,dStyle,nl) { // by Paul Davis - http://www.kaosweaver.com
  loc=window.location.toString();subs=loc.substr(loc.indexOf(base)+base.length+1).split("/");
  document.write('<a href="'+getLoc(subs.length-1)+defp+'" class="'+cStyle+'">Home</a>  '+'<span class="'+dStyle+'"><'+delStr+'></span> ');
  a=(loc.indexOf(defp)==-1)?1:2;for (i=0;i<(subs.length-a);i++) { subs[i]=makeCaps(unescape(subs[i]));
  document.write('<a href="'+getLoc(subs.length-i-2)+defp+'" class="'+cStyle+'">'+subs[i]+'</a>  '+'<span class="'+dStyle+'"><'+delStr+'></span> ');}
  if (nl==1) document.write("<br>");document.write('<span class="'+tStyle+'">'+document.title+'</span>');
}

function makeCaps(a) {
  g=a.split(' ');for (l=0;l<g.length;l++) g[l]=g[l].toUpperCase().slice(0,1)+g[l].slice(1);
  return g.join(" ");
}

function getLoc(c) {
  var d="";if (c>0) for (k=0;k<c;k++) d=d+"../"; return d;
}


// Addition of pop-up window function


function get_ref() {

	var the_url = location.href;
	var lead_slashes = the_url.indexOf("//");
	var domain_start = lead_slashes + 2;
	var without_resource = the_url.substring(domain_start, the_url.length);
	var next_slash = without_resource.indexOf("/");
	var domain = without_resource.substring(0, next_slash);
	
	//alert (domain);
	
	if ((domain == 'www.cccoe.k12.ca.us') || (domain == 'www.cocoschools.org') || (domain == 'cocoschools.org') || (domain == 'intergate.cccoe.k12.ca.us')) {		
		
		//alert ("The referring domain: " + domain);
		return domain;
		
	} else {
		
		return null;
	
	}

}

// When caling this function,the 'url' parameter must be an absolute URL

function poponclick(url,name) {
	
	var url = url;
	var name = name;
	var lead_slashes = url.indexOf("//");
	var domain_start = lead_slashes + 2;
	var without_resource = url.substring(domain_start, url.length);
	var next_slash = without_resource.indexOf("/");
	var domain = without_resource.substring(0, next_slash);
	
	//alert(domain);
		
	if ((domain == 'www.cccoe.k12.ca.us') || (domain == 'www.cocoschools.org') || (domain == 'cocoschools.org') || (domain == 'intergate.cccoe.k12.ca.us')) {		
		
		new_window = window.open (url, name, "location=0,status=0,scrollbars=0,toolbar=0,menubar=0,width=650,height=480");
		new_window.moveTo(0,0);
		return true;
		
	} else {
		
		alert("Bad domain");
		return false;
	
	}
							
}
