function addEvent(elm, evType, fn, useCapture)
{
	if (elm.addEventListener)
	{
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent)
	{
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else
	{
		elm['on' + evType] = fn;
	}
}

function getElement(e)
{
  var el;
  if (window.event && window.event.srcElement)
  {
    el = window.event.srcElement;
  }
  if (e && e.target)
  {
    el = e.target;
  }
  
  if (!el)
  {
    return false;
  }
  else
  {
    return el;
  }
}

function ShowParts(e)
{
	var inp = getElement(e)
	var impval = inp.value;
	impval = parseInt(impval);
	impval = impval + 1;
	
	for(var i=inp.value; i>0; i--)
	{
		document.getElementById('P' + i).style.display = 'block';
		//alert("add" + i);
	}
	
	for(var i=impval; i<21; i++)
	{
		document.getElementById('P' + i).style.display = 'none';
		document.getElementById('P' + i + 'D').value = '';
		document.getElementById('P' + i + 'Q').value = '';
		document.getElementById('P' + i + 'NO').value = '';
		document.getElementById('P' + i + 'OI').value = '';
		//alert("del" + i);
	}
	
	
	
	if (window.event)
	{
		window.event.returnValue = false;
	}
	else if (e && e.preventDefault)
	{
		e.preventDefault();
	}
	else
	{
		return false;
	}

}


function Step2image(e){
	
	if(document.getElementById('Step2Types').value == "Counter Balance"){
		document.getElementById('Step2Image').src = '/images/cb.jpg';
	}
	
	if(document.getElementById('Step2Types').value == "Pallet Truck"){
		document.getElementById('Step2Image').src = '/images/pt.jpg';
	}
	
	if(document.getElementById('Step2Types').value == "Reach"){
		document.getElementById('Step2Image').src = '/images/r.jpg';
	}
	
	if(document.getElementById('Step2Types').value == "Multi Direction"){
		document.getElementById('Step2Image').src = '/images/md.jpg';
	}
	
	if(document.getElementById('Step2Types').value == "Articulated"){
		document.getElementById('Step2Image').src = '/images/a.jpg';
	}
	
	if(document.getElementById('Step2Types').value == "VNA"){
		document.getElementById('Step2Image').src = '/images/vna.jpg';
	}
}

function addListeners()
{
	
	var input = document.getElementsByTagName('select');
	for (var i = 0; i < input.length; i++)
	{
	
		if(input[i].className == 'NOP')
		{
			addEvent(input[i], 'change', ShowParts, false); 
		}
		
		if(input[i].className == 'Step2Types')
		{
			addEvent(input[i], 'change', Step2image, false); 
		}
	
	}
	
	addFormValidation();
	
	for (var iV = 0; iV < validators.length; iV++)
	{
		addEvent(validators[iV].Form, 'submit', validators[iV].ValidateForm, false);
	}
}

addEvent(window, 'load', addListeners, false);


var validators = new Array();

function addFormValidation()
{
	if(document.forms["PartsRequest"]){
	var frmVal = new formValidator("PartsRequest", "Please fill out the mandatory fields" , "");
	validators.push(frmVal); // Add to the validation array
	frmVal.AddValidationRule("N", "required", "No value for name");
	frmVal.AddValidationRule("CN", "required", "No value for contact number");
	frmVal.AddValidationRule("EA", "required", "No value for email address");
	frmVal.AddValidationRule("CON", "required", "No value for company name");
	frmVal.AddValidationRule("NOP", "select[0]", "No value for number of parts");
	}
	if(document.forms["TruckWiz"]){
	var frmVal = new formValidator("TruckWiz", "Please fill out the mandatory fields" , "");
	validators.push(frmVal); // Add to the validation array
	frmVal.AddValidationRule("N", "required", "No value for name");
	frmVal.AddValidationRule("CN", "required", "No value for contact number");
	frmVal.AddValidationRule("EA", "required", "No value for email address");
	frmVal.AddValidationRule("CON", "required", "No value for company name");
	}
	
	if(document.forms["ServiceRequest"]){
	var frmVal = new formValidator("ServiceRequest", "Please fill out the mandatory fields" , "");
	validators.push(frmVal); // Add to the validation array


	frmVal.AddValidationRule("N", "required", "No value for name");
	frmVal.AddValidationRule("CN", "required", "No value for contact number");
	frmVal.AddValidationRule("EA", "required", "No value for email address");
	frmVal.AddValidationRule("CON", "required", "No value for company name");
	}
}
//  ~~ End of Add Event Listener Functions ~~


function formValidator(name, msgBegin, msgEnd)
{
	// Get handle for the form
	this.Form = document.forms[name];
	
	var me = this;
	
	//  Declare an array to store the names of the forms elements
	this.ElementNames = new Array();
	
	for (var iE = 0; iE < this.Form.elements.length; iE++)
	{
		this.ElementNames.push(this.Form.elements[iE].name);
	}
	
	//  Declare an array to store the Valadation Rule objects
	this.ValidationRules = new Array();
	
	//  Declare a variable to store whether the form has been tested
	this.Tested = false;
	
	//  Declare a variable to store whether the form has been validated
	this.Validated = false;
	
	//  Declare variables to store the beginning and end of the error message
	this.msgB = msgBegin;
	this.msgE = msgEnd;
	
	//  Declare variable to store the final error message
	this.FailedMsg = "";
	
	//  Method: Add a validation rule to the Form Validator Object
	this.AddValidationRule = function(elementName, validationType, msg)
	{
		//  If validating a radio button group
		if (validationType == "radio" || /^s_radio\[\w+\]$/.test(validationType))
		{
			//  Declare an array to store the group of radio button
			var element = new Array();
			
			for (var iFE = 0; iFE < this.Form.elements.length; iFE++)
			{
				if (this.Form.elements[iFE].name == elementName)
				{
					//  Add the radio buttons in the group to the array
					element.push(this.Form.elements[iFE]);
				}
			}
			
			// If there are no radio buttons in the group return false
			if (element.length == 0)
			{
				return false;
			}
			//  Else create a Validation Rule object for the radio button group and return true
			else
			{
				var vr = new ValidationRule(element, validationType, msg);
				this.ValidationRules.push(vr);
				return true;
			}
		}
		//  Else if valadating any other form element
		else
		{
			//  Check element is in the form
			var inform = false;
			
			for (var iFE = 0; iFE < this.Form.elements.length; iFE++)
			{
				if (this.Form.elements[iFE].name == elementName)
				{
					inform = true;
					var element = this.Form.elements[iFE];
					break;
				}
			}
			
			//  If element is in form create a Validation Rule object for the element and return true
			if (inform)
			{
				// alert ("adding validation rule: " + validationType + "\nIs Array: " + isArray(element));
				var vr = new ValidationRule(element, validationType, msg);
				this.ValidationRules.push(vr);
				return true;
			}
			
			//  Else do nothing and return false
			else
			{
				return false;
			}
		}
	}
	
	//  Method: Validate Form (onsubmit handler)
	this.ValidateForm = function(e)
	{
		var result = true;
		me.FailedMsg = me.msgB;
		for (var iVR = 0; iVR < me.ValidationRules.length; iVR++)
		{
			test = me.ValidationRules[iVR].ValidateElement();
			result = result && test;
			if (!test)
			{
				me.FailedMsg += "\n->\t" + me.ValidationRules[iVR].FailedMsg;
			}
		}
		
		me.FailedMsg += "\n" + me.msgE;
		
		//  If all elements successfully validate
		if (result)
		{
			
		}
		
		//  If validation fails return error message and stop the form submitting
		else
		{
			alert (me.FailedMsg);
			if (window.event)
			{
				window.event.returnValue = false;
			}
			else if (e && e.preventDefault)
			{
				e.preventDefault();
			}
			else
			{
				return false;
			}
		}
	}
}

//  Object: Valadation Rule - Assigns a valadation rule to form element
function ValidationRule(element, validationType, msg)
{
	//  If group of radio buttons store the array
	if (validationType == "radio" || validationType == "s_radio")
	{
		this.Elements = element;
	}
	
	//  Else store the element
	else
	{
		this.Element = element;
	}
	
	var me = this;
	
	//  Variable to store whether the element has been tested
	this.Tested = false;
	
	//  Variable to store whether the element has been validated
	this.Validated = false;
	
	//  Variable to store the error message output should the validation fail
	this.FailedMsg = msg;
	
	//  If select validation then get the value of the option to not be allowed and set type of validation
	if (/^select\[\w+\]$/.test(validationType))
	{
		this.ValidationType = "select";
		this.NotSelectedValue = validationType.substring(7,validationType.lastIndexOf("]"));
	}
	//  If single radio validation then get the id and set type of validation
	else if (/^s_radio\[\w+\]$/.test(validationType))
	{
		this.ValidationType = "s_radio";
		this.SelectedValue = validationType.substring(8,validationType.lastIndexOf("]"));
	}
	//  Else just set the type of validation
	else
	{
		this.ValidationType = validationType;
	}
	
	//  Get the type of the element to be validated
	
	if (validationType == "radio" || validationType == "s_radio")
	{
		switch (element[0].tagName.toLowerCase())
		{
			case "input" :
				{
					this.ElementType = element[0].getAttribute("type");
					break;
				}
			case "select" :
				{
					this.ElementType = "select";
					break;
				}
			case "textarea" :
				{
					this.ElementType = "textarea";
					break;
				}
		}

	}
	else
	{
		switch (element.tagName.toLowerCase())
		{
			case "input" :
				{
					this.ElementType = element.getAttribute("type");
					break;
				}
			case "select" :
				{
					this.ElementType = "select";
					break;
				}
			case "textarea" :
				{
					this.ElementType = "textarea";
					break;
				}
		}
	}
	//  Method: Validate Element
	this.ValidateElement = function()
	{
		me.Validated = validateElement(me);
		me.Tested = true;
		return me.Validated;
	}
	
	//  Method: Reset the elements value
	this.Reset = function()
	{
		switch (this.ElementType)
		{
			case "text" :
			case "textarea" :
				{
					this.Element.value = "";
				}
				
			case "select" :
				{
					
				}
				
			case "checkbox" :
				{
					this.Element.checked = false;
				}
				
			case "radio" :
				{
					for (var iRB = 0; iRB < this.Elements.length; iRB++)
					{
						this.Elements[iRB].checked = false;
					}
				}
		}
	}
}

//  ~~ End of Form Validating Objects ~~

//  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//  ~~ Form Validating Functions ~~
//  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// Function: Validate Form Element
function validateElement(rule)
{
	//  Declare variable to store the result of the validation
	var result = false;
	//  Get the type of the validation
	switch(rule.ValidationType)
	{	
		// Required Validation [text or textarea or file]
		case "required" :
			{
				if (rule.ElementType == "text" || rule.ElementType == "textarea" || rule.ElementType == "file")
				{
					if (rule.Element.value == "")
					{
						result = false;
					}
					// If just whitespace return false
					else if (/^\s*$/.test(rule.Element.value))
					{
						result = false;
					}
					//  Else return true
					else
					{
						result = true
					}
				}
				else if (rule.ElementType == "checkbox")
				{
					if (rule.Element.checked == true)
					{
						result = true;
					}
					else
					{
						result = false;
					}
				}
				else
				{
					result = false;
				}
				break;
			}
			
		// Numeric Validation [text or textarea]
		case "numeric" :
			{
				if (rule.ElementType == "text" || rule.ElementType == "textarea")
				{
					result = /^-?\d*$/.test(rule.Element.value);
				}
				else
				{
					result = false;
				}
				break;
			}
			
		// Decimal Validation [text or textarea]
		case "decimal" :
			{
				if (rule.ElementType == "text" || rule.ElementType == "textarea")
				{
					result = /^-?\d*$|^-?\d+(\.\d+)?$/.test(rule.Element.value);
				}
				else
				{
					result = false;
				}	
				break;
			}
		
		// Sterling Validation [text or textarea]
		/*case "sterling" :
			{
				if (rule.ElementType == "text" || rule.ElementType == "textarea")
				{
					result = /^\d*$|^-?£?\d+(\.\d\d)?$/.test(rule.Element.value);
				}
				else
				{
					result = false;
				}
				break;
			}
		*/
		// Email Validation [text ortextarea]
		case "email" :
			{
				if (rule.ElementType == "text" || rule.ElementType == "textarea")
				{
					if (rule.Element.value == "")
					{
						result = true;
					}
					else if (/^\s*$/.test(rule.Element.value))
					{
						rule.Element.value = "";
						result = true;
					}
					else
					{
						result = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/.test(rule.Element.value);
					}
				}
				else
				{
					result = false;
				}
				break;
			}
			
		// Select Box Not Selected Validation [select]
		case "select" :
			{
				//alert(rule.Element.value + " || " + rule.NotSelectedValue);
				if (rule.Element.value == rule.NotSelectedValue)
				{
					result = false;
				}
				else
				{
					result = true;
				}
				break;
			}
			
		// Checkbox Required Validation [checkbox]
		case "checkbox" :
			{
				if (rule.ElementType == "checkbox")
				{
					if (rule.Element.checked == true)
					{
						result = true;
					}
					else
					{
						result = false;
					}
				}
				else
				{
					result = false;
				}
				break;
			}
			
		// Radio Button One Selected Validation [radio]
		case "radio" :
			{
				if (rule.ElementType == "radio")
				{
					result = false;
					for (var iRB = 0; iRB < rule.Elements.length; iRB++)
					{
						if (rule.Elements[iRB].checked == true)
						{
							result = true;
							break;
						}
					}

				}
				else
				{
					result = false;	
				}
				break;
			}
			
		// Radio Button Specific Validation [radio]
		case "s_radio" :
			{
				if (rule.ElementType == "radio")
				{
					result = false;
					for (var iRB = 0; iRB < rule.Elements.length; iRB++)
					{
						if (rule.Elements[iRB].id == rule.SelectedValue)
						{
							if (rule.Elements[iRB].checked == true)
							{
								alert(rule.Elements[iRB].id);
								result = true;
							}
						}
					}
				}
				else
				{
					result = false;	
				}
				break;
			}
		
		default : 
			{
				result = false;
				break;
			}
	}
	return result;
}


//  ~~ End of Form Validating Objects ~~

