function order_append(userid, staffid){
	$.ajax({
		url		:	"/order/appendorder",
		type	: 	"POST",
		data	:	({
			staffid : staffid,
			userid : userid,
			use_ajax	: true
		}),
		datatype: 'html',
		success : function(data){
			$('#orderlist').html(data);
		}
	});
}
function order_item_add(orderid){
	$.ajax({
		url		:	"/article/articletoorder",
		type	: 	"POST",
		data	:	({
			orderid		: orderid,
			use_ajax	: true,
			showWindow	: true
		}),
		datatype: 'html',
		success : function(data){
			$('body').append(data);
			$("#window[name='articletoorderwindow']").fadeIn();
		}
	})
}

function order_reload(userid){
	$.ajax({
		url		:	"/order/reload",
		type	: 	"POST",
		data	:	({
			userid : userid,
			use_ajax	: true
		}),
		datatype: 'html',
		success : function(data){
			$('#orderlist').html(data);
		}
	});
} 


function order_newtodo(orderid){
	$.ajax({
		url		:	"/order/newtodo",
		type	: 	"POST",
		data	:	({
			orderid 	: orderid,
			use_ajax	: true
		}),
		datatype: 'html',
		success : function(data){
			$('#order_todolist').html(data);
		}
	});
}

function order_todo_change(orderid, erledigt, todoid){
	var todo = $('#order_todo_'+todoid).val();

	$.ajax({
		url		:	"/order/changetodo",
		type	: 	"POST",
		data	:	({
			todoid	 	: todoid,
			orderid	 	: orderid,
			erledigt	: erledigt,
			todo		: todo,
			use_ajax	: true
		}),
		datatype: 'html',
		success : function(data){
			$('#order_todolist').html(data);
		}
	});
}

function order_todo_remove(orderid, todoid){
	$.ajax({
		url		:	"/order/removetodo",
		type	: 	"POST",
		data	:	({
			todoid	 	: todoid,
			orderid	 	: orderid,
			use_ajax	: true
		}),
		datatype: 'html',
		success : function(data){
			$('#order_todolist').html(data);
		}
	});
}


function order_wizard_search(){
	$.ajax({
		url		:	"/order/lookupuser",
		type	: 	"POST",
		data	:	({
			firma		: $('.zend_form_wizard #firma').val(),
			nachname	: $('.zend_form_wizard #nachname').val(),
			plz			: $('.zend_form_wizard #plz').val(),
			ort			: $('.zend_form_wizard #Ort').val(),
			use_ajax	: true
		}),
		datatype: 'html',
		success : function(data){
			$('#order_wizard_lookupuser').html(data);
		}
	})
}

function order_printorder(orderid){
	$.ajax({
		url		:	"/order/printorder",
		type	: 	"POST",
		data	:	({
			orderid		: orderid,
			use_ajax	: true,
			showWindow	: true
		}),
		datatype: 'html',
		success : function(data){
			$('body').append(data);
			$("#window[name='pdfwindow']").fadeIn();				
		}
	})
}
function order_printinvoice(invoiceid){
	$.ajax({
		url		:	"/order/printinvoice",
		type	: 	"POST",
		data	:	({
			invoiceid	: invoiceid,
			use_ajax	: true,
			showWindow	: true
		}),
		datatype: 'html',
		success : function(data){
			$('body').append(data);
			$("#window[name='pdfwindow']").fadeIn();				
		}
	})
}
function order_changestatus(orderid, pos, newstatus){
	$.ajax({
		url		:	"/order/changeorderstatus",
		type	: 	"POST",
		data	:	({
			use_ajax	: true,
			orderid		: orderid,
			pos			: pos,
			newstatus 	: newstatus
		}),
		datatype: 'html',
		success : function(data){
		}
	})
}


function order_newStatusDefinition(){
	$.ajax({
		url		:	"/order/newstatusdefinition",
		type	: 	"POST",
		data	:	({
			use_ajax	: true,
			showWindow	: true
		}),
		datatype: 'html',
		success : function(data){
			$('body').append(data);
			$("#window[name='newstatusdefinitionwindow']").fadeIn();
		}
	})
}

function orders_search(){
	var statussearch = new Array();
	var a = "";
	
	for(i = 0; i< 20; i++){
		statussearch[i] = '_';
		a = ""+i;
		if ($("input[name='order_statussearch_"+a+"']:checked").length) 
			if ($("input[name='order_statussearch_"+a+"']:checked").val() == 0) {
				statussearch[i] = '_';
			}
			else 
			if ($("input[name='order_statussearch_"+a+"']:checked").val() == 1) {
				statussearch[i] = 0;
			}
			else 
			if ($("input[name='order_statussearch_"+a+"']:checked").val() == 2) {
				statussearch[i] = 1;
			}
	}
	$.ajax({
		url		:	"/order/lookuporder",
		type	: 	"POST",
		data	:	({
			statussearch 			: statussearch,
			searchtext				: $('#searchtext').val(),
			kk_email				: $('.zend_form_ordersearch #kk_email').val(),
			kk_antrag				: $('input[name=kk_antrag]:checked"').val(),
			domain					: $('.zend_form_ordersearch #domain').val(),
			use_custom_template		: $('input[name=use_custom_template]:checked').val(),
			want_design_help		: $('input[name=want_design_help]:checked').val(),
			want_design_transfer	: $('input[name=want_design_transfer]:checked').val(),
			use_ajax	: true
		}),
		datatype: 'html',
		success : function(data){
			$('#user_searchcontainer').html(data);
		}
	})
}

function order_show_orderitems(orderid){
	$.ajax({
		url		:	"/order/showorderitems",
		type	: 	"POST",
		data	:	({
			orderid		: orderid,
			use_ajax	: true,
			showWindow	: true
		}),
		datatype: 'html',
		success : function(data){
			$('body').append(data);
			$("#window[name='showorderitemswindow']").fadeIn();
		},
		error : function(xhr, ajaxOptions, thrownError){
		}
	})
}

function order_wizard_save(orderid){
	goTo('/order/wizard/pos/4/orderid/'+orderid);
}

function order_wizard_setuserid(userid){
	goTo('/order/wizard/pos/2/userid/'+userid);
}


function order_getmaxamount(orderid, referenceid, exceptitem, element){
	var curanzahl = $(element).val();
	var sum = 0;
	
	$.ajax({
		url		:	"/order/getmaxamount",
		type	: 	"POST",
		data	:	({
			orderid		: orderid,
			referenceid	: referenceid,
			exceptitem	: exceptitem,
			use_ajax	: true,
			showWindow	: true
		}),
		datatype: 'html',
		success : function(data){
			var arr = data.split(',');
			sum = parseInt(arr[0])+parseInt(curanzahl);
			if(sum > parseInt(arr[1])){
				$(element).val(parseInt(arr[1])-parseInt(arr[0]));
			}			
			order_wizard_item_change(exceptitem, orderid);			
		}
	})
}

function order_addarticletoorder(articleid, orderid,vk_single, vk_mtl){
	$.ajax({
		url		:	"/order/addarticletoorder",
		type	: 	"POST",
		data	:	({
			orderid		: orderid,
			articleid	: articleid,
			vk_single	: vk_single,
			vk_mtl		: vk_mtl,
			use_ajax	: true
		}),
		datatype: 'html',
		success : function(data){
			$('#order_items').html(data);
			closeWindow('articletoorderwindow');
			
		}
	})
}

function order_item_change(id, orderid){
	if($('#order_item_anzahl_'+id).val() != '' && $('#order_item_rabatt_'+id).val() != '')
	delay(function(){
		$.ajax({
			url		:	"/order/changeorderitem",
			type	: 	"POST",
			data	:	({
				id			: id,
				orderid		: orderid,
				anzahl		: $('#order_item_anzahl_'+id).val(),
				rabatt		: $('#order_item_rabatt_'+id).val(),
				vk_single	: $('#order_item_vk_single_'+id).val(),
				vk_mtl		: $('#order_item_vk_mtl_'+id).val(),			
				use_ajax	: true
			}),
			datatype: 'html',
			success : function(data){
				$('#order_items').html(data);
				$('#order_item_anzahl_'+id).focus()
			}
		})
	},800);
}

function order_item_remove(id, orderid){
	$.ajax({
		url		:	"/order/removeorderitem",
		type	: 	"POST",
		data	:	({
			id			: id,
			orderid		: orderid,
			use_ajax	: true
		}),
		datatype: 'html',
		success : function(data){
			$('#order_items').html(data);
		}
	})
}


