/*CHECKBOX*/
function initRichCheckbox() { 
    var els = document.getElementsByTagName('input');
    for(var i=0; i<els.length; i++) {
       if (els[i].type=='checkbox'){
		if(els[i].style.display!='none') { new RichCheckbox(els[i]); }
       } 
    }
} 
   
function RichCheckbox(org) {
    this.state=false;
    this.orgcheckbox=org;
    this.checkboxDiv;
    this.initialize();
} 
RichCheckbox.prototype.initialize = function () {
    
    this.checkboxDiv = document.createElement('div');
    if(this.orgcheckbox.checked==true) {
        this.checkboxDiv.className='richCheckboxOn';
    }
    else {
        this.checkboxDiv.className='richCheckboxOff';
    }
    this.orgcheckbox.parentNode.insertBefore(this.checkboxDiv, this.orgcheckbox);
    var self=this;
    this.checkboxDiv.onclick = function() { 
        self.togglebox();
    }
    this.orgcheckbox.style.display='none';

}
RichCheckbox.prototype.togglebox = function(num) {
    if(this.orgcheckbox.checked==true) {
        this.orgcheckbox.checked=false;
        this.checkboxDiv.className='richCheckboxOff';
    }
    else {
        this.orgcheckbox.checked=true;
        this.checkboxDiv.className='richCheckboxOn';
    }
	if(this.orgcheckbox.onchange) { this.orgcheckbox.onchange(); }
	if(this.orgcheckbox.onclick) { this.orgcheckbox.onclick(); }
}

/*RADIO*/
function initRichRadio() { 
    names=new Array();
    var els = document.getElementsByTagName('input');
    for(var i=0; i<els.length; i++) {
        if (els[i].type=='radio'){
            addnew=true;
            for(j=0; j<names.length; j++) {
                if(names[j]==els[i].name) {
                    addnew=false;
                }
            }
            if(addnew) {
                names[names.length]=els[i].name;
                new RichRadio(els[i].name);
            }
        }
    }
}   
function RichRadio(radioname) {
    this.radioname=radioname;
    this.options=new Array();
    this.initialize();
} 
RichRadio.prototype.initialize = function () {
    
    var els = document.getElementsByTagName('input');
    for(var i=0; i<els.length; i++) {
        if (els[i].type=='radio'&&els[i].name==this.radioname){
            this.options[this.options.length]=Array(els[i],null);
        }
    }
    for(var i=0; i<this.options.length; i++) {
        
        this.options[i][1]=document.createElement('div');
        this.options[i][1].optionid=i;
        if(this.options[i][0].checked==true) {
            this.options[i][1].className='richRadio_on';
        }
        else {
            this.options[i][1].className='richRadio_off';
        }
        var self=this;
        this.options[i][1].onclick=function() { self.toggleOptions(this.optionid); }
        this.options[i][0].parentNode.insertBefore(this.options[i][1], this.options[i][0]);
        this.options[i][0].style.display='none';
    }
}
RichRadio.prototype.toggleOptions = function(newoption) {
    for(i=0; i<this.options.length; i++) {
        this.options[i][0].checked=false;
        this.options[i][1].className='richRadio_off';
    }
    this.options[newoption][0].checked=true;
    this.options[newoption][1].className='richRadio_on';
}
/*RICHSELECT*/
function initRichSelect() { 
    var els = document.getElementsByTagName('select');
    for(var i=0; i<els.length; i++) {
        if(els[i].style.display!='none') { new RichSelect(els[i]);  }
		
    }
}
    
   
function RichSelect(org) {
    this.options=new Array();
    this.optcount=0;
    this.curoption=0;
    this.orgselect=org;
    this.initialize();
    
    this.containerDiv;
    this.mainDiv;
    this.optionsDiv;
    this.expanded=false;
    this.classextra='';
}

RichSelect.prototype.initialize = function () {
    this.id="richSelect_"+this.orgselect.name;
    this.classextra=this.orgselect.className;
    for(var i=0; i<this.orgselect.childNodes.length; i++) {
        if(this.orgselect.childNodes[i].nodeName=='OPTION') {
            this.options[this.optcount]=Array(this.orgselect.childNodes[i].value, this.orgselect.childNodes[i].innerHTML, null, this.orgselect.childNodes[i]);
            if(this.orgselect.childNodes[i].selected==true) {
                this.curoption=this.optcount;
            }
            this.optcount++;
        }
    }
    this.render();

    //this.orgselect.parentNode.insertBefore(myElement, this.orgselect);
}
RichSelect.prototype.render = function () {
    //tempw=this.orgselect.offsetWidth;
    this.orgselect.style.display='none';
    this.containerDiv=document.createElement('div');
    this.containerDiv.className='richSelect'+' '+this.classextra;
    //this.containerDiv.style.width=tempw+'px';
    this.mainDiv=document.createElement('div');
    this.mainDiv.innerHTML=this.options[this.curoption][1];
    this.mainDiv.className='richSelectMain';
    /*if(this.classextra=='smallSelect') {
        this.mainDiv.style.width=tempw-19+'px';
    }
    else {
        this.mainDiv.style.width=tempw-30+'px';
    }*/
    var self=this;
    this.mainDiv.onclick=function() { self.toggleOptions(); }
    this.mainDiv.onmouseover=function() { self.mainOver(); }
    this.mainDiv.onmouseout=function() { self.mainOut(); }
    this.containerDiv.appendChild(this.mainDiv);
    
    this.optionsDiv=document.createElement('div');
    this.optionsDiv.className='richSelectOptions';
    this.optionsDiv.style.display='none';
    if(this.options.length>8) {
        this.optionsDiv.style.overflow='auto';
        this.optionsDiv.style.height=25*8+'px';
        //this.optionsDiv.style.width=tempw+20+'px';
        //prawyborder=false;
    }
    //else { prawyborder=true; this.optionsDiv.style.width=tempw+2+'px';}
    for(var i=0; i<this.options.length; i++) {
        this.options[i][2]=document.createElement('div');
        //this.options[i][2].style.width=(this.optionsDiv.offsetWidth-10)+'px';
        this.options[i][2].innerHTML=this.options[i][1];
        if(i==this.curoption) {
            this.options[i][2].className='richSelectOption richSelectOptionCurrent';
        }
        else {
            this.options[i][2].className='richSelectOption';
        }
        this.options[i][2].optionid=i;
        if(this.options[i][3].disabled==false) {
            this.options[i][2].onmouseover=function() { self.optionOver(this.optionid);}
            this.options[i][2].onmouseout=function() { self.optionOut(this.optionid);}
            this.options[i][2].onclick=function() { self.optionClick(this.optionid);}
        }
        //if(!prawyborder) {
		//	this.options[i][2].style.borderRightWidth='0px';
        //}
        this.optionsDiv.appendChild(this.options[i][2]);
        
    }
    
    this.containerDiv.appendChild(this.optionsDiv);
    this.orgselect.parentNode.insertBefore(this.containerDiv, this.orgselect); 
    
}
RichSelect.prototype.toggleOptions = function () {
    if(this.expanded) {
        this.optionsDiv.style.display='none';
        this.expanded=false;
        this.mainDiv.className='richSelectMain';
    }
    else {
        this.expanded=true;
        this.optionsDiv.style.display='block';
        this.mainDiv.className='richSelectMain richSelectMainExpand';
    }
}
RichSelect.prototype.mainOver = function () {
    if(!this.expanded) { this.mainDiv.className='richSelectMain  richSelectMainOver'; }
}
RichSelect.prototype.mainOut = function () {
    if(!this.expanded) { this.mainDiv.className='richSelectMain'; }
}
RichSelect.prototype.optionOver = function (optover) {
    if(this.curoption==optover) {
        this.options[optover][2].className='richSelectOption richSelectOptionCurrent richSelectOptionOver';
    }
    else {
        this.options[optover][2].className='richSelectOption richSelectOptionOver';    
    }
}
RichSelect.prototype.optionOut= function (optout) {
    if(this.curoption==optout) {
        this.options[optout][2].className='richSelectOption richSelectOptionCurrent';
    }
    else {
        this.options[optout][2].className='richSelectOption';
    }
}
RichSelect.prototype.optionClick = function (optclick) {
    this.curoption=optclick;
    this.rerender();
    this.toggleOptions();
    this.update();
    if(this.orgselect.onchange) { this.orgselect.onchange(); }
}
RichSelect.prototype.rerender = function () {
    this.mainDiv.innerHTML=this.options[this.curoption][1];
    for(var i=0; i<this.options.length; i++) {
        if(i==this.curoption) {
            this.options[i][2].className='richSelectOption richSelectOptionCurrent';
        }
        else {
            this.options[i][2].className='richSelectOption';
        }
    }   
}
RichSelect.prototype.update = function () {
    for(var i=0; i<this.options; i++) {
        this.options[i][3].selected=false;
    }
    this.options[this.curoption][3].selected=true;
}

function initRichForms() { /*initRichRadio();*/ initRichCheckbox(); initRichSelect(); }
    


if (window.addEventListener) {
	window.addEventListener("load",initRichForms,false);
} else if (window.attachEvent) {
	window.attachEvent("onload",initRichForms);
} else {
	window.onload = function() {initRichForms();}
}