// prevent double clicks
var doingSearch = false;

function reloadMakes(snow) {
    var yearInput = document.getElementById('iYear');
    var y = yearInput.selectedIndex;
    var year = yearInput.options[y].value;
    var s = (snow == true) ? '1' : '0';
    if (year != '') {
        document.location.href = '/shopForTires/?step=1&year=' + year + '&snow=' + s;
    }
}

function goToStep2() {
    var s1Frm = document.getElementById('step1_0');
    var yearInput = document.getElementById('iYear');
    var makeInput = document.getElementById('iMake');
    var y = yearInput.selectedIndex;
    var m = makeInput.selectedIndex;
    var year = yearInput.options[y].value;
    var make = makeInput.options[m].value;

    if (year != '' && make != '') {
        try {
         pageTracker._trackPageview('/goals/ShopforTires');
        } catch(e) {
        }

        s1Frm.submit();
    } else {
        alert('Please select a Year and Make.');
    }
}

function goToStep3(year, make) {
    var step2Frm = document.getElementById('step2');
    var modelInput = document.getElementById('iModel');
    var m = modelInput.selectedIndex;
    var sizes = modelInput.options[m].value;
    if (sizes != '') {
        step2Frm.submit();
    } else {
        alert('Please select a Model for your car.');
    }
}

function skipToStep3() {
    var step1Frm = document.getElementById('step1_1');
    var size0 = document.getElementById('size0');
    var size1 = document.getElementById('size1');
    var size2 = document.getElementById('size2');
    var s0 = size0.selectedIndex;
    var s1 = size1.selectedIndex;
    var s2 = size2.selectedIndex;
    var val0 = size0.options[s0].value;
    var val1 = size1.options[s1].value;
    var val2 = size2.options[s2].value;
    if (val0 != '' && val1 != '' && val2 != '') {
        step1Frm.submit();
    } else {
        alert('Please select a valid tire size.');
    }
}

function doSizeSearch2() {
    var step1Frm = document.getElementById('step1_2');
    var size0 = document.getElementById('size0');
    var size1 = document.getElementById('size1');
    var s0 = size0.selectedIndex;
    var s1 = size1.selectedIndex;
    var val0 = size0.options[s0].value;
    var val1 = size1.options[s1].value;
    if (val0 != '' && val1 != '') {
        step1Frm.submit();
    } else {
        alert('Please select a valid tire size.');
    }
}

function doTireSearch() {
  var submitForm = false;
  var step3Frm = document.getElementById('step3');

  // only continue if the search isn't already going on (double-click)
  if (doingSearch == false) {
    // prevent another search
    doingSearch = true;

    // make sure that at least 1 store is selected
    var foundStore = false;
    for (var i = 0; i < step3Frm.elements.length; i++) {
      var curElm = step3Frm.elements.item(i);
      if (curElm.name.substr(0, 8) == 'location') {
        if (curElm.checked == true) {
          foundStore = true;
          break;
        }
      }
    }

    if (!foundStore) {
      alert('Please select at least one location.');
    } else {
      var email = document.getElementById('email');

      if (!validateEmail(email.value)) {
        alert('Without a valid Email address we won\'t be able to deliver your Shop For Tires results. Please enter a valid Email address.');
      } else {
        submitForm = true;
      }
    }

    if (submitForm) {
      step3Frm.submit();
    }
    // enable the search again
    else {
      doingSearch = false;
    }
  }

  return submitForm;
}

function validateEmail(emailAddress) {
  return ((emailAddress.indexOf('.') > 2) && (emailAddress.indexOf('@') > 0)) ? true : false;
}

function showModelDescriptions(year, make, snow) {
    var dataDiv = document.getElementById('modelDescriptions');

    var modSel = document.getElementById('iModel');
    var curModel = modSel.options[modSel.selectedIndex].value;
    var s = (snow == true) ? '1' : '0';

    if (curModel != '') {
        // remove old data
        if (dataDiv.childNodes) {
            while (dataDiv.childNodes.length > 0) {
                dataDiv.removeChild(dataDiv.lastChild);
            }
        }

        var modelHelp = document.createElement('P');
        dataDiv.appendChild(modelHelp);
        modelHelp.appendChild(document.createTextNode('Please review your vehicle\'s options below and click on the appropriate size to continue.'));

        // insert new data
        for (var i = 0; i < modelInfo[curModel].length; i++) {
            var bgColor = (i % 2 == 0) ? '#eee' : '#fff';

            var desc = modelInfo[curModel][i][0];
            var size0 = (typeof modelInfo[curModel][i][1] != 'undefined') ? modelInfo[curModel][i][1] : null;
            var size1 = (typeof modelInfo[curModel][i][2] != 'undefined') ? modelInfo[curModel][i][2] : null;
            var size2 = (typeof modelInfo[curModel][i][3] != 'undefined') ? modelInfo[curModel][i][3] : null;

            var descDiv = document.createElement('DIV');
            descDiv.appendChild(document.createTextNode(desc));
            changeElmClass(descDiv, 'desc');

            var sizeDiv = document.createElement('DIV');
            var sizeLink = document.createElement('A');
            var linkTxt = '/shopForTires/?step=3&year=' + year + '&make=' + escape(make) + '&model=' + curModel + '&desc=' + escape(desc) + '&snow=' + s + '&size0=' + size0 + '&size1=' + size1 + '&size2=' + size2;
            sizeLink.href = linkTxt;
            sizeLink.style.backgroundColor = bgColor;
            sizeLink.appendChild(document.createTextNode(size0 + '/' + size1 + 'R' + size2));
            sizeDiv.appendChild(sizeLink);
            changeElmClass(sizeDiv, 'size');

            var infoDiv = document.createElement('DIV');
            changeElmClass(infoDiv, 'modelInfo');
            infoDiv.style.backgroundColor = bgColor;
            infoDiv.appendChild(descDiv)
            infoDiv.appendChild(sizeDiv)

            dataDiv.appendChild(infoDiv);
        }
    }
}


function changeElmClass(elm, cName){
    if (elm.nodeType){
        if (elm.setAttribute){
            elm.setAttribute('class', cName);
        }else if (elm.className){
            elm.className=cName;
        }else if (elm.style.className){
            elm.style.className=cName;
        }
    }
}

function submitStep3() {
  var searchButton = document.getElementById('searchButton');
  searchButton.onclick = null;

  var email0 = document.getElementById('email');
  var email1 = document.getElementById('emailAddyConfirm');
  var validateCode = document.getElementById('validateCode');
  var sendIt = false;
  var msg = '';

  var atPos = email0.value.indexOf('@');
  var dotPos = email0.value.indexOf('.');

  if (!email0.value.length || !email1.value.length || email1.value.length < 5) {
    email0.focus();
    msg = 'Please enter your email address.';
  }

  else if (email0.value != email1.value) {
    email0.focus();
    msg = 'Your confirmation email address doesn\'t match the original. Please verify you have entered it correctly.';
  }

  else if (atPos < 0 || dotPos < 0) {
    email0.focus();
    msg = 'Please enter a valid email address.';
  }

  else if (validateCode.value.length != 4) {
    validateCode.focus();
    msg = 'Please enter a validtion code to prevent SPAM abuse.';
  }

  else {
    sendIt = true;
  }

  if (sendIt) {
    searchButton.src = staticImgUrl + '/shopForTires/loading.gif';
    searchButton.height = 32;
    searchButton.width = 32;
    document.getElementById('step3').submit();
  } else {
    alert(msg);
    searchButton.onclick = submitStep3;
  }
}

