/* Function to validate the form fields entered. */
function ValidateForm()
{
  var Flagged = 0;
  var strOut = "";
  
  var errorMsg = document.getElementById('errorMsg');

  // Reset Fields
 
  document.theForm.emailFullName.style.backgroundColor = "#ffffff";
  document.theForm.emailPhone.style.backgroundColor = "#ffffff";
  document.theForm.emailAddress.style.backgroundColor = "#ffffff";
  document.theForm.emailSuburb.style.backgroundColor = "#ffffff";
  document.theForm.emailPostcode.style.backgroundColor = "#ffffff";  
  document.theForm.emailCountry.style.backgroundColor = "#ffffff";
  document.theForm.emailFrom.style.backgroundColor = "#ffffff";
  document.theForm.emailProductOrService.style.backgroundColor = "#ffffff";  
  document.theForm.emailNatureOfComplaint.style.backgroundColor = "#ffffff";
  document.theForm.emailTermsandConditions.style.backgroundColor = "#ffffff";
    
  // Activate Fields
  if(document.theForm.emailFrom.value == "")
  {
    strOut = strOut + "<li>Email</li>"
    Flagged = 1;
    document.theForm.emailFrom.style.backgroundColor = "#ffff66";
  }
  else
  {
    if(echeck(document.theForm.emailFrom.value) == false)
    {
      strOut = strOut + "<li>Invalid Email address</li>"
      Flagged = 1;
      document.theForm.emailFrom.style.backgroundColor = "#ffff66";
    }
  }
  if(document.theForm.emailFullName.value == "")
  {
	strOut = strOut + "<li>Full Name</li>"
    Flagged = 1;
    document.theForm.emailFullName.style.backgroundColor = "#ffff66";
  }
  if(document.theForm.emailPhone.value == "")
  {
	strOut = strOut + "<li>Phone Number</li>"
    Flagged = 1;
    document.theForm.emailPhone.style.backgroundColor = "#ffff66";
  }
  if(document.theForm.emailAddress.value == "")
  {
	strOut = strOut + "<li>Address</li>"
    Flagged = 1;
    document.theForm.emailAddress.style.backgroundColor = "#ffff66";
  }
  if(document.theForm.emailSuburb.value == "")
  {
	strOut = strOut + "<li>Suburb</li>"
    Flagged = 1;
    document.theForm.emailSuburb.style.backgroundColor = "#ffff66";
  } 
  if(document.theForm.emailPostcode.value == "")
  {
	strOut = strOut + "<li>Postcode</li>"
    Flagged = 1;
    document.theForm.emailPostcode.style.backgroundColor = "#ffff66";
  } 
  else
  {
    if(numbersonly_check(document.theForm.emailPostcode.value) == false)
    {
      strOut = strOut + "<li>Postcode - Please enter only digit characters</li>"
      Flagged = 1;
      document.theForm.emailPostcode.style.backgroundColor = "#ffff66";
    }
	if(document.theForm.emailPostcode.value.length != 4)
    {
      strOut = strOut + "<li>Postcode - Please enter 4 digits</li>"
      Flagged = 1;
      document.theForm.emailPostcode.style.backgroundColor = "#ffff66";
    }		
  }
  if(document.theForm.emailCountry.value == "")
  {
	strOut = strOut + "<li>Country</li>"
    Flagged = 1;
    document.theForm.emailCountry.style.backgroundColor = "#ffff66";
  } 
//  if(document.theForm.emailComplaintDepartment.value == "")
//  {
//	strOut = strOut + "<li>D&B Department</li>"
//    Flagged = 1;
//    document.theForm.emailComplaintDepartment.style.backgroundColor = "#ffff66";
//  }
  if(document.theForm.emailProductOrService.value == "")
  {
	strOut = strOut + "<li>Product or Service</li>"
    Flagged = 1;
    document.theForm.emailProductOrService.style.backgroundColor = "#ffff66";
  }
  if(document.theForm.emailNatureOfComplaint.value == "")
  {
	strOut = strOut + "<li>Nature of Complaint</li>"
    Flagged = 1;
    document.theForm.emailNatureOfComplaint.style.backgroundColor = "#ffff66";
  }
  if(document.theForm.emailTermsandConditions.checked == false)
  {
	strOut = strOut + "<li>Terms & Conditions</li>"
    Flagged = 1;
    document.theForm.emailTermsandConditions.style.backgroundColor = "#ffff66";
  }
  
  if(Flagged == 1)
  {
    strOut = "<ul style='padding-top:10px;'>" + strOut;
    strOut = "Please review the following fields: <br />" + strOut;
    strOut = strOut + "</ul>"
    errorMsg.innerHTML = strOut;
    //alert("Flagged");
    return false;
  }

  //alert(strOut);
}
/* Function to validate email address. */
function echeck(str) {
  var at = "@";
  var dot = ".";
  var lat = str.indexOf(at);
  var lstr = str.length;
  var ldot = str.indexOf(dot);

  if(str.indexOf(at) == -1)
  {
    return false;
  }
  if(str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr)
  {
    return false;
  }
  if(str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr)
  {
    return false;
  }
  if(str.indexOf(at, (lat + 1)) != -1)
  {
    return false;
  }
  if(str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot)
  {
    return false;
  }
  if(str.indexOf(dot, (lat + 2)) == -1)
  {
    return false;
  }
  if(str.indexOf(" ") != -1)
  {
    return false;
  }
  return true;
}

function numbersonly_check(str)
{
	// only allow numbers to be entered
	var checkOK = "0123456789";
	var checkStr = str;
	var allValid = true;
	var allNum = "";
	for (i = 0;  i < checkStr.length;  i++)
	{
	ch = checkStr.charAt(i);
	for (j = 0;  j < checkOK.length;  j++)
	if (ch == checkOK.charAt(j))
	break;
	if (j == checkOK.length)
	{
	allValid = false;
	break;
	}
	if (ch != ",")
	allNum += ch;
	}
	if (!allValid)
	{
	return (false);
	}
}
