
/******************************
**
**	Iteracy Content Management System
**	(c) 2007 Iteracy Ltd
**	All rights reserved
**	www.iteracy.com
**
*******************************/

function preLoadImages() {
	imagesArray = new Array();
	n = 0;
	if (document.images) {
		for (i=0; i<document.images.length; i++) {
			if (document.images[i].src.indexOf("_n.png")>0) { 
				img_on = document.images[i].src;
				img_on = img_on.substring(0,img_on.length-5) + "h.png";
				imagesArray[n] = new Image();
				imagesArray[n].src = document.images[i].src;
				imagesArray[n+1] = new Image();
				imagesArray[n+1].src = img_on; 
				n = n+2;
			}
		}
	}
}


function mouseOut(imgName) {
	if (document.images) {
		img_on = document.images[imgName].src;
		document.images[imgName].src = img_on.substring(0,img_on.length-5) + "n.png";
	}
}


function mouseOver(imgName) {
	if (document.images) {	
		img_on = document.images[imgName].src;
		document.images[imgName].src = img_on.substring(0,img_on.length-5) + "h.png";
	}
}

function Element(sID) {
	// returns an element
	if (document.getElementById(sID)) {
		return document.getElementById(sID);
	} else {
		return null;
	}
}

function hideDiv(sID) {
	// hides an element
	if (Element(sID)) {
		Element(sID).style.display = "none";
	}
}

function showDiv(sID) {
	// shows an element
	if (Element(sID)) {
		Element(sID).style.display = "block";
	}
}

function emptySearch(searchElement) {
	if (Element(searchElement)) {
		if (Element(searchElement).value=="Enter search") {
			Element(searchElement).value = "";
		}
	}
}

function showHide(sID) {
	if (Element(sID)) {
		if (Element(sID).style.display != "block") {
			showDiv(sID);
		} else {
			hideDiv(sID);
		}
	}
}

function quickLink(sParams) {
	sURL = sParams.split("|")[0];
	bNewWin = sParams.split("|")[1];
	if (bNewWin == "true") {
		window.open(sURL);
	} else {
		document.location = sURL;
	}
}

function showPreview(sURL) {
	window.open(sURL,null,
    "height=250,width=220,status=no,toolbar=no,menubar=no,location=no");
}

function setRate(oCompany) {
	document.forms[0].tas_rate.value = aRates[oCompany.value];
}

function setFormat() {
	if (Element("pf1").checked) {
		sFormat = Element("pf1").value;
	} else if (Element("pf2").checked) {
		sFormat = Element("pf2").value;
	} else {
		sFormat = Element("pf3").value;
	}
	if (navigator.userAgent.indexOf("MSIE") != -1) {
		displayStyle = "block";
	} else {
		displayStyle = "table-row";
	}
	if (sFormat == 'single') {
		Element("area1").style.display = "none";
		Element("area2").style.display = displayStyle;
		Element("area3").style.display = "none";
	}
	if (sFormat == 'two') {
		Element("area1").style.display = "none";
		Element("area2").style.display = displayStyle;
		Element("area3").style.display = displayStyle;
	}
	if (sFormat == 'two_header') {
		Element("area1").style.display = displayStyle;
		Element("area2").style.display = displayStyle;
		Element("area3").style.display = displayStyle;
	}
}

//suckerfish dropdown
sfHover = function() {
	if (document.getElementById("nav")) {
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

function showHelp(sID) {
	helpItems = getElementsByClassName("help", "div");
	for (keyVar in helpItems) {
		hideDiv(helpItems[keyVar].id);
	}
	showDiv(sID);
}

function getElementsByClassName(classname,tag) {
	if(!tag) {
		tag = "*";
	}
	var anchs =  document.getElementsByTagName(tag);
	var total_anchs = anchs.length;
	var regexp = new RegExp('\\b' + classname + '\\b');
	var class_items = new Array();
	
	for(var i=0;i<total_anchs;i++) { 
		var this_item = anchs[i];
		if(regexp.test(this_item.className)) {
			class_items.push(this_item);
		}
	}
	return class_items;
}

function showHidePageList(sID) {
	var cookie_value = "";
	if (Element(sID)) {
		if (Element(sID).style.display == "block" || (Element(sID).style.display == "" && Element(sID).className == "displayBlock")) {
			hideDiv(sID);
			cookie_value = getCookie("pagelist_tree");
			cookie_value = cookie_value.replace("|" + sID + "|", "");
			setCookie("pagelist_tree",cookie_value,9999);
		} else {
			showDiv(sID);
			cookie_value = getCookie("pagelist_tree");
			setCookie("pagelist_tree",cookie_value + "|" + sID + "|",9999);
		}
	}
}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
  		if (c_start!=-1) { 
    		c_start=c_start + c_name.length+1; 
    		c_end=document.cookie.indexOf(";",c_start);
    		if (c_end==-1) c_end=document.cookie.length;
    		return unescape(document.cookie.substring(c_start,c_end));
   		} 
  	}
	return "";
}

function sumTotal(input_field,total_fields,display_field) {
	currentTotal = document.getElementById(display_field);
	newTotal = 0;
	for(var i=1;i<=total_fields;i++) { 
		thisInput = document.getElementById(input_field + i + 'cost');
		if(thisInput.value) {
			newTotal += parseFloat(thisInput.value);
		}
	}
	currentTotal.value = newTotal;
	recalculateTotal('total_cost','total_funding','eatsome_grant');
}

function recalculateTotal(field_a, field_b, result_field) {
	fieldA = document.getElementById(field_a);
	fieldB = document.getElementById(field_b);
	resultField = document.getElementById(result_field);
	sumResult = parseFloat(fieldA.value) - parseFloat(fieldB.value);
	if(sumResult && sumResult>0) {
		resultField.value=sumResult;
	} else {
		resultField.value=0;
	}
}

function bigBiteSum1() {
	// total cost
	var total = 0;
	for (n=0; n<10; n++) {
		if (Element("item_" + n + "_total_cost").value) {
			total += parseFloat(Element("item_" + n + "_total_cost").value);
		}
	}
	Element("item_total_cost").value = total;
	// other cash income
	total = 0;
	for (n=0; n<10; n++) {
		if (Element("item_" + n + "_other_cash_income").value) {
			total += parseFloat(Element("item_" + n + "_other_cash_income").value);
		}
	}
	Element("item_other_cash_income").value = total;
	// in kind income
	total = 0;
	for (n=0; n<10; n++) {
		if (Element("item_" + n + "_in_kind_income").value) {
			total += parseFloat(Element("item_" + n + "_in_kind_income").value);
		}
	}
	Element("item_in_kind_income").value = total;	
	// set grant totals
	for (n=0; n<10; n++) {
		total = 0;
		if (Element("item_" + n + "_total_cost").value) {
			total += parseFloat(Element("item_" + n + "_total_cost").value);
		}
		if (Element("item_" + n + "_other_cash_income").value) {
			total -= parseFloat(Element("item_" + n + "_other_cash_income").value);
		}
		if (Element("item_" + n + "_in_kind_income").value) {
			total -= parseFloat(Element("item_" + n + "_in_kind_income").value);
		}
		if (total < 0) {
			total = 0;
		}
		Element("item_" + n + "_grant_request").value = total;
	}
	// grant request
	total = 0;
	for (n=0; n<10; n++) {
		if (Element("item_" + n + "_grant_request").value) {
			total += parseFloat(Element("item_" + n + "_grant_request").value);
		}
	}
	Element("item_grant_request").value = total;
}

function bigBiteSum2() {
	var total = 0;
	if (Element("own_fundraising").value) {
		total += parseFloat(Element("own_fundraising").value);
	}
	if (Element("local_authority").value) {
		total += parseFloat(Element("local_authority").value);
	}
	if (Element("primary_care_trust").value) {
		total += parseFloat(Element("primary_care_trust").value);
	}
	if (Element("other_public_source").value) {
		total += parseFloat(Element("other_public_source").value);
	}
	if (Element("other_income_other").value) {
		total += parseFloat(Element("other_income_other").value);
	}
	Element("other_income_total").value = total;
}

function bigBiteSum3() {
	var total = 0;
	if (Element("labour").value) {
		total += parseFloat(Element("labour").value);
	}
	if (Element("specialist_support").value) {
		total += parseFloat(Element("specialist_support").value);
	}
	if (Element("private_sources").value) {
		total += parseFloat(Element("private_sources").value);
	}
	if (Element("professional_sources").value) {
		total += parseFloat(Element("professional_sources").value);
	}
	if (Element("in_kind_other").value) {
		total += parseFloat(Element("in_kind_other").value);
	}
	Element("total_in_kind").value = total;
}