// Validation Functions V1.0
// stellasoft Revised 20-02-09
var FormValidation =
{
  init: function()
  {
    var forms = document.getElementsByTagName("form");
    
    for (var i = 0; i < forms.length; i++)
    {
      Core.addEventListener(forms[i], "submit", FormValidation.submitListener);
    }
  },

  rules:
  {
    wholename: /./,
	required: /./,
    requiredNotWhitespace: /\S/,
    positiveInteger: /^\d*[1-9]\d*$/,
    positiveOrZeroInteger: /^\d+$/,
    integer: /^-?\d+$/,
    decimal: /^-?\d+(\.\d+)?$/,
    email: /^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/,
    telephone: /^(\+\d+)?( |\-)?(\(?\d+\)?)?( |\-)?(\d+( |\-)?)*\d+$/,
    dropdown: /./,
    dropdays: /./,
    dropmonth: /./,
    dropyear: /./,
    addtype: /./,
    addname: /./,
    townname: /./,
    county: /./,
    postcode: /./,
    country: /./
  },

  errors:
  {
	wholename: "Please fill in your name",
    required: "Please fill in this required field.",
    requiredNotWhitespace: "Please fill in this required field.",
    positiveInteger: "This field may only contain a positive whole number.",
    positiveOrZeroInteger: "This field may only contain a non-negative whole number.",
    integer: "This field may only contain a whole number.",
    decimal: "This field may only contain a number.",
    email: "Please enter a valid email address in this field.",
    telephone: "Please enter a valid Mobile Telephone Number.",
    dropdown: "Please select a Title from the list.",
    dropdays: "Please select a Day from the list.",
    dropmonth: "Please select a Month from the list.",
    dropyear: "Please select a Year from the list.",
    addtype: "Please select an Address Type from the list.",
    addname: "Please enter a valid Address Line in this field.",
    townname: "Please enter a valid Town/City Name in this field.",
    county: "Please select a County from the list.",
    postcode: "Please enter a valid Post Code in this field.",
    country: "Please select a Country from the list."
  },
  
  submitListener: function(event)
  {
    var fields = this.elements;
    
    for (var i = 0; i < fields.length; i++)
    {
      var className = fields[i].className;
      var classRegExp = /(^| )(\S+)\b/g;
      var classResult;
      
      while (classResult = classRegExp.exec(className))
      {
        var oneClass = classResult[2];
        var rule = FormValidation.rules[oneClass];
        if (typeof rule != "undefined")
        {
          if (!rule.test(fields[i].value))
          {
            fields[i].focus();
            alert(FormValidation.errors[oneClass]);
			var type = FormValidation.errors[oneClass];
			//generateError(type);
            Core.preventDefault(event);
            return;
          }
        }
      }
    }
  }
};
function generateError(type)
{
	var container = document.getElementById("formresult");
		var text = container.firstChild;
		var result = "";
		result += "Please review the following:";
		result += "<br />";
		result += type;
		//container.removeChild(text); 
		/* Remove the original P tag and the php echo for the original script using DOM
		and obliterate and create a new p tag with the result text using innerHTML */
	//container.innerHTML = result;	
	//alert(result);
	//swaperrorimage();
}
function swaperrorimage()
{
	var imageOne = document.getElementById("image1");
	var imageTwo = document.getElementById("image2");
	var origimage = imageOne.firstChild;
	var origimagetwo = imageTwo.firstChild;
	imageOne.removeChild(origimage);
	imageTwo.removeChild(origimagetwo);
	
	var newimageOne = document.createElement("img");
	newimageOne.setAttribute("src","images/giveway2_02.jpg");
	newimageOne.setAttribute("alt","Failed...");
	imageOne.appendChild(newimageOne);
	var newimageTwo = document.createElement("img");
	newimageTwo.setAttribute("src","images/giveway2_03b.jpg");
	newimageTwo.setAttribute("alt","Failed...");
	imageTwo.appendChild(newimageTwo);
}
Core.start(FormValidation);