/***************************************************************** * file: remote.js * * Abundatrade calculator communication and processing scripts. * Handles the AJAX calls to the Abundatrade server. * *****************************************************************/ /* jQuery('td:contains("024543525998")').parent() .find('td') .wrapInner('
') .parent() .find('td > div') .slideUp("slow", function(){ jQuery(this).parent().parent().remove(); }); ^^ Hides a row ^^ jQuery('#my_table > tbody > tr') .find('td') .wrapInner('') .parent() .find('td > div') .slideDown("slow", function(){ var $set = jQuery(this); $set.replaceWith($set.contents()); ^^ adds a row ^^ }); */ var number_item = 1; var itemsToDispose = []; /* * function: clear_product_code() * * Format the product code with alphanumerics only. * */ function clean_product_code(element) { element.value = element.value.replace(/\W+/g, ""); } /* * function: validateEmail() * * Validates an email address * */ function validateEmail(email, confirm) { if (email.toLowerCase() == confirm.toLowerCase()) { var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; return re.test(email); } else return 'nomatch'; } /* * function: clear_session() * * Clear out all data for this session, including prior saved data. * Request goes to the backend to clear data. * */ function clear_session(obj) { if (!jQuery(obj).hasClass('disabled')) { jQuery.prompt( 'Are you sure you want to delete the entire listing?', { buttons: { Delete: true, Cancel: false }, callback: function (v, m, f) { if (v) { var request = jQuery.ajax( { type: 'GET', url: 'http://' + abundacalc.server + '/trade/process/request.php', data: 'action=clear_session', dataType: 'jsonp' }); request.done(function (data) { //jQuery('#abundaCalcTbl > tbody').children().remove(); jQuery('#abundaCalcBody_request').children().remove(); data.total_qty = "0.00"; data.currency_for_total = "$"; data.total = "0.00"; display_totals(data); console.log(data); }); request.fail(function (jqXHR, textStatus, errorThrown) { alert("Request failed: " + textStatus + " - " + errorThrown); }); } } } ); } } /* * function: new_session() * * Give user a new session id. This detaches any prior connection to the backend data. * */ function new_session(this_link) { number_item = 0; var request = jQuery.ajax( { type: 'GET', url: 'http://' + abundacalc.server + '/trade/process/request.php', data: 'action=new_session', dataType: 'jsonp' }); request.done(function (data) { }); request.fail(function (jqXHR, textStatus, errorThrown) { alert("Request failed: " + textStatus + " - " + errorThrown); please_wait(false); }); } /* * function: display_totals() * * Render the header/footer totals from JSONP data. * */ function display_totals(data) { jQuery('#product_code').val(''); jQuery('#item_count').html(data.total_qty); jQuery('#total_item_count').html(data.total_qty); jQuery('#grand_total').html(data.currency_for_total + data.total); jQuery('#total_prevaluation').html(data.currency_for_total + data.total); // Turn off the UI Lock // please_wait(false); // Enable/Disable Submit based on Total Qty // if (jQuery('#total_item_count').text() > 0) { jQuery('#submitList').removeClass('disabled'); jQuery('#submitList').removeAttr('style'); } else { jQuery('#submitList').addClass('disabled'); jQuery('#submitList').attr('style', 'cursor:default'); } // Set the delete button // jQuery('.delete_this_row').attr('onclick', 'delete_the_row(this); return false;'); jQuery('#product_code').focus(); } /* * function: delete_the_row() * * Delete a selected row. * */ function delete_the_row(obj) { var product_code = jQuery(obj).parents('tr').children('.upc').text(); var request = jQuery.ajax( { type: 'GET', url: 'http://' + abundacalc.server + '/trade/process/deleteItem.php', data: 'product=' + product_code, dataType: 'jsonp' }); request.success(function (data) { display_totals(data); }); //jQuery(obj).parents('tr').remove(); jQuery('td:contains("' + product_code + '")').parent() .find('td') .wrapInner('') .parent() .find('td > div') .slideUp("slow", function () { jQuery(this).parent().parent().remove(); }); } /* * function: load_previous_session() * * Lookup the session on the Abundatrade backend and * load up the table based on the results. * */ function load_previous_session() { var request = jQuery.ajax( { type: 'GET', url: 'http://' + abundacalc.server + '/trade/process/request.php', data: 'action=load_previous_session', dataType: 'jsonp' }); request.success(function (data) { for (i = 0; i < data.length; i++) { part = data[i]; part.row = jQuery.parseJSON(part.row); part = build_row(part); console.log(part); jQuery('#abundaCalcTbl').prepend(part.row_html); jQuery('td:contains("' + part.product_code + '")').parent() .find('td') .wrapInner('') .parent() .find('td > div') .slideDown("slow", function () { var $set = jQuery(this); $set.replaceWith($set.contents()); }) display_totals(part); } //build_row(data); //jQuery('#abundaCalcTbl > tbody').prepend(data.row_html); //jQuery('#abundaCalcTbl').prepend(data.row_html); //display_totals(data); }); request.fail(function (jqXHR, textStatus, errorThrown) { alert("Request failed: " + textStatus + " - " + errorThrown); }); } function addDuplicatesToQuantity(inputUPC) { var upcs = $$('td.upc'); var quantities = $$('td.quantity'); var duplicateQuantity = 0; //Compare the new UPC to each UPC in the existing list for (var idx = 0; idx < upcs.length; idx++) { //Check for existing UPCs matching the new UPC if (inputUPC == upcs[idx].children[0].innerHTML) { //Add the found UPC's quantity to the new UPC's //quantity duplicateQuantity += parseInt(quantities[idx].children[0].innerHTML); queueForDisposal(upcs[idx].getParent()); } } } var lastItem; function Remove_Item(product_code) { var selector = 'td:contains("' + product_code + '")'; jQuery(selector).parents('tr').remove(); } function waitFor(product_code) { jQuery('#product_code').val(''); row_html = "
The values shown on the calculator are a pre-valuation.
Item quality needs to be verified for final valuation.
This valuation is not a commitment.
All data is kept private.
', buttons: [{ title: 'Back', value: -1 }, { title: 'Cancel', value: 0 }, { title: 'Agree and Submit', value: 'submit'}], focus: 2, submit: function (ev, but, message, val) { if (but == "submit") { return true; } if (but != 0) { if (val['scanner'] == 'on') { state += but; } else if (but == -1) { state -= 2; } else { state += but; } jQuery.prompt.goToState('state' + state); return false; } else { jQuery.prompt.close(); } } }, invalid: { html: 'Please enter a valid email address.
', buttons: { OK: true }, focus: 0, submit: function (v, m, f) { jQuery.prompt.goToState('state0'); return false; } }, nomatch: { html: 'Please check your email address to make sure it is correct.
', buttons: { OK: true }, focus: 0, submit: function (v, m, f) { jQuery.prompt.goToState('state0'); return false; } } }; var str = ''; jQuery.prompt(states, { callback: function (ev, v, m, f) { if (v) { jQuery.each(f, function (i, obj) { str += '&' + i + '=' + obj; }); var request = jQuery.ajax( { type: 'GET', url: 'http://' + abundacalc.server + '/trade/process/submit.php', data: 'action=submit_list' + str, dataType: 'jsonp' }); request.done(function (data) { // No errors // if (data.error == '') { jQuery('#abundaCalcTbl > tbody').children().remove(); display_totals(data); jQuery.prompt('Your list has been received.