// Domestic Repairs Form js

var intQuestionIndex=1;

function imValid() {

    switch (intQuestionIndex) {
        case 1:
            if (document.quoteme.skill.selectedIndex == 0) {
                document.quoteme.skill.focus();
                alert("I REQUIRE ... What Type of Engineer?");
                return false;
            }
            if (document.quoteme.urgency.selectedIndex == 0) {
                document.quoteme.urgency.focus();
                alert("How urgent is this job? Please select!");
                return false;
            }
            if (document.quoteme.post_code.value.length < 4) {
                document.quoteme.post_code.focus();
                alert("Please enter your Post Code");
                return false;
            }

// Swap panel to page 2
            document.getElementById('form_1').style.visibility = 'hidden';
            document.getElementById('form_2').style.visibility = 'visible';
            intQuestionIndex++;
            return false;
            break;

         case 2:
            if (document.quoteme.message.value.length < 8) {
                document.quoteme.message.focus();
                alert("Please enter a brief job description!");
                return false;
            }
// Swap panel to page 3
            document.getElementById('form_1').style.visibility = 'hidden';
            document.getElementById('form_2').style.visibility = 'hidden';
            document.getElementById('form_3').style.visibility = 'visible';
            document.getElementById('formButton').src = 'images/request_quote_button.jpg';
            intQuestionIndex++;
            return false;
            break;

         case 3:
            if (document.quoteme.name.value.length < 2) {
                alert('Please enter your name');
                document.quoteme.name.focus();
                return false;
            }
          var myTelNo = document.getElementById('day_phone').value;
          // If invalid number, report back error
              if (!checkUKTelephone (myTelNo)) {
              alert (telNumberErrors[telNumberErrorNo]);
              document.quoteme.day_phone.focus();
              return false ;
          }
            break;
    }
    return true;
}


function clearForms()
{
  var i;
  for (i = 0; (i < document.forms.length); i++) {
    document.forms[i].reset();
  }
}


/*Check Tel Number Validation */

function checkUKTelephone (telephoneNumber) {

  // Convert into a string and check that we were provided with something
  var telnum = telephoneNumber + " ";
  if (telnum.length == 1)  {
     telNumberErrorNo = 1;
     return false
  }
  telnum.length = telnum.length - 1;

  // Don't allow country codes to be included (assumes a leading "+")
  var exp = /^(\+)[\s]*(.*)$/;
  if (exp.test(telnum) == true) {
     telNumberErrorNo = 2;
     return false;
  }

  // Remove spaces from the telephone number to help validation
  while (telnum.indexOf(" ")!= -1)  {
    telnum = telnum.slice (0,telnum.indexOf(" ")) + telnum.slice (telnum.indexOf(" ")+1)
  }

  // Remove hyphens from the telephone number to help validation
  while (telnum.indexOf("-")!= -1)  {
    telnum = telnum.slice (0,telnum.indexOf("-")) + telnum.slice (telnum.indexOf("-")+1)
  }

  // Now check that all the characters are digits
  exp = /^[0-9]{10,11}$/;
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 3;
     return false;
  }

  // Now check that the first digit is 0
  exp = /^0[0-9]{9,10}$/;
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 4;
     return false;
  }

	// Disallow numbers allocated for dramas.

  // Array holds the regular expressions for the drama telephone numbers
  var tnexp = new Array ();
	tnexp.push (/^(0113|0114|0115|0116|0117|0118|0121|0131|0141|0151|0161)(4960)[0-9]{3}$/);
	tnexp.push (/^02079460[0-9]{3}$/);
	tnexp.push (/^01914980[0-9]{3}$/);
	tnexp.push (/^02890180[0-9]{3}$/);
	tnexp.push (/^02920180[0-9]{3}$/);
	tnexp.push (/^01632960[0-9]{3}$/);
	tnexp.push (/^07700900[0-9]{3}$/);
	tnexp.push (/^08081570[0-9]{3}$/);
	tnexp.push (/^09098790[0-9]{3}$/);
	tnexp.push (/^03069990[0-9]{3}$/);

	for (var i=0; i<tnexp.length; i++) {
    if ( tnexp[i].test(telnum) ) {
      telNumberErrorNo = 5;
      return false;
    }
	}

  // Finally check that the telephone number is appropriate.
  exp = (/^(01|02|03|05|070|071|072|073|074|075|07624|077|078|079)[0-9]+$/);
	if (exp.test(telnum) != true) {
     telNumberErrorNo = 5;
     return false;
  }

  // Telephone number seems to be valid - return the stripped telehone number
  return telnum;
}
var telNumberErrorNo = 0;
var telNumberErrors = new Array ();
telNumberErrors[0] = "Valid UK telephone number";
telNumberErrors[1] = "Please provide a valid telephone number";
telNumberErrors[2] = "Please provide a valid telephone number";
telNumberErrors[3] = "Please provide a valid telephone number";
telNumberErrors[4] = "Please provide a valid telephone number";
telNumberErrors[5] = "Please provide a valid telephone number";

