/*

This file is called from the enssc.com site. It must reside at http://www.calacademy.org/include/enssc.js

This JS tricks IE and safari browser into allowing cookies (and therefore session variables) to be set in a "parent" page at one domain that has an iframe pointing to a "child" page at another domain.  It does this by taking advantage of the browser behavior of allowing cookies to shared among any windows that are opened from the same page.

What we''ll do for IE and safari only users is open the Academy store page at Event Networks so that it can set cookies. Then we'll use a location.href to open the store page at the Academy in the same window. The Academy store page includes an iframe that points to the Academy store page at Event Networks. Voila! We're sharing cookies between the parent page and the child frame.

To make this seamless, we put all the content of the Event Networks page into a div with display set to none. That way on the first visit to the page (before the location.href),  the user doesn't see anything while the javascript below figures our if this is the first visit to the Event Networks page, or if it is the second visit that happenes inside the iframe.  It does this by looking for the existance of a GET paramenter "cas=h" that the first visit uses (h is for hide).  If cas=h does not exist, the javascript will unhide the div and exit. But if cas=h does exist, the javascript will wait for the session cookie to be set and once it is, location.href will be used to refresh the page with the Academy store page. The Academy store page then opens the Event Networks store page inside the iframe without the cas=h parameter and this time the javascript unhides the div and exits.

Note that the javascript checks if the the including page was called with a GET parameter "cas". if so then the code that follows applies. If not, the following code does not apply. This is important because all cas pages at enssc use this same js code and we only want to execute the js code on the home page. 

Event Networks URL: http://www.calacademy.org/enssc/

Academy URL: http://enssc.com/default.aspx?store=532

On a related note, there is a "Museum Store" link at the top of all Academy web pages that points to /store/. However, for ie and safari users, that link is redefined in default.js to point to http://enssc.com/default.aspx?store=532&cas=h and an onClick event is added that sets a "store_click" cookie to "y".  Likewise, for users who don't click on the link, arrive at the /store/ page some other way, the /store/ page sends them to enssc and sets the store_click cookie to "y". Then the next time the user goes to /store/, in the iframe, they are allowed to see the /store/ page instead of being sent off to enssc.


UPDATES:

15oct09 - Removed the polling function in favor of an immediate redirect to CAS. This is because Event Networks changed their server and the Session ID can no longer be detected, breaking the page for IE and Safari. We were trying to detect the Session ID to ensure that it was set. Now it always seems to be set immediately anyway so the polling is not necessary. If problems arise because the user is moved off the enssc.com site too fast for the session id to be set, try delaying the redirect back to cas. The code is there - you just have to remark it out.  The polling function has been left in the code in case it is needed in the future but is not used. - pperez

*/

// Find out what browser and version we're dealing with and if any parameters were passed

var b_name		=	'unknown';
var b_version	=	'unknown';

if (/Safari[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {

	b_name		=	'Safari';
	b_version	=	new Number(RegExp.$1);
	
}
else {
	if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {

		b_name		=	'Firefox';
		b_version	=	new Number(RegExp.$1);
		
	}
	else {
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {

			b_name		=	'IE';
			b_version	=	new Number(RegExp.$1);
			
		}
		
	}

}

var cas_param = get_url_param('cas');

// The style sheet has no background or background color assigned to the body. However, IE must have a background or else it uses white, so we must give it one. And there are other styles that need adjusting too...

// Style changes for all browsers. These should be moved into the site styles.

document.write("<style>\n");
	
document.write("#homeBSGrid {\n");
document.write("	font-size:11px\n");
document.write("}\n");
	
document.write("#homeBSGrid img {\n");
document.write("	border:6px solid #fff;\n");
document.write("}\n");
	
document.write("#itemTable img {\n");
document.write("	border:6px solid #fff;\n");
document.write("}\n");
	
document.write("#categoryTable img {\n");
document.write("	border:6px solid #fff;\n");
document.write("}\n");

document.write("</style>\n");

	
if (b_name == 'IE') {

	document.body.style.backgroundColor = '#697191';

	var the_style = getStyleObject('wrapper');
	the_style.padding = '10px 0px 0px 10px';
	
	document.write("<style>\n");
		
	document.write(".csDGItem {\n");
	document.write("	font-weight: bold;\n");
	document.write("}\n");

	if (b_version == '6') {
		
		document.write(".navTitle {\n");
		document.write("	font-size: 16px;\n");
		document.write("}\n");
		
		document.write(".navLink {\n");
		document.write("	font-size: 15px;\n");
		document.write("}\n");
		
		document.write(".tmCartDetails {\n");
		document.write("	font-size: 15px;\n");
		document.write("}\n");
		
		document.write("#tmViewCart {\n");
		document.write("	font-size: 15px;\n");
		document.write("}\n");
		
		document.write(".tmProductSearch {\n");
		document.write("	font-size: 15px;\n");
		document.write("}\n");
		
		document.write("#homeBSGrid {\n");
		document.write("	font-size: 11px;\n");
		document.write("}\n");
		
		document.write(".productsQtyLabel {\n");
		document.write("	font-size: 15px;\n");
		document.write("}\n");
		
		document.write(".productsQtyLabel {\n");
		document.write("	font-weight: bold;\n");
		document.write("}\n");
		
		document.write(".itemDescription {\n");
		document.write("	font-weight: bold;\n");
		document.write("}\n");
		
		document.write(".itemTitle {\n");
		document.write("	font-weight: bold;\n");
		document.write("}\n");
		
		document.write(".itemPrice {\n");
		document.write("	font-weight: bold;\n");
		document.write("}\n");
		
		document.write(".categoryDescription {\n");
		document.write("	font-weight: bold;\n");
		document.write("}\n");
		
		document.write(".checkoutSectionTitle {\n");
		document.write("	font-weight: bold;\n");
		document.write("}\n");
		
		document.write(".checkoutLabelMedium {\n");
		document.write("	font-weight: bold;\n");
		document.write("}\n");
		
		document.write(".checkoutContent {\n");
		document.write("	font-weight: bold;\n");
		document.write("}\n");
		
		document.write(".checkoutSubSectionTitle {\n");
		document.write("	font-weight: bold;\n");
		document.write("}\n");
		
		document.write(".checkoutLabelSmall {\n");
		document.write("	font-weight: bold;\n");
		document.write("	font-size: 14px;\n");
		document.write("}\n");
		
		document.write(".checkoutNote {\n");
		document.write("	font-weight: bold;\n");
		document.write("	font-size: 14px;\n");
		document.write("}\n");

	}
		
	document.write("</style>\n");
	
}
	
// Hide the content if the "hide" parameter was passed

if (cas_param) {
	
	if ( ((b_name == 'IE') || (b_name == 'Safari')) && (cas_param == 'h')) {
	
		hide('wrapper');		
		send_to_cas();
		//timer=setTimeout("send_to_cas()", 200);	// Giving it a little time to set the session id before leaving.
				
	}
    
}
else {

	show('wrapper');

}

/*

FUNCTIONS

*/

function send_to_cas() {

		location.href = 'http://www.calacademy.org/store/';
}

function poll_for_store_cookie() {

	//Note, the session id name may be ASPSESSIONID with ASP.NET 3.5. Event Networks upgraded to ASP.NET 3.5 in Oct 2009.
	var re=new RegExp("ASP.NET_SessionId"+"=[^;]+", "i");  
	
	if (document.cookie.match(re)) {

		location.href = 'http://www.calacademy.org/store/';
	}
	else {

		timer=setTimeout("poll_for_store_cookie()", 200);		
	}

}

function get_url_param(name) { 

	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); 
	var regexS = "[\\?&]"+name+"=([^&#]*)"; 
	var regex = new RegExp( regexS ); 
	var results = regex.exec( window.location.href ); 
	
	if ( results == null ) {
		return "";
	}
	else {
		return results[1];
	}
	
}

function setStyle (theId, theStyle, theValue) {

	x = document.getElementById(theId);
	x.style[theStyle] = theValue;

} 

function hide(the_div) {

	var the_style = getStyleObject(the_div);
	the_style.display = 'none';
	
}

function show(the_div) {

	var the_style = getStyleObject(the_div);
	the_style.display = 'block';

}

function getStyleObject(objectId) {

	if (document.getElementById && document.getElementById(objectId)) {
		return document.getElementById(objectId).style;
	}
	else {
		if (document.all && document.all(objectId)) {
			return document.all(objectId).style;
		}
		else {
			return false;
		}
	}
	
}
