﻿function Radio(id,name){
	this.id = id;
	this.name = name;
	
	this.label = document.getElementById( id + "_Label" );
	this.label = ( this.label ) ? this.label : {};
	
	this.hidden = document.getElementById( id + "_Hidden" );
	this.img = document.getElementById( id + "_Img" );
	this.textBox = document.getElementById(id + "_TextBox");

	this.label.radio = this.hidden.radio = this.img.radio = this;
	this.img.onmouseover = this.label.onmouseover = function (){ this.radio.mouseover(); }
	this.img.onmouseout = this.label.onmouseout = function (){ this.radio.mouseout(); }
	this.img.onclick = this.label.onclick = function (){ this.radio.click(); }
	
	if( this.textBox ){
		this.textBox.radio = this;
		this.textBox.onfocus = function(){ this.radio.setChecked(true); }
	}
	
	
	if( ! Radio.groups[this.name] ){
		Radio.groups[this.name] = [];
	}
	Radio.groups[this.name][ Radio.groups[this.name].length ] = this;
	Radio.all[this.id] = this;
	
	this.setChecked( this.isChecked() );
}
Radio.groups = [];
Radio.all = {}

Radio.prototype.isChecked = function(){
	return (this.hidden.value == "checked");
}
Radio.prototype.setChecked = function(newValue){
	if( newValue ){
		this.hidden.value = "checked";
		this.img.src = this.img.src.replace( "_unchecked", "_checked" );
		for( var i=0; i<Radio.groups[this.name].length; i++ ){
			if( Radio.groups[this.name][i].id != this.id ){
				Radio.groups[this.name][i].setChecked( false );
			}
		}
		if( this.textBox ){
			this.textBox.focus();
		}
	}
	else{
		this.hidden.value = '';
		this.img.src = this.img.src.replace( "_checked", "_unchecked" );
		if( this.textBox ){
			this.textBox.value = '';
		}
	}
}
Radio.prototype.mouseover = function(){
	if( this.img.src.indexOf("_over") == -1 ){
		this.img.src = this.img.src.replace('.gif','_over.gif');
	}
}
Radio.prototype.mouseout = function(){
	if( this.img.src.indexOf("_over") != -1 ){
		this.img.src = this.img.src.replace('_over.gif','.gif');
	}
}
Radio.prototype.click = function(){
	this.setChecked( true );
}

Radio.callMouseover = function(id){
	var r = Radio.all[id];
	if( r ){
		r.mouseover();
	}
}
Radio.callMouseout = function(id){
	var r = Radio.all[id];
	if( r ){
		r.mouseout();
	}
}
Radio.callClick = function(id){
	var r = Radio.all[id];
	if( r ){
		r.click();
	}
}







function CheckBox(id,disabled){
	this.id = id;
	this.label = document.getElementById( id + "_Label" );
	this.label = ( this.label ) ? this.label : {};
	this.hidden = document.getElementById( id + "_Hidden" );
	this.img = document.getElementById( id + "_Img" );
	this.textBox = document.getElementById(id + "_TextBox");
	this.disabled = disabled;
	
	this.label.radio = this.hidden.radio = this.img.radio = this;
	if( ! this.disabled ){
		this.img.onmouseover = this.label.onmouseover = function (){ this.radio.mouseover(); }
		this.img.onmouseout = this.label.onmouseout = function (){ this.radio.mouseout(); }
		this.img.onclick = this.label.onclick = function (){ this.radio.click(); }
		if( this.textBox ){
			this.textBox.radio = this;
			this.textBox.onfocus = function(){ this.radio.setChecked(true); }
		}
	}
	else{
		// this.label.style.cursor = "not-allowed";
		// this.img.style.cursor = "not-allowed";
	}
	

	this.setChecked( this.isChecked() );
	CheckBox.all[this.id] = this;	
}
CheckBox.all = [];

CheckBox.prototype.isChecked = function(){
	return (this.hidden.value == "checked");
}
CheckBox.prototype.setChecked = function(newValue){
	if( newValue ){
		this.hidden.value = "checked";
		this.img.src = this.img.src.replace( "_unchecked", "_checked" );
		if( this.textBox ){
			this.textBox.focus();
		}
	}
	else{
		this.hidden.value = "";
		this.img.src = this.img.src.replace( "_checked", "_unchecked" );
		if( this.textBox ){
			this.textBox.value = '';
		}
	}
	this.onchange(newValue);
}
CheckBox.prototype.onchange = function(newValue){
	// overwrite this if you want
}
CheckBox.prototype.mouseover = function(){
	if( this.img.src.indexOf("_over") == -1 ){
		this.img.src = this.img.src.replace('.gif','_over.gif');
	}
}
CheckBox.prototype.mouseout = function(){
	if( this.img.src.indexOf("_over") != -1 ){
		this.img.src = this.img.src.replace('_over.gif','.gif');
	}
}
CheckBox.prototype.click = function(){
	this.setChecked( !this.isChecked() );
}

CheckBox.callMouseover = function(id){
	var r = CheckBox.all[id];
	if( r ){
		r.mouseover();
	}
}
CheckBox.callMouseout = function(id){
	var r = CheckBox.all[id];
	if( r ){
		r.mouseout();
	}
}
CheckBox.callClick = function(id){
	var r = CheckBox.all[id];
	if( r ){
		r.click();
	}
}

