function getWindowWidth() {
	if (typeof(window.innerWidth) == 'number') return(window.innerWidth);
	if (document.documentElement && document.documentElement.clientWidth) return(document.documentElement.clientWidth);
	if (document.body && document.body.clientWidth) return(document.body.clientWidth);
	return(800);
}
function getWindowHeight() {
	if (typeof(window.innerHeight) == 'number') return(window.innerHeight);
	if (document.documentElement && document.documentElement.clientHeight) return(document.documentElement.clientHeight);
	if (document.body && document.body.clientHeight) return(document.body.clientHeight);
	return(600);
}
function getOffsetLeft(element) { return(element ? element.offsetLeft + getOffsetLeft(element.offsetParent) : 0); }
function getOffsetTop(element) { return(element ? element.offsetTop + getOffsetTop(element.offsetParent) : 0); }

function ClientProperty(name, value) { this.name = name; this.value = value; }
function appendClientProperties(form) {
	if (form && form.client_width) form.client_width.value = getWindowWidth();
	if (form && form.client_height) form.client_height.value = getWindowHeight();
	if (document.getElementById && document.getElementById('imageList') && form.imageListScrollTop) {
		form.imageListScrollTop.value = document.getElementById('imageList').scrollTop;
	}
}

function openWindow(url, width, height) {
	var popupWindow = window.open(url, "ProductWindow", "width=" + width + ",height=" + height + ",location=no,directories=no,status=no,scrollbars=no,resizable=yes,menubar=no,toolbar=no", false);
	if (popupWindow) {
		popupWindow.focus();
		return(popupWindow);
	} else {
		alert("Pop-up blocker detected!\n\nSomething on your computer is blocking pop-up windows.\n\nThe pop-up window could not be displayed.")
		return(false);
	}
}

function openFeaturedWindow(url, winName, width, height, features) {
	var popupWindow = window.open(url, winName, features + ((features != '') ? ',' : '') + "width=" + width + ",height=" + height, false);	
	if (popupWindow) {
		popupWindow.focus();
		return(popupWindow);
	} else {
		alert("Pop-up blocker detected!\n\nSomething on your computer is blocking pop-up windows.\n\nThe pop-up window could not be displayed.")
		return(false);
	}
}

function playClip(artistName, voiceQuality, fileUrl) {
	openWindow("playclip.asp?artist="+artistName+"&vq="+voiceQuality+"&url="+fileUrl, 400, 160, "VoiceClip");
}


function openScrollingWindow(url, width, height) {
	var popupWindow = window.open(url, "ProductWindow", "width=" + width + ",height=" + height + ",location=no,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no,toolbar=no", false);
	if (popupWindow) {
		popupWindow.focus();
		return(popupWindow);
	} else {
		if ( confirm("Pop-up blocker detected!\n\nSomething on your computer is blocking pop-up windows.\n\nWould you like to open the printing page in your main browser window?") ) {
			window.location = url;
			return(false);
		}
	}
}

function displayHelp(context) {
	var helpWindow = window.open("/help/help.asp?page="+context, "helpWindow", "width=400,height=400,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no,toolbar=no", false);
	return(helpWindow);
}

function checkYear(theTextBox) {
	if(/^(\d\d\d\d)?$/.test(theTextBox.value)) return(true);
	alert('Please enter the year as a four-digit number (eg. 1995)'); 
	theTextBox.focus();
	theTextBox.select();
	return(false);
}
function checkSections(theForm, checkboxName, checked) {
	for (var i = 0; i < theForm[checkboxName].length; i++) theForm[checkboxName][i].checked = checked;
}

function setCookie (name,value,expires,path,domain,secure) {
	if (expires) {
		var expiryDate = new Date();
		expiryDate.setDate(expiryDate.getDate() + expires);
		expires = expiryDate.toUTCString();
	}
	document.cookie = name + "=" + escape (value) +
	((expires) ? "; expires=" + expires : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

function getCookie(name) { // use: getCookie("name");
	var index = document.cookie.indexOf(name + "=");
	if (index == -1) return null;
	index = document.cookie.indexOf("=", index) + 1;
	var endstr = document.cookie.indexOf(";", index);
	if (endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(index, endstr));
}

function togglePanel(img, panelId, title) {
	if (document.getElementById) {
		var panel = document.getElementById(panelId);
		img.src = (panel.style.display == 'block' ? expandIcon.src : collapseIcon.src);
		img.title = (panel.style.display == 'block' ? "Show " : "Hide ") + title;
		panel.style.display = (panel.style.display == 'block' ? 'none' : 'block');
		// Use cookie to persist setting for 365 days
		setCookie(panelId + "_display", panel.style.display, 365);
	}
	return(false);
}

function toggleElement(id) {
	if (document.getElementById) {
		var element = document.getElementById(id);
		element.style.display = (element.style.display == 'block' ? 'none' : 'block');
	}
	return(! document.getElementById);
}

function removePanel(img, panelId, title) {
	return(confirm('Are you sure you want to remove this panel from your sidebar?'));
}

function elementChanged(element) {
	switch (element.type) {
		case "checkbox":
		case "radio": return (element.checked != element.defaultChecked);
		case "select-one":
			for (var n = 1; n < element.options.length; n++) {
				option = element.options[n];
				if (option.selected != option.defaultSelected) return(true);
			}
			return(false);
		case "text":
		case "textarea":
		case "password": return (element.value != element.defaultValue);
		default: return(false);
	}
}

function changed(theForm) {
	for (var i = 0; i < theForm.elements.length; i++) if(elementChanged(theForm.elements[i])) return(true);
	return(false);
}

function confirmWizardChange(form, link) {
	if (changed(form)) {
		if (form.redirectTarget) form.redirectTarget.value = link.href;
		form.submit();
		return(false);
	} else { 
		return(true);
	
	}
}

function displayBlock(elementId) {
	if (! document.getElementById) return(true);
	var element = document.getElementById(elementId);
	if (element && element.style) element.style.display = 'block';
}
function displayNone(elementId) {
	if (! document.getElementById) return(true);
	var element = document.getElementById(elementId);
	if (element && element.style) element.style.display = 'none';
}
	
function show(elementId) {
	if (! document.getElementById) return(true);
	var element = document.getElementById(elementId);
	if (element && element.style) element.style.visibility = 'visible';
}

function hide(elementId) {
	if (! document.getElementById) return(true);
	var element = document.getElementById(elementId);
	if (element && element.style) element.style.visibility = 'hidden';
}

function setVisibility(elementId, visible) {
	if (! document.getElementById) return(true);
	var element = document.getElementById(elementId);
	if (element && element.style) element.style.visibility = (visible ? 'visible' : 'hidden');
}
	
function setElementBackgroundColor(elementId, color) {
	if (! document.getElementById) return(false);
	var element = document.getElementById(elementId);
	if (element && element.style) element.style.backgroundColor = color;
	return(true);
}

function getBackgroundColor(element) {
	for (; element; element = element.parentElement) {
		if (element.style && element.style.backgroundColor) return(element.style.backgroundColor);
	}
}

function flashElement(element, usualColor, flashColor, duration, flashCount, preDelay, steps) {
	if (window.setTimeout && element) {
		usualColor = (getBackgroundColor(element) ? getBackgroundColor(element) : usualColor);
		if (! duration) duration = 2000; // how long should the entire flashing last
		if (! flashCount) flashCount = 3; // how long should the entire flashing last
		if (! preDelay) preDelay = 100; // initial delay in milliseconds
		if (! steps) steps = 8; // how many steps to fade across
		var flashDuration = (duration / flashCount);
		var stepDuration = (flashDuration / steps);
		var hexColorRegExp = /#(..)(..)(..)/;
		var usualRgb = hexColorRegExp.exec(usualColor);
		var flashRgb = hexColorRegExp.exec(flashColor);
		for (var i = 0; i <= steps; i++) {
			if (i < Math.floor(steps / 2)) { 
				r = 256 + (parseInt(usualRgb[1], 16) + Math.floor(i * ((parseInt(flashRgb[1], 16) - parseInt(usualRgb[1], 16)) / steps)));
				g = 256 + (parseInt(usualRgb[2], 16) + Math.floor(i * ((parseInt(flashRgb[2], 16) - parseInt(usualRgb[2], 16)) / steps)));
				b = 256 + (parseInt(usualRgb[3], 16) + Math.floor(i * ((parseInt(flashRgb[3], 16) - parseInt(usualRgb[3], 16)) / steps)));
			} else { 
				r = 256 + (parseInt(flashRgb[1], 16) + Math.floor(i * ((parseInt(usualRgb[1], 16) - parseInt(flashRgb[1], 16)) / steps)));
				g = 256 + (parseInt(flashRgb[2], 16) + Math.floor(i * ((parseInt(usualRgb[2], 16) - parseInt(flashRgb[2], 16)) / steps)));
				b = 256 + (parseInt(flashRgb[3], 16) + Math.floor(i * ((parseInt(usualRgb[3], 16) - parseInt(flashRgb[3], 16)) / steps)));
			}
			rgb = "#" + r.toString(16).substring(1,3) + g.toString(16).substring(1,3) + b.toString(16).substring(1,3);
			var functionCall = "";
			if (element.length) {
				for (var k = (element.length > 10 ? element.length - 10 : 0); k < element.length; k++) functionCall += "setElementBackgroundColor('" + element[k].id + "', '" + rgb + "');"
			} else {
				functionCall += "setElementBackgroundColor('" + element.id + "', '" + rgb + "');";
			}
			for (var j = 0; j < flashCount; j++) window.setTimeout(functionCall, Math.floor(preDelay + (j * flashDuration) + (i * stepDuration)));
		}
	}
}

function makeHttpRequest() {
	var request = false;
	if (typeof(XMLHttpRequest) != 'undefined') {
		request = new XMLHttpRequest();
	} else if (window.XMLHttpRequest) {
		request = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				request = false;
			}
		}
	}
	return(request);
}

function makeHandler(request) {
	return function() {
		switch(request.readyState) {
			case 0: // uninitialized
				window.status = "Initialising request...";
				break;
			case 1: // loading
				window.status = "Sending data to server...";
				break;
			case 2: // loaded
				window.status = "Sending data to server...";
				break;
			case 3: // interactive
				window.status = "Processing request...";
				break;
			case 4: // complete
				if (request.status == 200) {
					window.status = request.responseText;
					window.setTimeout("window.status = window.defaultStatus", 2000);
				} else {
					window.status = request.responseText; //  + "\n" + request.status); // window.status = request.statusText;
					window.setTimeout("window.status = window.defaultStatus", 2000);
				}
				break;
		}
	}
}

function writeHtml(html) {
	document.write(html);
}

function submitWithRedirect(form, url, fieldName) {
	if (! (form && url && fieldName)) return(true);

	var validated = (form.onsubmit ? form.onsubmit() : 1);
	if (validated) {
		if (! form[fieldName]) {
			var hiddenField = document.createElement("input");
			hiddenField.setAttribute("type", "hidden");
			hiddenField.setAttribute("name", fieldName);
			form.appendChild(hiddenField);
			form[fieldName] = hiddenField;	// This is odd, but IE7 requires it.
		}
		try {
			form[fieldName].value = encodeURI(url);
			form.submit();
			return(false);
		} catch (ex) {	
			alert(ex);
			return(true);
		}
	}
	return(false);
}

function confirmArchive(checkboxes) {
	var checked = false;
	if (checkboxes.length) {
		for (var i = 0; i < checkboxes.length; i++) checked = (checked || checkboxes[i].checked);
	} else {
		checked = (checkboxes.checked);
	}
	if (checked) {
		return(confirm('Are you sure you want to archive the selected lists?'));
	} else {
		alert('You have not selected any lists.\n\nPlease select one or more lists using the tick-boxes to the left of each list');
		return(false);
	}
}

function selectAllNone(checkbox) {
	var checkBoxes = checkbox.form.lists;
	if (checkBoxes.length) {
		for (var i = 0; i < checkBoxes.length; i++) {
			checkBoxes[i].checked = checkbox.checked;
		}
	} else {
		checkBoxes.checked = checkbox.checked;
	}
}

function formatNumberWithCommas(digits) {
    digits += '';
    var tokens = digits.split('.');
    var integralPart = tokens[0];
    var fractionPart = tokens.length > 1 ? '.' + tokens[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(integralPart)) integralPart = integralPart.replace(rgx, '$1' + ',' + '$2');
    return integralPart + fractionPart;
}

function getDictionary(valueString) {
    var dictionary = {};
    var e,
    a = /\+/g, // Regex for replacing addition symbol with a space
    r = /([^&=]+)=?([^&]*)/g,
    d = function(s) { return decodeURIComponent(s.replace(a, " ")); };

    while (e = r.exec(valueString))
        dictionary[d(e[1])] = d(e[2]);
    return dictionary;
}

function outputKeyValuePairString(obj) {
    var s = new Array();
    for (var key in obj) {
        if (typeof (obj[key]) == "function") continue;
        s.push(key + "=" + obj[key]);
    }
    return s.join("&");
}
