OCLC.Cart = new Class({
/*
params.instanceVar: the instance variable for this class
params.affiliation_select: the select element for affiliations
params.affiliation_hidden: the hidden input that stores affiliations
params.add_affiliation: the button that adds affiliations
params.newsletter: if the newsletter is selected
params.subscription_hidden: where the total input of subscription goes
*/
initialize: function(params) {
var instance = this;
instance.params = params;
var country_select = jQuery( '#' + params.country_select);
var state_select = jQuery( '#' + params.state_select );
var newCountry = country_select.val();
instance.populateStates(params.state_select,newCountry);
country_select.change(
function() {
var newCountry = country_select.val();
instance.populateStates(params.state_select,newCountry);
}
);
},
displayStates: function(instance, entries) {
var params = instance.params;
var state_select = jQuery( '#' + params.state_select );
var state_selected = params.state_selected;
var optionId;
var html = "";
jQuery.each(
entries,
function(i, entry) {
html += "<option id=\""+ entry.stringValue + "_option\" value=\"" + entry.stringValue + "\">" + entry.label + "</option>";
var entryId = entry.stringValue + "_option";
if (state_selected && (state_selected == entryId)) {			
optionId = "#"+entryId;
}
}
);
state_select.html(html);
if (optionId) {
jQuery( '#' + params.state_select).find(optionId).attr("selected","selected");
}
},
populateStates: function(select_id, country) {
var instance = this;
var params = instance.params;
var state_select = jQuery('#'+ select_id);
state_select.html("");
Liferay.Service.Util.WJRegionEnum.getEnumRegionByCountryCode(
{
countryCode: country
},
function(entries) {
instance.displayStates(instance, entries);
}
);
}
});