// Rotating image on home page

var currentImg = 0;



function rotateImg() {



  var rotate = new Array();



  rotate[0] = "images/carpet01.gif";

  rotate[1] = "images/carpet02.gif";

  rotate[2] = "images/carpet03.gif";

  rotate[3] = "images/carpet04.gif";

  rotate[4] = "images/carpet05.gif";

  rotate[5] = "images/carpet06.gif";

  rotate[6] = "images/carpet07.gif";

  rotate[7] = "images/carpet08.gif";

  rotate[8] = "images/carpet09.gif";

  rotate[9] = "images/carpet10.gif";



  if (currentImg < 10) {

    document.carpet.src = rotate[currentImg];

    currentImg++;

  } else {

    currentImg = 0; // reset currentImg variable

    document.carpet.src = rotate[currentImg]

    currentImg++;

  }



  setTimeout("rotateImg()",5000);

}



// Calculator/estimate page

function hideFields() {

  xHide("other");

  xHide("steps");

  //xHide("pet");

}

function checkSelected() {

  var areaType = document.estimate.areaType.value;

  var petOdor = document.estimate.petOdor[0].checked;



  if (areaType == "Other") {

    xShow("other");

  }

  if (areaType == "Steps") {

    sqFt.disabled = true;

    xShow("steps");

  }

  //if (petOdor == true) {

  //  xShow("pet");

  //}

}

function toggleArea(area_value,array_length) {

  var sqFt = document.estimate.sqFt;



  if (area_value == "Other") {

    xShow("other");

    xHide("steps");

    sqFt.disabled = false;

  } else if (area_value == "Steps") {

    xHide("other");

    xShow("steps");

    sqFt.disabled = true;

  } else {

    xHide("other");

    xHide("steps");

    sqFt.disabled = false;

  }

}

function setCookie(name, value, expires, path, domain, secure)

{

  document.cookie = name + "=" + escape(value) +

    ((expires) ? "; expires=" + expires.toGMTString() : "; expires=Fri, 01 Jan 2010 00:00:01 GMT") +

    ((path) ? "; path=" + path : "") +

    ((domain) ? "; domain=" + domain : "") +

    ((secure) ? "; secure" : "");

}

function getCookie(name)

{

  var dc = document.cookie;

  var prefix = name + "=";

  var begin = dc.indexOf("; " + prefix);

  if (begin == -1)

  {

    begin = dc.indexOf(prefix);

    if (begin != 0) return null;

  }

  else

  {

    begin += 2;

  }

  var end = document.cookie.indexOf(";", begin);

  if (end == -1)

  {

    end = dc.length;

  }

  return unescape(dc.substring(begin + prefix.length, end));

}

function deleteCookie(name, path, domain)

{

  if (getCookie(name))

  {

    document.cookie = name + "=" + 

      ((path) ? "; path=" + path : "") +

      ((domain) ? "; domain=" + domain : "") +

      "; expires=Thu, 01-Jan-70 00:00:01 GMT";

  }

}
// Request appointment functions
function validate(form) {
  var f_name = form.firstName;
  var l_name = form.lastName;
  var addr = form.addr;
  var city = form.city;
  var zip = form.zip;
  var phone1 = form.phone1;
  var phone2 = form.phone2;
  var phone3 = form.phone3;
  var month = form.month;
  var day = form.day;
  var time = form.time;

  if (f_name.value == "") {
    alert("Please enter your first name.");
    f_name.focus();
    return false;
  }
  if (l_name.value == "") {
    alert("Please enter your last name.");
    l_name.focus();
    return false;
  }
  if (addr.value == "") {
    alert("Please enter your address.");
    addr.focus();
    return false;
  }
  if (city.value == "") {
    alert("Please enter your city name.");
    city.focus();
    return false;
  }
  if (!zip.value.match(/\d{5}/)) {
    alert("Please enter your 5-digit zip code.");
    zip.focus();
    return false;
  }
  if (!phone1.value.match(/\d{3}/) || !phone2.value.match(/\d{3}/) || !phone3.value.match(/\d{4}/)) {
    alert("Please enter your phone number.");
    phone1.focus();
    return false;
  }
  if (month.selectedIndex == 0) {
    alert("Please select the month you are requesting an appointment for.");
    month.focus();
    return false;
  }
  if (day.selectedIndex == 0) {
    alert("Please select the day you are requesting an appointment for.");
    day.focus();
    return false;
  }
  if (time.selectedIndex == 0) {
    alert("Please select the time you are requesting an appointment for.");
    time.focus();
    return false;
  }
}
function autoTab(input,len, e) {
  var keyCode = e.keyCode; 
  var filter = [0,8,9,16,17,18,37,38,39,40,46];

  if(input.value.length >= len && !containsElement(filter,keyCode)) {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
  }
}
function autoTab(input,len,e) {
  var keyCode = e.keyCode; 
  var filter = [0,8,9,16,17,18,37,38,39,40,46];

  if(input.value.length >= len && !containsElement(filter,keyCode)) {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
  }
  function containsElement(arr,ele) {
    var found = false, index = 0;

    while(!found && index < arr.length) {
      if(arr[index] == ele) {
        found = true;
      } else {
        index++;
      }
    }
    return found;
  }
  function getIndex(input) {
    var index = -1, i = 0, found = false;

    while (i < input.form.length && index == -1) {
      if (input.form[i] == input) {
        index = i;
      } else {
        i++;
      }
    }
    return index;
  }
  return true;
}
function prefill() {
  var firstName = document.request.firstName;
  var lastName = document.request.lastName;
  var addr = document.request.addr;
  var city = document.request.city;
  var zip = document.request.zip;
  var email = document.request.email;
  var phone1 = document.request.phone1;
  var phone2 = document.request.phone2;
  var phone3 = document.request.phone3;
  var altPhone1 = document.request.altPhone1;
  var altPhone2 = document.request.altPhone2;
  var altPhone3 = document.request.altPhone3;

  if (getCookie('firstName') != null) {
    firstName.value = getCookie('firstName');
  }
  if (getCookie('lastName') != null) {
    lastName.value = getCookie('lastName');
  }
  if (getCookie('addr') != null) {
    addr.value = getCookie('addr');
  }
  if (getCookie('city') != null) {
    city.value = getCookie('city');
  }
  if (getCookie('zip') != null) {
    zip.value = getCookie('zip');
  }
  if (getCookie('email') != null) {
    email.value = getCookie('email');
  }
  if (getCookie('phone1') != null) {
    phone1.value = getCookie('phone1');
  }
  if (getCookie('phone2') != null) {
    phone2.value = getCookie('phone2');
  }
  if (getCookie('phone3') != null) {
    phone3.value = getCookie('phone3');
  }
  if (getCookie('altPhone1') != null) {
    altPhone1.value = getCookie('altPhone1');
  }
  if (getCookie('altPhone2') != null) {
    altPhone2.value = getCookie('altPhone2');
  }
  if (getCookie('altPhone3') != null) {
    altPhone3.value = getCookie('altPhone3');
  }
}
// Contact us functions
function validate(form) {
  var name = form.name;
  var email = form.email;
  var msg = form.message;

  if (name.value.length == 0) {
    alert("Please enter your name.");
    name.focus();
    return false;
  }
  if (email.value.length == 0) {
    alert("Please enter your e-mail address.");
    email.focus();
    return false;
  }
  if (!email.value.match(/\S+[@]\S+[.]\S+/)) {
	  alert("Please enter a valid e-mail address.");
    email.focus();
    return false;
  }
  if (msg.value.length == 0) {
    alert("Please enter a message.");
    msg.focus();
    return false;
  }
}
// Calculator functions
function validate(form) {
  var areaType = document.getElementById("area");
  var other = document.getElementById("other");
  var steps = document.getElementById("steps");
  var sqFt = document.getElementById("sqFt");
  var cleaning = document.getElementById("cleaning");
  var scotchgard = document.getElementById("scotchgard");
  var petOdor = document.getElementById("pet_odor");

  if (areaType.selectedIndex == 0) {
	  alert("Please select an area type.");
		areaType.focus();
		return false;
	}
	if (areaType.selectedIndex == 15 && other.value == "") {
	  alert("You have selected 'Other' as an area type.\nPlease enter the type of area in the field marked 'Other'.");
		other.focus();
		return false;
	}
	if (areaType.selectedIndex == 16 && steps.value == "") {
	  alert("You have selected 'Steps' as an area type.\nPlease enter the number of steps in the field marked 'Steps'.");
		steps.focus();
		return false;
	}
	if (sqFt.selectedIndex == 0 && areaType.selectedIndex != 16) {
	  alert("Please select the square footage.");
		sqFt.focus();
		return false;
	}
	if (cleaning.checked == false && scotchgard.checked == false && petOdor.checked == false) {
	  alert("You must select at least one service.");
		return false;
	}
}
function toggleArea(area) {

  var sqFt = document.getElementById("sqFt");
  var other = document.getElementById("other");
  var steps = document.getElementById("steps");

  if (area[area.selectedIndex].text == "Other") {
    other.style.display = "";
    steps.style.display = "none";
    sqFt.disabled = false;
  } else if (area[area.selectedIndex].text == "Steps") {
    other.style.display = "none";
    steps.style.display = "";
    sqFt.disabled = true;
  } else {
    other.style.display = "none";
    steps.style.display = "none";
    sqFt.disabled = false;
  }
}