fC = new Array()
thisFranchise = null
thisYear = null
thisModelFamily = null
thisBodyModel = null

function Franchise (p_code, p_desc, p_seq) {
  this.f_code = p_code
  this.f_desc = p_desc
  this.f_seq = p_seq
  this.f_default = false
  this.f_mY = new Array()
}

function mY (p_code, p_desc, p_parent, p_seq) {
  this.y_code = p_code
  this.y_desc = p_desc
  this.y_seq = p_seq
  this.y_parent = p_parent
  this.y_default = false
  this.y_mF = new Array()
}

function mF (p_code, p_desc, p_parent, p_seq) {
  this.m_code = p_code
  this.m_desc = p_desc
  this.m_seq = p_seq
  this.m_parent = p_parent
  this.m_default = false
  this.m_bM = new Array()
}

function bM (p_code, p_desc, p_parent, p_msrp, p_seq) {
  this.b_code = p_code
  this.b_desc = p_desc
  this.b_seq = p_seq
  this.b_parent = p_parent
  this.b_MSRP = p_msrp
  this.b_default = false
}

function fillFranchiseList() {
  i = 0
  document.vehicleForm.FranchiseCode.options.length = 1
  document.vehicleForm.FranchiseCode.options.selectedIndex = 0
  thisFranchise = null
  for (fIndex in fC)
  {
    document.vehicleForm.FranchiseCode.options[i] = new Option(fC[fIndex].f_desc, fC[fIndex].f_code)
    if (fC[fIndex].f_default == true) {
      document.vehicleForm.FranchiseCode.selectedIndex = i
      thisFranchise = fC[fIndex]
      fillYearList(thisFranchise)
    }
    i++
  }
}
function SetCookie (name, value) {
  document.cookie = name + "=" + escape(value)+ "; path=/credit/cfc/";
}
function changeFranchise() {
  thisFranchise = fC[document.vehicleForm.FranchiseCode.selectedIndex]
  fillYearList(thisFranchise);
  SetCookie("Franchise",MM_findObj("FranchiseCode").options.selectedIndex + 1);
  SetCookie("ModelYear",MM_findObj("YearCode").options.selectedIndex + 1);
  SetCookie("Family",MM_findObj("ModelFamilyCode").options.selectedIndex + 1);
  SetCookie("Model",MM_findObj("VehicleCode").options.selectedIndex + 1);
}

function fillYearList(p_franchise) {
  j = 0
  document.vehicleForm.YearCode.options.length = 1
  document.vehicleForm.YearCode.options.selectedIndex = 0
  thisYear = null
  for (yIndex in p_franchise.f_mY)
  {
    theYear = p_franchise.f_mY[yIndex]
    document.vehicleForm.YearCode.options[j] = new Option(theYear.y_desc, theYear.y_code)
    if (theYear.y_default == true) {
      thisYear = p_franchise.f_mY[yIndex]
      document.vehicleForm.YearCode.selectedIndex = j
      fillModelFamilyList(thisYear)
    }
    j++
  }
}

function changeYear() {
  thisYear = thisFranchise.f_mY[document.vehicleForm.YearCode.selectedIndex]
  fillModelFamilyList(thisYear)
  SetCookie("ModelYear",MM_findObj("YearCode").options.selectedIndex + 1);
  SetCookie("Family",MM_findObj("ModelFamilyCode").options.selectedIndex + 1);
  SetCookie("Model",MM_findObj("VehicleCode").options.selectedIndex + 1);
}

function fillModelFamilyList(p_year) {
  k = 0
  document.vehicleForm.ModelFamilyCode.options.length = 1
  document.vehicleForm.ModelFamilyCode.options.selectedIndex = 0
  thisModelFamily = null
  for (mIndex in p_year.y_mF)
  {
    theModelFamily = p_year.y_mF[mIndex]
    document.vehicleForm.ModelFamilyCode.options[k] = new Option(theModelFamily.m_desc, theModelFamily.m_code)
    if (theModelFamily.m_default == true) {
      document.vehicleForm.ModelFamilyCode.selectedIndex = k
      thisModelFamily = p_year.y_mF[mIndex]
      fillBodyModelList(thisModelFamily)
    }
    k++
  }
}

function changeModelFamily() {
  thisModelFamily = thisYear.y_mF[document.vehicleForm.ModelFamilyCode.selectedIndex]
  fillBodyModelList(thisModelFamily)
  SetCookie("Family",MM_findObj("ModelFamilyCode").options.selectedIndex + 1);
  SetCookie("Model",MM_findObj("VehicleCode").options.selectedIndex + 1);
}

function changeModel() {
  SetCookie("Model",MM_findObj("VehicleCode").options.selectedIndex + 1);
}

function fillBodyModelList(p_modelFamily) {
  l = 0
  document.vehicleForm.VehicleCode.options.length = 1
  document.vehicleForm.VehicleCode.options.selectedIndex = 0
  thisBodyModel = null
  for (bIndex in p_modelFamily.m_bM)
  {
    theBodyModel = p_modelFamily.m_bM[bIndex]
    document.vehicleForm.VehicleCode.options[l] = new Option(theBodyModel.b_desc + " " + theBodyModel.b_MSRP, theBodyModel.b_code)
    if (theBodyModel.b_default == true) {
      document.vehicleForm.VehicleCode.selectedIndex = l
      thisBodyModel = p_modelFamily.m_bM[bIndex]
    }
    l++
  }
}

function submitAgreeForm (frm) {
  if (document.vehicleForm.theZip) {
    if (checkZIP(document.vehicleForm.theZip.value) == false) {
      alert("Invalid Postal Code");
    }
    else {
      document.vehicleForm.ZipCode.value = frm.theZip.value;
      document.forms.vehicleForm.submit();
    }
  }
  else {
    document.forms.vehicleForm.submit()
  }
}


function submitForm () {
  if (checkZIP(document.vehicleForm.ZipCode.value) == false) {
    alert("Invalid Postal Code")
  }
  else {
    document.vehicleForm.submit()
  }
}

// takes a string and removes leading whitespace
function ltrim (s)
{
    return s.replace( /^\s*/, "" );
}
// takes a string and removes trailing whitespace
function rtrim (s)
{
    return s.replace( /\s*$/, "" );
}
// takes a string and removes leading and trailing whitespace
function trim (s)
{
    return ltrim(rtrim(s));
}

function checkZIP(postalCode){
  regEx = /[a-zA-Z][0-9][a-zA-Z][0-9][a-zA-Z][0-9]/;
  if (!(regEx.test(postalCode))) {
    return false;
  } else {
    return true;
  }
}

