
// global url redirection function
function href(input_URL)
{
  window.location = input_URL;
}

// updates the text at the bottom of the page
function status_message(input_STRING)
{
  window.status = input_STRING;
}

// build the makes array
// used on the search page
function Build_Makes()
{
  if(typeof lookup_make != 'undefined')
  {
    // New Method
    var z = 0;
    var x = document.getElementById('make_target');
    x.options.length = 1;    
    for (var item in lookup_make)
    {
      if (lookup_make[item] == null)
      {
        x.options[z] = new Option('< All Makes >','');
      }
      else
      {
        if(lookup_make[item]['count'])
          x.options[z] = new Option(lookup_make[item]['name'] + ' (' + lookup_make[item]['count'] + ')',item);
        else
          x.options[z] = new Option(lookup_make[item]['name'],item);
      }
      z++;
    }
  }
  else
  {
    // Old Method
    var x = document.search.make;
    for (var i=0;i < makes.length;i++)
    {
      if (makes[i][1] == "")
      {
        x.options[i] = new Option("< All Makes >","");
      }
      else
      {
        x.options[i] = new Option(makes[i][1] + ' (' + makes[i][0] + ')',makes[i][1]);
      }
    }
  }
}

// build the models array
// used on the search page
function Build_Models()
{
  if(typeof lookup_model != 'undefined')
  {
    // New Method
    var i = document.getElementById('make_target').value;
    var x = document.getElementById('model_target');
    var z = 0;
    x.options.length = 1;
    if(lookup_model[i])
    {
      x.disabled = false;
      for (var item in lookup_model[i])
      {
        if (lookup_model[i][item] == null)
        {
          x.options[z] = new Option('< All Models >','');
        }
        else
        {
          if(lookup_model[i][item]['count'])
            x.options[z] = new Option(lookup_model[i][item]['name'] + ' (' + lookup_model[i][item]['count'] + ')',item);
          else
            x.options[z] = new Option(lookup_model[i][item]['name'],item);
        }
        z++;
      }
    }
    else
    {
      x.options[0] = new Option('< Please Select a Make >','');
      x.disabled = true;
    }
  }
  else
  {
    // Old Method
    var i = document.search.make.selectedIndex;
    var x = document.search.model;
    x.options.length = 1;
    for (var j=0;j < models[i].length;j++)
    {
      if (models[i][j] == "#")
      {
        x.options[j] = new Option("< All Models >","");
      }
      else
      {
        x.options[j] = new Option(models[i][j][1] + ' (' + models[i][j][0] + ')',models[i][j][1]);
      }
    }
  }
}



// build the makes array
// used on the search page
function Build_Makes_ID()
{
  var z = 0;
  var x = document.getElementById('makeid_target');
  x.options.length = 1;
  if (lookup_make[item] == null)
  {
    x.options[z] = new Option('< All Makes >','');
  }
  else
  {
    if(lookup_make[item]['count'])
      x.options[z] = new Option(lookup_make[item]['name'] + ' (' + lookup_make[item]['count'] + ')',item);
    else
      x.options[z] = new Option(lookup_make[item]['name'],item);
  }
  z++;
}

// build the models array
// used on the search page
function Build_Models_ID()
{
  var i = document.getElementById('makeid_target').value;
  var x = document.getElementById('makemodelid_target');
  var z = 0;
  x.options.length = 1;
  if(lookup_model[i])
  {
    x.disabled = false;
    for (var item in lookup_model[i])
    {
      if (lookup_model[i][item] == null)
      {
        x.options[z] = new Option('< All Models >','');
      }
      else
      {
        if(lookup_model[i][item]['count'])
          x.options[z] = new Option(lookup_model[i][item]['name'] + ' (' + lookup_model[i][item]['count'] + ')',item);
        else
          x.options[z] = new Option(lookup_model[i][item]['name'],item);
      }
      z++;
    }
  }
  else
  {
    x.options[0] = new Option('< Please Select a Make >','');
    x.disabled = true;
  }
}

// build the states array
// used on the search page
function Build_States()
{
  var x = document.search.state;
  for (var i=0;i < states.length;i++)
  {
    if (states[i] == "#")
    {
      x.options[i] = new Option("< All States >","");
    }
    else
    {
      x.options[i] = new Option(states[i],states[i]);
    }
  }
}

// build the areas array
// used on the search page
function Build_Areas()
{
  var i = document.search.state.selectedIndex;
  var x = document.search.area;
  x.options.length = 1;
  for (var j=0;j < areas[i].length;j++)
  {
    if (areas[i][j] == "#")
    {
      x.options[j] = new Option("< All Areas >","");
    }
    else
    {
      x.options[j] = new Option(areas[i][j],areas[i][j]);
    }
  }
}

// build the groupmembers array
// used on the search page
function Build_GroupMembers()
{
  var x = document.search.clientno;
  for (var i=0;i < groupmembers.length;i++)
  {
    if (groupmembers[i] == "#")
    {
      x.options[i] = new Option("< All Dealerships >","");
    }
    else
    {
      x.options[i] = new Option(groupmembers[i][1],groupmembers[i][0]);
    }
    //alert(default_groupmembers);
    if (default_groupmembers == groupmembers[i][1])
    {
      x.options[i].selected = true;
    }
  }
}

function Validate_YearFrom()
{
  var from = parseInt(document.search.yearfrom.value);
  var to = parseInt(document.search.yearto.value);
  if (from > to && document.search.yearfrom.value != "" && document.search.yearto.value != "")
    document.search.yearto.value = from;
}

function Validate_YearTo()
{
  var from = parseInt(document.search.yearfrom.value);
  var to = parseInt(document.search.yearto.value);
  if (from > to && document.search.yearfrom.value != "" && document.search.yearto.value != "")
    document.search.yearfrom.value = to;
}

function Validate_RetailFrom()
{
  var from = parseInt(document.search.retailfrom.value);
  var to = parseInt(document.search.retailto.value);
  if (from > to && document.search.retailfrom.value != "" && document.search.retailto.value != "")
    document.search.retailto.value = from;
}

function Validate_RetailTo()
{
  var from = parseInt(document.search.retailfrom.value);
  var to = parseInt(document.search.retailto.value);
  if (from > to && document.search.retailfrom.value != "" && document.search.retailto.value != "")
    document.search.retailfrom.value = document.search.retailto.value;
}

// initialize the search form
function Initialize_Form ()
{
  if (document.getElementById('makeid_target'))
  {
    Build_Makes_ID();
    document.getElementById('makeid_target').value = default_makeid;
  }
  else if (document.search.make)
  {
    Build_Makes();
    document.search.make.value = default_make;
  }
  if (document.getElementById('makemodelid_target'))
  {
    Build_Models_ID();
    document.getElementById('makemodelid_target').value = default_makemodelid;
  }
  else if (document.search.model)
  {
    Build_Models();
    document.search.model.value = default_model;
  }
  if (document.search.state)
  {
    Build_States();
    document.search.state.value = default_state;
  }
  if (document.search.area){
    Build_Areas();
    document.search.area.value = default_area;
  }
  if (document.search.groupmembers){
    Build_GroupMembers();
    document.search.clientno.value = default_groupmembers;
  }
  if (document.search.yearfrom) document.search.yearfrom.value = default_yearfrom;
  if (document.search.yearto) document.search.yearto.value = default_yearto;
  if (document.search.retailfrom)document.search.retailfrom.value = default_retailfrom;
  if (document.search.retailto) document.search.retailto.value = default_retailto;
}

// reset the search form
function reset_search_form()
{
  document.search.reset();
  if (document.search.make) Build_Makes();
  if (document.search.model) Build_Models();
  if (document.search.state) Build_States();
  if (document.search.area) Build_Areas();
  if (document.search.area) Build_GroupMembers();
}

function launch_window_fixed(url,name,width,height)
{
  window.open(url,name,"frameborder=no,border=no,hotkeys=no,toolbar=no,status=no,location=no,menubar=no,directories=no,scrollbars=no,resizable=yes,width=" + width +",height=" + height +"");
}

function launch_window(url,name,width,height,scrollbars)
{
  window.open(url,name,"frameborder=no,border=no,hotkeys=no,toolbar=no,status=no,location=no,menubar=no,directories=no,scrollbars=" + scrollbars +",resizable=yes,width=" + width +",height=" + height +"");
}

function fit_window()
{
  window.focus();
  resizeWinTo('level_zero');
}

var garage = new Array();
function garage_checkbox(target,ucnid,current,baseurl,searchdef)
{
  var action = '';
  if(garage[ucnid] == true || (current == 1 && garage[ucnid] !== false))
  {
    garage[ucnid] = false;
    action = 'remove';
  }
  else
  {
    garage[ucnid] = true;
    action = 'add';
  }

  // Append garage.php to the end of the URL if it doesn't exist in baseurl
  if(baseurl.search("garage.php") == -1 && baseurl.search("page=garage") == -1)
  {
    baseurl = baseurl + 'garage.php';
  }

  if(baseurl.match("\\?"))
    var seperator = '&';
  else
    var seperator = '?';

  target.src = baseurl + seperator + 'ucnid=' + ucnid + '&action=' + action + '&rand=' + Math.floor ( Math.random ( ) * 999 + 1 );

}

function getRefToDivMod( divID, oDoc ) {
	if( !oDoc ) { oDoc = document; }
	if( document.layers ) {
		if( oDoc.layers[divID] ) { return oDoc.layers[divID]; } else {
			for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
				y = getRefToDivNest(divID,oDoc.layers[x].document); }
			return y; } }
	if( document.getElementById ) { return oDoc.getElementById(divID); }
	if( document.all ) { return oDoc.all[divID]; }
	return oDoc[divID];
}

function resizeWinTo( idOfDiv ) {
	var div = document.getElementById( idOfDiv);
	if( !div ) { return false; }
	window.resizeTo(div.offsetWidth,div.offsetHeight);
  difwidth = div.offsetWidth - document.body.clientWidth;
  difheight  = div.offsetHeight - document.body.clientHeight;
	window.resizeTo(div.offsetWidth + difwidth,div.offsetHeight + difheight);
}

function resizeWinToOld( idOfDiv ) {
	var oH = getRefToDivMod( idOfDiv ); if( !oH ) { return false; }
	var x = window; x.resizeTo( screen.availWidth, screen.availWidth );
	var oW = oH.clip ? oH.clip.width : oH.offsetWidth;
	var oH = oH.clip ? oH.clip.height : oH.offsetHeight; if( !oH ) { return false; }
	x.resizeTo( oW + 200, oH + 200 );
	var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;
	if( x.innerWidth ) { myW = x.innerWidth; myH = x.innerHeight; }
	else if( d && d.clientWidth ) { myW = d.clientWidth; myH = d.clientHeight; }
	else if( b && b.clientWidth ) { myW = b.clientWidth; myH = b.clientHeight; }
	if( window.opera && !document.childNodes ) { myW += 16; }
	//second sample, as the table may have resized
	var oH2 = getRefToDivMod( idOfDiv );
	var oW2 = oH2.clip ? oH2.clip.width : oH2.offsetWidth;
	var oH2 = oH2.clip ? oH2.clip.height : oH2.offsetHeight;
	x.resizeTo( oW2 + ( ( oW + 200 ) - myW ), oH2 + ( (oH + 200 ) - myH ) );
}