function checkBuyABrickForm(myForm)
{
	var sError = "";
	var iBrickCount = 0;
	if(myForm.first_name.value == "") { sError += "- Firstname\n"; }
	if(myForm.last_name.value == "") { sError += "- Surname\n"; }
	if(myForm.address1.value == "") { sError += "- Address\n"; }
	if(myForm.city.value == "") { sError += "- Town/City\n"; }
	if(myForm.state.value == "") { sError += "- County/Region/State\n"; }
	if(myForm.postcode.value == "") { sError += "- Postcode\n"; }
	if(myForm.phone.value == "") { sError += "- Telephone\n"; }
	if(isValidEmail(myForm.email.value) == false) { sError += "- Email\n"; }
	
	for(i=1;i<=4;i++)
	{
		if(trim(myForm["TextLine1_" + i].value).length > 0 || trim(myForm["TextLine2_" + i].value).length > 0)
		{
			iBrickCount++;
		}
	}
	
	if(iBrickCount == 0) { sError += "- You have not entered any brick details\n"; }
	for(i=1;i<=4;i++)
	{
		if(trim(myForm["TextLine1_" + i].value).length > 15)
		{
			sError += "- The text is too long for Brick " + i + " Line 1 (max 15 characters)\n";
		}
		if(trim(myForm["TextLine2_" + i].value).length > 15)
		{
			sError += "- The text is too long for Brick " + i + " Line 2 (max 15 characters)\n";
		}
		
		if(trim(myForm["TextLine1_" + i].value).length > 0 || trim(myForm["TextLine2_" + i].value).length > 0)
		{
			if(trim(myForm["TypeOfBrick_" + i].value).length == 0)
			{
				sError += "- Please select type of brick for brick " + i + "\n";
			}
		}		
	}
	
	if(myForm.AcceptTandC.checked == false) { sError += "- You must accept the Terms & Conditions to continue\n"; }
	
	if(sError != "")
	{
		alert("The following information is missing or incorrect:-\n\n" + sError);
		return false;
	}
	else
	{
		myForm.submit();
		return true;
	}
}

function checkCharCount(txtBrick)
{
	var sCurrentItem = txtBrick.name.substring(txtBrick.name.length - 3);
	var spanCount = document.getElementById("CountValueLine" + sCurrentItem);
	var iCharsLeft = 15 - parseInt(txtBrick.value.length);
	
	if(iCharsLeft < 0) 
	{ 
		iCharsLeft = 0; 
		txtBrick.value = txtBrick.value.substring(0, 15)
	}
	
	spanCount.innerHTML = "(" + iCharsLeft + " chars left)";
}

function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
 
}


function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


