$(document).ready(function(){

});

function UpdateForm(objects) {
  document.forms['add_frm']['fetch_meta'].disabled = false;
  for (var i in objects) {
    if (i != "toJSONString") {
      if (document.forms['add_frm'][i]) {
        document.forms['add_frm'][i].value = objects[i];
      }
    }
  }
}

function FetchMeta() {
  var url = document.forms['add_frm']['url'].value;
  document.forms['add_frm']['fetch_meta'].disabled = true;
  
  $.ajax({
    data : 'cmd=fetch_meta&url=' + url,
    dataType:"json",
    url:"ajax.php",
    success: UpdateForm
  });
}

function UpdateSubCategory(objects) {
  document.getElementById(objects['div_tag']).innerHTML = objects['select_box'];
}

function FetchSubCategory(cat, selected_cat, div_tag) {
  var address_url = location.href;
  var get_split = address_url.split("/");
  var num_split = get_split.length;
  var last_folder = get_split[(num_split - 2)];
  var set_url = '';
  if (last_folder == 'cp'){ set_url = '../'; } 
  document.getElementById(div_tag).innerHTML = "loading..."+last_folder;
  $.ajax({
    data : 'cmd=fetch_sub_category&cat=' + cat + "&selected_cat=" + selected_cat+ "&div_tag=" + div_tag,
    dataType:"json",
    url:"ajax.php",
    success: UpdateSubCategory
  });
  
}

function UpdatePricing(objects) {
  $("#listing_type_div").html(objects);
  CouponChecking();
}


function FetchPricing() {
  
  cat1 = add_cat1;
  cat2 = add_cat2;
  
  var periodprice = $("[name=periodprice]").find("option:selected").val();
  
  if (document.forms['add_frm']['periodprice']) {
    var periodprice = document.forms['add_frm']['periodprice'].options[document.forms['add_frm']['periodprice'].selectedIndex].value;
  }
  if ($("#listing_type_div")){
  $("#listing_type_div").html('Loading..');
  $.ajax({
    data : 'cmd=fetch_pricing&cat=' + cat + "&cat1=" + cat1 + "&cat2=" + cat2 + "&periodprice=" + periodprice,
    dataType:"json",
    url:"ajax.php",
    success: UpdatePricing
  });
  }
  
}

/*-------------*/
function GetAliasById(nameElement){
  var doc = window.document.getElementById(nameElement);
  return doc;
}

function CreateElement(object,element_id) {
  var ni = document.getElementById('myDiv');
  var numi = document.getElementById('theValue');
  var num = (document.getElementById('theValue').value -1)+ 2;
  numi.value = num;
  var newdiv = document.createElement('div');
  var divIdName = 'my'+num+'Div';
  newdiv.setAttribute('id',divIdName);
  newdiv.innerHTML = 'Element Number '+num+' has been added! <a href=\'#\' onclick=\'removeElement('+divIdName+')\'>Remove the div "'+divIdName+'"</a>';
  ni.appendChild(newdiv);
}

function CouponChecking() {
  var periode_price       = document.forms['add_frm']['periodprice'];
  var periode_price_value = periode_price.options[periode_price.selectedIndex].value;
  var coupon_code = $("#coupon_code").val();
  
  if (coupon_code){
  $.ajax({
    data : 'cmd=coupon_checking&coupon_code=' + coupon_code +'&periode_price='+periode_price_value,
    dataType:"json",
    url:"ajax.php",
    success: UpdateDiscount
  });
  }
  
}

function UpdateDiscount(object){

  var discount_price;
  var new_periodprice; 
  var new_price           = 0;
  var hidden_price        = document.getElementById("hidden_periodprice_id"); //= GetAliasById("hidden_periodprice_id");
  var periode_price       = document.forms['add_frm']['periodprice'];
  var periode_price_value = periode_price.options[periode_price.selectedIndex].value;
  var get_price_value     = periode_price_value.split('|');
  var message;
  
  if (object.is_valid ){
    if (object.is_percent == 1){
      discount_price  = (object.coupon_price/100);
      new_price       = parseFloat(get_price_value[2]) - ( parseFloat(get_price_value[2]) * parseFloat(discount_price));
    }else{
      new_price       = (parseFloat(get_price_value[2]) -  parseFloat(object.coupon_price));
    }
    
    if (!isNaN(new_price)){
      new_price = (new_price<=0) ? parseFloat("0.00") : new_price.toFixed(2);
      new_periodprice = get_price_value[0] +'|'+ get_price_value[1] +'|'+ new_price+'|'+object.coupon_value;
      
      if (hidden_price){
        hidden_price.setAttribute("value",new_periodprice);
      }else{
        var hidden_periodprice = document.createElement('input');
        hidden_periodprice.setAttribute('type','hidden');
        hidden_periodprice.setAttribute('id','hidden_periodprice_id');
        hidden_periodprice.setAttribute('name','hidden_periodprice');
        hidden_periodprice.setAttribute('value',new_periodprice);
        GetAliasById("hidden_price").appendChild(hidden_periodprice);
      }
      message = object.message + ' '+ ((object.is_valid) ? object.currency_symbol+' '+ new_price : '') ;
    }else{
      new_price = 0;
      hidden_price.setAttribute("value","");
      message = '';
    }
  }else{
    if (hidden_price) hidden_price.setAttribute("value","");
    message = object.message;
  }
   GetAliasById("price_message_place").innerHTML = message ;
}
