// removes all options from the select box
HTMLSelectElement.prototype.clear = function(){
 while (this.length > 0) {
  this.remove(0);
 }
}

// changes the selection to a specified position
HTMLSelectElement.prototype.select = function(position){
 if(position > this.length-1) position = this.length-1;

 this.selectedIndex = position;
}

// changes the selection to a specific id
HTMLSelectElement.prototype.selectID = function(id){
 var idx = 0;
 for (idx=0;idx<this.length;idx++){
  if(this.options[idx].value == id){
   this.select(idx);
   break;
  }
 }
}

// changes the selection to a specific text value
HTMLSelectElement.prototype.selectElementByText = function(textValue){
 var idx = 0;
 for (idx=0;idx<this.length;idx++){
  if(this.options[idx].text == textValue){
   this.select(idx);
   break;
  }
 }
}


// changes the selection to a specific text value
HTMLSelectElement.prototype.textValue = function(){
 return this.options[selectedIndex].text;
}



// clears a select box and optionaly inserts a first object
HTMLSelectElement.prototype.init = function(id,textValue){
 this.clear();
 if(textValue){
  this.options[0] = new Option(textValue,id);
 }
}


HTMLSelectElement.prototype.insertOption = function(id,textValue,position){
 var newOpt = new Option(textValue,id);

 if(!isNaN(position) && position < this.length-1 && position >= 0) {
  this.add(newOpt,this.options[position]);
 } else {
  this.add(newOpt,null);
 }
}

function SubmitProductSearchForm(s,l,c,p)
{
  if (document.ProductSearchForm.selectedForm.value == "")
  {
    document.ProductSearchForm.action = '/termekek';
  } else {
    var sel = document.ProductSearchForm.selectedForm.value;

    switch (sel) {
        case  "1":
            if (s == 0 || p == 21) document.ProductSearchForm.action = '/kezikonyv';
            else if (s == 1) {
              if (l != 0) document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+l+'&tartalom='+sel;
              else document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+sel;
            } else if (s == 2) {
              if (p != 12) document.ProductSearchForm.action = '/kezikonyv';
              else {
                if (c != 0) document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+l+'&tartalom='+sel;
                else document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+sel;
              }
            }
          break;
        case  "2":
            if (s == 0 || p == 21) document.ProductSearchForm.action = '/cd-kiadvany';
            else if (s == 1) {
              if (l != 0) document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+l+'&tartalom='+sel;
              else document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+sel;
            } else if (s == 2) {
              if (p != 12) document.ProductSearchForm.action = '/cd-kiadvany';
              else {
                if (c != 0) document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+l+'&tartalom='+sel;
                else document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+sel;
              }
            }
          break;
        case  "3":
            if (s == 0 || p == 21) document.ProductSearchForm.action = '/ingyenes-hirlevelek';
            else if (s == 1) {
              if (l != 0) document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+l+'&tartalom='+sel;
              else document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+sel;
            } else if (s == 2) {
              if (p != 12) document.ProductSearchForm.action = '/ingyenes-hirlevelek';
              else {
                if (c != 0) document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+l+'&tartalom='+sel;
                else document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+sel;
              }
            }
          break;
        case  "4":
            if (s == 0 || p == 21) document.ProductSearchForm.action = '/e-dokumentum';
            else if (s == 1) {
              if (l != 0) document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+l+'&tartalom='+sel;
              else document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+sel;
            } else if (s == 2) {
              if (p != 12) document.ProductSearchForm.action = '/e-dokumentum';
              else {
                if (c != 0) document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+l+'&tartalom='+sel;
                else document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+sel;
              }
            }
          break;
        case  "5":
            if (s == 0 || p == 21) document.ProductSearchForm.action = '/online-konyvek';
            else if (s == 1) {
              if (l != 0) document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+l+'&tartalom='+sel;
              else document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+sel;
            } else if (s == 2) {
              if (p != 12) document.ProductSearchForm.action = '/online-konyvek';
              else {
                if (c != 0) document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+l+'&tartalom='+sel;
                else document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+sel;
              }
            }
          break;
        case  "6":
            if (s == 0 || p == 21) document.ProductSearchForm.action = '/kepzesnaptar';
            else if (s == 1) {
              if (l != 0) document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+l+'&tartalom='+sel;
              else document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+sel;
            } else if (s == 2) {
              if (p != 12) document.ProductSearchForm.action = '/kepzesnaptar';
              else {
                if (c != 0) document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+l+'&tartalom='+sel;
                else document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+sel;
              }
            }
          break;
        case  "7":
            if (s == 0 || p == 21) document.ProductSearchForm.action = '/kepzesnaptar';
            else if (s == 1) {
              if (l != 0) document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+l+'&tartalom='+sel;
              else document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+sel;
            } else if (s == 2) {
              if (p != 12) document.ProductSearchForm.action = '/kepzesnaptar';
              else {
                if (c != 0) document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+l+'&tartalom='+sel;
                else document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+sel;
              }
            }
          break;
        case  "8":
            if (s == 0 || p == 21) document.ProductSearchForm.action = '/szoftver-szingularia';
            else if (s == 1) {
              if (l != 0) document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+l+'&tartalom='+sel;
              else document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+sel;
            } else if (s == 2) {
              if (p != 12) document.ProductSearchForm.action = '/szoftver-szingularia';
              else {
                if (c != 0) document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+l+'&tartalom='+sel;
                else document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+sel;
              }
            }
          break;
        case "10":
            if (s == 0 || p == 21) document.ProductSearchForm.action = '/ragasztott-konyv';
            else if (s == 1) {
              if (l != 0) document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+l+'&tartalom='+sel;
              else document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+sel;
            } else if (s == 2) {
              if (p != 12) document.ProductSearchForm.action = '/ragasztott-konyv';
              else {
                if (c != 0) document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+l+'&tartalom='+sel;
                else document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+sel;
              }
            }
          break;
        case "11":
            if (s == 0 || p == 21) document.ProductSearchForm.action = '/online-hirlevelek';
            else if (s == 1) {
              if (l != 0) document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+l+'&tartalom='+sel;
              else document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+sel;
            } else if (s == 2) {
              if (p != 12) document.ProductSearchForm.action = '/online-hirlevelek';
              else {
                if (c != 0) document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+l+'&tartalom='+sel;
                else document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+sel;
              }
            }
          break;
        case "12":
            if (s == 0 || p == 21) document.ProductSearchForm.action = '/online-kiadvanyok';
            else if (s == 1) {
              if (l != 0) document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+l+'&tartalom='+sel;
              else document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+sel;
            } else if (s == 2) {
              if (p != 12) document.ProductSearchForm.action = '/online-kiadvanyok';
              else {
                if (c != 0) document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+l+'&tartalom='+sel;
                else document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+sel;
              }
            }
          break;
        case "14":
            if (s == 0 || p == 21) document.ProductSearchForm.action = '/kapcsos-konyv';
            else if (s == 1) {
              if (l != 0) document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+l+'&tartalom='+sel;
              else document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+sel;
            } else if (s == 2) {
              if (p != 12) document.ProductSearchForm.action = '/kapcsos-konyv';
              else {
                if (c != 0) document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+l+'&tartalom='+sel;
                else document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+sel;
              }
            }
          break;
        case "15":
            if (s == 0 || p == 21) document.ProductSearchForm.action = '/nyomtatott-hirlevel';
            else if (s == 1) {
              if (l != 0) document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+l+'&tartalom='+sel;
              else document.ProductSearchForm.action = '/?fejezet=1&alfejezet='+sel;
            } else if (s == 2) {
              if (p != 12) document.ProductSearchForm.action = '/nyomtatott-hirlevel';
              else {
                if (c != 0) document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+l+'&tartalom='+sel;
                else document.ProductSearchForm.action = '/?fejezet=2&alfejezet='+sel;
              }
            }
          break;
    }
  }
  document.ProductSearchForm.submit();
}


function gotoSearch()
{
  if ($('searchType'))
  {
    var type = $('searchType');
    switch (type.value)
    {
      case '1': break;
      case '2':
        qWin = window.open('http://wm.hu/search.php?host=dashofer&where=hun&word=' + $('q').value, 'qWin');
        if (qWin)
        {
          qWin.focus();
        }
        return false;
      break;
      case '3':
        qWin = window.open('http://www.google.com/search?hl=hu&q=' + $('q').value, 'qWin');
        if (qWin)
        {
          qWin.focus();
        }
        return false;
        break;
    }
  }
  return true;
}

function gotoEnews()
{
  if ($('newsletter'))
  {
    var nlWin = window.open('http://www.dashofer.hu/?product=' + $('newsletter').value, 'nlWin');
    if (nlWin)
    {
      nlWin.focus();
    }
  }
  return false;
}

function gotoPortal()
{
  if ($('goto'))
  {
    var pWin = window.open($('goto').value, 'pWin');
    if (pWin)
    {
      pWin.focus();
    }
  }
  return false;
}

function toggle(id)
{
  var div = $(id);
  if (div.style.display == 'inline')
  {
    div.style.display = 'none';
  }
  else
  {
    div.style.display = 'inline';
  }
  return false;
}

function toggleList(id)
{
  var div = $(id);
  if (div.style.display == 'none' || div.style.display == '')
  {
    div.style.display = 'block';
  }
  else
  {
    div.style.display = 'none';
  }
  return false;
}

function openLinkUrlAsPrintWindow()
{
  var pWin = window.open(this.href, 'printWindow', 'toolbar=1,location=0,width=790,height=600,resizable=1,scrollbars=1');
  if (pWin)
  {
    pWin.focus();
    return false;
  }
  return true;
}

function openLegalNotice()
{
  var pWin = window.open(this.href, 'legalWindow', 'toolbar=0,location=0,width=430,height=410,resizable=1,scrollbars=0');
  if (pWin)
    {
    pWin.focus();
    return false;
    }
  return true;
}

function companyType_load()
{
  if ( $('company_type') )
  {
    var v = $('company_type');
    alert(v.value);
  }
}

function companyTypeHandler()
{
	if ($('company_type'))
	{

  var v = $('company_type');

  if ( v.value == 16 )
  {
    $("company_branche").selectID('TEV999');
    $("fixed_job_function").selectID('MAGAN');
    $("job_position").selectID('MAGAN');
    $("company_branche").up('tr').hide();
    $("fixed_job_function").up('tr').hide();
    $("job_position").up('tr').hide();
  }
  else
  {
    $("company_branche").select(0);
    $("fixed_job_function").select(0);
    $("job_position").select(0);

    $("company_branche").up('tr').show();
    $("fixed_job_function").up('tr').show();
    $("job_position").up('tr').show();
  }
  	}
}

function SeminarPriceReCount()
{
  var priceone  = $("one_price");
  var price     = $("price");
  var multi     = $("multi");
  var count     = $("count");
  var v         = "";
  var va        = count.value;

  // alert (priceone.value + "|" + price.value + "|" + multi.value + "|" + count.value);
  for (i = 0; i < va.length; i++)
  {
    ch = va.charAt(i);
    if ((ch >= 0) && (ch <= 9))
    {
      v = v + ch;
    }
  }

  if (va != v)
  {
    count.value = v;
  }

  var i         = multi.value;
  var pv        = count.value * priceone.value;
  var total     = count.value;
  var discount  = 0;
  // if (total < 3) discount = 0;
  // else if(total < 6) discount = 10;
  // else discount = 20;
  // if(discount != 0){
  //  pv = Math.floor(pv*((100-discount)/100));
  // }
  // alert(priceone.value +"|"+ price.value +"|"+ multi.value +"|"+ count.value);

  pv = pv.toFixed(2);
  price.value = pv.replace(".",",");
}

function commonInit()
{
  if ($('goto'))
  {
    Event.observe($('goto'), 'change', gotoPortal);
    Event.observe($('smtPortal'), 'click', gotoPortal);;
  }

  if ($('newsletter'))
  {
    Event.observe($('newsletter'),    'change', gotoEnews);
    Event.observe($('smtNewsletter'), 'click',  gotoEnews);
  }

  if ($('smtSearch'))       Event.observe($('smtSearch'),     'click', gotoSearch);
  if ($('docprintimg'))     Event.observe($('docprintimg'),   'click', openLinkUrlAsPrintWindow);
  if ($('docprintlink'))    Event.observe($('docprintlink'),  'click', openLinkUrlAsPrintWindow);
  if ($('legalnotice'))     Event.observe($('legalnotice'),   'click', openLegalNotice);

  if ($('reflexVoteBox'))   $('reflexVoteBox').innerHTML = $('votebox').innerHTML;

  if ($('formTypeSelect'))  Event.observe($('formTypeSelect'),  'change', SubmitProductSearchForm);
  if ($('formSortSelect'))  Event.observe($('formSortSelect'),  'change', SubmitProductSearchForm);


  if ($('company_type'))  Event.observe($('company_type'),  'change', companyTypeHandler);

  if (count = $("count"))
  {
    Event.observe(count, 'change', SeminarPriceReCount);
    Event.observe(count, 'keyup', SeminarPriceReCount);
    SeminarPriceReCount();
  }

  companyTypeHandler();
}

function createXMLHttp()
{
  var request = null;
  if (window.XMLHttpRequest) { request = new XMLHttpRequest(); }
  else if (typeof(ActiveXObject != undefined)) { request = ActiveXObject('Microsoft.XMLHTTP'); }
  return request;
}

Event.observe(window, 'load', commonInit);

function hidePromoLayer()
{
  var pL = $('promoLayer');
  pL.style.display = 'none';
}

function freshAndFavSelect(id)
{
  var fresh = $("freshArticleBox");
  var fav   = $("favArticleBox");
  if (id == 1) {
    fresh.style.display = 'inline';
    fav.style.display = 'none';
  } else {
    fav.style.display = 'inline';
    fresh.style.display = 'none';
  }
}

function jobAndAdSelect(id)
{
  var job = $("jobBox");
  var ad = $("adBox");
  if (id == 1) {
    job.style.display = 'inline';
    ad.style.display = 'none';
  } else {
    ad.style.display = 'inline';
    job.style.display = 'none';
  }
}
