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

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);
//}

Event.observe(window, 'load', function() {
//  if ($('search_form') != null) {
//    $('search_form').observe('change', function(event) {
//      getLocationsByCode(Event.element(event).value);
//    });
//    if ($('search_form').value != '') {
//      getLocationsByCode($('search_form_post_code').value);
//    }
//  }

  if ($('search_form_range') != null) {
    var s2 = new Control.Slider('r_scroll', 'r_scroller', {
      axis:'horizontal',
      minimum: 2,
      maximum: 230,
      increment: 5,
      values: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200],
      range: $R(10, 200),
      sliderValue: $('search_form_range') == null ? 10 : $('search_form_range').value,

      onChange: function(value) {
        $('search_form_range').value = value;
      },
      onSlide: function(value) {
        $('search_form_range').value = value;
      }
    });
  }
});

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_post_code').observe('change', function(event) {
      getLocationsByCode(Event.element(event).value, 'user');
    });
  }
  if ($('product_post_code') != null) {
    $('product_post_code').observe('change', function(event) {
      getLocationsByCode(Event.element(event).value, 'product');
    });
  }

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

//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));
//    }
//  });
