//Validationfunctions
addLoadListener(init);

function init()
{
  document.forms[0].onsubmit = validateFields;

  return true;
}


function validateFields()
{
  var elements = document.forms["quoteform"].elements;
// var emailPattern = /^([\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+)$/; 
// the above expression does not allow multiple email address wehreas the nest expression does.
  var emailPattern = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*([,;]\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*$/;
  var secCodePattern = /^[A-Z0-9]{5}$/

// Check that a Country has been selected
  if (document.forms["quoteform"]["recountry"].selectedIndex == 0) 
  {
	  alert("You must select your Country from the dropdown menu");
	  return false;
  }

// Check that a Sales Person has been selected
  if (document.forms["quoteform"]["FromMM"].selectedIndex == 0) 
  {
	  alert("You must select a sales person from the dropdown menu");
	  return false;
  }

  //Check that a Mouse Mat has been selected
  if (document.forms["quoteform"]["mattype"].selectedIndex == 0) 
  {
	  alert("You must select a Mat Shape from the dropdown menu");
	  return false;
  }

// Check if fields need to be corrected and change colour
  for (var i = 0; i < elements.length; i++)
  {
	elements[i].style.backgroundColor = "#EBEBEB"; //Reset background colour of fields
	elements[i].style.borderColor = "#666666"; //Reset border colour of fields

// Check that either quantity, email or security code have been entered
    if (/(^| )checkRequired( |$)/.test(elements[i].className) && elements[i].value == "")
    {
      alert("Please complete the highlighted field.");
      elements[i].focus();
	  elements[i].style.backgroundColor = "#FFFFFF"; //Set background colour of fields to White
	  elements[i].style.borderColor = "#FF0000"; //Set border colour of fields to Red
      return false;
    }

// Check that a valid email has been entered
    if (/(^| )checkEmail( |$)/.test(elements[i].className) && !emailPattern.test(elements[i].value))
    {
      alert("Please enter a valid e-mail address in the highlighted field.");
      elements[i].focus();
	  elements[i].style.backgroundColor = "#FFFFFF"; //Set background colour of fields to White
	  elements[i].style.borderColor = "#FF0000"; //Set border colour of fields to Red
      return false;
    }

// Check that the Quantity only contains numbers
	if (/(^| )checkNumber( |$)/.test(elements[i].className) && elements[i].value != parseInt(elements[i].value, 10))
    {
      alert("Please ensure the Quantity only contains numbers.");
      elements[i].focus();
	  elements[i].style.backgroundColor = "#FFFFFF"; //Set background colour of fields to Yellow
	  elements[i].style.borderColor = "#FF0000"; //Set border colour of fields to Red
      return false;
    }

// Check that the Quantity is less than 500,000
	if (/(^| )checkValue( |$)/.test(elements[i].className) && elements[i].value > 500000)
    {
      alert("Please ensure the Quantity is less than 500000.");
      elements[i].focus();
	  elements[i].style.backgroundColor = "#FFFFFF"; //Set background colour of fields to Yellow
	  elements[i].style.borderColor = "#FF0000"; //Set border colour of fields to Red
      return false;
    }

// Check that the Security code only contains Uppercase letters and numbers
    if (/(^| )checkCode( |$)/.test(elements[i].className) && !secCodePattern.test(elements[i].value))
    {
      alert("The Visual Security Code field must contain 5 characters consisting only of:\n\n - Uppercase Letters\(A-Z\)\n - Numbers\(1-9\)");
      elements[i].focus();
	  elements[i].style.backgroundColor = "#FFFFFF"; //Set background colour of fields to White
	  elements[i].style.borderColor = "#FF0000"; //Set border colour of fields to Red
      return false;
    }

  }
  
return true;

}

function addLoadListener(fn)
{
  if (typeof window.addEventListener != 'undefined')
  {
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined')
  {
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined')
  {
    window.attachEvent('onload', fn);
  }
  else
  {
    var oldfn = window.onload;
    if (typeof window.onload != 'function')
    {
      window.onload = fn;
    }
    else
    {
      window.onload = function()
      {
        oldfn();
        fn();
      };
    }
  }
}

// Function to dynamically load iFrame
function loadFrame()
{	
	document.getElementById('codeframe').src = 'http://www.logcall.com/cgi-bin/hd/secrityprompt.p'+new Date().valueOf();
	dis();
}

// Function to re-enable fields before submitting
function undis()
{	document.getElementById("select11").disabled=false;
	document.getElementById("austfreight").disabled=false;
	document.getElementById("die").disabled=false;
}

function dis()
{
	document.getElementById("die").value = "no";
	
	customDieYes();
	
	document.getElementById("select11").disabled=false;
	document.getElementById("austfreight").disabled=false;
  	if (document.getElementById("recountry").value == "UK")
	{ 
		document.getElementById("select11").value = "melanie@mousematters.com";
		document.getElementById("select11").disabled=true;
		document.getElementById("austfreight").disabled=true
	} else if (document.getElementById("recountry").value == "NZ")
		{ 
			document.getElementById("select11").value = "melanie@mousematters.com";
			document.getElementById("select11").disabled=true;
			document.getElementById("austfreight").disabled=true
		}
	
}

//Check Custom Die as Yes
function customDieYes()
{
	document.getElementById("die").disabled=false;
	if (document.getElementById("mattype").value==11) 
	{
		document.getElementById("die").value = "yes";
		document.getElementById("die").disabled=true;
	} 
	else
	{
		document.getElementById("die").disabled=false;
	}
}

