/**
 * Conditional Code Helper Script
 * @author nicSoft
 * @version 648
 */
var fbc = {
	
	FormBoss_Utility_Version: '648',
	
	working: false,
	
	// Hides a field on page load
	hideOnLoad: function(){
	
	},
	
	disableField:function(elt){
		elt.disabled = true;
		this.working = true;
	},
	
	enableField:function(elt){
		elt.disabled = false;
		this.working = false;
		elt.focus();
	},
	
	getSrcElement:function(event){
		var elt;
		if (!event) var event = window.event;
		if (event.target) elt = event.target;
		else if (event.srcElement) elt = event.srcElement;
		if (elt.nodeType == 3) // defeat Safari bug
			elt = elt.parentNode;
		
		return elt;
	},
	
	hideField:function(event, fieldId, animation){
		
		var elt = this.getSrcElement(event);
		this.disableField(elt);
		
		switch(animation){
			case 'fade' :
				this.working = true;
				new Effect.Fade('fb_fld-' + fieldId, { duration:.5, afterFinish:function() { fbc.enableField(elt); } });
				break;
			case 'blind' :
				this.working = true;
				new Effect.BlindUp('fb_fld-' + fieldId, { duration:.5, afterFinish:function() { fbc.enableField(elt); } });
				break;
			default :
				document.getElementById('fb_fld-' + fieldId).style.display = 'none';
				this.enableField(elt);
				break;
		}
		
	},
	
	showField:function(event, fieldId, animation){
		
		var elt = this.getSrcElement(event);
		this.disableField(elt);
		
		switch(animation){
			case 'fade' :
				this.working = true;
				new Effect.Appear('fb_fld-' + fieldId, { duration:.5, afterFinish:function() { fbc.enableField(elt); } }  );
				break;
			case 'blind' :
				this.working = true;
				new Effect.BlindDown('fb_fld-' + fieldId, { duration:.5, afterFinish:function() { fbc.enableField(elt); } });
				break;
			default :
				document.getElementById('fb_fld-' + fieldId).style.display = 'block';
				this.enableField(elt);
				break;
		}
		
	},
	
	hideWrapper:function(event, wrapperId, animation){
		
		var elt = this.getSrcElement(event);
		this.disableField(elt);
		
		switch(animation){
			case 'fade' :
				items = $$('.'+wrapperId);
				items.each(function(t){
					new Effect.Fade(t, { duration:.5, afterFinish:function() { fbc.enableField(elt); } });
				});
				break;
			case 'blind' :
				items = $$('.'+wrapperId);
				items.each(function(t){
					new Effect.BlindUp(t, { duration:.5, afterFinish:function() { fbc.enableField(elt); } });
				});
				break;
			default :
				i = 0;
				a = document.getElementsByTagName("li");
				while (element = a[i++]) {
					if (element.className == wrapperId) {
						element.style.display = 'none';
				    }
				}
				this.enableField(elt);
				break;
		}

	},
	
	showWrapper:function(event, wrapperId, animation){
		
		var elt = this.getSrcElement(event);
		this.disableField(elt);
		
		switch(animation){
			case 'fade' :
				items = $$('.'+wrapperId);
				items.each(function(t){
					new Effect.Appear(t, { duration:.5, afterFinish:function() { fbc.enableField(elt); } });
				});
				break;
			case 'blind' :
				items = $$('.'+wrapperId);
				items.each(function(t){
					new Effect.BlindDown(t, { duration:.5, afterFinish:function() { fbc.enableField(elt); } });
				});
				break;
			default :
				i = 0;
				a = document.getElementsByTagName("li");
				while (element = a[i++]) {
					if (element.className == wrapperId) {
						element.style.display = 'block';
				    }
				}
				this.enableField(elt);
				break;
		}
		
	},
	
	handleEvent:function(event, value, condition, conditionValue, action, field, wrapper, toggle, animation){
		
		pass = false;
		override = false;
		
		if(condition !== ''){
			// test condition
			switch(condition){
				case '!=' :
					if(value != conditionValue){ pass = true; } 
					break;
				case '==' :
					if(value == conditionValue){ pass = true; } 
					break;
				case '<' :
					if(parseInt(value)){
						if(parseInt(value) < parseInt(conditionValue)){ pass = true; }
					} else if(parseFloat(value)){
						if(parseFloat(value) < parseFloat(conditionValue)){ pass = true; }
					} else {
						pass = false;	
					}
					break;
				case '>' :
					if(parseInt(value)){
						if(parseInt(value) > parseInt(conditionValue)){ pass = true; }
					} else if(parseFloat(value)){
						if(parseFloat(value) > parseFloat(conditionValue)){ pass = true; }
					} else {
						pass = false;	
					}
					break;
				case '<=' :
					if(parseInt(value)){
						if(parseInt(value) <= parseInt(conditionValue)){ pass = true; }
					} else if(parseFloat(value)){
						if(parseFloat(value) <= parseFloat(conditionValue)){ pass = true; }
					} else {
						pass = false;	
					}
					break;
				case '>=' :
					if(parseInt(value)){
						if(parseInt(value) >= parseInt(conditionValue)){ pass = true; }
					} else if(parseFloat(value)){
						if(parseFloat(value) >= parseFloat(conditionValue)){ pass = true; }
					} else {
						pass = false;	
					}
					break;
				case 'checked' :
					var targ;
					if (!event) var event = window.event;
					if (event.target) targ = event.target;
					else if (event.srcElement) targ = event.srcElement;
					if (targ.nodeType == 3) // defeat Safari bug
						targ = targ.parentNode;
					if(targ.checked){ pass = true; } 
					break;
				case 'unchecked' :
					var targ;
					if (!event) var event = window.event;
					if (event.target) targ = event.target;
					else if (e.srcElement) targ = e.srcElement;
					if (targ.nodeType == 3) // defeat Safari bug
						targ = targ.parentNode;
					if(!targ.checked){ pass = true; } 
					break;
			}
		} else { 
			// no condition, the event itself is the trigger to perform the action
			pass = true;
		}
		
		// If true, and we failed the condition test, perform the opposite action, such as hide instead of show, then set pass to true
		// We prevent the action if it would be redundant (e.g., already hidden, do not hide again)
		if(toggle){
			if(pass == false){
				switch(action){
					case 'hide' :
						action = 'show';
						break;
					case 'show' :
						action = 'hide';
						break;
				}
			}
			
			if(wrapper === ''){
				
				currentMode = document.getElementById('fb_fld-' + field).style.display;
				
				if(action == 'hide' && currentMode == 'none'){
					return;
				}
				
				if(action == 'show' && currentMode == 'block' || action == 'show' && currentMode == ''){
					return;
				}
				
			}
			
			if(wrapper !== ''){
				
				i = 0;
				a = document.getElementsByTagName("li");
				while (element = a[i++]) {
					if (element.className == wrapper) {
						currentMode = element.style.display;
						break;
				    }
				}
				
				if(action == 'hide' && currentMode == 'none'){
					return;
				}
				
				if(action == 'show' && currentMode == 'block' || action == 'show' && currentMode == ''){
					return;
				}
				
			}
			
			pass = true;
		}
		
		if(pass){
			
			/*
			var targ;
			if (!event) var event = window.event;
			if (event.target) targ = event.target;
			else if (event.srcElement) targ = event.srcElement;
			if (targ.nodeType == 3) // defeat Safari bug
				targ = targ.parentNode;
			targ.disabled = true;
			*/
			
			switch(action){
				case 'hide' :
					if(wrapper !== ''){
						this.hideWrapper(event, wrapper, animation);
					} else {
						this.hideField(event, field, animation);
					}
					break;
				case 'show' :
					if(wrapper !== ''){
						this.showWrapper(event, wrapper, animation);
					} else {
						this.showField(event, field, animation);
					}
					break;
			
			} // action switch
			
		} // if pass
	},
	
	handleCustomCondition:function(event, value){
		
	}
		
};
