var mstCombo = {
	url:'/modules/mstcombo/',

	defMakerText : '',
	defShashuText : '',

	defMaker : '',
	defShashu : '',

	maker : '',
	shashu : '',

	type : '',

	initExec : 0,

	init: function(type) {
		mstCombo.type = type;
		if(mstCombo.type == "top") {
			if($('#ar').val());
			$('#ar').val('');
		}
		mstCombo.main();
	},
	main: function(maker) {

		if(mstCombo.initExec > 2) return false;
		mstCombo.initExec++;

		// set default select value
		if(mstCombo.type == "assess") {
			mstCombo.defMaker = $('#mk').val();
			mstCombo.defShashu = $('#ct').val();
			if(!maker) {
				mstCombo.maker = "";
			}
			else if(maker != "") {
				mstCombo.maker = maker;
			}
			$('#mk').val(maker);
		}
		mstCombo.requestAjax();
	},
	selChange: function(maker) {
		if(!maker) {
			mstCombo.maker = "";
			mstCombo.shashu = "";
		}
		else if(maker != "") {
			mstCombo.maker = maker;
		}
		mstCombo.defMaker = "";
		mstCombo.defShashu = "";

		$('#mk').val(maker);
		mstCombo.requestAjax();
	},
	requestAjax: function(){
		var args = '';
		if(mstCombo.maker!="") {
			args = '?mk=' + encodeURIComponent(mstCombo.maker);
		}
		var mstComboUrl = mstCombo.url + args;
		$.ajax({
				type:'GET', 
				url:mstComboUrl, 
				async:false, 
				dataType: 'json', 
				success: function(json){mstCombo.setSelectBox(json, status)},
				error: function(){
				}
		});
		return false;
	},
	setSelectBox: function(json, status){
		var data = eval(json);
		var stat = status;
		if(!mstCombo.maker) {
			mstCombo.setBrandList(data, stat);
		}
		else {
			mstCombo.setShashuList(data, stat);
		}
		return false;
	},
	setBrandList: function(data, stat){
		var cnt = data.length;
		$("#mkSel").children().remove();
		$('#mkSel').append($('<option>').attr({ value: '' }).text(mstCombo.defMakerText));
		$(data.brand).each(function() {
			if(mstCombo.defMaker == this.brand_cd) {
				$('#mkSel').append($('<option>').attr({value: this.brand_cd, selected:'selected'}).text(this.brand_name));
				$('#mk').val(this.brand_cd);
			}
			else {
				$('#mkSel').append($('<option>').attr({value: this.brand_cd}).text(this.brand_name));
			}
		});
		if(mstCombo.defMaker != "") {
			mstCombo.main(mstCombo.defMaker);
		}
		else {
			var val = [];
			mstCombo.setShashuList(val);
		}
		return false;
	},
	setShashuList: function(data, stat){
		$('#ct').val("");
		$("#ctSel").children().remove();
		$('#ctSel').append($('<option>').attr({ value: '' }).text(mstCombo.defShashuText));
		$(data.shashu).each(function() {
			if(mstCombo.defShashu == this.shashu_cd) {
				$('#ctSel').append($('<option>').attr({ value: this.shashu_cd, selected:'selected'}).text(this.shashu_name));
				$('#ct').val(this.shashu_cd);
			}else{	
				$('#ctSel').append($('<option>').attr({ value: this.shashu_cd }).text(this.shashu_name));
			}
		});
		return false;
	}
}

