// used for arrows on navigation
var theBaseUri = "/";
var today = new Date();
var expires = new Date(today.getTime() + (365 * 86400000));
function showPopup(url, name, width, height, scroll, resizable, statusbar, screenpos) {
	var Xwin = width;
	var Ywin = height;
	var Xsize = screen.availWidth;
	var Ysize = screen.availHeight;
	var Xpos = 0;
	var Ypos = 0;
	switch(screenpos) {
		case "topleft":
			Xpos = 0;
			Ypos = 0;
			break;
		case "topright":
			Xpos = Xsize - width;
			Ypos = 0;
			break;
		case "bottomleft":
			Xpos = 0;
			Ypos = Ysize - height;
			break;
		case "bottomright":
			Xpos = Xsize - width;
			Ypos = Ysize - height;
			break;
		default:
			Xpos = (Xsize - width) / 2;
			Ypos = (Ysize - height) / 2;
			break;
	}
	var popwin;
	var opts = "status=" + statusbar + ",resizable=" + resizable + ",scrollbars=" + scroll + ",height=" + Ywin + ",width=" + Xwin + ",left=" + Xpos + ",top=" + Ypos;
	popwin = window.open(url, name, opts);
	popwin.focus();
}
function quickWin(url, name, source) {
	showPopup(url, name, 450, 450, 0, 0, 1, 'default');
	if (source != "quickfind") {
		return false;
	}
}
function submitForm(url, name) {
	showPopup(url, name, 150, 300, 0, 0, 1, 'default');
}
function showConfirm() {
	return confirm("Are you sure?");
}
function getCookie(name) {
	var start = document.cookie.indexOf(name+"=");
	var len = start+name.length+1;
	if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
	if (start == -1) return null;
	var end = document.cookie.indexOf(";",len);
	if (end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(len,end));
}
function setCookie(name, value, expires, path, domain, secure) {
	path = "/";
	document.cookie = name + "=" +escape(value) +
	((expires) ? ";expires=" + expires.toGMTString() : "") +
	((path) ? ";path=" + path : "") + 
	((domain) ? ";domain=" + domain : "") +
	((secure) ? ";secure" : "");
}
function pickRandomNumber(num) {
	return Math.round(num*Math.random());
}
function arrayShuffle(u_array, n) {
	var i, j, k;
	var temp;
	for (i = 0; i < n; i++)
	for (j = 0; j < u_array.length; j++) {
		k = Math.floor(Math.random() * u_array.length);
		temp = u_array[j];
		u_array[j] = u_array[k];
		u_array[k] = temp;
	}
}
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}
function changeImages() {
	if (document.images) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}
function changeStyle(element, strClass) {
	element.className = strClass;
}
function toggleNode(strNode) {
	theNode = document.getElementById(strNode);
	theMark = document.getElementById("mark"+strNode);
	if (theNode.style.display == "none") {
		theNode.style.display = "block";
		// theMark.innerHTML = "-";
		changeImages("mark" + strNode, theBaseUri + "images/minus.gif");
	} else if (theNode.style.display == "block") {
		theNode.style.display = "none";
		// theMark.innerHTML = "+";
		changeImages("mark" + strNode, theBaseUri+ "images/plus.gif");
	}
}
function collapseAll() {
	// collapse all switches
	NavAnchors = document.getElementsByTagName("img");
	for (i=0; i<NavAnchors.length; i++) {
		switchString = String(NavAnchors[i].id);
		if (switchString.indexOf("mark") != -1) {
			theNodeToBeSwitched = switchString.slice(switchString.indexOf("mark") + 4);
			if (doDebug) alert("collapsed: " + nodesToSwitch[i]);
			toggleNode("" + theNodeToBeSwitched);
		}
	}
}
function expandSelectedNodes() {
	// expand path to this page
	// nodesToSwitch is generated from navStrings output include
	if (nodesToSwitch != "") {
		if (nodesToSwitch.slice(nodesToSwitch.lastIndexOf("|||")) == "|||") {
			nodesToSwitch = nodesToSwitch.slice(0, nodesToSwitch.lastIndexOf("|||"));
		}
		nodesToSwitch = nodesToSwitch.split("|||");
		for (i=0; i<nodesToSwitch.length; i++) {
			theNode = document.getElementById(nodesToSwitch[i]);
			if (theNode != -1) {
				if (doDebug) alert("expanded: " + nodesToSwitch[i]);
				toggleNode("" + nodesToSwitch[i]);
			}
		}
	}
}
function showMenuStatus(theID) {
	theNode = document.getElementById(theID);
	if (theNode.style.display == "none") {
		strStatus = "Click to expand this menu";
	} else if (theNode.style.display == "block") {
		strStatus = "Click to contract this menu";
	}
	window.status = strStatus;
}
function clearMenuStatus() {
	window.status = '';
}
function pickRandomNumber(num) {
	return Math.round(num*Math.random());
}
function verifyListSubscription(theForm) {
	var f = theForm;
	var errors = "";
	
	if (f.theFullName.value == "") {
		errors += "Your name is empty;\n"; 
	} else {
		if (f.theFullName.value.indexOf(" ") == -1) {
			errors += "You must enter your full name (first and second name);\n";
		}
	}
	
	if (f.theEmailAddress.value == "") {
		errors += "Your email address is empty;\n"; 
	} else {
		if ((f.theEmailAddress.value.indexOf("@") == -1) || (f.theEmailAddress.value.indexOf(".") == -1)) {
			errors += "Your email address is invalid;\n";
		}
	}
	
	if (!f.checkbox1.checked && !f.checkbox2.checked && !f.checkbox3.checked && !f.checkbox4.checked) {
		errors += "You must select a list to subscribe to;\n";
	}

	if (errors != "") {
		alert(errors);
		return false;
	} else {
		return true;
	}
}
function verifyHelpForm(theForm) {
	var f = theForm;
	var errors = "";
	var kindOfHelpOptions = 0;
	for (var i=0;i<5;i++) kindOfHelpOptions += f.kindOfHelp[i].checked;
	if (kindOfHelpOptions == 0) errors += "You must tell us what kind of help you need;\n";
	if (f.theFirstName.value == "") errors += "Your name is empty;\n";
	if (f.theDescription.value == "") errors += "You have to tell us about your problem;\n";
	if (CountWords(f.theDescription) > 100) errors += "Your description of your problem is too long;\n";

	if (f.thePostalAddress.value == "" && (!f.contactYou1.checked && !f.contactYou2.checked)) errors += "Your address is empty;\n";
	if (f.thePostCode.value == "" && (!f.contactYou1.checked && !f.contactYou2.checked)) errors += "Your postcode is empty;\n";
	
	if (f.theEmail.value == "" && (f.contactYou1.checked)) errors += "Your email address is empty;\n";
	if (f.thePhone.value == "" && (f.contactYou2.checked)) errors += "Your phone number is empty;\n";
	
	if (f.theEmail.value == "" && f.thePhone.value == "" && (f.thePostalAddress.value == "" || f.thePostCode.value == "")) errors += "You must tell us how we can contact you;\n";

	if (f.theEmail.value != "") {
		if ((f.theEmail.value.indexOf("@") == -1) || (f.theEmail.value.indexOf(".") == -1)) {
			errors += "Your email address is invalid;\n";
		}
	}

	/*
	if (f.contactYou1.checked || f.contactYou2.checked) {
		if (f.contactYou1.checked) {
			if (f.theEmail.value == "") {
				errors += "Your email address is empty;\n";
			} else {
				if ((f.theEmail.value.indexOf("@") == -1) || (f.theEmail.value.indexOf(".") == -1)) {
					errors += "Your email address is invalid;\n";
				}
			}
		}
		if (f.contactYou2.checked) {
			if (f.thePhone.value == "") errors += "Your phone number is empty;\n";
		}
	} else {
		errors += "You must tell us how we can contact you;\n";
	}
	*/

	if (errors != "") {
		alert(errors);
		return false;
	} else {
		return true;
	}
}
function setWordCounter(this_field, target_field) {
	document.getElementById(target_field).innerHTML = 100 - CountWords(this_field);
}
function CountWords(this_field, show_word_count, show_char_count) {
	if (show_word_count == null) show_word_count = false;
	if (show_char_count == null) show_char_count = false;
	var char_count = this_field.value.length;
	var fullStr = this_field.value + " ";
	var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
	var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
	var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
	var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
	var splitString = cleanedStr.split(" ");
	var word_count = splitString.length -1;
	if (fullStr.length < 2) word_count = 0;
	wordOrWords = (word_count == 1) ? " word" : " words";
	charOrChars = (char_count == 1) ? " character" : " characters";
	if (show_word_count && show_char_count) {
		alert (word_count + wordOrWords + "\n" + char_count + charOrChars);
	} else {
		if (show_word_count) {
			alert ("Word Count:" + word_count + wordOrWords);
		} else {
			if (show_char_count) {
				alert ("Character Count:" + char_count + charOrChars);
			}
		}
	}
	return word_count;
}
function checkPoll(f) {
	if (!f["poll"][0].checked && !f["poll"][1].checked && !f["poll"][2].checked) {
		alert("You haven't answered the Question!");
		return false;
	} else {
		return true;
	}
}
function checkDeletePolls(theForm) {
	inputs = theForm.getElementsByTagName("input");
	checkBoxes = new Array();
	j = 0;
	for (i=0;i<inputs.length;i++) {
		if (inputs[i].type == "checkbox" && inputs[i].checked) {
			checkBoxes[j++] = inputs[i];
		}
	}
	if (checkBoxes.length > 0) {
		return showConfirm();
	} else {
		alert("You have to select a poll to delete!");
		return false;
	}
}

function checkDiaryUnload() {
	if(!document.clicked && document.checkClick) {
		return confirm("if you navigate away from this page without saving, you will lock this date and prevent further editing.\n\nAre you sure?");
	}
}
