function round_price(price){

 var decimal_places = 2;
 var tmp_value = price;

 tmp_value = (tmp_value*100);
	tmp_value =  Math.round(tmp_value);
	tmp_value = tmp_value.toString()

	tmp_value_pre = tmp_value.substring(0,tmp_value.length-decimal_places);
	tmp_value_dez = tmp_value.substr(tmp_value.length-decimal_places,decimal_places);

	return tmp_value = tmp_value_pre + '.' + tmp_value_dez;

}


function calculate_qtyprice (price_id, qty_id, pfull_id) {
	var price = jQuery('#' +price_id).attr("value");
	var qty = jQuery('#' +qty_id).attr("value");

	jQuery('#' + pfull_id).attr("value", round_price(price * qty));
}


function show_selected (id, parameters)
{
	jQuery('#container_'+id).html('<center><img src="images/ajax-loader.gif" alt="loading"/></center>');
	jQuery.ajax({
	  type: "POST",
	  url: "ajax_request.php",
	  data: parameters,
	  success: function(data) {
		jQuery('#container_'+id).html(data);
	  }
	});
}

function calculate_sales_discount (discount, total) {
	var discount_price = discount.split("%");
	if (discount_price.length > 1) {
		var price = (total * (discount_price[0] / 100));
		total = total - price;
	} else {
		if (discount > 0) {
			total = total - discount;
		}
	}
	jQuery('#total').attr("value", round_price(total))

	var percent = Number(jQuery('#tax_percent').attr("value"));
	jQuery('#tax_clac').attr("value", round_price(total / (100 + percent)  * percent));

//    add_form_hidden('sales_discount', discount);

	var sales_discount = Number(jQuery('#sales_discount').attr("value"));
	var total = round_price(total + Number(jQuery('#shipping_costs').attr("value")));
		jQuery('#tmp_total').attr("value", total);
		jQuery('#sales_discount').attr("value", discount);
}

function add_email (email) {
	if (jQuery('#email_input').attr("value"))
	var value = jQuery('#email_input').attr("value");
	else
	var value = '';

	jQuery('#email_input').attr("value", email);
}

function add_name (name) {
	if (jQuery('#name_input').attr("value"))
	var value = jQuery('#name_input').attr("value");
	else
	var value = '';

	jQuery('#name_input').attr("value", name);
}

function add_street (street) {
	if (jQuery('#street_input').attr("value"))
	var value = jQuery('#street_input').attr("value");
	else
	var value = '';

	jQuery('#street_input').attr("value", street);
}

function add_city (city) {
	if (jQuery('#city_input').attr("value"))
	var value = jQuery('#city_input').attr("value");
	else
	var value = '';

	jQuery('#city_input').attr("value", city);
}

function add_plz (plz) {
	if (jQuery('#plz_input').attr("value"))
	var value = jQuery('#plz_input').attr("value");
	else
	var value = '';

	jQuery('#plz_input').attr("value", plz);
}

function add_country (country) {
	if (jQuery('#country_input').attr("value"))
	var value = jQuery('#country_input').attr("value");
	else
	var value = '';

	jQuery('#country_input').attr("value", country);
}

function add_phone (phone) {
	if (jQuery('#phone_input').attr("value"))
	var value = jQuery('#phone_input').attr("value");
	else
	var value = '';

	jQuery('#phone_input').attr("value", phone);
}

function add_notice (notice) {
	if (jQuery('#notice_input').attr("value"))
	var value = jQuery('#notice_input').attr("value");
	else
	var value = '';

	jQuery('#notice_input').attr("value", notice);
}

function add_form_hidden (name, value) {
	jQuery('<input type="hidden"  id="form_hidden_' + name + '" name="' + name + '" value="' + value +'" >').after("#offers_form");
}

function change_form_hidden (name, value) {
	jQuery('#form_hidden_' + name + '"').attr("value", value);
}


function change_form_type(type) {
	if (type == 'add_offer') {
	}
		var value = jQuery('#tmp_sales_discount').attr("value");

		jQuery('#sales_discount').attr("value", value);
	jQuery('#hidden_type').attr("value", type);

}
function change_pqty (pid, value) {
	jQuery('#pqty_' + pid).attr("value", value);
	calculate_qtyprice('pprice_' + pid, 'pqty_' + pid, 'pfull_id_' + pid);

}
function delete_offers_product (pid) {
	var check = jQuery('#delete_' + pid).attr("value");

	if (check != 1)
		jQuery('<input type="hidden" class="forms_hidden" id="delete_' + pid + '" name="delete[' + pid + ']" value="1" >').after("#hidden_type");
	else
		jQuery('#delete_' + pid).remove();

}


function date_formated(mydates) {
	var p = mydates;
	var dateObjekt = new Date(Date.parse(p));
	var month = dateObjekt.getMonth() +1;
	var day = dateObjekt.getDate();
	var year= dateObjekt.getFullYear() + "-" + month + "-" + day;
	return year;
}

function set_date(calendar_id, date) {
	jQuery('.to_date').attr("value", date);
	calendar(calendar_id, date);
}

function calendar (calendar_id, date) {
	Date.firstDayOfWeek = 7;
	Date.format = 'yyyy-mm-dd';
	var start_date = new Date().asString();
	if (date.lenght == 0)
		date = start_date;

		jQuery(calendar_id)
		.datePicker({inline:false,startDate:start_date}).dpSetSelected(date)
		.bind(
			'dateSelected',
			function(e, selectedDate, $td) {
				jQuery('.to_date').attr("value", date_formated(selectedDate));
			}
		);

}

///////////////////////////////////////////////////
	var options = {
		target:        '#offers_products',
		type: 'post',
		url: 'ajax_request.php'

	};
///////////////////////////////////////////////////

function auto_submit(id) {
		jQuery(id).ajaxSubmit(options);
		return false;

}

jQuery(document).ready(function() {
	jQuery('#busy')
	.ajaxStart(function() {$(this).show();})
	.ajaxStop(function() {$(this).hide();})
	.after('#main');


	jQuery('#offers_form').submit(function() {
			jQuery('#offers_products').html('<center><img src="images/ajax-loader.gif" alt=""/></center>');
	   // jQuery("#name_input").attr("value", encodeURIComponent(jQuery("#name_input").attr("value")));
		jQuery(this).ajaxSubmit(options);
		jQuery(".forms_hidden").remove();
		return false;
	});
	jQuery('.offers_list').submit(function() {
		jQuery('#offers_products').html('<center><img src="images/ajax-loader.gif" alt=""/></center>');
		jQuery(this).ajaxSubmit(options);
		return false;
	});

	var options_cs = {
		target:        '#coustomers_results',
		type: 'post',
		url: 'ajax_request.php'
	};
	jQuery('#quick_find_customers').submit(function() {
		jQuery('#coustomers_results').html('<center><img src="images/ajax-loader.gif" alt=""/></center>');
		jQuery(this).ajaxSubmit(options_cs);
		return false;
	});

});


