/* Ajax Construction Kit  */
/* Validation Toolkit Functions  */

function validateNonEmpty(inputControl, helpControl) {
  // See if the input value contains any text
  return validateRegEx(/.+/,
    inputControl.value, helpControl,
    "This Feild can not be left Blank.");
}

function validateInteger(inputControl, helpControl) {
  // First see if the input value contains data
  if (!validateNonEmpty(inputControl, helpControl))
    return false;

  // Then see if the input value is an integer
  return validateRegEx(/^[-]?\d*$/,
    inputControl.value, helpControl,
    "Please enter an integer.");
}

function validateNumber(inputControl, helpControl) {
  // First see if the input value contains data
  if (!validateNonEmpty(inputControl, helpControl))
    return false;

  // Then see if the input value is a number
  return validateRegEx(/^[-]?\d*\.?\d*$/,
    inputControl.value, helpControl,
    "Please enter a number.");
}

function validateZipCode(inputControl, helpControl) {
  // First see if the input value contains data
  if (!validateNonEmpty(inputControl, helpControl))
    return false;

  // Then see if the input value is a ZIP code
  return validateRegEx(/^\d{5}$/,
    inputControl.value, helpControl,
    "Enter a 5-digit ZIP code.");
}

validateAddEmpty
function validateFnameEmpty(inputControl, helpControl) {
  // First see if the input value contains data
  if (!validateNonEmpty(inputControl, helpControl))
    return false;

  // Then see if the input value is an email address
  return validateRegEx(/^([a-zA-Z])+$/,
    inputControl.value, helpControl,
    "Letters Only.");
}

function validatePhone(inputControl, helpControl) {
  // First see if the input value contains data
  if (!validateNonEmpty(inputControl, helpControl))
    return false;

  // Then see if the input value is a phone number
  return validateRegEx(/^\d{10}$/,
    inputControl.value, helpControl,
    "Example of phone number 9496617890.");
}

function validateEmail(inputControl, helpControl) {
  // First see if the input value contains data
  if (!validateNonEmpty(inputControl, helpControl))
    return false;

  // Then see if the input value is an email address
  return validateRegEx(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
    inputControl.value, helpControl,
    "Enter a Valid email ( john@doe.com )");
}

function validateDate(inputControl, helpControl) {
  // First see if the input value contains data
  if (!validateNonEmpty(inputControl, helpControl))
    return false;

  // Then see if the input value is a date
  return validateRegEx(/(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d/,
    inputControl.value, helpControl,
    "Please enter a date (for example, 01/14/1975).");
}

function validateRegEx(regex, input, helpControl, helpMessage) {
  if (!regex.test(input)) {
    if (helpControl != null)
      helpControl.innerHTML = helpMessage;
    return false;
  }
  else {
    if (helpControl != null)
      helpControl.innerHTML = "";
    return true;
  }
}



function handleCityStateRequest() {
        if (request.readyState == 4 && request.status == 200) {
          // Store the XML response data
          var xmlData = request.responseXML;

          // Display the city/state results
          if (xmlData != null && getText(xmlData.getElementsByTagName("CITY")[0]) != "")
		{
document.getElementById("C").innerHTML = "<input type='text' name='Customer[city]' value='"+getText(xmlData.getElementsByTagName("CITY")[0])+"'/>";
	
	document.getElementById("S").innerHTML = "<input type='text' name='Customer[state]' value='"+getText(xmlData.getElementsByTagName("STATE")[0])+"' />";
		}
		else
            document.getElementById("C").innerHTML = "Could not find the ZIP code.";
        }
        ajaxUpdateState();
      }

      function getCityState(zipCode) {
        // Display the wait image
        document.getElementById("C").innerHTML = "<img src='./images/wait.gif' alt='Looking up ZIP code...' />";

        // Send the Ajax request to load the city/state
        ajaxSendRequest("GET", "./js/ziplookup.php?zipCode=" + zipCode, handleCityStateRequest);
      }
