//----------------------------------------------------------------------------------------------------
// Function:	checkOrder
// Description:	Validate Order Form
// Parameters:	
//----------------------------------------------------------------------------------------------------
// Used to prevent multiple orders
var submit_count = 0;
function checkOrder(form)
{
	// Customer Title
	if(isEmpty(form.customer_title.value) )
	{
		alert("Please select a title");
		return false;
	}
	
	// Customer Name
	if(isEmpty(form.customer_firstname.value))
	{
		alert("Please enter your first name");
		return false;
	}
	if(isEmpty(form.customer_lastname.value))
	{
		alert("Please enter your last name");
		return false;
	}
	
	// Customer Email
	if (isEmpty(form.email.value) )
	{
		alert("Please enter your email");
		return false;
	}
	else
	{
		if (!isEmailAddress(form.email.value))
		{
			alert("Please enter a valid email");
			return false;
		}
	}
	
	// Customer Phone
	//Removed as requested for DNB Express Phase 2 UAT - Martin Robbins
	//if(form.productcode.value == "HBRG" )
	//{
	//	if( isEmpty(form.phone_number.value) || form.phone_number.value.length < 8 )
	//	{
	//		alert("Please enter your phone or mobile number");
	//		return false;
	//	}
		
	//}
	
	if (!isEmpty(form.phone_number.value))
	{
		if (!isWholeNumber2(form.phone_number.value, false, false))
		{
			alert("Please enter your valid phone or mobile number");
			return false;
		}

	}
	
	// Customer Fax
//	if(form.delivery.value == 4 )
//	{
//		if(isEmpty(form.fax_country.value) || isEmpty(form.fax_area.value) || isEmpty(form.fax_number.value) || form.fax_number.value.length < 8 )
//		{
//			alert("Please enter your fax number");
//			return false;
//		}	
//	}
	
	if (!isEmpty(form.fax_number.value))
	{
		if (!isWholeNumber2(form.fax_number.value, false, false))
		{
			alert("Please enter your valid fax number");
			return false;
		}
	}
	
	// Customer State
	if(isEmpty(form.customer_state.value) )
	{
		alert("Please enter your state");
		return false;
	}
	
	// Customer Postcode
	if(isEmpty(form.customer_postcode.value) )
	{
		alert("Please enter your postcode");
		return false;
	}	
	
	// Customer Postcode - check if numeric
	if(!isEmpty(form.customer_postcode.value) )
	{
		if (!isWholeNumber2(form.customer_postcode.value, false, false))
		{
			alert("Postcode must be numeric");
			return false;

		}
	}
	
	// Customer ABN
	if(form.no_abn.checked == 0 )
	{
		if(!isEmpty(form.customer_abn.value) )
		{
			if (!isWholeNumber2(form.customer_abn.value, false, false))
			{
				alert("ABN must be numeric");
				return false;

			}
		}
		if (!FixABN(form.customer_abn.value))
		{
			return false;
		}
	}
	
	// Ensure Trust checkbox has been checked
	if (form.AcceptedTrust != null)
	{
	    if (form.AcceptedTrust.checked == 0)
	    {
	        alert("Please tick the check box to accept the advice regarding ordering on Trust entities");
	        return false;
	    }
	}
	
	// Ensure Terms & Conditions checkbox has been checked
	if (form.AcceptedTC.checked == 0)
	{
	    alert("Please tick the check box to agree to the D&B Terms and Conditions to proceed");
	    return false;
	}
	
	// Verify submit count then combine values
	if (submit_count > 0)
	{
		return false;
	}
	else
	{
		//form.fullname.value    = form.customer_title.value   + " " + form.customer_name.value;
		//form.fullcompany.value = form.customer_company.value + " " + form.customer_abn.value;
		//form.fulladdress.value = form.customer_address.value + " " + form.customer_suburb.value + " " + form.customer_state.value + " " + form.customer_postcode.value;
		
		submit_count = submit_count + 1;
		fComp.submit_c.value = "Please Wait...";
	}
}
//----------------------------------------------------------------------------------------------------
// Function:	FixABN
// Description:	Validate and/or fix ABN
// Parameters:	
//----------------------------------------------------------------------------------------------------
function FixABN(ABN)
{
var i=0;
  	var iErr=0;

  	if(ABN.length != 11)
    	iErr = 1;

  	else
    	for(; i < ABN.length; i++)
    	{
        	var ch = ABN.charAt(i);
        	if(ch < "0" || ch > "9")
        	{
           		iErr = 1;
           		break;
        	}
     	}

	if(iErr)
	{
			alert("Please enter a valid ABN. If you do not have an ABN please tick the checkbox.");	
			return false;
	}

	// mod check on ABN
	var iSum= 0;
	var weight = 1;
	for (i = 1; i < 11; i++)
	{		//alert(" digit is " + parseInt(ABN.value.substring(i, i+1), 10));
			iSum = iSum + parseInt(ABN.substring(i, i+1), 10) * weight;
			weight = weight + 2;
			//alert("iSum is " + iSum);
	}

	iSum = iSum + (parseInt(ABN.substring(0, 1), 10) - 1) * 10;
	//alert("iSum is " + iSum);
	var iRemainder = iSum % 89;

	if (iRemainder != 0)
	{ 			alert("Please enter a valid ABN. If you do not have an ABN please tick the checkbox.");	
			return false;
	}
	return true;
}

//----------------------------------------------------------------------------------------------------
// Function:	SetABNState
// Description:	Disables or enables the ABN field according to whether the no_abn check box is checked
// Parameters:	
//----------------------------------------------------------------------------------------------------
function SetABNState()
{
	if (fComp.no_abn.checked == 0)
	{
		fComp.customer_abn.disabled = false;
	}
	else
	{
		fComp.customer_abn.disabled = true;
		fComp.customer_abn.value = '';
	}
}

//----------------------------------------------------------------------------------------------------
// Function:	isEmailAddress
// Description:	
// Parameters:	
//----------------------------------------------------------------------------------------------------
function isEmailAddress(string) 
{
  	var addressPattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
  	return addressPattern.test(string);
}

function isEmpty(s)
{
	var r, re;
	re = /^\s*$/ 	// regular expression: 0 or more whitespace chars
	if (s.match(re))
		return true;
	else
		return false;
}

function isWholeNumber2(s, bAllowSpace, bAllowEmpty)
{
	var r, re;

	if (bAllowSpace && bAllowEmpty)	
		re = /^[\d\s]*$/ ;			// reg exp: 0 or more numbers and spaces
	else if (bAllowSpace && !bAllowEmpty)
		re = /^\s*\d+[\d\s]*$/ ;	// reg exp: 0 or more spaces followed by one or more numbers followed by 0 or more numbers and spaces
	else if (!bAllowSpace && bAllowEmpty)
		re = /^\d*$/ ;				// reg exp: 0 or more numbers
	else
		re = /^\d+$/ ;				// reg exp: 1 or more numbers
			
	if (s.match(re))
		return true;
	else
		return false;
}

