
var AssociatedContent = {
  warrantyForm: function() {
    var url = 'http://www.customersaskford.com/warranty/warranty.asp';
    var result = new Ajax(url, { method: 'get', update: $('associated-content') }).request();
  },
  recallForm: function() {
    var url = '/servlet/ContentServer?pagename=FordCom/AskFord/RecallForm';
    var result = new Ajax(url, { method: 'get', update: $('associated-content') }).request();
  },
  warrantyLookup: function() {
    if ($('askford_vin') && $('askford_vin').value.length == 17) {
      result = this.vinCheck($('askford_vin').value);
      if (result == false) {
        alert('This is not a valid VIN number. Please try again.');
      } else {
        var url = '/servlet/ContentServer?pagename=FordCom/AskFord/RecallResult&vin='+($('askford_vin').value);
        var result = new Ajax(url, { method: 'get', update: $('associated-content') }).request();
      }
    } else {
      alert("VIN must be at exactly 17 characters");
    }
  },
  recallLookup: function() {
    if ($('askford_vin') && $('askford_vin').value.length == 17) {
      result = this.vinCheck($('askford_vin').value);
      if (result == false) {
        alert('This is not a valid VIN number. Please try again.');
      } else {
        var url = '/servlet/ContentServer?pagename=FordCom/AskFord/RecallResult&vin='+($('askford_vin').value);
        var result = new Ajax(url, { method: 'get', update: $('associated-content') }).request();
      }
    } else {
      alert("VIN must be at exactly 17 characters");
    }
  },
  vinCheck: function(str) {
    // check length
    if (str.length == 17) {
      var a_vin = new Array(17);
      var i_checksum = 0, i_pos = 0;
      var a_alphavalue = new Array(1,2,3,4,5,6,7,8,-1,1,2,3,4,5,-1,7,-1,9,2,3,4,5,6,7,8,9);
      var a_positionvalue = new Array(8,7,6,5,4,3,2,10,0,9,8,7,6,5,4,3,2);
      var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      var x;
      i_checksum = 0;
      for (x = 0; x < 17; x++) { 
        a_vin[x] = str.substring(x,x+1);
          a_vin[x] = a_vin[x].toUpperCase();

          i_pos = uppercaseLetters.indexOf(a_vin[x],0);
          if (i_pos == -1) {
            i_checksum += parseInt(a_vin[x]) * a_positionvalue[x];
          }
          else {
              i_checksum += a_alphavalue[i_pos] * a_positionvalue[x];
          }
      } // end for x
      i_checksum %= 11;
      if (((i_checksum >= 0 || i_checksum <= 9) && (parseInt(a_vin[8]) == i_checksum)) || ((i_checksum == 10) && (a_vin[8] == "X"))){
              return true;
      } 
    } // end if length == 17
    return false;
  }
} // end AssociatedContent

