// JavaScript Document

// to check if the user entered something in the desired search box
function validate(objElement)
{
  var acceptableLength = 2;
  if(objElement == 'keyword'){ acceptableLength = 3; }
  if(document.getElementById(objElement).value.length < acceptableLength)
  {
    alert('Please enter a valid search item. '+acceptableLength+' or more characters');
    return false;
  }
  else
  {
    return true;
  }
}

// function to redirect user to the sub-category page
function redirect(location,categoryId,isIframe)
{
  if(isIframe == true) { var addToUrl = '&show=iframe'; } else { var addToUrl = ''; }
  
  companyId = document.getElementById('manufacturers').value;
  if(companyId == 0)
  {
    var url = location+'/category.php?catid='+categoryId+addToUrl;
  }
  else
  {
    var url = location+'/category.php?catid='+categoryId+'&compid='+companyId+addToUrl;
  }
  
  goLink(url);
}

// function to redirect user to appropriate page when selecting a value from any dropdown menu
function searchRedirect(location,objElement,isIframe)
{
  if(isIframe == true) { var addToUrl = '&show=iframe'; } else { var addToUrl = ''; }
  
  var id = document.getElementById(objElement).value;
  if(id != 0)
  {
    switch(objElement)
    {
      case 'categories':
        var url = location+'/category.php?catid='+id+addToUrl;
      break;
      case 'companies':
        var url = location+'/brandsearch.php?detailedlisting='+id+'&catid=4048'+addToUrl;
      break;
    }
  }
  else
  {
    var url = window.location.href;
  }
  
  goLink(url);
}

// function to redirect the user once a manufacturer is chosen from the parts menu
function manRedirect(location,partNumber,isIframe)
{
  if(isIframe == true) { var addToUrl = '&show=iframe'; } else { var addToUrl = ''; }
  var id = document.getElementById('manufacturers').value;
  if(id == 0)
  {
    var url = location+'/search.php?part='+partNumber+'&submitPart=Go%21'+addToUrl;
  }
  else
  {
    var url = location+'/search.php?part='+partNumber+'&manufacturer='+id+'&submitManufacturer=Go%21'+addToUrl;
  }
  
  goLink(url);
}

// function to redirect the user once a manufacturer is chosen from the parts menu
function manRedirect2(partNumber,submitType)
{
  switch(submitType)
  {
    case 'subYmmMan':
      var submit = 'submitYmm';
    break;
    case 'submitManufacturer':
      var submit = 'submitPart';
    break;
  }
  
  var id = document.getElementById('manufacturers').value;
  if(id == 0)
  {
    window.location = 'search.php?part='+partNumber+'&'+submit+'=Go%21';
  }
  else
  {
    window.location = 'search.php?part='+partNumber+'&manufacturer='+id+'&'+submitType+'=Go%21';
  }
}

// function to create the pop-ups
function popup(URL,box)
{
  switch(box)
  {
    case 'offer':
      var width = '400';
      var height = '325';
    break;
    case 'review':
      var width = '700';
      var height = '450';
    break;
  }
  window.open(URL,"creation","width="+width+",height="+height+",toolbar=0,scrollbars=auto,location=0,status=0,menubar=0,resizeable=yes");
}

// function to activate/de-activate the stars for the rating
function stars(starNum,location)
{  
  // active star image
  active = new Image();
  active.src = location+"ART/active_star.gif";
  
  // inactive star image
  inactive = new Image();
  inactive.src = location+"ART/inactive_star.gif";
  
  switch(starNum)
  {
    case 1:
      document.getElementById('star1').src = active.src;
      document.getElementById('star2').src = inactive.src;
      document.getElementById('star3').src = inactive.src;
      document.getElementById('star4').src = inactive.src;
      document.getElementById('star5').src = inactive.src;
    break;
    case 2:
      document.getElementById('star1').src = active.src;
      document.getElementById('star2').src = active.src;
      document.getElementById('star3').src = inactive.src;
      document.getElementById('star4').src = inactive.src;
      document.getElementById('star5').src = inactive.src;
    break;
    case 3:
      document.getElementById('star1').src = active.src;
      document.getElementById('star2').src = active.src;
      document.getElementById('star3').src = active.src;
      document.getElementById('star4').src = inactive.src;
      document.getElementById('star5').src = inactive.src;
    break;
    case 4:
      document.getElementById('star1').src = active.src;
      document.getElementById('star2').src = active.src;
      document.getElementById('star3').src = active.src;
      document.getElementById('star4').src = active.src;
      document.getElementById('star5').src = inactive.src;
    break;
    case 5:
      document.getElementById('star1').src = active.src;
      document.getElementById('star2').src = active.src;
      document.getElementById('star3').src = active.src;
      document.getElementById('star4').src = active.src;
      document.getElementById('star5').src = active.src;
    break;
  }
}
