/*F*/
function OC_doNothing(){ }
function OC_cancelEvent(e){ event.returnValue = false; }

function OC_navigateTo(sUrl) {
	if (sUrl) document.location = sUrl;
}

function OC_flipProgressIndicator(inBool) {
	// give the user some feedback that their purchase is being processed.
	document.getElementById("progress-content-container").style.display = (inBool) ? "none" : "block";
	document.getElementById("progress-indicator-container").style.display = (inBool) ? "block" : "none";
}

function OC_failureIndicator(inBool,inCondition,inResponse) {
	document.getElementById("progress-content-container").style.display = (inBool) ? "none" : "block";
	document.getElementById("progress-failure-container").style.display = (inBool) ? "block" : "none";
	// we should perhaps hide all the child elements first
	document.getElementById(inCondition).style.display = (inBool) ? "block" : "none";
}
function OC_getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

//  Function to return the value of the cookie specified by "name".
//    name - String object containing the cookie name.
//    returns - String object containing the cookie value, or null if
//      the cookie does not exist.
function OC_getCookie(name) {
	//cvc cookie code
	var pantryCookie = getPantryCookie(name) 
	if(pantryCookie != false) {
		return pantryCookie;
	}
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return OC_getCookieVal(j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}

/* This function sets a cookie that expires in 24 hours */
function OC_setPantryCookie(name, value) {

	//cvc cookie code
	addPantryCookie(name,value);
}

/* This function sets a cookie that expires in 24 hours */
function OC_setCookie(name, value) {

	var expDate = new Date();
	expDate.setTime (expDate.getTime() + (24 * 60 * 60 * 1000));
	document.cookie = name + "=" + escape(value) + "; expires=" + expDate.toGMTString() + "; path=/";    
}

/* This function deletes a cookie */
function OC_deleteCookie(name) {
	//cvc cookie code
	removePantryCookie(name);
/*  var expDate = new Date();
	expDate.setTime (expDate.getTime() - (24 * 60 * 60 * 1000));
	document.cookie = name + "=" + escape ('') +
    "; expires=" + expDate.toGMTString() + "; path=/";
    */
}

function OC_openWindow(target, hname) {
/* REMOVED, individualized player is a requirement of CSM installation & browser check is done as needed
	var indiv = "";
	//first do a browser check
	if (!OC_isValidBrowser())
		indiv = "invalidBrowser";
	else
		indiv = OC_individualized; // globally defined, see below
    target += (target.indexOf("?") != -1) ? ("&indiv=" + indiv) : ("?indiv=" + indiv);
*/
	if (hname) target += "#" + hname;
	optPopup = window.open(target, "oolVideoDownloadsPopup", "toolbar=no,address=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=1,width=500,height=400,top=100,left=100");
	optPopup.focus();
}

function OC_openModalWindow(obj) {
	var modal = obj.getAttribute("modal");
	var elObject = new Object();
	elObject.action = obj.getAttribute("action");
	var returnAction = window.showModalDialog(modal, elObject, "dialogHeight: 250px; dialogWidth: 500px; edge: Raised; center: Yes; help: No; resizable: No; status: Yes;");
	if (returnAction) eval(returnAction);
}

function OC_winResize(inWidth,inHeight) {
	if (document.all || document.getElementById) {
		window.resizeTo(inWidth,inHeight);
	} else if (document.layers) {
		window.outerWidth = inWidth;
		window.outerHeight = inHeight;
	}
}

function OC_imageChange(inImg,inSrc) {
	var inObj = document.getElementById(inImg);
	if (inObj) inObj.src = inSrc;
}
// this checks only for IE 5.5+
function OC_isValidBrowser() {
	var result = false;
	var agt = navigator.userAgent.toLowerCase();
	var appVer = navigator.appVersion.toLowerCase();
	
	var is_minor = parseFloat(appVer);
    var is_major = parseInt(is_minor);
    // Note: On IE, start of appVersion return 3 or 4
    // which supposedly is the version of Netscape it is compatible with.
    // So we look for the real version further on in the string
    // And on Mac IE5+, we look for is_minor in the ua; since 
    // it appears to be more accurate than appVersion - 06/17/2004

	var is_mac = (agt.indexOf("mac") != -1);
	var iePos = appVer.indexOf("msie");
	if (iePos !=-1) {
		if (is_mac) {
			var iePos = agt.indexOf("msie");
			is_minor = parseFloat(agt.substring(iePos+5,agt.indexOf(';',iePos)));
		} else is_minor = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)));
		is_major = parseInt(is_minor);
	}
	var is_ie = (iePos != -1 && agt.indexOf("opera") == -1);

	if (is_ie && is_minor >= 5.5)
		result = true;
	return result;	
}

function OC_isPlayerIndividualized() {
	var secVer, digits, indiv, obj;
	var indivMinimum = 519;
	//alert('Inside Individualization function');
	try
	{
		obj = new ActiveXObject("DRM.GetLicense");
	} catch (e) {return false;}
	//alert('DRM.GetLicense ActiveXObject is not null');
	if (null != obj)
	{
		//alert('DRM.GetLicense ActiveXObject getting securityVersion');
		secVer = obj.GetDRMSecurityVersion();
		//alert('Individualization is ' + secVer);

		digits = secVer.split(".");
		//check that the 4th digit is odd and the first 2 digits are 2.4 or greater
		if ((((digits[3]*1)%2) > 0) && (((digits[0]*256) + (digits[1]*1)) > 515)) 
		{
			return true;
		}
	}
	return false;
}

function OC_getDRMVersion() {
	var obj, drmVer = "0.0.0.0";
	try 
	{
		obj = new ActiveXObject("DRM.GetLicense");
	} catch (e) { return false;}

	if (null != obj) 
	{
		drmVer = obj.getDRMVersion();
	}
	return drmVer;
}

// REMOVED, individualized player is a requirement of CSM installation var OC_individualized = OC_isPlayerIndividualized();

/*
var rules = {
	'ul li' : function(element){
		element.onmouseover = function(){ showSubmenu(this); }
		element.onmouseout = function(){ hideSubmenu(this); }
	},
	'.collapse-header' : function(element){
		element.onclick = function(){ collapse(this); }
	}
};

Behaviour.register(rules);
*/

var rules = {
	".toggle-search" : function(element) {
		element.onclick = function() { OC_toggleStyleDisplay("search-options", "block", "none"); OC_toggleValue(document.getElementById("displayType"),"visible","notVisible"); OC_toggleStyleInnerHTML(this, "hide options", "show options"); }
	},
	".video-group" : function(element) {
		element.onclick = function() { return OC_toggleResultsGroup(this); }
	},
	".row-select-group" : function(element) {
		element.onclick = function() { return OC_toggleRowSelectGroup(this); }
	},
	".search-results-pager li" : function(element) {
		element.onclick = function() { return eval(this.getAttribute("action")); }
	},
	"a.submit" : function(element) {
		element.onclick = function(){ eval(this.getAttribute("action")); OC_submitForm(this); return false; }
	},
	"a.submit-search-form" : function(element) {
		element.onclick = function() { OC_validateSearchForm(this.getAttribute("searchAction"),this.getAttribute("searchForm")); return false; }
	},
	"a.conditional-submit" : function(element) {
		element.onclick = function(){ if (eval(this.getAttribute("action"))) { OC_submitForm(this); return false; } }
	},
	"a.modal" : function(element) {
		element.onclick = function(){ OC_openModalWindow(this); return false; }
	},
	"a.conditional-modal" : function(element) {
		element.onclick = function(){ if (eval(this.getAttribute("condition"))) { OC_openModalWindow(this); return false; } }
	},
	"input.subnav" : function(element) {
		element.onclick = function() { return OC_navigateTo(this.getAttribute("action")); }
	}
};

Behaviour.register(rules);

// truncated version from opencase-admin
function OC_initMenu(section) {
	var menuItem = document.getElementById("nav-" + section);
	if (menuItem != null) { menuItem.className = "selected"; }
	var menuTopItem = document.getElementById("navtop-" + section);
	if (menuTopItem != null) { menuTopItem.className = "selectedTop"; }
}

function OC_initSubMenu(section,subsection) {
	if (section && subsection) {
		var menuItems = document.getElementsByName("subnav-" + section);
		var i = 0;
		do {
			if (menuItems[i] && menuItems[i].value == subsection) {
				menuItems[i].checked = true;
				break;
			}
			i++;
		} while (i < menuItems.length)
	}
}
function OC_toggleStyleDisplay(vObj, vOn, vOff) {
	inObj = (document.getElementById(vObj)) ? document.getElementById(vObj).style : vObj.style;
	if (inObj) inObj.display = (inObj.display == vOn) ? vOff : vOn;
}

function OC_toggleStyleInnerHTML(vEl, vOn, vOff) {
	vEl.innerHTML = (vEl.innerHTML == vOn) ? vOff : vOn;
}

function OC_toggleValue(vEl, vOn, vOff) {
	vEl.value = (vEl.value == vOn) ? vOff : vOn;
}

function OC_toggleStyleClassName(vEl, vOn, vOff) {
	vEl.className = (vEl.className == vOn) ? vOff : vOn;
}

// this function allows a number of child elements to be (un)displayed however it does rely on a specific parent/child relationship
function OC_toggleResultsGroup(o) {
	OC_toggleStyleClassName(o, "open bold", "close bold");
	nextElement = getNextSibling(o.parentNode.parentNode);
	childElements = getElementsByClass("child-group-container","div",nextElement);
	for (var i=0; i<childElements.length; i++) {
		OC_toggleStyleDisplay(childElements[i], "block", "none");
	}
}

function OC_toggleRowSelectGroup(o) {
	OC_toggleResultsGroup(o);
	var rowCollection = o.parentNode.parentNode.getElementsByTagName("td");
	for (var i=0; i<rowCollection.length; i++) {
		rowCollection[i].style.backgroundImage = (rowCollection[i].style.backgroundImage == "url(images/Common/rowbar_gradient.gif)") ? "none" : "url(images/Common/rowbar_gradient.gif)";
		rowCollection[i].style.backgroundColor = (rowCollection[i].style.backgroundColor == "#f9fbff") ? "transparent" : "#f9fbff";
		if(i != 0) {
			if(rowCollection[i].childNodes != null && rowCollection[i].childNodes.length > 0) {
				for(j = 0; j < rowCollection[i].childNodes.length; j++) {					
					if(rowCollection[i].childNodes[j].nodeType == 1 && 
					rowCollection[i].childNodes[j].getAttribute("hideOnOpen") != null && 
					rowCollection[i].childNodes[j].getAttribute("hideOnOpen") == "true") { //element
						rowCollection[i].childNodes[j].style.display = (rowCollection[i].childNodes[j].style.display == "none") ? "block" : "none";
					}
				}
			}
		}
	}
}
function OC_modalSubmitForm(obj){
	var element = document.getElementById(obj);
	OC_submitForm(element);
}

function OC_submitForm(element) {
	var buttonId = element.getAttribute("buttonId");
	element.removeAttribute("buttonId");
	if (buttonId) {
		var button = document.getElementById(buttonId);	
		button.click();
		var indicator = document.getElementById("progress-bar-image");
		if (indicator) indicator.src = indicator.src;
	}
}

function OC_submitOnEnterKey(myfield,e,searchAction,formName) {
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	if (keycode == 13) {
		OC_validateSearchForm(searchAction,formName);
		return false;
	} else
		return true;
}

function OC_validateSearchForm(searchAction,formName) {
	var formElem = document.getElementById(formName);
	if(formElem != null) {
		if (formElem.displayType.value =='visible') {
			formElem.submitType.value = searchAction;			
			formElem.submit();
		} else if (doCheckLuceneQuery(formElem.query)) {
			formElem.submitType.value = searchAction;
			formElem.submit();
		} else {
			alert("Please enter a valid search term");
			return false;
		}
	} else {	
		return false;
	}
}
