/**
* Retrive an HTML Element Collection By name
* @return Array HTML Elements
* @param Object Parent Element
* @param String name
* @param String Tag name to limit search to
*/
function getElementsByName(parent, name, tag) {

	var els = new Array();
	var allChildren = parent.getElementsByTagName(tag || '*');
	
	for (var i = 0; i < allChildren.length; i++) {
		var child = allChildren[i];
		if (child.getAttribute('name') == name) {
			els.push(child);
		}
	}
	
	return els;
}



function newProductCheck(form) {
	var warning;
	warning = "";


/*
	var checkgroup=checkgroup
	var limit=limit
	for (var i=0; i<checkgroup.length; i++){
		checkgroup[i].onclick=function(){
		var checkedcount=0
		for (var i=0; i<checkgroup.length; i++)
			checkedcount+=(checkgroup[i].checked)? 1 : 0
		if (checkedcount>limit){
			alert("You can only select a maximum of "+limit+" checkboxes")
			this.checked=false
			}
		}
	}
*/
	var valueSelectNull = false;


	var cbChecked = 0;
	var cbGroup = getElementsByName(form, "cbCategory[]", "input");
	for (var i = 0; i < cbGroup.length; i++) {
		if ( cbGroup[i].checked )
			cbChecked = cbChecked + 1;
	}
	if ( cbChecked == 0 )
		warning = warning + "You need to check atleast one category\n\n";

	cbChecked = 0;
	cbGroup = getElementsByName(form, "cbSystem[]", "input");
	for (i = 0; i < cbGroup.length; i++) {
		if ( cbGroup[i].checked )
			cbChecked = cbChecked + 1;
	}
	if ( cbChecked == 0 )
		warning = warning + "You need to check atleast one system\n\n";


	if ( document.getElementById("selectNewType") )
//		if (form.selectNewType.options[form.selectNewType.selectedIndex].value == "-1" || form.selectNewCategory.options[form.selectNewCategory.selectedIndex].value == "-1")
		if ( form.selectNewType.options[form.selectNewType.selectedIndex].value == "-1" )
			warning = warning + "You need to choose a category and type for your product\n";

/*
	// Check for products with material
	if(form.selectNewMaterial.options[form.selectNewMaterial.selectedIndex].value == -1 && form.newMaterial.value == '')
	{
		warning = warning + "You need to choose a material for the product\n";
		valueSelectNull = true;
	}
*/
/*
	if(form.selectNewSurface.options[form.selectNewSurface.selectedIndex].value == -1 && form.newSurface.value == '')
	{
		warning = warning + "You need to choose a surface treatment for the product\n";
		valueSelectNull = true;
	}
*/
	var d = getElementsByName(form, "propertyId[]", "input");
	for (x in d)
	{
		if ( d[x].className.indexOf("boolCheckInput") != -1 ) {
//		if ( d[x].value != 9 ) {
		if ( (document.getElementById("selectPropertyValue_" + d[x].value).value) == -1 && (document.getElementById("propertyValue_" + d[x].value).value) == "" )
		{
/*			if ( (document.getElementById("selectPropertyValue_" + d[x].value).value) != -1 && (document.getElementById("propertyValue_" + d[x].value).value) != "" )
			{
				warning = warning + "Either choose a value from the dropdown OR enter a new one, do not use both \n";
				valueSelectNull = true;
			}
			else
			{*/
				warning = warning + "You need to choose a value for every property\n";
				valueSelectNull = true;
//			}
		}
		}
/*
		***********************************************************************************************************************
		***********************************************************************************************************************
		***********************************************************************************************************************
		***********************************************************************************************************************
		***********************************************************************************************************************
		***********************************************************************************************************************
		***********************************************************************************************************************
		***********************************************************************************************************************
*/
/*		else if ( (document.getElementById("selectPropertyValue_" + d[x].value).value) != -1 && (document.getElementById("propertyValue_" + d[x].value).value) != "" )
		{
			valueSelectNull = true;
		}*/
//		alert(d[x].value + ":" + (document.getElementById("selectPropertyValue_" + d[x].value).value));
	}
	if (valueSelectNull)
		warning = warning + "Either choose one from the dropdown or enter a new one in the textfield."
//	for (var i = 0; i < d.length; i++)
//		alert(d[i].value + ":" + (document.getElementById('selectPropertyValue_' + d[i].value).value));

/*
	for (x in form.propertyId)
	{
		alert(x + ":" + (document.getElementById('selectPropertyValue_' + x).value));
	}
*/
	if (warning != "")
	{
		alert (warning);
		return false;
	}
	return true;
}



function checkRFQ(form) {
	var warning;
	warning = "";

	if (form.rfqName.value == '')
	{
		warning = warning + "We would like to know your name\n";
	}
	if (form.rfqCompany.value == '')
	{
		warning = warning + "We would like to know what company you are from\n";
	}
	if (form.rfqCountry.value == '')
	{
		warning = warning + "We would like to know what country you are from\n";
	}
	if (form.rfqPhone.value == '')
	{
		warning = warning + "We would like to have your phonenumber if we need to contact you about your quotation\n";
	}
	if (form.rfqEmail.value == '')
	{
		warning = warning + "We need an email to be able to get back to you for a quotation\n";
	}
	else if ((/\w+@\w+\.+\w/.test(form.rfqEmail.value)) != 1)
	{
		warning = warning + "Please check your email, it does not appear to be valid\n";
	}

	if (warning != "")
	{
		alert (warning);
		return false;
	}
	return true;
}