/*
	File: wws-index-form.js
	Author: Abram Adams
	Date: 1/31/2008
	Comment: This file will create a dynamic form
	with validation for selecting the weighting
	criteria for custom indexes.
	
 */

Ext.onReady(function(){

    Ext.QuickTips.init();
	var total = 0;	
	

	function convertTextBoxes(fieldName){ 
		var opt = new Object ();
		var arr = new Array();
		if (Ext.get(fieldName).dom.attributes['regex']){
			list = Ext.get(fieldName).dom.attributes['regex'].nodeValue.split(',');
			for(i=0;i<list.length;i++){
				opt[list[i].split(':')[0]]=list[i].split(':')[1];
			}
			Ext.get(fieldName).set({'title':opt.description}); 
		}
		if (!opt.min){
			opt.min = -50;
			opt.max = 50;
		}
		task = new Ext.util.DelayedTask( sumForm,null,[fieldName]);
		
		var f1 = new Ext.ux.form.Spinner({
			allowBlank:false,
			applyTo: fieldName,
			invalidText:opt.description,
			id: fieldName.id,
			vtypeText:opt.description,
				invalid: Ext.form.NumberField.prototype.invalid,
				validateValue: Ext.form.NumberField.prototype.validateValue,
				parseValue: Ext.form.NumberField.prototype.parseValue,
				format: Ext.form.NumberField.prototype.format,
				minText: Ext.form.NumberField.prototype.minText,
				maxText: Ext.form.NumberField.prototype.maxText,
				nanText: Ext.form.NumberField.prototype.nanText,
				fireEvent:Ext.form.TextField.prototype.fireEvent,
			validator: function (v){				
				this.el[v < 0 ? 'addClass' : 'removeClass']("redText");
				if (v <= parseInt(opt.max) && v >= parseInt(opt.min)){
						
						task.delay( 50 , null,null, [fieldName]);
						//d = new Ext.util.DelayedTask(function(this){sumForm(this)}).delay(500)
						//d.delay(500,sumForm(this));
					return true;
				}else{			
					this.setValue( 0 );
					return opt.description;
				}
			},
			onMouseUp : function() {
					this.trigger.removeClass(this.__tmpccls);
					this.validate(fieldName);      // check value
				},			
			minValue:opt.min, 
			maxValue:opt.max,
			validationEvent:'change',
			allowBlank:true,
			strategy: new Ext.ux.form.Spinner.NumberStrategy({minValue:opt.min, maxValue:opt.max})
		});

	}

	sumForm = function (item){
			var total = 0;
			var runningTotal = Ext.get('runningTotal');
			var eventItem = item;

		 		Ext.each(Ext.query(".weighting"), function(item){
			 		if (!isNaN(Math.abs(parseInt(item.value)))) {
			 			//console.log("Item value: " + Math.abs(parseInt(item.value)) + " Running Total:" + total);
			 			total += Math.abs(parseInt(item.value));
			 		}
			 		runningTotal.update(total);
					// If running total is greater than 100, we need to reduce to 100
			 		if (runningTotal.dom.innerHTML > 100) {
							//console.log(eventItem);
							if (eventItem.value < 0) {
								newval = parseInt(eventItem.value) + parseInt((runningTotal.dom.innerHTML - 100));
							}else{
								newval = parseInt(eventItem.value) - parseInt((runningTotal.dom.innerHTML - 100));
							}
						 	Ext.getCmp(eventItem.id).setValue(newval);								

							total = 100;
							runningTotal.update(total);
							Ext.Msg.alert('Status', 'Total Must Not Exeed 100!');
							thisEl = Ext.get('generateIndex')
							thisEl.dom.disabled = true;
							thisEl.dom.className= "disabled-button-link";	
							//document.getElementById('wwsindex').value = 'none';
						
					}else if (runningTotal.dom.innerHTML == 100){
						thisEl = Ext.get('generateIndex')
						thisEl.dom.disabled = false;
						thisEl.dom.className= "button-link";
					}else{
						thisEl = Ext.get('generateIndex')
						thisEl.dom.disabled = true;
						thisEl.dom.className= "disabled-button-link";	
						//document.getElementById('wwsindex').value = 'none';
					}
					});
				if (Ext.get('generateIndex').dom.disabled == true){
					Ext.each(Ext.query(".wws-index-highlighted"),function (el){
						el.className = 'wws-index';
						document.getElementById('wwsindex').value = 'none';
					});						
				}
	};
	
	
	Ext.each(Ext.query(".weighting"),
		function (item){ 
			convertTextBoxes(item);
			Ext.getCmp(item.id).on("valid",
				function(item){
					task.delay( 50 , null, [item]);
				}
			);
			//task.delay( 0 , null, item);
		} 
	);
	
});
	
