//Separated payments and shipping

/**
* Shows payments for selected shipping based on rows' id.
*/
function selectPayments(id_ship){
// alert('tr[id|="pay_'+id_ship+'"]');
 $('#platby').children().hide();
 $('[id^="pay_'+id_ship+'_"]').show();
 input = $('[id^="pay_'+id_ship+'_"]:eq(0)').find("input");
 input.attr('checked', 'checked');
 setTotalPrice(input.val());
}

/**
* Shows shipping for selected payment based on rows' id.
*/
function selectShipping(id_pay){
// alert('tr[id|="pay_'+id_ship+'"]');
 $('#dopravy').children().hide();
 $('[id^="ship_'+id_pay+'_"]').show();
 input = $('[id^="ship_'+id_pay+'_"]:eq(0)').find("input");
 input.attr('checked', 'checked');
 setTotalPrice(input.val());
}

/**
 * Loads payments and shipping based on 'country_code' using
 * template 'dopravy_platby_rows.tpl' and appends payments to element with
 * id = 'pay_elem_id' and shipping to element with id = 'ship_elem_id'.
 * @param country - country code or name
 * @param radio - radio button which must be checked to perform country selection
 */
var pay_index=0;
function loadPayShipSeparately(pay_elem_id, ship_elem_id, country, radio){
 if(radio && !$('#'+radio).attr('checked')){
		return false;
 }
 pay_index++;
 var my_index = pay_index;

 var pay_elem = $('#'+pay_elem_id);
 var ship_elem = $('#'+ship_elem_id);

 pay_elem.find('[id!="platby_loading"]').remove();
 ship_elem.find('[id!="platby_loading"]').remove();

 $('#platby_loading').show();
  var dataToSend = "country=" + country + "&separately=1";
	$.ajax({
		type: "GET",
		url: "/order/loadPayShip/",
		data: dataToSend,
		dataType: 'json',
		success: function(msg){
		if(my_index == pay_index){
      $('#platby_loading').hide();
      pay_elem.append(msg.pay);
      ship_elem.append(msg.ship);
	 }
		}
	}
	);
	return false;
}
/**
 * Loads payu template, inits payU radio button and joins it with payment radio button.
 * Used when payments is first.
 */
function payUInit(type){
  PlnPrintTemplate();
  if($('#payu_radio').attr("checked")) {
    if (type){
      $("input[value='"+type+"']:radio").attr("checked", "checked");
    }
    else{
      $("input[name=pay_type]:first").attr("checked", "checked");
    }
  }
  $('input[name=pay_type]:radio').click(function() {
    $('input[name=payment]:radio').removeAttr("checked");
    $('#payu_radio').attr("checked", "checked");
    $('#payu_radio').click();
    setTotalPrice($('#payu_radio:first').val());
  });
}

/**
 * Loads payu template, inits payU radio button and joins it with payment radio button.
 * Used when shipping is first.
 */
function payUInit2(type){
 PlnPrintTemplate();
  ship = $('input[name=shipping]:radio:checked').val();
 $("input[name=pay_type]").removeAttr("id");
 if($('#payu_radio_'+ship).attr("checked")) {
    if (type){
      $("input[value='"+type+"']:visible").attr("checked", "checked");
    }
    else{
      $("input[name=pay_type]:visible:first").attr("checked", "checked");
    }
  }

  $('input[name=pay_type]:radio').click(function() {
     ship = $('input[name=shipping]:radio:checked').val();

     $('input[name=payship]:radio').removeAttr("checked");
     $('#payu_radio_'+ship).attr("checked", "checked");
     $('#payu_radio_'+ship).click();
     setTotalPrice($('#payu_radio_'+ship).val());
    });
}

/**
 * Set total price, payship included, into element with id="ctp".
 * @param value - selected input value, containing payship identifier (usually called with this.value)
 */
function setTotalPrice(value){
  tp = $("input[name='cp_"+value+"']").val();
  $('#ctp').html(tp);
}
/**
 * Unchecks payu radio button.
 */
function unsetPayU(){
 $('input[name=pay_type]:radio').removeAttr('checked');
}
