function previewImage(formname, listname, intwidth, intheight) {

	var sel = document.forms[formname].elements[listname];
	var imageid = sel.options[sel.selectedIndex].value;
	
	var ShowPage = 'preview_image.php?file=' + imageid;
	var leftpos = (screen.width - intwidth) / 2;
	var toppos = (screen.height - intheight) / 2;
	var winprops = 'toolbar=0, scrollbars=1, location=0, statusbars=0, menubar=0, width=' + intwidth + ', height=' + intheight + ', left=' + leftpos + ', top=' + toppos + ', resizable=1';
    var popupwindow = window.open(ShowPage, 'preview_image', winprops);
}

function clearMe(thefield){
	if (thefield.defaultValue==thefield.value) {
		thefield.value = "";
	}
} 

function pop(ShowPage, intwidth, intheight) {

	var leftpos = (screen.width - intwidth) / 2;
	var toppos = (screen.height - intheight) / 2;
	var winprops = 'toolbar=0, scrollbars=1, location=0, statusbars=0, menubar=0, width=' + intwidth + ', height=' + intheight + ', left=' + leftpos + ', top=' + toppos + ', resizable=1';
    var popupwindow = window.open(ShowPage, 'poppage', winprops);

}


function toggleEnableField(myForm) {

	foo = myForm.send_invoice.checked;
	
	if (foo) {
		myForm.invoice_name.disabled=false;
		myForm.invoice_name.style.backgroundColor='#fff';
	} else {
		myForm.invoice_name.disabled=true;
		myForm.invoice_name.value='';
		myForm.invoice_name.style.backgroundColor='#eee';
	}
	
}

function duplicateAddresses(myForm) {

	myForm.billing_address1.value = myForm.postal_address1.value;
	myForm.billing_address2.value = myForm.postal_address2.value;
	myForm.billing_town.value = myForm.postal_town.value;
	myForm.billing_city.value = myForm.postal_city.value;
	myForm.billing_postcode.value = myForm.postal_postcode.value;
	myForm.billing_country.selectedIndex = myForm.postal_country.selectedIndex;


}

// toggle visibility

function toggle(targetID) {
	if (document.getElementById) {
		target = document.getElementById(targetID);
		if (target.style.display == "none") {
			target.style.display = "";
		} else {
			target.style.display = "none";
		}
	}
}

function toggleItem(targetID, target2ID) {
	if (document.getElementById) {
		target = document.getElementById(targetID);
		target2 = document.getElementById(target2ID);
		if (target.style.display == "none") {
			target.style.display = "";
			target2.style.backgroundImage = "url('/lifecentre/images/triangle-open.gif')";
		} else {
			target.style.display = "none";
			target2.style.backgroundImage = "url('/lifecentre/images/triangle-close.gif')";
		}
	}
}

function newImage(arg) {	
	if (document.images) {		
		rslt = new Image();		
		rslt.src = arg;		
		return rslt;	
	}
}

function changeImages() {	
	if (document.images && (preloadFlag == true)) {		
		for (var i=0; i<changeImages.arguments.length; i+=2) {			
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];		
		}	
	}
}

var preloadFlag = false;

function preloadImages() {	
	if (document.images) {		
		img_1 = newImage("../images/donate_button_on.gif");
		preloadFlag = true;
	}
}
	
function goJump(which_form) {
	self.location.href=which_form.jump.options[which_form.jump.selectedIndex].value;
}

function selectJump(which_form) {
	self.location.href=which_form.jump.options[which_form.jump.selectedIndex].value;
}

function activeEnquiryType(which_form) {

	if (document.getElementById) {
	
		// hide all submenus to begin with
		document.getElementById("submenu_4").style.display = "none";
		document.getElementById("submenu_5").style.display = "none";
		document.getElementById("submenu_16").style.display = "none";
		
		foo = which_form.enquiryType.options[which_form.enquiryType.selectedIndex].value;
		
		//alert(foo);
		targetID = "submenu_" + foo;
	
		target = document.getElementById(targetID);
		if (target) {
			target.style.display = "";
		}
	} else {
		// don't support the DOM, show all submenus
		document.getElementById("submenu_4").style.display = "";
		document.getElementById("submenu_5").style.display = "";
		document.getElementById("submenu_16").style.display = "";
	}
	
}

function validateNumber(field, msg, min, max) {
	if (!min) { min = 0 }
	if (!max) { max = 255 }
	if ( (parseInt(field.value) != field.value) || field.value.length < min || field.value.length > max) {
		alert(msg);
		field.focus();
		field.select();
		return false;
	}
	return true;
}

function validateString(field, msg, min, max) {
	if (!min) { min = 1 }
	if (!max) { max = 65535 }
	if (!field.value || field.value.length < min || field.value.max > max) {
		alert(msg);
		field.focus();
		field.select();
		return false;
	}
	return true;
}

function validateEmail(email, msg, optional) {
	if (!email.value && optional) {
		return true;
	}

	var re_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
	
	if (!re_mail.test(email.value)) {
		alert(msg);
		email.focus();
		email.select();
		return false;
	}
	return true;
}

function validateList(listname, msg) {

	var foo = listname.selectedIndex;

	if (foo == 0 || foo == -1) {
		alert(msg);
		return false;
	}
	
	return true;
	
}

function in_array(stringToSearch, arrayToSearch) {
	for (s = 0; s < arrayToSearch.length; s++) {
		thisEntry = arrayToSearch[s].toString();
		if (thisEntry == stringToSearch) {
			return true;
		}
	}
	return false;
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function addEvent(elm, evType, fn, useCapture) {
	// cross-browser event handling for IE5+, NS6 and Mozilla 
	// By Scott Andrew 
	if (elm.addEventListener) { 
		elm.addEventListener(evType, fn, useCapture); 
		return true; 
	} else if (elm.attachEvent) { 
		var r = elm.attachEvent('on' + evType, fn); 
		return r; 
	} else {
		elm['on' + evType] = fn;
	}
}

function findTarget(e) {
	// cross-browser function to find the event target
	var target;
	if (window.event && window.event.srcElement) {
		// IE does it differently... stores the event in a window.event object
		target = window.event.srcElement;
	}
	if (e && e.target) {
		target = e.target;
	}
	return target;
}

function toggleLabel(e) {

	// find the target
	var target = findTarget(e);
	
	// get some of the targets attributes
	var targetID = target.getAttribute('id');
	var targetName = target.getAttribute('name');
	var targetType = target.getAttribute('type');
	
	if (targetType == 'radio' && targetName != '') {
	
		// build up array of related radios
		var relatedRadioIDs = new Array();
		var inputs = document.getElementsByTagName('input');
		var arrayCount = 0;
			
		// loop through all the inputs
		for (var j = 0; j < inputs.length; j++) {
			var inputElement = inputs[j];
			var inputName = inputElement.getAttribute('name');
			var inputID = inputElement.getAttribute('id');
			// if the name matches the targetName (i.e. a grouping of radios), store it's ID in the relatedRadioIDs array
			if (inputName == targetName) {
				relatedRadioIDs[arrayCount] = inputID;
				arrayCount++;
			}
		}
		
	}
	
	// find all labels
	var labels = document.getElementsByTagName('label');
	// loop through all label elements
	for (var i = 0; i < labels.length; i++) {
		var label = labels[i];
		//var labelFor = label.getAttribute('for');
		var labelFor = label.htmlFor;
		
		if (targetType == 'checkbox') {
			if (labelFor == targetID) {
				if (target.checked) {
					// add class
					label.className += ' checked';
				} else {
					// remove the class from the label
					label.className = label.className.replace(/\b ?checked\b/,'');
				}
			}
		}
		if (targetType == 'radio') {
			// remove class from all related Radios
			if (in_array(labelFor, relatedRadioIDs)) {
				// remove the class from the label
				label.className = label.className.replace(/\b ?checked\b/,'');
			}
				
			if (target.checked) {
				if (labelFor == targetID) {
					label.className += ' checked';
				}
			}
		}
	}
}

function setUpCheckboxLabels() {
	// find all checkboxes
	var inputs = document.getElementsByTagName('input');
	for (var i = 0; i < inputs.length; i++) {
		inputElement = inputs[i];
		if (inputElement.getAttribute('type') == 'checkbox' || inputElement.getAttribute('type') == 'radio') {
			// attach onclick function
			addEvent(inputElement, 'click', toggleLabel, false);
			
			var targetID = inputElement.getAttribute('id');
			// find all labels
			var labels = document.getElementsByTagName('label');
			for (var x = 0; x < labels.length; x++) {
				label = labels[x];
				if (label.getAttribute('for') == targetID) {
					if (inputElement.checked) {
						// add class
						label.className += ' checked';
					} else {
						// remove class
						label.className = label.className.replace(/checked/g,'');
					}
				}
			}
		}
	}
}

addLoadEvent(setUpCheckboxLabels);