// JavaScript Document

var controls=new controlsWrapper();
function controlsWrapper() {

	this.isIE=(navigator.appName=='Microsoft Internet Explorer');

	this.browserIsCapable = function () {
		return (document.getElementById && document.createElement);
	}
	
	this.createControl = function (elementType) {
		if (this.browserIsCapable()) {
			return document.createElement(elementType);
		} else {
			alert('Your browser is not capable to create elements!');
			return null;
		}
	}

	this.createTag = function (elementType) {
		if (arguments.length==0) {
			alert('input type not specified');
			return null;
		} else {
			if (arguments.length>=1) {
				theObj=this.createControl(elementType);
				if (theObj==null) return;
				theObj.type=arguments[0];
			}
			if (arguments.length>=2) {
				for (var attr in arguments[1]) {
					if (controls.isIE && (attr=='class')) 
						theObj.setAttribute('className',arguments[1][attr]);
					else
						theObj.setAttribute(attr,arguments[1][attr]);
				}
			}
			if (arguments.length==3) {
				for (var evnt in arguments[2]) {
					if (theObj.addEventListener)
						theObj.addEventListener(evnt,eval(arguments[2][evnt]),false);
					else if (this.isIE) {
						theObj.attachEvent('on'+evnt,eval(arguments[2][evnt]));
					}
				}
			}
			return theObj;
		}
	}

	this.createInput = function () {
		if (arguments.length==0) {
			alert('input type not specified');
			return null;
		} else {
			if (arguments.length>=1) {
				theObj=this.createControl('input');
				if (theObj==null) return;
				theObj.type=arguments[0];
			}
			if (arguments.length>=2) {
				for (var attr in arguments[1]) {
					if (attr=='class') {
						if (this.isIE)
							theObj.className=arguments[1][attr];
						else
							theObj.setAttribute(attr,arguments[1][attr]);
					} else
						theObj.setAttribute(attr,arguments[1][attr]);
				}
			}
			if (arguments.length==3) {
				for (var evnt in arguments[2]) {
					if (theObj.addEventListener)
						theObj.addEventListener(evnt,eval(arguments[2][evnt]),false);
					else if (this.isIE) {
						theObj.attachEvent('on'+evnt,eval(arguments[2][evnt]));
					}
				}
			}
			return theObj;
		}
	}
	this.createTextBox = function (ctrlAttr,ctrlEvents) {
		if (arguments.length==0)
			return this.createInput('text');
		else if (arguments.length==1)
			return this.createInput('text',ctrlAttr);
		else if (arguments.length==2)
			return this.createInput('text',ctrlAttr,ctrlEvents);
		else
			return null;
	}

	this.createFileBox = function (ctrlAttr,ctrlEvents) {
		if (arguments.length==0)
			return this.createInput('file');
		else if (arguments.length==1)
			return this.createInput('file',ctrlAttr);
		else if (arguments.length==2)
			return this.createInput('file',ctrlAttr,ctrlEvents);
		else
			return null;
	}

	this.createButton = function (ctrlAttr,ctrlEvents) {
		if (arguments.length==0)
			return this.createInput('button');
		else if (arguments.length==1)
			return this.createInput('button',ctrlAttr);
		else if (arguments.length==2)
			return this.createInput('button',ctrlAttr,ctrlEvents);
		else
			return null;
	}

	this.createImg = function (ctrlAttr,ctrlEvents) {
		if (arguments.length==0)
			return this.createTag('img');
		else if (arguments.length==1)
			return this.createTag('img',ctrlAttr);
		else if (arguments.length==2)
			return this.createTag('img',ctrlAttr,ctrlEvents);
		else
			return null;
	}
	
	this.createIframe = function (ctrlAttr,ctrlEvents) {
		if (arguments.length==0)
			return this.createTag('iframe');
		else if (arguments.length==1)
			return this.createTag('iframe',ctrlAttr);
		else if (arguments.length==2)
			return this.createTag('iframe',ctrlAttr,ctrlEvents);
		else
			return null;
	}
	
	this.setEvents = function(theObj,ctrlEvents) {
		if (arguments.length<2)
			alert('You must specify the Object and the Events to be set');		
		else {
			for (var evnt in arguments[1]) {
				if (theObj.addEventListener) {
					if (arguments[1][evnt]!='')
						theObj.addEventListener(evnt,eval(arguments[1][evnt]),false);
					else
						theObj.removeEventListener(evnt,eval(arguments[1][evnt]),false);
				} else if (this.isIE) {
					if (arguments[1][evnt]!='')
						theObj.attachEvent('on'+evnt,eval(arguments[1][evnt]));
					else
						theObj.detachEvent('on'+evnt,eval(arguments[1][evnt]));
				}
			}
			return theObj;
		}
	}
	this.setAttributes = function(theObj,ctrlAttr) {
		if (arguments.length<2)
			alert('You must specify the Object and the Attributes to be set');		
		else {
			for (var attr in arguments[1]) {
				theObj.setAttribute(attr,arguments[1][attr]);
			}
			return theObj;
		}
	}
	
	this.clearSelect = function (theObj)  {
		if (typeof(theObj)=='string')
			selBox=document.getElementById(theObj);
		else
			selBox=theObj;
			
		while(selBox.options.length>0)
			selBox.remove(0);
	}
	
	this.getSelectedOption = function (theObj) {
		if (typeof(theObj)=='string')
			selBox=document.getElementById(theObj);
		else
			selBox=theObj;
			
//		alert('selectedIndex de '+selBox.name+': '+selBox.selectedIndex);
		if (selBox.selectedIndex!=-1) {
			return selBox.options[selBox.selectedIndex].value;
		}
		return '';
	}
	
	this.getComboText = function (theObj,value) {
		if (typeof(theObj)=='string')
			selBox=document.getElementById(theObj);
		else
			selBox=theObj;
			
		for(i=0; i<selBox.options.length; i++) {
			if (selBox.options[i].value==value) {
				return selBox.options[i].text;
			}
		}
		return '--';
	}
	
	this.getTextValue = function (theObj) {
		if (typeof(theObj)=='string')
			txtBox=document.getElementById(theObj);
		else
			txtBox=theObj;
			
		return txtBox.value;
	}
	
	this.selectOption = function(theObj,selectedValue) {
		if (typeof(theObj)=='string')
			selBox=document.getElementById(theObj);
		else
			selBox=theObj;
			
		for(i=0; i<selBox.options.length; i++) {
			if (selBox.options[i].value==selectedValue) {
				selBox.options[i].selected=true;
				return;
			}
		}
	}

	this.addOption = function (theObj,text,value) {
		if (typeof(theObj)=='string')
			selBox=document.getElementById(theObj);
		else
			selBox=theObj;
			
		opt=new Option(text,value);
		try {
			selBox.add(opt,null);
		} catch (e) {
			selBox.add(opt);
		}
	}

	this.moveOptionsBetweenCombos = function (srcObj,dstObj,allItems) {
		if (typeof(srcObj)=='string')
			srcSelect=document.getElementById(srcObj);
		else
			srcSelect=srcObj;
		
		if (typeof(dstObj)=='string')
			dstSelect=document.getElementById(dstObj);
		else
			dstSelect=dstObj;
		
		if (allItems==true) {
			for(idx=0; idx<srcSelect.options.length; idx++) {
				option=srcSelect.options[idx];
				controls.addOption(dstSelect,option.text,option.value);
			}
			for(i=(srcSelect.options.length-1); i>=0; i--) {
				srcSelect.remove(i);
			}
		} else {
			var selectedArray = new Array();
			var count = 0;
			for(opt=0; opt<srcSelect.options.length; opt++) {
				option=srcSelect.options[opt];
				if (option.selected) {
					selectedArray[count] = opt;
					count++;
					controls.addOption(dstSelect,option.text,option.value);
				}
			}
			for(i=(selectedArray.length-1); i>=0; i--) {
				srcSelect.remove(selectedArray[i]);
			}
		}
	}

	this.getOptionsAsString = function(theObj,separator) {
		if (typeof(theObj)=='string')
			selBox=document.getElementById(theObj);
		else
			selBox=theObj;

		if (selBox.options.length==0) return '';
		allOptions=new Array();
		for(i=0; i<selBox.options.length; i++) {
			allOptions[i]=selBox.options[i].value;
		}
		return allOptions.join(separator);
	}

	this.getSelectedRadio = function ( theObj ) {

		if (typeof(theObj)=='string')
			radioObj=document.getElementById(theObj);
		else
			radioObj=theObj;


		if(!radioObj)
			return "";
			
		var radioLength = radioObj.length;
		if(radioLength == undefined)
			if(radioObj.checked)
				return radioObj.value;
			else
				return "";
		for(var i = 0; i < radioLength; i++) {
			if(radioObj[i].checked) {
				return radioObj[i].value;
			}
		}
		return "";

		var value = '';

		alert(theRadioField.value.length);
		
		return theRadioField.value;
		alert(theRadioField.name);
		for (var i=0; i<theRadioField.length;  i++) {
			if (theRadioField[i].checked) {
				value = theRadioField[i].value;
				break;			
			}
		}
		
		return value ;
	}
	

}