function WingRankingValidator(oInput){
	if(oInput.value < oInput.getAttribute("_boundary"))
		return false;	
	return true;
}


function WingMultiValidator(oInput){
	if(oInput.getAttribute("_required")!==null && oInput.value==0)
		return false;	
	if(oInput.getAttribute("_max") !== null && oInput.getAttribute("_max") > 0 && oInput.getAttribute("_max") < oInput.value)
		return false;
	if(oInput.getAttribute("_min") !== null && oInput.getAttribute("_min") > 0 && oInput.getAttribute("_min") > oInput.value)
		return false;
	return true;
}

function WingMultiValidatorAdditional(oInput){
	if(oInput.getAttribute('_additional')!==null && oInput.getAttribute('additional_required')!==null){
		checkbox= document.getElementById(oInput.getAttribute('_additional'));
		if(checkbox && checkbox.checked===true && oInput.value==''){

			return false;
		}
	}
	return true;
}

function WingRankingExtendedValidator(oInput){
	if(oInput.value==null)
		return false;
	else 
		return true;
}


function wingCheckbox(input,field){
	object=input.form.elements[field];
	if(input.checked==true){
		object.value=parseInt(object.value)+1;
	}else if(object.value>0){
		object.value=parseInt(object.value)-1;
	}
}

function WingRankingSetup(formID){
	this.form=document.getElementById(formID);
	this.inputs=new Array();
	this.radioGroups=new Array();
	this.current=false;
	this.ranking=false;
	this.rankings=new Array();
	
	this.newRanking=function(name){
		this.collector=false;
		this.name=name;
		this.inputs=new Array();
		this.required=false;
		this.boundary=false;
		this.errorMsg=false;
		this.addInput=function(input,index){
			if(input.value==0){
				input.checked=true;
			}else{
				input.checked=false;
			}
			input.parent=this;
			input.index=index;
			input.onmousedown=function(){
				values=new Array();
				for(inputIndex=0; inputIndex<this.parent.inputs.length; inputIndex++){
					if(this.parent.inputs[inputIndex].checked){
						values.push(this.parent.inputs[inputIndex].value);
					}
				}
				for(indexValues=0; indexValues<values.length; indexValues++){
					if(values[indexValues]==this.value && this.value!=0 && this.form[this.parent.name+this.name].value!=this.value){
						alert(this.parent.errorMsg);
						this.checked=false;
						return false;
					}
				}
				if(this.value!=this.form[this.parent.name+this.name].value){
					if(this.value==0 && this.form[this.parent.name+this.name].value!=0){
						this.form[this.parent.name].value=parseInt(this.form[this.parent.name].value)-1;
					}else if(input.form[this.parent.name+this.name].value==0){
						this.form[this.parent.name].value=parseInt(this.form[this.parent.name].value)+1;
					}
				}
				this.form[this.parent.name+this.name].value=this.value;
				this.checked=true;						
			}
			this.inputs.push(input);
		}
		this.reset=function(){
			for(i=0;i<this.inputs.length;i++){
				this.inputs[i].checked=false;
			}
		}
	}
	
	this.setupInput=function(oInput){
		if(oInput.type=='radio') {
			if(!oInput.name && oInput.getAttribute('_ranking')!==undefined)
				return false;
			for(cGroups=0;cGroups<this.radioGroups.length;cGroups++) {
				if(this.radioGroups[cGroups].name==oInput.name) {
					this.radioGroups[cGroups].addRadio(oInput);
					return false;
				}
			}
			oRadioGroup=new Object();
			oRadioGroup.tagName='INPUT';
			oRadioGroup.id=null;
			oRadioGroup._ranking=oInput.getAttribute('_ranking');
			oRadioGroup.type='radiogroup';
			oRadioGroup.name=oInput.name;
			oRadioGroup.value='';
			oRadioGroup.checked=false;
			oRadioGroup.required=false;
			oRadioGroup.inputs=Array();
			oRadioGroup.controller=this;
			oRadioGroup.className='default';
			oRadioGroup.error='Input invalid, no description specified!';
			oRadioGroup.onchange=function () {
				//this.controller.verifyInput(this,true);
			}
			oRadioGroup.getAttribute=function(attribute) {
				switch(attribute) {
					case '_required':		return this.required;
					case '_defaultClass':	return 'default';
					case '_errorClass':		return 'error';
					case '_error':			return this.error;
					case '_id':				return this.id;
					default:				return null;
				}
			}
			oRadioGroup.apply=function() {
				for(cRadios=0;cRadios<this.inputs.length;cRadios++) {
					if(this.className=='default')
						this.inputs[cRadios].className=this.inputs[cRadios].getAttribute('_defaultClass');
					if(this.className=='error')
						this.inputs[cRadios].className=this.inputs[cRadios].getAttribute('_errorClass');
				}
			}
			oRadioGroup.addRadio=function(oRadio) {
				if(!oRadio.getAttribute('_defaultClass'))
					oRadio.setAttribute('_defaultClass',oRadio.className);
				if(!oRadio.getAttribute('_errorClass'))
					oRadio.setAttribute('_errorClass',oRadio.className);
				this.inputs.push(oRadio);
				oRadio.radioGroup=this;
				bRequired=oRadio.getAttribute('_required');
				if(bRequired!==null)
					this.required=bRequired;
				sError=oRadio.getAttribute('_error');
				if(sError!==null)
					this.error=sError;
				sId=oRadio.getAttribute('_id');
				if(sId!==null)
					this.id=sId;
				oRadio.onchange=function() {
					this.radioGroup.onchange();
				}
				oRadio.onclick=oRadio.onchange;
			}
			oRadioGroup.validate=function() {
				this.value='';
				this.checked=false;
				for(cRadios=0;cRadios<this.inputs.length;cRadios++) {
					if(this.inputs[cRadios].checked) {
						this.value=this.inputs[cRadios].value;
						this.checked=true;
						return;
					}
				}
			}
			oRadioGroup.addRadio(oInput);
			this.radioGroups.push(oRadioGroup);
			return oRadioGroup;
		}
		return false;
	}
	
	this.setup=function(){
		cInputs=this.form.getElementsByTagName('input');
		for(i=0;i<cInputs.length;i++) {
			if(cInputs[i].getAttribute('_ranking')!=null){
				oInput=this.setupInput(cInputs[i]);
				if(oInput!==false){
					this.inputs.push(oInput);
				}
			}
		}
		for(i=0;i<this.inputs.length;i++){
			if(this.current!=this.inputs[i]._ranking){
				if(this.ranking!==false){
					this.rankings.push(this.ranking);
				}
				this.current=this.inputs[i]._ranking;
				this.ranking=new this.newRanking(this.inputs[i]._ranking);
				//this.ranking.boundary=this.inputs[i].inputs[0].getAttribute('_boundary');
				this.ranking.errorMsg=this.inputs[i].inputs[0].getAttribute('_error');
				if(this.inputs[i].getAttribute('_required')!==undefined){
					this.ranking.required=true;
				}
			}
			//add radio button to the current ranking
			if(this.ranking!=false){
				for(j=0;j<this.inputs[i].inputs.length;j++){
					this.ranking.addInput(this.inputs[i].inputs[j],j);
				}
			}
		}
		if(this.ranking!=false){
			this.rankings.push(this.ranking);
		}
	}
	this.reset=function(radiogroup){
		this.form[radiogroup][0].onmousedown();
		return false;
	}
	this.triggerOnclick=function(radiogroup,value){
		this.form[radiogroup].item(value,this.form[radiogroup]).onmousedown();
	}
	this.setup();
}

// AF20101027: maxlength for textarea's, working on keyup and mouseup
function wing_textarea_maxlength(obj){
	maxlen=parseInt(obj.getAttribute('maxlength'));
	if(maxlen>0 && obj.value.length > maxlen){
		obj.value=obj.value.substr(0,maxlen);
	}
}
