var BWSubBuilder = {
addMorePeople: function() {
// First check the people count, if it is going to go over 12, show the warning and remove the button to add more.
var people_count = parseInt($('people_count').value);
if(people_count + 3 >= 12) {
$('too-many-people-warning').show();
$('builder_addmorepeoplebutton').hide();
}
var new_count = people_count + 3;
if(new_count > 12)
new_count = 12;
while(people_count < new_count) {
people_count++;
// Add a new person.
//var current_table_text = $('builder-people').innerHTML;
// New row.
var new_row = '
';
new_row += '
'+people_count+'
';
new_row += '
';
new_row += '
';
new_row += '
';
new_row += '
';
$('builder-people').insert(new_row);
}
$('people_count').value = people_count;
},
applyDiscountCode: function() {
// Get the entered discount code, if there is one.
var discount_code = $('discount_code').value;
if(discount_code != '') {
// Try to get the tab_index of a tab that matches this, or a new tab.
new Ajax.Request("/store/_check_discount_code.php?discount_code="+discount_code, {
method: 'get',
onSuccess: function(t) {
var discount_code_data = t.responseText;
// The data is a semicolon-separated string. Each chunk represents either an error code, or
// a product_id:discount pair.
var dcd_parts = discount_code_data.split(';');
// Check for errors, if there are any, we aren't doing anything else.
for(var i = 0; i < dcd_parts.length; i++) {
var dcd_subparts = dcd_parts[i].split(':');
if(dcd_subparts[0] == 'ERROR') {
switch(dcd_subparts[1]) {
case 'INVALID':
$('discount_code_status').style.color = '#FF0000';
$('discount_code_status').update('You have entered an invalid discount code, please try again');
return;
break;
case 'INACTIVE':
$('discount_code_status').style.color = '#FF0000';
$('discount_code_status').update('The discount code you have entered is not yet active, please check the dates for your code.');
return;
break;
case 'EXPIRED':
$('discount_code_status').style.color = '#FF0000';
$('discount_code_status').update('The discount code you have entered has expired.');
return;
break;
}
}
}
// Is there a discount for standard or plus?
var discount_for_standard = 0;
var discount_for_plus = 0;
var discount_applies_to_this_order = false;
for(var i = 0; i < dcd_parts.length; i++) {
var dcd_subparts = dcd_parts[i].split(':');
if(dcd_subparts[0] == '201') {
discount_for_standard = dcd_subparts[1];
discount_applies_to_this_order = true;
} else if(dcd_subparts[0] == '252') {
discount_for_plus = dcd_subparts[1];
discount_applies_to_this_order = true;
}
}
$('per_person_price_discount').value = discount_for_standard;
$('plus_upgrade_price_discount').value = discount_for_plus;
if(discount_applies_to_this_order) {
$('discount_code_status').style.color = '#336699';
$('discount_code_status').update('Your discount has been applied to this order.');
} else {
$('discount_code_status').style.color = '#FF000';
$('discount_code_status').update('Your discount does not apply to this order.');
}
BWSubBuilder.updateTotal();
}
});
}
},
selectPlus: function(on) {
if(on) {
$('plus_unchecked').hide();
$('plus_checked').show();
$('plus').checked = true;
} else {
$('plus_unchecked').show();
$('plus_checked').hide();
$('plus').checked = false;
}
// Update total value.
BWSubBuilder.updateTotal();
},
submitForm: function() {
var people_count = $('people_count').value;
// Check that the form has some people.
var has_people = false;
for(var i = 1; i <= people_count; i++) {
// Check to see if we have complete data for this person.
var name=$('name_'+i).value;
var email=$('email_'+i).value;
if(name !='' && email != '')
has_people = true;
}
if(!has_people) {
alert("You need to specify at least one user's name and e-mail address.");
return false;
} else {
$('builder-form').submit();
}
},
updateTotal: function() {
var people_count = $('people_count').value;
var total = 0;
var person_price = parseFloat($('per_person_price').value) - parseFloat($('per_person_price_discount').value);
var plus_upgrade_price = parseFloat($('plus_upgrade_price').value) - parseFloat($('plus_upgrade_price_discount').value);
// Add in each person.
for(var i = 1; i <= people_count; i++) {
// Check to see if we have complete data for this person.
var name=$('name_'+i).value;
var email=$('email_'+i).value;
// If we have both values, then update the person price.
if(name !='' && email != '') {
$('person_price_'+i).update('$'+person_price);
total += person_price;
} else {
$('person_price_'+i).update('');
}
}
// Add in plus, if applicable.
if($('plus').checked) {
$('plus_price').update('$'+plus_upgrade_price);
total += plus_upgrade_price;
} else {
$('plus_price').update('');
}
$('builder-grand-total').update('$'+total);
}
};