//On page load
$(function() {
$('input#coupon-code').keydown(function(event) { if (event.keyCode == 13) { ajax_coupon(); } });
$('#checkout_same_address').sameAddress();
$('span#bcountry select').change(function() { update_state('b'); });
$('span#scountry select').change(function() { update_state('s'); });
get_states();
$('input#checkout_creditcard_number').blur(set_card_validation);
// hook up the continue buttons for each section
for(var i=0; i < regions.length; i++) {
var section = regions[i];
$('#continue_' + section).click(function() { eval( "continue_button(this);"); return false; });
// enter key should be same as continue button (don't submit form though)
$('#' + section + ' input').bind("keyup", section, function(e) {
if(e.keyCode == 13) {
continue_section(e.data);
}
});
}
//disable submit
$('div#checkout :submit').attr('disabled', 'disabled');
$('div#checkout-summary :submit').attr('disabled', 'disabled');
// hookup the radio buttons for registration
$('#choose_register').click(function() { $('div#new_user').show(); $('div#guest_user, div#existing_user').hide(); });
$('#choose_existing').click(function() { $('div#existing_user').show(); $('div#guest_user, div#new_user').hide(); });
$('#choose_guest').click(function() { $('div#guest_user').show(); $('div#existing_user, div#new_user').hide(); });
var reg_choice = $('input[name=choose_registration]:checked').val();
if(reg_choice) {
$('#choose_' + reg_choice).click();
} else {
$('#choose_register').attr('checked', true);
}
// activate first region
shift_to_region(regions[0]);
})
jQuery.fn.sameAddress = function() {
this.click(function() {
if(!$(this).attr('checked')) {
//Clear ship values?
return;
}
$('input#hidden_sstate').val($('input#hidden_bstate').val());
$("#billing input, #billing select").each(function() {
$("#shipping #"+ $(this).attr('id').replace('bill', 'shipment_attributes')).val($(this).val());
})
update_state('s');
})
};
//Initial state mapper on page load
var state_mapper;
var get_states = function() {
$('span#bcountry select').val($('input#hidden_bcountry').val());
update_state('b');
$('span#bstate :only-child').val($('input#hidden_bstate').val());
$('span#scountry select').val($('input#hidden_scountry').val());
update_state('s');
$('span#sstate :only-child').val($('input#hidden_sstate').val());
};
// replace the :only child of the parent with the given html, and transfer
// {name,id} attributes over, returning the new child
var chg_state_input_element = function (parent, html) {
var errorlabel = parent.find('label');
errorlabel.remove();
var child = parent.find(':only-child');
var name = child.attr('name');
var id = child.attr('id');
//Toggle back and forth between id and name
if(html.attr('type') == 'text' && child.attr('type') != 'text') {
name = name.replace('_id', '_name');
id = id.replace('_id', '_name');
} else if(html.attr('type') != 'text' && child.attr('type') == 'text') {
name = name.replace('_name', '_id');
id = id.replace('_name', '_id');
}
html.addClass('required')
.attr('name', name)
.attr('id', id);
child.remove(); // better as parent-relative?
parent.append(html);
return html;
};
// TODO: better as sibling dummy state ?
// Update the input method for address.state
var update_state = function(region) {
if ($('span#' + region + 'state').length == 0) {
return;
}
var country = $('span#' + region + 'country :only-child').val();
var states = state_mapper[country];
var hidden_element = $('input#hidden_' + region + 'state');
var replacement;
if(states) {
// recreate state selection list
replacement = $(document.createElement('select'));
var states_with_blank = [["",""]].concat(states);
$.each(states_with_blank, function(pos,id_nm) {
var opt = $(document.createElement('option'))
.attr('value', id_nm[0])
.html(id_nm[1]);
replacement.append(opt);
if (id_nm[0] == hidden_element.val()) { opt.attr('selected', 'true') }
// set this directly IFF the old value is still valid
});
} else {
// recreate an input box
replacement = $(document.createElement('input'));
if (! hidden_element.val().match(/^\d+$/)) { replacement.val(hidden_element.val()) }
}
chg_state_input_element($('span#' + region + 'state'), replacement);
hidden_element.val(replacement.val());
// callback to update val when form object is changed
// This is only needed if we want to preserve state when someone refreshes the checkout page
// Or... if someone changes between countries with no given states
replacement.change(function() {
$('input#hidden_' + region + 'state').val($(this).val());
});
};
var continue_button = function(button) {
continue_section(button.id.substring(9));
};
var continue_section = function(section) {
// validate
if (!validate_section(section)) { return; };
// submit
var success = eval("submit_" + section + "();");
if (!success) { return; }
// move to next section
for(var i=0; i
';
if($('p#' + region + 'address2').val() != '') {
address += $('p#' + region + 'address2').val() + '
';
}
address += $('p#' + region + 'city input').val() + ', ';
if($('span#' + region + 'state input').length > 0) {
address += $('span#' + region + 'state input').val();
} else {
address += $('span#' + region + 'state :selected').html();
}
address += ' ' + $('p#' + region + 'zip input').val() + '
';
address += $('p#' + region + 'country :selected').html() + '
';
address += $('p#' + region + 'phone input').val();
$('div#' + region + 'display div').html(address);
return;
};
var submit_shipping = function() {
$('div#methods :child').remove();
$('div#shipping_method div.error').hide();
$('div#methods').append($(document.createElement('img')).attr('src', '/images/ajax_loader.gif').attr('id', 'shipping_loader'));
// Save what we have so far and get the list of shipping methods via AJAX
$.ajax({
type: "POST",
url: '../checkout',
beforeSend : function (xhr) {
xhr.setRequestHeader('Accept-Encoding', 'identity');
},
dataType: "json",
data: $('#checkout_form').serialize(),
success: function(json) {
update_shipping_methods(json.available_methods);
update_confirmation(json);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
$('div#methods :child').remove();
$('div#shipping_method div.error').show();
return false;
}
});
build_address('s');
return true;
};
var submit_shipping_method = function() {
//TODO: Move to validate_section('shipping_method'), but must debug how to validate radio buttons
var valid = false;
$('div#methods :child input').each(function() {
if($(this).attr('checked')) {
valid = true;
}
});
if(valid) {
// Save what we have so far and get the updated order totals via AJAX
$.ajax({
type: "POST",
url: '../checkout',
beforeSend : function (xhr) {
xhr.setRequestHeader('Accept-Encoding', 'identity');
},
dataType: "json",
data: $('#checkout_form').serialize(),
success: function(json) {
update_confirmation(json);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
// TODO - put some real error handling in here
//$("#error").html(XMLHttpRequest.responseText);
return false;
}
});
return true;
} else {
var p = document.createElement('p');
$(p).append($(document.createElement('label')).addClass('error').html('Please select a shipping method').css('width', '300px').css('top', '0px'));
$('div#methods').append(p);
return false;
}
};
var update_shipping_methods = function(methods) {
$(methods).each( function(i) {
$('div$methods img#shipping_loader').remove();
var p = document.createElement('p');
var s = this.name + ' ' + this.rate;
var i = $(document.createElement('input'))
.attr('id', this.id)
.attr('type', 'radio')
.attr('name', 'checkout[shipment_attributes][shipping_method_id]')
.val(this.id)
.click(function() { $('div#methods input').attr('checked', ''); $(this).attr('checked', 'checked'); });
if($(methods).length == 1) {
i.attr('checked', 'checked');
}
var l = $(document.createElement('label'))
.attr('for', this.id)
.html(s);
$('div#methods').append($(p).append(i).append(l));
});
$('div#methods input:first').attr('validate', 'required:true');
return;
};
var update_confirmation = function(order) {
var textToInsert = '';
var summaryText = '';
for (var key in order.charges) {
textToInsert += '