// JavaScript for Trivia Party
// Written by Berk McGowan - BigCat Web Design & Photography
// http://www.bigcat.net.au

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 calcFigures(poElement, pnMultiplier, pnDiscMultiplier)
{
	var loElement = poElement;
	var loElementVal = loElement.value;
	var lnTotNum = 0, lnTotPrice = 0;
	
	if(loElementVal > 0)
	{
		if(loElementVal >= 5)
		{
			var lnElementTot = (loElementVal*pnDiscMultiplier);
			if(lnElementTot.toString().indexOf('.')>0)
			{
				var t = lnElementTot.toString()
				var r = t.substring(lnElementTot.toString().indexOf('.')+1,t.length)
				if(r.length == 1)
				{
					document.getElementById(loElement.name+'Tot').value = lnElementTot.toString() + "0" ;
				}
				if(r.length == 2)
				{
					document.getElementById(loElement.name+'Tot').value = lnElementTot.toString();
				}
			}
			else
			{
				document.getElementById(loElement.name+'Tot').value = lnElementTot.toString() + ".00" ;
			}
		}
		else
		{
			var lnElementTot2 = (loElementVal*pnMultiplier);
			if(lnElementTot2.toString().indexOf('.')>0)
			{
				var t = lnElementTot2.toString();
				var r = t.substring(lnElementTot2.toString().indexOf('.')+1,t.length);
				if(r.length == 1)
				{
					document.getElementById(loElement.name+'Tot').value = lnElementTot2.toString() + "0";
				}
				if(r.length == 2)
				{
					document.getElementById(loElement.name+'Tot').value = lnElementTot2.toString();
				}
			}
			else
			{
				document.getElementById(loElement.name+'Tot').value = lnElementTot2.toString() + ".00";
			}
//			document.getElementById(loElement.name+'Tot').value = (loElementVal*pnMultiplier) + ".00" ;
//			document.getElementById(loElement.name+'Tot').value = (loElementVal*pnMultiplier);
		}
	}
	else
	{
		document.getElementById(loElement.name+'Tot').value = "0.00";
	}
	
//	//Total counts
//	for(var i=1; i <= parseInt(document.getElementById('numRows').value); i++)
//	{
//		lnTotNum += parseFloat(document.getElementById('txtBasic'+i).value);
//	}
//	
//	document.getElementById('txtNumTotal').value = lnTotNum;
//
//	//Work out total price
//	
//	
//	for(var i=1; i <= parseInt(document.getElementById('numRows').value); i++)
//	{
//		lnTotPrice += parseFloat(document.getElementById('txtBasic'+i+'Tot').value);
//	}
//	
//	if(lnTotPrice.toString().indexOf('.')>0)
//	{
//		var m = lnTotPrice.toString();
//		var n = m.substring(lnTotPrice.toString().indexOf('.')+1,m.length);
//		if(n.length==1)
//		{
//			if(document.getElementById('deltype').value == "Post - Printed" && document.getElementById('numteams').value == "15+")
//			{
//				lnTotPrice += 10;
//			}
//			document.getElementById('txtPriceTotal').value = "$" + lnTotPrice + "0";
//		}
//		if(n.length==2)
//		{
//			if(document.getElementById('deltype').value == "Post - Printed" && document.getElementById('numteams').value == "15+")
//			{
//				lnTotPrice += 10;
//			}
//			document.getElementById('txtPriceTotal').value = "$" + lnTotPrice;
//		}
//	}
//	else
//	{
//		if(document.getElementById('deltype').value == "Post - Printed" && document.getElementById('numteams').value == "15+")
//		{
//			lnTotPrice += 10;
//		}
//		document.getElementById('txtPriceTotal').value = "$" + lnTotPrice + ".00";
//	}
	calcTotal();
}

function calcTotal()
{
	var lnTotNum = 0, lnTotPrice = 0;

	//Total counts
	for(var i=1; i <= parseInt(document.getElementById('numRows').value); i++)
	{
		lnTotNum += parseFloat(document.getElementById('txtBasic'+i).value);
	}
	
	document.getElementById('txtNumTotal').value = lnTotNum;

	//Work out total price
	
	
	for(var i=1; i <= parseInt(document.getElementById('numRows').value); i++)
	{
		lnTotPrice += parseFloat(document.getElementById('txtBasic'+i+'Tot').value);
	}
	
	if(lnTotPrice.toString().indexOf('.')>0)
	{
		var m = lnTotPrice.toString();
		var n = m.substring(lnTotPrice.toString().indexOf('.')+1,m.length);
		if(n.length==1)
		{
			if(document.getElementById('deltype').value == "Post - Printed" && document.getElementById('numteams').value == "15+")
			{
				lnTotPrice += 10;
			}
			if(document.getElementById('deltype').value == "Post - CD" || document.getElementById('deltype').value == "Email")
			{
				lnTotPrice -= 20;
			}
			document.getElementById('txtPriceTotal').value = "$" + lnTotPrice + "0";
		}
		if(n.length==2)
		{
			if(document.getElementById('deltype').value == "Post - Printed" && document.getElementById('numteams').value == "15+")
			{
				lnTotPrice += 10;
			}
			if(document.getElementById('deltype').value == "Post - CD" || document.getElementById('deltype').value == "Email")
			{
				lnTotPrice -= 20;
			}
			document.getElementById('txtPriceTotal').value = "$" + lnTotPrice;
		}
	}
	else
	{
		if(document.getElementById('deltype').value == "Post - Printed" && document.getElementById('numteams').value == "15+")
		{
			lnTotPrice += 10;
		}
		if(document.getElementById('deltype').value == "Post - CD" || document.getElementById('deltype').value == "Email")
		{
			lnTotPrice -= 20;
		}
		document.getElementById('txtPriceTotal').value = "$" + lnTotPrice + ".00";
	}
}

function checkSelected(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 == "chkCat")
			{
				lChecked = lChecked || loForm.elements[i].checked;
			}
		}
	
		if (!lChecked)
		{
			alert("You need to choose at least one category before continuing to the next step!");
			return false;
		}
		else
		{
			loForm.submit();
			return true;
		}
	}
}

function checkNumProds(poForm)
{
	var llGoodCnt = 0, lnTotNum = 0;
	if(typeof(poForm)=="object")
	{
		for(var i=1; i <= parseInt(document.getElementById('numRows').value); i++)
		{
			lnTotNum = parseInt(document.getElementById('txtBasic'+i).value);
			if(lnTotNum > 0)
			{
				llGoodCnt++;
			}
		}
	}
	
	if(!llGoodCnt>0)
	{
		alert("You are required to nominate one pack before continuing on to the next step");
		return false;
	}
	else
	{
		return true;
	}
}

function checkValidEmail(pcField,pcEmail,plMessage)
{
	//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)))
	{
		if(plMessage)
		{
			alert("Email address appears to be invalid. Please correct.\ne.g. name@domain.com\tor\n        name@domain.com.au");
		}
		var cEmail = document.getElementById(pcField);
		cEmail.focus();
		cEmail.select();
		return false;
	}
	return true;
}

function checkIsNumber(pcField,pnValue,plMessage)
{
	var lcMessage = "";
	if(pnValue != "")
	{
		var lnValue = parseInt(pnValue);
		if(isNaN(lnValue))
		{
			if(plMessage)
			{
				alert("Number entered must be a numeric value, eg 1-9");
			}
			var lcField = document.getElementById(pcField);
			lcField.focus();
			lcField.select();
			return false;
		}
		return true;
	}
	return true;
}

function showAnswer(poElement,pcAnswer)
{
	document.getElementById(poElement).value = pcAnswer;
}

function setDiscount(pcValue)
{
	document.frmWizard.cDiscount.value=pcValue;
	document.frmWizard2.cDiscount.value=pcValue;
	document.frmWizard3.cDiscount.value=pcValue;
}