function checkAll(poForm,elmnt,tf,x) {
	var o = document.forms[poForm].elements
	if (o){
		for (i=0; i<o.length; i++){
			if (elmnt != ''){
				if ((o[i].type == 'checkbox')&&(o[i].name.indexOf(elmnt+"") != -1)){
					o[i].checked = tf
				}
			}
			else {
				if (o[i].type == 'checkbox'){
					o[i].checked = tf
				}
			}			
		}
	}	
	for (var j = 0; j < document.links.length; j++){
		if ((document.links[j].href.indexOf(poForm) != -1) && (document.links[j].href.indexOf('checkAll') != -1)){
			if (tf == true){
				document.links[j].href = "javascript:checkAll('"+poForm+"','"+elmnt+"',false)";
				//document.links[j].innerText = "- all";
			}
			else {
				document.links[j].href = "javascript:checkAll('"+poForm+"','"+elmnt+"',true)";
				//document.links[j].innerText = "+ all";
			}
		}
	}
}

function deleterecord(poForm)
{
	var lChecked = false;
	if(typeof(poForm)=="object")
	{
		var loForm = poForm;
		for(var i = 0; i < loForm.elements.length; i++)
		{
			if(loForm.elements[i].name == "chkDelete")
			{
				lChecked = lChecked || loForm.elements[i].checked;
			}
		}
	
		if (!lChecked)
		{
			alert("Please choose a record to delete.")
			return false;
		}
		else
		{
			question = window.confirm("Are you sure you wish to delete selected records?");
			if (question == true)
			{
				loForm.submit();
				return true;
			}
			return false;
		}
	}
}

function doHelp(pcHelpPage,plPublic,pnLevelDown)
{
	var lcLeveller = "";
	for(var i = 0;i < pnLevelDown; i++)
	{
		lcLeveller += "../"
	}
	window.open(lcLeveller+"help.asp?page="+pcHelpPage,"BGHelp","width=465,height=500,scrollbars=yes");
}

function doPopUpWin(pcURL,pnLevelDown)
{
	var lcLeveller = "";
	for(var i = 0;i < pnLevelDown; i++)
	{
		lcLeveller += "../"
	}
	window.open(lcLeveller+pcURL,"BGPopWin","width=465,height=500,scrollbars=yes");
}

function setBottleId(pnBottleID)
{
	if (window.opener && !window.opener.closed)
		window.opener.frmAddBottle.nBottleId.value = pnBottleID;
	window.close();
}

function basicValidate(poForm)
{
	poForm.submit();
}

function validateNews(poForm)
{
	var lsMissing = "";
	if(poForm.cNewsTitle.value == "")
	{
		if(lsMissing == "")
		{
			lsMissing += "News Title";
		}
		else
		{
			lsMissing += ",\nNews Title";
		}
	}

	if(poForm.dDateStart.value == "")
	{
		if(lsMissing == "")
		{
			lsMissing += "Date Start";
		}
		else
		{
			lsMissing += ",\nDate Start";
		}
	}

	if(poForm.dDateEnd.value == "")
	{
		if(lsMissing == "")
		{
			lsMissing += "Date End";
		}
		else
		{
			lsMissing += ",\nDate End";
		}
	}

	if(poForm.cmbSiteSec.value == "newItem")
	{
		if(poForm.cSiteSec.value == "" || poForm.cSiteSec.value == "Specify")
		{
			if(lsMissing == "")
			{
				lsMissing += "Section of Site";
			}
			else
			{
				lsMissing += ",\nSection of Site";
			}
		}
	}

	if(poForm.cNewsbody.value == "")
	{
		if(lsMissing == "")
		{
			lsMissing += "News Body";
		}
		else
		{
			lsMissing += ",\nNews Body";
		}
	}
	
	doValidationMsg(poForm,lsMissing);
}

function doValidationMsg(poForm,pcMissing)
{
	if(pcMissing.length > 0)
	{
		alert("The following fields are required before this form can be submitted:\n"+pcMissing);
		return false;
	}
	else
	{
		poForm.submit();
		return true;
	}
}

function doCheckUpload(poForm,pcImg1,pcImg2)
{
	if(pcImg1.length==0 && pcImg2.length==0)
	{
		alert("At least one image is required for the upload process to work. Please select an image to upload and try again.");
		return false;
	}
	else
	{
		poForm.submit();
		return true;
	}
}

function regsiterValidate(poForm)
{
	var lsMissing = "";
	if(poForm.fname.value == "")
	{
		if(lsMissing == "")
		{
			lsMissing = "First Name";
		}
		else
		{
			lsMissing += ",\nFirst Name";
		}
	}
	
	if(poForm.lname.value == "")
	{
		if(lsMissing == "")
		{
			lsMissing = "Surname";
		}
		else
		{
			lsMissing += ",\nSurname";
		}
	}

	if(poForm.username.value == "")
	{
		if(lsMissing == "")
		{
			lsMissing = "Username";
		}
		else
		{
			lsMissing += ",\nUsername";
		}
	}

	if(poForm.password.value == "")
	{
		if(lsMissing == "")
		{
			lsMissing = "Password";
		}
		else
		{
			lsMissing += ",\nPassword";
		}
	}

	if(poForm.email.value == "")
	{
		if(lsMissing == "")
		{
			lsMissing = "Email Address";
		}
		else
		{
			lsMissing += ",\nEmail Address";
		}
	}

	doValidationMsg(poForm,lsMissing);
}

function checkUpdateUser(poForm)
{
	var lsMissing = "";
	if(poForm.userid.value == "")
	{
		if(lsMissing == "")
		{
			lsMissing = "Username";
		}
		else
		{
			lsMissing += ",\nUsername";
		}
	}
	
	if(poForm.fname.value == "")
	{
		if(lsMissing == "")
		{
			lsMissing = "Firstname";
		}
		else
		{
			lsMissing += ",\nFirstname";
		}
	}
	
	if(poForm.lname.value == "")
	{
		if(lsMissing == "")
		{
			lsMissing = "Lastname";
		}
		else
		{
			lsMissing += ",\nLastname";
		}
	}

	if(poForm.email.value == "")
	{
		if(lsMissing == "")
		{
			lsMissing = "Email Address";
		}
		else
		{
			lsMissing += ",\nEmail Address";
		}
	}
	
	doValidationMsg(poForm,lsMissing);
}

function helpValidate(poForm)
{
	var lsMissing = "";
	if(poForm.helpcode.value == "")
	{
		if(lsMissing == "")
		{
			lsMissing = "Help Code";
		}
		else
		{
			lsMissing += ",\nHelp Code";
		}
	}

	if(poForm.helptitle.value == "")
	{
		if(lsMissing == "")
		{
			lsMissing = "Help Title";
		}
		else
		{
			lsMissing += ",\nHelp Title";
		}
	}

	if(poForm.helptext.value == "")
	{
		if(lsMissing == "")
		{
			lsMissing = "Help Text";
		}
		else
		{
			lsMissing += ",\nHelp Text";
		}
	}

	doValidationMsg(poForm,lsMissing);
}

function addArticleValidate(poForm)
{
	if(poForm.articletitle.value=="" || poForm.articleblurb.value=="" || poForm.articlename.value=="")
	{
		alert("All fields are required before this form can be submitted");
		return false;
	}
	else
	{
		poForm.submit();
		return true;
	}
}

function show_other(poForm,poSelectField,poTextBox)
{
	var loForm = poForm;
	var loField = poSelectField;
	if (loField.value == "newItem")
		{
		poTextBox.style.visibility = "visible"
		poTextBox.value = "Specify"
		poTextBox.focus()
		poTextBox.select()
		}
	else
		{
		poTextBox.value = ""
		poTextBox.style.visibility = "hidden"
		}	
}

function isNumberEntered(pnValue)
{
	if(pnValue == "")
	{
		return true;
	}
	
	var numPattern = /\d{0,}/;
	var matchArray = pnValue.match(numPattern);
	
	if(matchArray == null)
	{
		alert("Value entered must be a number");
		return false;
	}
	//return true;
}

//Function to check the date entered is a valid one
function isValidDate(dateStr,dateobj)
{
	if (dateStr == "") 
	{
		return true;
	}
	
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	
	if (matchArray == null) 
	{
		alert("Date is not in a valid format.\n(eg dd/mm/yyyy)")
		return false;
	}
	month = matchArray[3]; // parse date into variables
	day = matchArray[1];
	year = matchArray[4];
	
	if (month < 1 || month > 12) 
	{ // check month range
		alert("Month must be between 1 and 12.");
		return false;
	}
	if (day < 1 || day > 31) 
	{
		alert("Day must be between 1 and 31.");
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) 
	{
		alert("Month "+month+" doesn't have 31 days!")
		return false
	}
	
	if (month == 2) 
	{ // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) 
		{
			alert("February " + year + " doesn't have " + day + " days!");
			return false;
		}
	}
	if (year.length < 4)
	{
		if (year > 50) {year = 19 + year }
		else { year = 20 + year }
	}
	dateobj.value = day + "/" + month + "/" + year;
	return true;  // date is valid
}

function SavePageAndNavigate(poForm)
{
	poForm.submit();	
}

function checkMandatory(poElement)
{
	// 14/10/2003 tom - handle multiple entities with the same name
	if(poElement.length)
	{
		var llOK = true;
		for(var i = 0; i < poElement.length; i++)
		{
			if(!checkMandatory(poElement[i]))
			{
				llOK = false;
			}
		}
		return llOK;
	}

	// don't complain if in a template row
	var loTR = get_parent(poElement,"TR");
	while(loTR)
	{
		if(loTR.className.match(/\browtemplate\b/i))
		{
			return true;
		}
		loTR = get_parent(loTR.parentNode,"TR");
	}

	if(poElement.value.length==0)
	{
		if(! poElement.className.match(/\bmissing\b/))
		{
			poElement.className += " missing";
		}
		return false;
	}
	else
	{
		poElement.className = poElement.className.replace(/\bmissing\b/,"");
		return true;
	}
}

function get_parent(poItem, pcTagName)
{
	while(poItem != null && poItem.tagName != pcTagName)
	{
		poItem = poItem.parentNode;
	}
	return poItem;
}

function get_child(poItem, pcTagName, pcName)
{
	if(poItem.tagName == pcTagName && poItem.name == pcName)	
	{
		return poItem;
	}
	else
	{
		for(var i=0; i < poItem.childNodes.length; i++)
		{
			var loItem = get_child(poItem.childNodes[i], pcTagName, pcName);

			if(loItem)
			{
				return loItem;
			}
		}
	}
	return null;
}

function doFindButton(poForm)
{
	document.getElementById('btnFind').value = "Processing...";
	document.getElementById('btnFind').disabled=true;
	poForm.submit();
	return true;
}

function setNoHighLowValue()
{
	if(document.frmAddBottle.chkNoValue.checked)
	{
		document.frmAddBottle.nLowValue.value = 0;
		document.frmAddBottle.nLowValue.readOnly = true;
		document.frmAddBottle.nHighValue.value = 0;
		document.frmAddBottle.nHighValue.readOnly = true;
		document.frmAddBottle.hdnNoValue.value = '1';
	}
	if(!document.frmAddBottle.chkNoValue.checked)
	{
		document.frmAddBottle.nLowValue.value = '';
		document.frmAddBottle.nLowValue.readOnly = false;
		document.frmAddBottle.nHighValue.value = '';
		document.frmAddBottle.nHighValue.readOnly = false;
		document.frmAddBottle.hdnNoValue.value = '0';
	}
}

function showEditBox(pcID)
{
	if(document.getElementById("hdnContentsState").value == "1")
	{
		document.getElementById(pcID).style.visibility = "visible";
		document.getElementById("hdnContentsState").value = "0";
	}
	if(document.getElementById("hdnContentsState").value == "0")
	{
		document.getElementById(pcID).style.visibility = "hidden";
		document.getElementById("hdnContentsState").value = "1";
	}
}

function checkValidEmail(pcField,pcEmail)
{
	//commented in works nicely in all browsers apart from NN4.x
	var	filter=/^(\w+(?:[\.-]\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
//	var filter=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/i;
	if (!pcEmail == "" && !(filter.test(pcEmail)))
	{
		alert("Email address appears to be invalid. Please correct.\ne.g. name@domain.com\tor\n        name@domain.com.au");
		var lcEmail = document.getElementById(pcField);
		lcEmail.focus();
		lcEmail.select();
		return false;
	}
}

