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 = 1
  document.vehicleForm.vehicleMake.options.length = 1
  document.vehicleForm.vehicleMake.options.selectedIndex = 0
  thisFranchise = null
  document.vehicleForm.vehicleMake.options[0] = new Option('Select Make','')
  for (fIndex in fC)
  {
      document.vehicleForm.vehicleMake.options[i] = new Option(fC[fIndex].f_desc, fC[fIndex].f_desc);
      if (fC[fIndex].f_default == true) {
      document.vehicleForm.vehicleMake.selectedIndex = i
      thisFranchise = fC[fIndex]
      
      //fillYearList(thisFranchise)
    }
    i++
  }
}

function changeMake() {
  thisFranchise = fC[document.vehicleForm.vehicleMake.selectedIndex];
  var brand = document.vehicleForm.vehicleMake.options[document.vehicleForm.vehicleMake.selectedIndex].text;
  if(brand == 'SMART'){
 	document.getElementById("rowClass").style.display ='none';
  } else{
  	document.getElementById("rowClass").style.display ='';
  }
    
  if(document.vehicleForm.vehicleMake.selectedIndex == 0){
   fillYearListWithDefaultValues()
  }else{
  fillYearList(thisFranchise)
  fillModelListWithDefaultValues()
  }
}

function fillYearList(p_franchise) {
 document.vehicleForm.vehicleYear.options[0] = new Option('Select Year','')
  j = 1
  document.vehicleForm.vehicleYear.options.length = 1
  document.vehicleForm.vehicleYear.options.selectedIndex = 0
  thisYear = null;
  
  
  for (yIndex in p_franchise.f_mY)
  {
    theYear = p_franchise.f_mY[yIndex]
    document.vehicleForm.vehicleYear.options[j] = new Option(theYear.y_desc, theYear.y_code)
    if (theYear.y_default == true) {
      thisYear = p_franchise.f_mY[yIndex]
      document.vehicleForm.vehicleYear.selectedIndex = j
      
    }
    j++
  }
}

function changeYear() {
  thisYear = thisFranchise.f_mY[document.vehicleForm.vehicleYear.selectedIndex]
  
  if(document.vehicleForm.vehicleYear.selectedIndex == 0){
   fillModelListWithDefaultValues()
  }else{
  fillModelFamilyList(thisYear)
  fillBodyModelListWithDefaultValues()
  }
  
  var brand = document.vehicleForm.vehicleMake.options[document.vehicleForm.vehicleMake.selectedIndex].text;
    if(brand == 'SMART'){

		if(thisYear){
		  thisModelFamily = thisYear.y_mF[1];
		}
		if(document.vehicleForm.vehicleYear.selectedIndex == 0){
			fillBodyModelListWithDefaultValues();
		}
		else{
    		fillBodyModelList(thisModelFamily)
    	}
  	}
}

function fillModelFamilyList(p_year) {
	 	document.vehicleForm.vehicleClass.options[0] = new Option('Select Class','')
	  	k = 1
	  	document.vehicleForm.vehicleClass.options.length = 1
	  	document.vehicleForm.vehicleClass.options.selectedIndex = 0
	  	thisModelFamily = null
	  	for (mIndex in p_year.y_mF)
	  	{
	    	theModelFamily = p_year.y_mF[mIndex]
	    	document.vehicleForm.vehicleClass.options[k] = new Option(theModelFamily.m_desc, theModelFamily.m_desc)
	    	if (theModelFamily.m_default == true) {
	      		document.vehicleForm.vehicleClass.selectedIndex = k
	      		thisModelFamily = p_year.y_mF[mIndex]
	     
	    }
	    k++
	  }
}

function changeClass() {
  thisModelFamily = thisYear.y_mF[document.vehicleForm.vehicleClass.selectedIndex]
   if(document.vehicleForm.vehicleClass.selectedIndex == 0){
   fillBodyModelListWithDefaultValues()
  }else{
    fillBodyModelList(thisModelFamily)
  }
  
}

function fillBodyModelList(p_modelFamily) {
  document.vehicleForm.vehicleCode.options[0] = new Option('Select Body Style','')
  l = 1
  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_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.province.value) == false) {
      alert("Invalid Province 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 checkValueSelected(str){

  if (str=="") {
    return false;
  } else {
    return true;
  }
}



function fillYearListWithDefaultValues() {
 document.vehicleForm.vehicleYear.options[0] = new Option('Select Year','')
  
  document.vehicleForm.vehicleYear.options.length = 1
  document.vehicleForm.vehicleYear.options.selectedIndex = 0
  fillModelListWithDefaultValues()
}

function  fillModelListWithDefaultValues(){
 document.vehicleForm.vehicleClass.options[0] = new Option('Select Class','')
  document.vehicleForm.vehicleClass.options.length = 1
  document.vehicleForm.vehicleClass.options.selectedIndex = 0
  fillBodyModelListWithDefaultValues()
 
}

function fillBodyModelListWithDefaultValues() {
  document.vehicleForm.vehicleCode.options[0] = new Option('Select Body Style','')
   document.vehicleForm.vehicleCode.options.length = 1
  document.vehicleForm.vehicleCode.options.selectedIndex = 0
}






