var RH_FEE = 1.115; // 5.64 + 2.9 + 2.9
var map = false;

function validate_form() {
  if (!$('general').validate({
    //errorPlacement: function(error, element) {
    //error.insertAfter('#hint');
    //},
    errorLabelContainer: '#hint',
    wrapper: 'strong',
    validMessage: 'dzięki!'
  }).form()) {
    return false;
  }
  return true;
}


function submitForm(id, action) {
  if (action != 'null') $(id).action = action;
  $(id).submit();
}


/**
 $js_c_net = "calcCena(1);";
 $js_c_brut = "calcCena(2);";
 $js_c_vat = "calcCena(3);";
 $js_marza = "calcCena(4);";
 $js_marza_netto = "calcCena(5);";
 $js_marza_brutto = "calcCena(6);";
 */
function calcPrice(opcja, sender) {

  with (document.forms[sender.id]) {
    var net = elements["product[price_netto]"];
    var brut = elements["product[price_brutto]"];

    var vacik = RH_FEE

    var REG_EXP = /[^0-9,.]/gi;
    if (opcja == 1) {
      net.value = net.value.replace(REG_EXP, "");
      net.value = net.value.replace(",", ".");
      brut.value = roundNumber((net.value * vacik), 2);

    } else if (opcja == 2) {
      brut.value = brut.value.replace(REG_EXP, "");
      brut.value = brut.value.replace(",", ".");
      net.value = roundNumber(((brut.value / vacik) * 100) / 100, 2);

    }
  }
}

function roundNumber(num, dec) {
  result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
  return result;
}

function show_search() {
  if ($('search_form').style.display == 'none') {
    option = 'slideDown';
  } else {
    option = 'slideUp';
  }
  $('search_form').visualEffect(option);
}


//document.observe("dom:loaded", function() {
//  $('page-content').observe('click', respondToClick);
//
//  function respondToClick(event) {
//    var element = event.element();
//    alert(event.type)
//    //element.addClassName('active');
//  }
//})

//  new Ajax_upload('#main_picture', {
//    action: '/profile/products/upload',
//    name: 'main_picture',
//    onSubmit : function(file , ext){
//      //if (ext && new RegExp('^(' + allowed.join('|') + ')$').test(ext)){
//      if (ext && /^(jpg|png|jpeg|gif)$/.test(ext)){
//        /* Setting data */
//        this.setData({
//          'authenticity_token': $F(product_form['authenticity_token']),
//          'temp_id': $('product_temp_id').value,
//          'type' : 'main_picture'
//        });
//      //        $$('#example3 .text')[0].update('Uploading ' + file);
//      } else {
//        alert('Can`t upload file')
//        // extension is not allowed
//        //$$('#example3 .text')[0].update('Error: only images are allowed');
//        // cancel upload
//        return false;
//      }
//    },
//    onComplete : function(file, response){
//      $$('#example2 .files')[0].insert(new Element('li').update(file));
//    }
//  });

//function getLocationsByCode(code) {
//  var url = "/get_location/"+code;
//
//  new Ajax.Request(url, {
//    method: 'get',
//    onLoading: function() {
//      $('post_code_city_name').addClassName('busy');
//    },
//    onSuccess: function(response) {
//      //alert(response)
//      $('post_code_city_name').removeClassName('busy');
//    }
//  });
//}

function getLocationsByCode(code, prefix) {
  var url = '/get_location/' + code + '/' + prefix;

  new Ajax.Request(url, {
    method: 'get',
    onLoading: function() {
      $(prefix + '_city_name').addClassName('busy');
    },
    onSuccess: function(response) {
      //alert(response)
      $(prefix + '_city_name').removeClassName('busy');
    }
  });
}


function toggle_input(id, sender) {

  if ($(id).type != 'text') {
    var txtCtrl = document.createElement('input');
    txtCtrl.setAttribute('type', 'text');
    txtCtrl.setAttribute('id', id);
    txtCtrl.setAttribute('size', $(id).size);
    txtCtrl.setAttribute('value', $(id).value);
    txtCtrl.setAttribute('name', $(id).name);
    txtCtrl.setAttribute('class', $(id).className);
    $(id).replace(txtCtrl);
    $(sender).replace('');
  }


  //if (typeof appendTextInput.count == 'undefined') appendTextInput.count = 1;
  //if (document.getElementById) {
  //  var txtCtrl = document.createElement('input');
  //  var feed = document.createElement('br');
  //  txtCtrl.setAttribute('type', 'text');
  //  txtCtrl.setAttribute('id', 'text' + appendTextInput.count++);
  //  txtCtrl.setAttribute('size', '12');
  //  txtCtrl.setAttribute('value', txtCtrl.id);
  //  formObj.appendChild(feed);
  //  formObj.appendChild(txtCtrl);
  //} else if (document.all) {
  //  var HTML = '<br /><input type="text" size="12" name="text' + appendTextInput.count;
  //  HTML += '" value="text' + appendTextInput.count++ + '">';
  //  formObj.insertAdjacentHTML('beforeEnd', HTML);
  //}


}

//Event.observe(window, 'load', function() {

//  if ($('user_post_code') != null) {
//    $('user_code_code').observe('change', function(event) {
//      getLocationsByCode(Event.element(event).value, 'user');
//    });
//  }
//});


Event.observe(window, 'load', function() {

  if ($('user_post_code') != null) {
    $('user_post_code').observe('change', function(event) {
      getLocationsByCode(Event.element(event).value, 'user');
    });
  }
  if ($('station_post_code') != null) {
    $('station_post_code').observe('change', function(event) {
      getLocationsByCode(Event.element(event).value, 'station');
    });
  }

  if ($('post_code_code') != null) {
    $('post_code_code').observe('change', function(event) {
      getLocationsByCode(Event.element(event).value);
    });
    if ($('post_code_range').value != '') {
      getLocationsByCode($('post_code_code').value);
    }
  }


  if ($('search_form_range') != null) {

    var s2 = new Control.Slider('r_scroll', 'r_scroller', {
      axis:'horizontal',
      minimum: 1,
      maximum: 15,
      increment: 1,
      values: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
      range: $R(1, 15),
      sliderValue: $('search_form_range') == null ? 1 : $('search_form_range').value,

      onChange: function(value) {
        $('search_form_range').value = value;
        if (map != false) map.setObserver(null, value, null);
      },
      onSlide: function(value) {
        $('search_form_range').value = value;
      }
    });
  }
});

function change_search_tab(tab) {
  tab_id = 'tab_' + tab;
  field_id = 'search_' + tab;
  $('search').select('fieldset').each(function(selector) {
    selector.style.display = 'none';
    if (selector.id == field_id) {
      selector.style.display = 'block';
    }
  });

  $('search').select('h3').each(function(selector) {
    selector.removeClassName('active');
    if (selector.id == tab_id) {
      selector.addClassName('active');
    }
  });
}

var actual_tab = '1';
//Event.observe(window, 'load', function() {
//
//});

function change_time() {
  $('intro_tab'+ actual_tab).hide();
  $('tab'+actual_tab).removeClassName('on');

  actual_tab++;
  if (actual_tab > 3) actual_tab = 1;

  $('tab'+actual_tab).addClassName('on');
  $('intro_tab'+ actual_tab).show();

  change_item = setTimeout('change_time()', 10000);
}
function reset_intro() {
  clearTimeout(change_item);
  change_item = setTimeout('change_time()', 10000);
}