var groupArray = {{ groupArray|raw }};
var myInt = 0;
var product_options = '<option value="" title="Choose a product!">Choose a product here!</option>{{ productOptions|raw }}';
var sizeArray = {{ sizeArray|raw }};
var v_quantity = 0;
var objID;
var v_index = -1;
var RecipientNumber = 0;
var OrderLineNumber = 0;
var size_options = ' ';
var recip_nums = [];
var order_nums = [];
var ShipToSectionNumber = 0;
var myShip2Int = 0;
function format_number(pnumber, decimals) {
if (isNaN(pnumber) || pnumber === '') {
return 0;
}
var snum = String(pnumber);
var sec = snum.split('.');
var whole = parseFloat(sec[0]);
var result = '';
var dot;
var dec;
if (sec.length > 1) {
dec = String(sec[1]);
dec = String(parseFloat(sec[1]) / Math.pow(10, (dec.length - decimals)));
dec = String(whole + Math.round(parseFloat(dec)) / Math.pow(10, decimals));
dot = dec.indexOf('.');
if (dot == -1) {
dec += '.';
dot = dec.indexOf('.');
}
while (dec.length <= dot + decimals) {
dec += '0';
}
result = dec;
} else {
dec = String(whole);
dec += '.';
dot = dec.indexOf('.');
while (dec.length <= dot + decimals) {
dec += '0';
}
result = dec;
}
return result;
}
function calcPrice(lineNo, RecipientNo) {
var v_price = '0.00';
v_quantity = $('#' + RecipientNo.toString() + 'quantity' + lineNo.toString()).val();
v_index = $('#' + RecipientNo.toString() + 'product' + lineNo).val();
objID = '#' + RecipientNo.toString() + 'size' + lineNo.toString();
$(objID).removeAttr("disabled").attr("enabled", "enabled");
if (v_index > '') {
if (v_quantity == '' || isNaN(parseInt(v_quantity)) || v_quantity < 0) {
v_quantity = 0;
}
v_price = $('#' + RecipientNo.toString() + 'size' + lineNo.toString()).val() * v_quantity;
if (v_price == 0) {
v_price = '0';
}
$('#' + RecipientNo.toString() + 'price' + lineNo.toString()).val(format_number(v_price, 2));
var tmp_value = accumulatePrice();
$('#total_price').val(format_number(tmp_value, 2));
$('#submit_order').attr("disabled", (tmp_value === 0));
size_options = sizeArray[groupArray[v_index]['group_id'] - 1]['options'];
}
}
function accumulatePrice() {
var tmp_value = 0;
$.each($('.price'), function (i, val) {
tmp_value += parseFloat(val.value);
});
return tmp_value;
}
function removeOrderLine(lineNo, RecipientNo) {
if (confirm("Are you sure you want to remove the order line?")) {
$('#' + RecipientNo.toString() + 'row' + lineNo.toString()).remove();
$('#total_price').val(format_number(accumulatePrice(), 2));
calcPrice(lineNo, RecipientNo);
var i = 0;
while (order_nums[RecipientNo][i] <= lineNo && order_nums[RecipientNo][i] != 'undefined') {
i++;
}
order_nums[RecipientNo].splice(i, 1);
return false;
}
}
function processProductChange(lineNo, RecipientNo) {
calcPrice(lineNo, RecipientNo);
objID = '#' + RecipientNo.toString() + 'size' + lineNo.toString();
$(objID).html(size_options).change();
}
/**
* @return {string}
*/
function GenerateOrderLine(RecipientNumber) {
order_nums[RecipientNumber].push(OrderLineNumber);
var myOrderLine = '<tr class="order_line" id="' + RecipientNumber.toString() + 'row' +
OrderLineNumber.toString() +
'"><td>\n<select onchange="processProductChange(' +
OrderLineNumber.toString() + ', ' + RecipientNumber.toString() + ')" name="' + RecipientNumber.toString() + 'product' +
OrderLineNumber.toString() + '" id="' + RecipientNumber.toString() + 'product' +
OrderLineNumber.toString() + '">\n' + product_options +
'\n</select>\n</td><td style="text-align: left;"><select onchange="calcPrice(' +
OrderLineNumber.toString() + ', ' + RecipientNumber.toString() + ', false)" name="' + RecipientNumber.toString() + 'size' +
OrderLineNumber.toString() + '" id="' + RecipientNumber.toString() + 'size' +
OrderLineNumber.toString() + '" disabled="disabled">' + size_options +
'\n</select>\n</td>\n<td>\n<input size="2" maxlength="3" ' +
'onkeyup="calcPrice(' +
OrderLineNumber.toString() + ', ' + RecipientNumber.toString() +
')" style="text-align: right; width: 100%;" name="' + RecipientNumber.toString() + 'quantity' +
OrderLineNumber.toString() + '" id="' + RecipientNumber.toString() + 'quantity' +
OrderLineNumber.toString() + '" class="' + RecipientNumber.toString() + 'quantity' +
OrderLineNumber.toString() +
'" />\n</td>\n<td>\n<input id="' + RecipientNumber.toString() + 'price' +
OrderLineNumber.toString() + '" name="' + RecipientNumber.toString() + 'price' +
OrderLineNumber.toString() +
'" class="price" value="0.00" size="8" style="text-align: right;"' +
' disabled="disabled" />\n</td>\n<td>\n' +
'<a class="del_row" id="' + RecipientNumber.toString() + 'rem' + OrderLineNumber.toString() +
'" href="javascript:;" onclick="removeOrderLine(' +
OrderLineNumber.toString() + ', ' + RecipientNumber.toString() + '); return false;">X</a>\n</td>\n</tr>';
calcPrice(OrderLineNumber, RecipientNumber);
return myOrderLine;
}
/**
* @return {string}
*/
function GenerateShipToSection() {
recip_nums.push(ShipToSectionNumber);
order_nums[ShipToSectionNumber] = [];
return '\n<table class="ship2row" id="ship2row' + ShipToSectionNumber.toString() + '">' +
'\n<tr>' +
'\n<td class="ship2sectionheader" colspan="2">' +
'\n<input type="hidden" name="order_nums' + ShipToSectionNumber.toString() + '" />Ship To Address' +
'\n</td>' +
'\n</tr>' +
'\n<tr>' +
'\n<td class="ship2desc">' +
'\nName: ' +
'\n</td>' +
'\n<td class="ship2item" id="ship2row' + ShipToSectionNumber.toString() + '">' +
'\n<input id="ship_name' + ShipToSectionNumber.toString() + '" name="ship_name' + ShipToSectionNumber.toString() + '" type="text" size="63" />' +
'\n</td>' +
'\n</tr>' +
'\n<tr>' +
'\n<td class="ship2desc">' +
'\nStreet: ' +
'\n</td>' +
'\n<td class="ship2item" id="ship2row' + ShipToSectionNumber.toString() + '">' +
'\n<input name="ship_street' + ShipToSectionNumber.toString() + '" id="ship_street' + ShipToSectionNumber.toString() + '" type="text" size="63" />' +
'\n</td>' +
'\n</tr>' +
'\n<tr>' +
'\n<td>' +
'\n<input name="order_nums' + ShipToSectionNumber.toString() + '" id="order_nums' + ShipToSectionNumber.toString() + '" type="hidden" /> ' +
'\n</td>' +
'\n<td class="ship2item" id="ship2row' + ShipToSectionNumber.toString() + '">' +
'\n<input name="ship_street_2' + ShipToSectionNumber.toString() + '" id="ship_street_2' + ShipToSectionNumber.toString() + '" type="text" size="63" />' +
'\n</td>' +
'\n</tr>' +
'\n<tr>' +
'\n<td class="ship2desc">' +
'\n<label for="ship2row' + ShipToSectionNumber.toString() + '">City:</label> ' +
'\n</td>' +
'\n<td class="ship2item csz" id="ship2row' + ShipToSectionNumber.toString() + '">' +
'\n<input name="ship_city' + ShipToSectionNumber.toString() + '" id="ship_city' + ShipToSectionNumber.toString() + '" type="text" size="33" /> ' +
'\n<label>State: <input name="ship_state' + ShipToSectionNumber.toString() + '" id="ship_state' + ShipToSectionNumber.toString() + '" type="text" size="2" /></label>' +
'\n<label>Zip: <input name="ship_zip' + ShipToSectionNumber.toString() + '" id="ship_zip' + ShipToSectionNumber.toString() + '" type="text" size="10" /></label>' +
'\n</td>' +
'\n</tr>' +
'\n<tr>' +
'\n<td class="ship2item" id="ship2row' + ShipToSectionNumber.toString() + '" colspan="2">' +
'\n<input style="margin-left: 20%;" id="bus_add' + ShipToSectionNumber.toString() + '" name="bus_add' + ShipToSectionNumber.toString() + '" type="checkbox" /> This is a Business Address' +
'\n</td>' +
'\n</tr>' +
'\n<tr>' +
'\n<td class="ship2item" id="ship2row' + ShipToSectionNumber.toString() + '" colspan="2">' +
'\n<input style="margin-left: 20%;" id="ship_now' + ShipToSectionNumber.toString() + '" name="ship_now' + ShipToSectionNumber.toString() + '" type="checkbox" /> Ship now' +
'\n</td>' +
'\n</tr>' +
'\n<tr>' +
'\n<td class="csz" id="ship2row' + ShipToSectionNumber.toString() + '" colspan="2">' +
'\n<input style="margin-left: 20%;" id="ship_later' + ShipToSectionNumber.toString() + '" name="ship_later' + ShipToSectionNumber.toString() + '" type="checkbox" checked="checked" /> <label>To Arrive the week of ' +
'\n<input id="ship_month' + ShipToSectionNumber.toString() + '" name="ship_month' + ShipToSectionNumber.toString() + '" type="text" size="2" /></label> <label>/ <input id="ship_year' + ShipToSectionNumber.toString() + '" name="ship_year' + ShipToSectionNumber.toString() + '" type="text" size="2" /></label>' +
'\n</td>' +
'\n</tr>' +
'\n<tr>' +
'\n<td class="csz ship2row" id="ship2row' + ShipToSectionNumber.toString() + '" colspan="2">' +
'\n<input style="margin-left: 20%;" id="greeting' + ShipToSectionNumber.toString() + '" name="greeting' + ShipToSectionNumber.toString() + '" type="checkbox" checked="checked" /> <label>Greetings From ' +
'\n<input title="Maximum characters allowed is 100!" maxlength="100" id="greetings_to' + ShipToSectionNumber.toString() + '" name="greetings_to' + ShipToSectionNumber.toString() + '" type="text" size="57" /></label></td></tr>' +
'\n<tr>' +
'\n<td colspan="2"> </td>' +
'\n</tr>' +
'\n<tr>' +
'\n<td height="16" colspan="2">' +
'\n<table class="order_table" id="order_table' + ShipToSectionNumber.toString() + '">' +
'\n<thead>' +
'\n<tr>' +
'\n<th>Description</th>' +
'\n<th>Size</th>' +
'\n<th>Qty</th>' +
'\n<th>Total</th>' +
'\n<th> </th>' +
'\n</tr>' +
'\n</thead>' +
'\n<tbody id="order_head' + ShipToSectionNumber.toString() + '" style="height:10pt;">' +
'\n</tbody>' +
'\n<tfoot>' +
'\n<tr>' +
'\n<th colspan="5">' +
'\n<input type="button" value="Add Product" id="add_product' + ShipToSectionNumber.toString() + '" name="add_product' + ShipToSectionNumber.toString() + '" />' +
'\n</th>' +
'\n</tr>' +
'\n</tfoot>' +
'\n</table>' +
'\n</td>' +
'\n</tr>' +
'\n<tr>\n<td class="del_recip" colspan="2">\n<a name="rem' + ShipToSectionNumber.toString() +
'" href="javascript:;" onclick="removeShipToSection(' + (ShipToSectionNumber++).toString() + '); return false;">Remove Recipient</a>\n' +
'</td>\n' +
'</tr>\n' +
'</table>\n';
}
function removeShipToSection(ship2No) {
if (confirm("Are you sure you want to remove the ship to section?")) {
$("#ship2row" + ship2No.toString()).remove();
var i = 0;
while (recip_nums[i] <= ship2No && recip_nums[i] != 'undefined') {
i++;
}
recip_nums.splice(i, 1);
return false;
}
}
function popVals() {
$('#recip_nums').val(recip_nums.join());
var len = recip_nums.length;
for (var i = 0; i < len; i++) {
$('#order_nums' + recip_nums[i]).val(order_nums[recip_nums[i]].join());
}
}
$(document).ready(function () {
$('#add_ship_to' + RecipientNumber.toString())
.click(function () {
ShipToSectionNumber = myShip2Int;
$('#ship_to_head0').append(GenerateShipToSection());
myShip2Int++;
$('#add_product' + RecipientNumber.toString())
.click(function () {
size_options = "<option value='0' selected='selected'>Choose size here!</option>";
var recip = this.id.unformat('add_product{0}')[0];
OrderLineNumber = myInt;
$('#order_head' + recip).append(GenerateOrderLine(recip));
myInt++;
}).click();
RecipientNumber++;
}).click();
$("#bill_name").focus();
});