function highlightGlass(glassToHighlight){
  if (glassToHighlight == 'windscreen'){document.getElementById("car_glass_diagram").src = 'images/car_glass_diagram_windscreen.gif';}
  if (glassToHighlight == 'driver_side_front_window'){document.getElementById("car_glass_diagram").src = 'images/car_glass_diagram_driver_side_front_window.gif';}
  if (glassToHighlight == 'passenger_side_front_window'){document.getElementById("car_glass_diagram").src = 'images/car_glass_diagram_passenger_side_front_window.gif';}
  if (glassToHighlight == 'driver_side_rear_window'){document.getElementById("car_glass_diagram").src = 'images/car_glass_diagram_driver_side_rear_window.gif';}
  if (glassToHighlight == 'passenger_side_rear_window'){document.getElementById("car_glass_diagram").src = 'images/car_glass_diagram_passenger_side_rear_window.gif';}
  if (glassToHighlight == 'driver_side_quarter_light'){document.getElementById("car_glass_diagram").src = 'images/car_glass_diagram_driver_side_quarter_light.gif';}
  if (glassToHighlight == 'passenger_side_quarter_light'){document.getElementById("car_glass_diagram").src = 'images/car_glass_diagram_passenger_side_quarter_light.gif';}
  if (glassToHighlight == 'rear_windscreen'){document.getElementById("car_glass_diagram").src = 'images/car_glass_diagram_rear_windscreen.gif';}
}
function unHighlightGlass(){
  document.getElementById("car_glass_diagram").src = 'images/car_glass_diagram.gif';
}

function checkQuotePanelForm(){
  myErr = "";
  if ((document.quoteForm.postcode.value == "") || (document.quoteForm.postcode.value == "POSTCODE")){
    document.quoteForm.postcode.value = "";
//    myErr = myErr + "* You must enter your postcode\n";
  }

  if (myErr == ""){
    return true;
  }
  else{
    alert(myErr);
    return false;
  }
}

function checkQuoteForm(){
  updateFields();
  myErr = "";
  with (document.quoteForm){
    if (make.selectedIndex == 0){
      myErr = myErr + "* You must select your vehicle make and model\n";
    }
    if (regnumber.value == ""){
      myErr = myErr + "* You must enter your vehicle registration number, e.g. 'XY03 ABC '\n";
    }
    if (glass.selectedIndex == 0){
      myErr = myErr + "* You must select your glass required\n";
    }
    if (Name.value == ""){
      myErr = myErr + "* You must enter your full name, e.g. Mr John Smith\n";
    }
    if (Phone.value == ""){
      myErr = myErr + "* You must enter your phone number\n";
    }
    else{
      //validate phone number
      if (!validPhone(Phone.value)){
        myErr = myErr + "* You must enter a valid phone number\n";
      }
    }
    if (Email.value == ""){
      myErr = myErr + "* You must enter your email address\n";
    }
    else{
      //validate email address
      if (!validEmail(Email.value)){
        myErr = myErr + "* You must enter a valid email address\n";
      }
    }
    if (Postcode.value == ""){
      myErr = myErr + "* You must enter your postcode, e.g. PE27 5AD\n";
    }
  }

  if (myErr == ""){
    return true;
  }
  else{
    alert(myErr);
    return false;
  }
}


function checkCallbackForm(){
  myErr = "";
  with (document.callbackForm){
    if (Name.value == ""){
      myErr = myErr + "* You must enter your full name, e.g. Mr John Smith\n";
    }
    if (Phone.value == ""){
      myErr = myErr + "* You must enter your phone number\n";
    }
    else{
      //validate phone number
      if (!validPhone(Phone.value)){
        myErr = myErr + "* You must enter a valid phone number\n";
      }
    }
    if (Postcode.value == ""){
      myErr = myErr + "* You must enter your postcode, e.g. PE27 5AD\n";
    }
  }

  if (myErr == ""){
    return true;
  }
  else{
    alert(myErr);
    return false;
  }
}


function validEmail(emailString){
  if (emailString.indexOf("@") == -1){
    return false;  //contains no @ sign
  }
  else{
    var temp = emailString.split("@");
    if ((temp[0].length == 0) || (temp[1].length == 0) || (temp.length > 2)){
      return false;  //nothing either before or after the @ sign, or contains more than 1 @ sign
    }
    else{
      var validCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&'*+-./=?^_`{|}~";
      for (i=0; i<temp[0].length; i++){
        if (validCharacters.indexOf(temp[0].charAt(i)) == -1){
          return false;  //first part of email address does not contain a valid character
        }
      }
      validCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-."
      for (i=0; i<temp[1].length; i++){
        if (validCharacters.indexOf(temp[1].charAt(i)) == -1){
          return false;  //second part of email address does not contain a valid character
        }
      }
      if ((temp[1].indexOf(".") == -1) || (temp[1].indexOf(".") == 0) || (emailString.lastIndexOf(".") == (emailString.length - 1))){
        return false;  //either no dot found, dot is straight after the @ sign or dot is at the end of the address
      }
    }
  }
  return true;
}

function validPhone(numString){
  var validCharacters = "0123456789 ext.()+";
  for (i=0; i<numString.length; i++){
    if (validCharacters.indexOf(numString.charAt(i)) == -1){
      return false;  //phone number does not contain a valid character
    }
  }
  return true;
}

var rotatorTimeout;
var rotatorTimeDelay;
var rotatorImages = new Array()
// format = imagefolder/image_file_name^delay_until_image_loads
//rotatorImages[0] = "images/windscreen_1.jpg^3000"  this image will display for 1 second
//rotatorImages[1] = "images/windscreen_2.jpg^1000"  this image will display for 5 seconds
//rotatorImages[2] = "images/windscreen_3.jpg^5000"  this image will display for 3 seconds

rotatorImages[0] = "images/windscreen_daytime_1.jpg^10000"
rotatorImages[1] = "images/windscreen_daytime_2.jpg^1200"
rotatorImages[2] = "images/windscreen_helpline_man_1.jpg^3500"
rotatorImages[3] = "images/windscreen_helpline_man_2.jpg^1500"
rotatorImages[4] = "images/windscreen_helpline_man_3.jpg^1200"
rotatorImages[5] = "images/windscreen_helpline_man_4.jpg^1200"
myImage = 1;

function changeImageOpacity(numVal){
// function written by Julian Dale of Lifetime Web Services Ltd, Copyright 2009
  numValOpacity = "." + numVal
  numValFilter = "alpha(opacity=" + numVal + ")";
  document.getElementById("topImage").style.opacity = numValOpacity;
  document.getElementById("topImage").style.filter = numValFilter;
}
function initiateRotator(numVal){
// function written by Julian Dale of Lifetime Web Services Ltd, Copyright 2009
  if (numVal > 0) {
    myNum = numVal - 10;
    changeImageOpacity(myNum);
    setTimeout("initiateRotator(myNum)",40);
  }
  else {
    document.getElementById("topImage").src = document.getElementById("bottomImage").src;
    document.getElementById("topImage").style.opacity = "100";
    document.getElementById("topImage").style.filter = "alpha(opacity=100)";
    loadNewImage();
  }
}
function loadNewImage(){
// function written by Julian Dale of Lifetime Web Services Ltd, Copyright 2009
  tempImageArray = rotatorImages[myImage].split("^");
  document.getElementById("bottomImage").src = tempImageArray[0];
  rotatorTimeDelay = tempImageArray[1];
  myImage = myImage + 1;
  if (myImage >= rotatorImages.length){
    myImage = 0;
  }
  rotatorTimeout = setTimeout("initiateRotator(100)",rotatorTimeDelay);
}
function stopRotator(){
// function written by Julian Dale of Lifetime Web Services Ltd, Copyright 2009
  clearTimeout(rotatorTimeout);
}
function startRotator(){
// function written by Julian Dale of Lifetime Web Services Ltd, Copyright 2009
  loadNewImage();
}
