Skip to content

Instantly share code, notes, and snippets.

@Xethron
Created July 5, 2013 09:51
Show Gist options
  • Save Xethron/5933434 to your computer and use it in GitHub Desktop.
Save Xethron/5933434 to your computer and use it in GitHub Desktop.
Attempt to move over from jQuery to Prototype
var $j = jQuery.noConflict();
document.observe('dom:loaded', function() {
var isZA_B;
var isZA_S;
var setShipping = false;
function setFields (shipto) {
$$("label[for='" + shipto + ":region_id']")[0].addClassName('required');
$$("label[for='" + shipto + ":region_id']")[0].update('<em>*</em>Town');
var suburb_html =
'<div class="mds-' + shipto + ' field">' +
' <label class="required" for="' + shipto + ':mds_suburb"><em>*</em>Suburb</label>' +
' <div class="input-box">' +
' <select onchange="showTest(this)" class="required-entry" title="Suburb" name="' + shipto + '[mds_suburb]" id="' + shipto + ':mds_suburb" defaultvalue="">' +
' <option value="">Please select a Town first</option>' +
' </select>' +
' </div>' +
'</div>';
$(shipto + ":city").up(2).insert({top:suburb_html});
$(shipto + ":city").up(1).hide();
var building_html =
'<li class="mds-' + shipto + ' wide">' +
' <label for="' + shipto + ':mds_building" class="required"><em>*</em>Building Details</label>' +
' <div class="input-box">' +
' <input title="Building Details" name="' + shipto + '[mds_building]" id="' + shipto + ':mds_building" value="" class="input-text required-entry" type="text">' +
' </div>' +
'</li>';
$(shipto + ":street1").up(1).insert({before:building_html});
var cptypes_html =
'<div class="mds-' + shipto + ' field">' +
' <label class="required" for="' + shipto + ':mds_cptypes"><em>*</em>Location Type</label>' +
' <div class="input-box">' +
' <select class="required-entry" title="Location Type" name="' + shipto + '[mds_cptype]" id="' + shipto + ':mds_cptypes" defaultvalue="">' +
' <option value="">Loading...</option>' +
' </select>' +
' </div>' +
'</div>';
$(shipto + ":city").up(2).insert({bottom:cptypes_html});
var towns_html =
'<div class="mds-' + shipto + ' field" style="display: none;">' +
' <div class="input-box">' +
' <input name="' + shipto + '[mds_town]" id="' + shipto + ':mds_town" value="" class="input-text required-entry" type="text">' +
' </div>' +
'</div>';
$(shipto + ":city").up(2).insert({bottom:towns_html});
}
function getSuburbs (shipto) {
//$j("#" + shipto + "\\:mds_suburb").empty();
//$j("#" + shipto + "\\:mds_suburb").append('<option value="">Loading...</option>');
$(shipto + ":mds_suburb").update("<option value=\"\">Loading...</option>");
var data = {
town : $j("#" + shipto + "\\:region_id option:selected").text(),
};
/*jQuery.ajax({
type : 'POST',
url : "../../collivery/ajax/suburb",
data : data,
complete : function(response){
$j("#" + shipto + "\\:mds_suburb").empty();
$j("#" + shipto + "\\:mds_suburb").append(response['responseText']);
$j("#" + shipto + "\\:city").val($j("#" + shipto + "\\:mds_suburb option:selected").text());
}
});*/
new Ajax.Request('../../collivery/ajax/suburb', {
method: 'post',
parameters: data,
onSuccess: function(transport) {
var response = transport.responseText || "<option>Error, Please try again</option>";
$(shipto + ":mds_suburb").update(response);
},
onFailure: function() { $(shipto + ":mds_suburb").update("<option>Error, Please try again</option>"); }
});
}
function getCPTypes (shipto) {
//$j("#" + shipto + "\\:mds_cptypes").empty();
//$j("#" + shipto + "\\:mds_cptypes").append('<option value="">Loading...</option>');
/*jQuery.ajax({
url : "../../collivery/ajax/cptypes",
complete : function(response){
$j("#" + shipto + "\\:mds_cptypes").empty();
$j("#" + shipto + "\\:mds_cptypes").append(response['responseText']);
}
});*/
$(shipto + ":mds_cptypes").update("<option value=\"\">Loading...</option>");
new Ajax.Request('../../collivery/ajax/cptypes', {
method: 'get',
onSuccess: function(transport) {
var response = transport.responseText || "<option value=\"\">Error, Please try again</option>";
$(shipto + ":mds_cptypes").update(response);
},
onFailure: function() { $(shipto + ":mds_cptypes").update("<option value=\"\">Error, Please try again</option>"); }
});
}
function setZA (shipto) {
setFields(shipto);
getCPTypes(shipto);
}
function unSetZA (shipto) {
$$('.mds-' + shipto).invoke('remove');
$(shipto + ":city").up(1).show();
$$("label[for='" + shipto + ":region_id']")[0].removeClassName('required');
$$("label[for='" + shipto + ":region_id']")[0].update('<em style="display: none;">*</em>State/Province');
}
function showTest (sel) {
alert("Suburb Changed to:" + sel.value);
}
$("billing:country_id").observe('change', function(){
if ($F("billing:country_id") == "ZA") {
setZA('billing');
isZA_B = true;
if (!setShipping){
setZA('shipping');
Form.Element.setValue("shipping:country_id", 'ZA')
//$j("#shipping\\:country_id").val('ZA')
isZA_S = true;
}
} else {
if (isZA_B){
unSetZA('billing');
isZA_B = false;
}
}
});
$("shipping:country_id").observe('change', function(){
if ($F("shipping:country_id") == "ZA") {
setZA('shipping');
isZA_S = true;
setShipping = true;
} else {
if (isZA_S){
unSetZA('shipping');
isZA_S = false;
setShipping = true;
}
}
});
$("billing:region_id").observe('change', function(){
if ($F("billing:country_id") == "ZA") {
getSuburbs('billing');
}
});
$("shipping:region_id").observe('change', function(){
if ($F("shipping:country_id") == "ZA") {
getSuburbs('shipping');
}
});
$("billing:mds_suburb").observe('change', function(){
alert("hello");
alert($F("billing:mds_suburb"));
$j("#billing\\:city").val($F("billing:mds_suburb"));
if (!setShipping){
$j("#shipping\\:city").val($j("#mds\\:billing_suburb option:selected").text());
$j("#shipping:mds_suburb").val($j("#billing:mds_suburb option:selected").val());
}
});
$("shipping:mds_suburb").observe('change', function(){
$j("#shipping\\:city").val($j("#mds\\:shipping_suburb option:selected").text());
});
$("billing:mds_cptypes").observe('change', function(){
$j("#mds\\:shipping_cptypes").val($j("#mds\\:billing_cptypes option:selected").val());
});
$("shipping:mds_cptypes").observe('change', function(){
$j('#mds\\:shipping_building').val($j('#mds\\:billing_building').val())
});
if ($j("#billing\\:country_id").val() == "ZA") {
setZA('billing');
isZA_B = true;
if (!setShipping){
setZA('shipping');
isZA_S = true;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment