Function.prototype.bind = function(object) {
  var _method = this;
  return function() {
    _method.apply(object, arguments);
  }
}

//------------------------------------------------------------------
//	MyCheckBox
//------------------------------------------------------------------
function MyCheckBoxes(oForm)
{
	if (!oForm)
		return;	
	this.checkboxes = [];
	this.limit = 0;
	this.form = oForm;
	var elems = oForm.elements;	
	if (elems.length == 0)
		return;
	
	for (var i=0;i<elems.length;i++)
	{
		if (elems[i].type.toLowerCase() == 'checkbox')
		{
			var mycheckbox = new MyCheckBox(elems[i],this);
			this.addCheckbox(mycheckbox);
		}
	}
}

MyCheckBoxes.prototype = {
	addCheckbox:function(o)
	{
		this.checkboxes.push(o);
	},
	setLimit:function(nLimit)
	{
		this.limit = nLimit;
	},
	onClicked:function(oCheckbox)
	{
		var nChecked = 0;
		if (this.limit)
		{
			var els = this.form.elements;
			for (var i=0;i<els.length;i++)
			{
				var el = els[i];
				if (el.type == 'checkbox' && el.checked)
				{
					nChecked++;
					if (nChecked > this.limit)
					{
						alert(this.limit + "°³ ±îÁö¸¸ ¼±ÅÃÇÒ ¼ö ÀÖ½À´Ï´Ù");
						oCheckbox.onImgCheckBoxClick();
					}
				}
			}
		}
	},
	toggle:function()
	{
		if (this.toggled == false)
			this.checkall();
		else
			this.uncheckall();
		this.toggled = !this.toggled;

	},
	checkall:function()
	{
		for (var i=0;i<this.checkboxes.length;i++)
			this.checkboxes[i].check();
	},
	uncheckall:function()
	{		
		for (var i=0;i<this.checkboxes.length;i++)
			this.checkboxes[i].uncheck();
	},
	invertall:function()
	{		
		for (var i=0;i<this.checkboxes.length;i++)
			this.checkboxes[i].invert();
	}
}


function MyCheckBox(oCheck,oListener)
{
	this.value = null;
	this.imgCheckbox = null;
	this.checkbox = null;
	this.listener = null;
	this.func = null;
	this.resources = {
		normal	: '/myui/mycheckbox_normal.png',
		checked	: '/myui/mycheckbox_checked_alt.png'
	};
	this.styles = {
		width			: '14px',
		height			: '14px',
		border			: '0px',
		cursor			: 'hand',
		margin		 	: '2px 4px 2px 0px',
		verticalAlign 	: 'middle'
	}
	this.bind(oCheck);
	if (oListener)
		this.listener = oListener;
}


MyCheckBox.prototype = {
	bind:function(oCheck)
	{
		if (!oCheck.form)
			return;		
		this.checkbox = oCheck;
		this.value = oCheck.value;
		this.func = this.checkbox.onclick;
		this.checkbox.onclick = this.onCheckBoxClick.bind(this);
		this.generate();
	},
	check:function()
	{
		this.checkbox.checked = true;
		this.syncImgCheckbox();
	},
	uncheck:function()
	{	
		this.checkbox.checked = false;		
		this.syncImgCheckbox();
	},
	invert:function()
	{	
		this.checkbox.checked = !this.checkbox.checked;		
		this.syncImgCheckbox();
	},
	applyStyles:function(o,styles)
	{
		for (var prop in styles)
			o.style[prop] = styles[prop];
	},		
	generate:function()
	{
		var oImg = this.checkbox.parentNode.insertBefore(document.createElement('img'),this.checkbox);
		this.applyStyles(oImg,this.styles);	
		this.imgCheckbox = oImg;
		this.checkbox.style.position = "absolute";
		this.checkbox.style.left = "-10000px";
		oImg.onclick = this.onImgCheckBoxClick.bind(this);		
		this.syncImgCheckbox();
	},
	syncImgCheckbox:function()
	{
		this.imgCheckbox.src = this.isChecked() ? this.resources.checked : this.resources.normal;		
	},
	isChecked:function()
	{
		return this.checkbox.checked;
	},
	onCheckBoxClick:function()
	{
		this.syncImgCheckbox();		
		if(this.func)
			this.func();
		if (this.listener)
			this.listener.onClicked(this);
	},
	onImgCheckBoxClick:function()
	{
		this.checkbox.checked = !this.checkbox.checked;
		this.onCheckBoxClick();
	}
}






//------------------------------------------------------------------
// MyRadioButton	
//------------------------------------------------------------------
function MyRadioButtons(oForm)
{
	if (!oForm)
		return;	
	this.radiobuttons = [];
	this.form = oForm;
	var elems = oForm.elements;	
	if (elems.length == 0)
		return;	
	this.tmp = [];
	for (var i=0;i<elems.length;i++)
	{
		if (elems[i].type.toLowerCase() == 'radio')
		{
			var name = elems[i].name;
			if (!this.tmp[name])
				this.tmp[name] = [];
			this.tmp[name].push(elems[i]);
		}
	}
	
	for (var i in this.tmp)
	{
		var myradiobutton = new MyRadioButton(this.tmp[i],this);
		this.addRadioButton(myradiobutton);		
	}
		
}

MyRadioButtons.prototype = {
	addRadioButton:function(o)
	{
		this.radiobuttons.push(o);
	}
}



function MyRadioButton(oRadios,oListener)
{
	this.prev = null;
	this.funcs = [];
	this.imgbuttons = [];
	this.radiobuttons = [];
	this.listner = null;
	this.resources = {
		normal	: '/myui/myradiobutton_normal.png',
		checked	: '/myui/myradiobutton_checked_alt.png'
	};
	this.styles = {
		width			: '15px',
		height			: '15px',
		border			: '0px',
		cursor			: 'hand',
		margin		 	: '1px 4px 2px 0px',
		verticalAlign 	: 'middle'
	}
	if (oListener)
		this.listener = oListener;	
	this.bind(oRadios);
}


MyRadioButton.prototype = {
	bind:function(oRadios)
	{
		if (!oRadios[0].form)
			return;

		for (var i=0;i<oRadios.length;i++)
		{		
			var oRadio = oRadios[i];			
			var oImg = oRadio.parentNode.insertBefore(document.createElement('img'),oRadio);
			oImg.src = oRadio.checked ? this.resources.checked : this.resources.normal;
			this.applyStyles(oImg,this.styles);
			
			oRadio._index = i;
			oImg._index = i;
			
			if (oRadio.checked)
				this.prev = oImg;
			
			this.radiobuttons[i] = this.imgbuttons[i] = {radiobutton:oRadio,imgbutton:oImg,value:oRadio.value};				
			
			oRadio.listener = this;
			oImg.listener = this;
			
			
			this.funcs[i] = oRadio.onclick;
						
			oRadio.onclick = this.onRadioButtonClick;
			oImg.onclick = this.onImgButtonClick;
			
			oRadio.style.position = "absolute";
			oRadio.style.left = "-10000px";
		}
		
		return;
	},
	applyStyles:function(o,styles)
	{
		for (var prop in styles)
			o.style[prop] = styles[prop];
	},		
	isChecked:function(nIndex)
	{
		return this.imgbuttons[nIndex].radiobutton.checked;
		
	},
	resetPrev:function()
	{
		if (this.prev)
			this.prev.src = this.resources.normal;
	},
	changeImg:function(oImg,bChecked)
	{
		if(!bChecked) return;
		this.resetPrev();		
		oImg.src = bChecked ? this.resources.checked : this.resources.normal;	
		this.prev = oImg;
	},
	onRadioButtonClick:function()
	{
		if (this.listener.funcs[this._index])
			this.listener.funcs[this._index]();
		var oImg = this.listener.radiobuttons[this._index].imgbutton;		
		this.listener.changeImg(oImg,this.listener.isChecked(this._index));
	},
	onImgButtonClick:function()
	{
		if (this.listener.imgbuttons[this._index].radiobutton.checked)
			return;

		if (this.listener.funcs[this._index])
			this.listener.funcs[this._index]();

		this.listener.imgbuttons[this._index].radiobutton.checked = !this.listener.imgbuttons[this._index].radiobutton.checked;
		this.listener.changeImg(this,this.listener.isChecked(this._index));
	},
	changeIdxImg:function(nIndex)
	{
		var oImg = this.radiobuttons[nIndex].imgbutton;		
		this.changeImg(oImg,this.isChecked(nIndex));
	}
}
