/**
* Could (should) tidy this up to move the Quote/Invoice etc Specific
* functionality into seperate files.
*
*
* Karl - 20/5/2011
*/
$(function() {
/**
* A more generic way of handling the HABTM
[REMOVE BUTTON] NAME [HIDDEN INPUT]
* Copypasta'd from add_edit_shipment.js.
*/
function addToList(modelName, id, value, ULelement) {
var thisLI = $('');
var thisButton = $('');
thisButton.addClass('removeFromList');
thisButton.button();
var thisHiddenInput = $('');
var modelString = '['+modelName+']';
thisHiddenInput.attr('name', 'data[PurchaseOrder]'+modelString+'[]');
thisHiddenInput.attr('value', id);
thisLI.attr('id', modelName+'ID_'+id);
thisLI.html(value);
thisLI.prepend(thisButton);
thisLI.append(thisHiddenInput);
ULelement.append(thisLI);
}
//Remove X button clicked.
$('.removeFromList').live('click', function() {
$(this).parent().remove();
});
// var config defined in global.js
loadLineItems();
var docID = $('#documentID').html();
$("#lineItemDetails").hide();
$( "#addLineItemModal" ).dialog({
autoOpen: false,
height: 900,
width: 600,
modal: true,
buttons: {
"Add Line Item": function() {
$('#LineItemDescription').ckeditor(function() {
this.updateElement();
});
var thisLineItemInputs = $('#LineItemAddForm').find('input,select,textarea');
$.post('/line_items/ajax_add', thisLineItemInputs, function(data) {
if(data == 'SUCCESS') {
loadLineItems();
$( "#addLineItemModal" ).dialog('close');
}
else {
alert("Line Item could not be saved")
$('#LineItemDescription').ckeditor(config);
}
});
},
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
loadLineItems();
}
});
$( "#editLineItemModal" ).dialog({
autoOpen: false,
height: 900,
width: 600,
modal: true,
buttons: {
"Edit Line Item": function() {
$('#LineItemDescription').ckeditor(function() {
this.updateElement();
});
var thisLineItemInputs = $('#LineItemEditForm').find('input,select,textarea');
$.post('/line_items/ajax_edit', thisLineItemInputs, function(data) {
if(data == 'SUCCESS') {
$( "#editLineItemModal" ).dialog('close');
}
else {
alert("Line Item could not be saved")
$('#LineItemDescription').ckeditor(config);
}
});
},
Cancel: function() {
$( "#editLineItemModal" ).dialog('close');
}
},
close: function() {
loadLineItems();
}
});
$( "#QuoteDetails" ).dialog({
autoOpen: false,
height: 900,
width: 600,
modal: true,
buttons: {
"Edit Quote Details": function() {
$('#QuoteCommercialComments').ckeditor(function() {
this.updateElement();
this.destroy();
});
var quoteInputs = $('#QuoteEditForm').find('input,select,textarea');
$.post('/quotes/ajax_edit', quoteInputs, function(data) {
$( "#QuoteDetails" ).dialog('close');
});
},
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
loadLineItems();
}
});
$( "#addJobConfirmation" ).dialog({
autoOpen: false,
height: 400,
width: 400,
modal: true,
buttons: {
"Create Order Acknowledgement": function() {
var documentID = $("#documentID").html();
//window.location.href = "/documents/convert_to_oa/"+documentID;
//var newOAform = $('#DocumentConvertToOaForm').find('input');
var newOAform = $('#DocumentConvertToOaForm');
newOAform.submit();
/*$.post('/documents/convert_to_oa', newOAform, function(data) {
if(data =='SUCCESS') {
$("#flashMessage").html("Invoice Saved Successfully");
}
else {
$("#flashMessage").html("Unable to Save Invoice");
}
$("#flashMessage").show();
loadLineItems();
});
*/
},
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
}
});
$("#pageContentFactory").hide();
//Add a new Page Element.
$("#addPage").button().click(function(event) {
event.preventDefault();
newPage(false);
return false;
});
//Open the LineItem dialog
$(".addLineItem").button().click(function() {
$('#LineItemDescription').ckeditor(function() {
this.destroy();
});
$("#editLineItemModal").empty();
var nextItemNo = $(".lineItem").length;
nextItemNo++;
$.get('/line_items/add/'+docID, function(data) {
$("#addLineItemModal").html(data);
$("#LineItemItemNumber").val(nextItemNo); //Auto fill in the next Item No
$("#productDetails").hide();
$('#LineItemDescription').ckeditor(config);
showHideTextPrices();
$( "#addLineItemModal" ).dialog('open');
});
return false;
});
$(".editLineItem").live('click', function() {
$('#LineItemDescription').ckeditor(function() {
this.destroy();
});
$("#addLineItemModal").empty();
var thisLineItemID = $(this).parent('td').attr('id');
$.get('/line_items/edit/'+thisLineItemID, function(data) {
$("#editLineItemModal").html(data);
$("#productDetails").hide();
$('#LineItemDescription').ckeditor(config);
showHideTextPrices();
$( "#editLineItemModal" ).dialog('open');
});
});
var products = {};
$("#principleSelect").live('change',function() {
var principleID = getSelectedID('#principleSelect');
$("#productDetails").hide();
$.get('/documents/getProducts/'+principleID, function(data) {
$('#productsDiv').html(data);
var resp = $(data).filter(".products_json");
products = jQuery.parseJSON(resp.html());
});
});
//Search for a Product
$("#productSearch").live('change', function() {
productSearch();
});
$("#productSearchButton").live('click', function() {
productSearch();
});
function productSearch() {
var searchVal = $("#productSearch").val();
searchVal = searchVal.toLowerCase();
var param = "term="+searchVal;
$.getJSON("/products/autocomplete", param, function(data) {
$("#productList").empty();
for(var id in data) {
var link = "