//
//	JavaScript document for ResearchMaster Pty Ltd website - www.researchmaster.com.au
//	Written by Berk McGowan 10/01/2008
//

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 cEmail = document.getElementById(pcField);
		cEmail.focus();
		cEmail.select();
		return false;
	}
	return true;
}

function SavePageAndNavigate(poForm)
{
	poForm.submit();	
}

function checkMandatory(poElement)
{
	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 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 collapse(pcValue)
{
	var pVal = document.getElementById(pcValue);
	
	if (pVal.className == "collapsable collapsed")
	{
		pVal.className = "collapsable";
	}
	else
	{
		pVal.className = "collapsable collapsed";
	}
}

function checkCharLimit(field, countfield, maxlimit)
{
	if (field.value.length > maxlimit)
	{
		field.value = field.value.substring(0, maxlimit);
		return false;
	}
	else
	{
		//countfield.value = maxlimit - field.value.length;
	}
}
