/*
 * LovoMoodChooser
 * 
 * shows Categories/Moods in 2 dropdowns and stores values in hidden fields (for cforms)
 * 
 * Data: store[categoryName. dataStore];
 *       dataStore[moodName, msisdn]
 * 
 */

function LovoMoodChooser(catSelectId,moodSelectId,catStoreId,moodStoreId) {
	this.store = new Array();
	this.catSelectId = catSelectId;
	this.moodSelectId = moodSelectId;
	this.catStoreId = catStoreId;
	this.moodStoreId = moodStoreId;
}

LovoMoodChooser.prototype.storeCategory = function (name,moods) {
    this.store[name]= moods;
};

LovoMoodChooser.prototype.initCategorySelect= function (cat,mood) {
	var select = document.getElementById(this.catSelectId);
	if (select == null )  {
		return;
	}
	for(var i=0;i < select.options.length;i++) {
		if(select.options[i].value == cat) {
	    	select.options[i].selected = true;
	    	break;
		}
		else {
			select.options[i].selected = false;	
		}
	}
	this.populateMoodSelect(mood);
};

LovoMoodChooser.prototype.populateMoodSelect = function (mood) {
	var select = document.getElementById(this.catSelectId);
	if (select == null )  {
		return;
	}
    if (select.selectedIndex == -1) {
    	select.options[0].selected = true;
    }
	var value = select.options[select.selectedIndex].value;
	if (!value) return;
    var mood_list = this.store[value];
	var select2 = document.getElementById(this.moodSelectId);
	select2.options.length = 0;
	for(i=0;i<mood_list.length;i++)
	{
		select2.options[i] = new Option(mood_list[i]["name"],mood_list[i]["code"]);
        if (i==0) {
        	select2.options[i].selected = true;
        }
        else if(select2.options[i].value == mood) 
		{
	   		select2.options[i].selected = true;
		}
	}
	/*this.saveMood();*/
	/*this.saveCategory();*/
};

LovoMoodChooser.prototype.saveCategory = function () {
	var select = document.getElementById(this.catSelectId);
	if (select == null )  {
		return;
	}
	var value = select.options[select.selectedIndex].value;
	if (!value) return;
	document.getElementById(this.catStoreId).value = value;
};

LovoMoodChooser.prototype.saveMood = function () {
	var select = document.getElementById(this.moodSelectId);
	if (select == null )  {
		return;
	}
	var value = select.options[select.selectedIndex].value;
	if (!value) return;
	document.getElementById(this.moodStoreId).value = value;
};



