//smartsite site root: equivalent to ~/
var siteroot = (typeof(fullsiteroot) == "function") && (typeof(sitehost) == "function") ? fullsiteroot().replace(sitehost(), "") : "/";
//language
var lang = location.href.indexOf("/en/") > - 1 ? "en" : "fr";
//fixes double slash in URLs
var normalizePath = function(string) {return string.replace(/(\/+)|\\+/g, "/");}
//escapes <, > & and " into corresponding html entities
var escapeHTML = function(string) {return string.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");}
//rounds a number to two decimals places. Returns a string
var toCurrency = function(number) {return number.toFixed(2);}
//IE background image cache script
//@cc_on document.execCommand("BackgroundImageCache", false, true);
/*hover submenus*/
Event.observe(window,"load",function() {

	if (typeof(Prototype)!="undefined") {			
	  var lis = $$("div.splash li");
	  var out = /*@cc_on "mouseleave";@*/"mouseout";//mouseleave on ie, mouseout for other browsers
	  if(Element.addClassName){
		  for (var i=0;i<lis.length;i++) {
			Event.observe(lis[i], "mouseover", function() {
				var li = this;
				Element.addClassName(this,"hover");
				Element.removeClassName(this.down("img"),"hidden");
				$("banner").style.backgroundImage = "url(" + siteroot + "local/images/" + this.title + ")";
				$$("div.banner p", "div.banner a").each(function(e){
					e[(li.title.indexOf("cd") < 0) ? "addClassName" : "removeClassName"]("hidden");
				})
			}.bind(lis[i]));
			Event.observe(lis[i], out, function() {
				Element.removeClassName(this,"hover");
				Element.addClassName(this.down("img"),"hidden");				
			}.bind(lis[i]));
		  }
	  }
	}
	
	var openModal = function(url){
		var popup = document.createElement("div");
		document.body.appendChild(popup);
		popup = $(popup);
		popup.innerHTML = "\
			<div class='hidden modal-container'>\
				<div class='modal-mask'>" +
					//@cc_on "<iframe frameborder='0' src='javascript:document.write(\"<html><body></body></html>\");document.close();'></iframe>" +
				"</div>\
				<div class='modal-body'>\
					<a class='modal-closer' href='javascript:;'>Close</a>\
					<div class='modal-content'></div>\
				</div>\
			</div>\
		";
		var container = popup.down("div.modal-container");
		new Ajax.Updater(popup.select(".modal-content")[0], url, {method:"GET", evalScripts : true, onComplete : function() {
			container.removeClassName("hidden");
			var body = popup.down(".modal-body");
			var y = window.pageYOffset || document.documentElement.scrollTop;
			popup.select(".modal-mask,.modal-mask iframe").invoke("setStyle", {
				height : document.documentElement.scrollHeight + document.documentElement.scrollTop + "px",
				width : document.body.offsetWidth + "px"
			});
			var scroll = document.body.scrollTop || document.documentElement.scrollTop;
			var centered = ((document.viewport.getHeight() / 2) - (body.offsetHeight / 2));
			popup.select(".modal-body").invoke("setStyle", {
				top : (centered < 0 ? 20 : centered) + scroll + "px",
				left : ((document.viewport.getWidth() / 2) - (body.offsetWidth / 2)) + "px"
			});	
		}});
		popup.down("a.modal-closer").observe("click", function() {
			container.addClassName("hidden");
		});
	}
	
	$$(".klick-popup").each(function(el){
		el.observe("click", function(e){
			Event.stop(e);
			openModal(this);
		})
	})
})

/**
Return the value of a given radio button object.
If nothing is checked, return default_return_value
 */
function get_radio_value(radio_object, default_return_value) {
	for(var i=0; i<radio_object.length; i++) {
		if(radio_object[i].checked) {
			return radio_object[i].value;
		}
	}
	return default_return_value;
}
/* Code for calculations on BASDAI form and HAQ form */
/* Peform the calculations for HAQ and display them on the page.*/
function haq_calculate_totals() {
	
	var formString = "document.getElementById('self-assessment-haq')";
	var formObj = eval(formString);
	var formElements = formObj.elements;		
	
	var val_total_score = 0;
	var string = ""
	for(var i=0; i<formElements.length; i++) {
		if(formElements[i].type == "checkbox") {
			if(formElements[i].checked) {
				string += "\n"+formElements[i].name+":"+formElements[i].value;
				val_total_score += parseInt(formElements[i].value);
			}
		}
	}
	//if(string != "") alert(string);		
	// Update the total field
	var total_score = formObj.total_score;
	if(total_score != null) total_score.value = val_total_score;		
}

/*
Peform the calculations for BASDAI and display them on the page.
*/
function basdai_calculate_totals() {		
	var formObj = document.getElementById('self-assessment-basdai');
	
	// Get all the values and parse them as ints q1 - q4
	var q1 = parseInt(get_radio_value(formObj.q1, 0));
	var q2 = parseInt(get_radio_value(formObj.q2, 0));
	var q3 = parseInt(get_radio_value(formObj.q3, 0));
	var q4 = parseInt(get_radio_value(formObj.q4, 0));
	var q5 = parseInt(get_radio_value(formObj.q5, 0));
	var q6 = parseInt(get_radio_value(formObj.q6, 0));
	
	// Make the calculations and update the textfields
	var total1_4 = formObj.total1_4;
	var val_total1_4 = q1+q2+q3+q4;
	if(total1_4 != null) total1_4.value = val_total1_4;
	
	var mean5_6 = formObj.mean5_6;
	var val_mean5_6 = (q5+q6)/2;
	if(mean5_6 != null) mean5_6.value = val_mean5_6;
	
	var total_score = formObj.total_score;
	var val_total_score = val_total1_4 + val_mean5_6;
	if(total_score != null) total_score.value = val_total_score;
	
	var divide_5 = formObj.divide_5;
	if(divide_5 != null) divide_5.value = val_total_score/5;
}

