function initForm(){
	document.orderform.total.value=0;
	document.orderform.hiddentotal.value=0;
	document.orderform.hiddenpriorradio.value=0;
}

function checkChoice(whichbox){
	with(whichbox.form){
		if (whichbox.type == "radio"){
			document.orderform.hiddentotal.value = 0;
			if (document.orderform.DescriptionMediumSize.length > 1) {
				for (var i = 0; i < 10; i++) {
					if (document.orderform.DescriptionMediumSize[i].checked) {
						document.orderform.hiddentotal.value = document.orderform.DescriptionMediumSize[i].value;
					}
				}
			}
			if (document.orderform.DescriptionHeadOrBody.length > 1) {
				for (var i = 0; i < 3; i++) {
					if (document.orderform.DescriptionHeadOrBody[i].checked) {
						document.orderform.hiddentotal.value = document.orderform.hiddentotal.value * document.orderform.DescriptionHeadOrBody[i].value;
					}
				}
			}
			if (document.orderform.NumberofPeople.length > 1) {
				for (var i = 0; i < 5; i++) {
					if (document.orderform.NumberofPeople[i].checked) {
						document.orderform.hiddentotal.value = document.orderform.hiddentotal.value * document.orderform.NumberofPeople[i].value;
					}
				}
			}
			if (document.orderform.hiddentotal.value < 0){
				initForm();
			}
			return(formatCurrency(document.orderform.hiddentotal.value));
		}
	}
}

function formatCurrency(num){
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)){
		num = "0";
	}
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10){
		cents = "0" + cents;
	}
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++){
		num =num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
	}
	return (((sign)?'':'-') + num + '.' + cents);
}

function addOption(){
	document.addnewdatesform.action = document.addnewdatesform.datesubmitoptions.options[document.addnewdatesform.datesubmitoptions.selectedIndex].value;
	return true;
}

function showhide(id){
	if (document.getElementById){
		obj = document.getElementById(id);
		if (obj.style.display == "none"){
			obj.style.display = "";
		} 
		else {
			obj.style.display = "none";
		}
	}
}

function adddiv(){
	var counter = 0;
	
	if (document.getElementById("divupload" + counter) != null){
		while(document.getElementById("divupload" + counter) != null){
			counter++;
		}
	}
		
	if(document.getElementById("addnewstart") != undefined){
		document.getElementById("uploadfiles").removeChild(document.getElementById("addnewstart"));
	}
	
	var removeonclick = "removediv(\"divupload" + counter + "\");";
	var divuploadname = "divupload" + counter;
	
	if(document.getElementById("addnew") != undefined){
		document.getElementById("uploadfiles").removeChild(document.getElementById("addnew"));
	}
	
	div0 = document.createElement("div");
	div0.setAttribute("id",divuploadname);
	input0 = document.createElement("input");
	input0.setAttribute("type","file");
	input0.setAttribute("size","50");
	input0.setAttribute("name","input[]");  //input[]
	input0.setAttribute("id","input"); 
	remove0 = document.createElement("a");
	remove0.setAttribute("href","javascript:void(0)");
	remove0.setAttribute("onclick",removeonclick);
	remove0.appendChild(document.createTextNode("remove"));
	// Next line is for debugging purposes only
	// remove0.appendChild(document.createTextNode(counter));
	div0.appendChild(input0);
	div0.appendChild(remove0);
	
	div1 = document.createElement("div");
	div1.setAttribute("id","addnew");
	add0 = document.createElement("a");
	add0.setAttribute("href","javascript:void(0)");
	add0.setAttribute("onclick","adddiv();");
	add0.appendChild(document.createTextNode("Add another image"));
	div1.appendChild(add0);
	
	document.getElementById("uploadfiles").appendChild(div0);
	document.getElementById("uploadfiles").appendChild(div1);
}

function removediv(item){
	document.getElementById("uploadfiles").removeChild(document.getElementById(item));
}

function checkAllBoxes(obj,aDiv) {
	var num = document.getElementsByTagName("input");  //far too many inputs are getting picked up, this should somehow be narrowed down
	for (j=0; j<num.length; j++) {
		var k = document.getElementById(aDiv+j);  //when the above issue has been fixed, this line can be removed...
		if(k !=null){  // ...and this one...
			var arrInput = document.getElementById(aDiv+j).childNodes;
			for (i=0; i<arrInput.length; i++) {
				if (arrInput[i].type == 'checkbox') {
				arrInput[i].checked = obj.checked;
				} else {
					arrInput[i].value = "not a checkbox";
				}
			}
		}  // ...and this one and the next 3!
		else{
			break;
		}
	}
}

function checkDestroy(){
	var boxes = document.getElementsByTagName("input");
	var catarray = new Array();
	var catselected = false;
	for (i=0; i<boxes.length; i++){
		if (boxes[i].type == 'checkbox'){
			var trimname = (boxes[i].name).substring(0,10);
			if (trimname == 'destroycat'){
				if (boxes[i].checked == true){  // Ask if want to delete categories, then list selected categories (all in same confirm box)
					var catselected = true;
					var catactualnamedirty = (boxes[i].name).substring(10);
					var catactualname = catactualnamedirty.replace(/_/g," ");
					catarray.push(catactualname);
				}
			}
		}
	}
	if(catselected){
		var catarraystring = catarray.join(', ');
		var destroyconfirm = confirm('Are you sure you want to destroy the following categories and their contents?\nWARNING: This operation cannot be undone.\n\n' + catarraystring + '\n');
		if (destroyconfirm){
			return true;
		}
		else{
			return false;
		}
	}
	
	
	var radio = document.getElementById("destroyimg");
	if(radio.checked){
		var destroyimgconfirm = confirm('You have chosen to destroy checked deleted images.\nWARNING: This operation cannot be undone.');
		if (destroyimgconfirm){
			return true;
		}
		else{
			return false;
		}
	}
	
}

function checkUpload(){
	// var upinputs = document.uppingform.getElementsByTagName("input");
	var upinputs = getElementsByAttribute(document.uppingform, "input", "type", "file");
	var numfileinputs = upinputs.length;
	// var numfileinputs = upinputs.length-1;  // number of file inputs in the upload form, minus one for the select box
	
	// var forminputs = document.uppingform.input;
	var forminputs = document.getElementById("input");
	
	// Check file upload field exists
	if(!(forminputs)){
		alert("Please click \"Add new image\" and browse for an image.");
		return false;
	}
	

	// file extension check
	for (i=0; i<numfileinputs; i++){  // loop through items in form, ignore first one as it is hidden
		// Check that file upload fields have been filled
		if(!(upinputs[i].value)){
			alert("Please browse for and add images for upload.\nRemember to remove empty fields.");
			return false;
		}
		var filesplit = (upinputs[i].value).split(".");
		var extension = filesplit[filesplit.length-1];
		if(extension != "jpg" && extension != "jpeg" && extension != "jpe" && extension != "JPG" && extension != "JPEG" && extension != "JPE"){
			alert("Only JPEG format images are allowed.");
			return false;
		}
	}
	
	// Check category has been selected
	if(document.uppingform.catlistup){
		var catlist = document.uppingform.catlistup;
		if(catlist.options[catlist.selectedIndex].value == "default"){
			alert("Please choose a category to upload to.");
			return false;
		}
	}
	
	// Check maximum thumbnail width and height not exceeded
	if(document.getElementById("thumbwidth")){
		var width = document.getElementById("thumbwidth").value;
		if(width>700){
			alert("The maximum thumbnail width allowed is 700 px.");
			return false;
		}
	}
	if(document.getElementById("thumbheight")){
		var height = document.getElementById("thumbheight").value;
		if(height>700){
			alert("The maximum thumbnail height allowed is 700 px.");
			return false;
		}
	}

}

function changeLang(lang){
	document.cookie = "language="+lang;
}

function popSecure(nonce){
	var hash1 = hex_sha1(document.loginform.pword.value);
	var hash2 = nonce + hash1;
	var hash3 = hex_sha1(hash2);
	document.getElementById("pword").value = hash3;
}

function redirectLogin(baseurl){
	var t = setTimeout(redirectLocation(baseurl), 3000);
}

function redirectLocation(baseurl){
	location.href = baseurl;
}

/*
	Copyright Robert Nyman, http://www.robertnyman.com
	Free to use if this text is included
*/
function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
	var oCurrent;
	var oAttribute;
	for(var i=0; i<arrElements.length; i++){
		oCurrent = arrElements[i];
		oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName);
		if(typeof oAttribute == "string" && oAttribute.length > 0){
			if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){
				arrReturnElements.push(oCurrent);
			}
		}
	}
	return arrReturnElements;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
