/****************************************
| Checks dependencies of divs and  
| shows or hides them depending    
| on conditions                    
|
| Created by:	 James Hicks
| Date Created:	 2007-09-04
|
****************************************/

window.addEvent('domready', function() {
	
	var questionDivs = $ES('div.left div *');
	
	questionDivs.each(function(div) {
			if(div.getAttribute("dependancies") != null) {
				var dependancies = div.getAttribute("dependancies").split("|").include(div);
				if (dependancies.length == 3) 
				{setUpEvent(dependancies);}
				
				else if (dependancies.length >= 4)
				{setUpEventOR(dependancies);}
			} 
	});
	
	function setUpEvent(divInfomatics)
	{ 
		var watchField = $(divInfomatics[0]);
		var condition  = divInfomatics[1];
		var reveal     = divInfomatics[2];
		var conditionIsEqualTo = "";
		var eventType = 'change';
		if (window.ie7 || window.ie6) eventType = 'focus';
		
		// Format the condition
		if (condition.indexOf("!=") > -1)
		{conditionIsEqualTo = "!=";}
		else if (condition.indexOf(">=") > -1)
		{conditionIsEqualTo = ">=";}
		else if (condition.indexOf("<=") > -1)
		{conditionIsEqualTo = "<=";}
		else
		{conditionIsEqualTo = "==";}
		
		condition = condition.replace(/!/g, "").replace(/=/g, ""); // Clean
		condition = condition.replace(/</g, "").replace(/>/g, ""); // Clean stage2
		if (condition.indexOf("'") == -1)
		{
		    // Wrap in single quotes
		    condition = "'" + condition + "'";
		    // Just in case check and replace double single quotes
		    condition = condition.replace(/''/g, "'");	    
		}
		// Now prefix != or == or >= or <=
		condition = conditionIsEqualTo + condition;
		
		// Radio Button work around
		if (watchField.className == "radioSet") 
		{
			var radioButtons = $ES('input', watchField);
			
			radioButtons.each(function(radio) 
			{									   				   
				if (eval("radio.value " + condition))
				{
					if (radio.checked == true) revealing(reveal, true);
					radio.addEvent(eventType, function(event) 
					{
						event = new Event(event).stop();
				 		revealing(reveal, true);						
					});
				} 
				else 
				{
					if (radio.checked == true) revealing(reveal, false);
					radio.addEvent(eventType, function(event) 
					{
						event = new Event(event).stop();
						var inputs = $ES('input', reveal); inputs.each(function(input) { input.value = ''; });
				 		revealing(reveal, false);
						
					});
				}
			});
		// End radio button work around
		} 
		else 
		{
		 
		    var evalString = "if(watchField.value "+condition+")\n\r{ revealing(reveal,true); }\n\r else\n\r { var inputs = $ES('input', reveal);\n\r inputs.each(function(input) { input.value = ''; });\n\r revealing(reveal,false);\n\r }";
		    eval(evalString);
		
			watchField.addEvent('change', function(event) {
				event = new Event(event).stop();
				eval("if(watchField.value "+condition+") { revealing(reveal,true); } else { var inputs = $ES('input', reveal); inputs.each(function(input) { input.value = ''; }); revealing(reveal,false); }");
			});
		}
	}			
	
	
		function setUpEventOR(divInfomatics)
	{ 
	//TODO WRITE LOGIC TO ALLOW OR FUNCTIONALITY IN DEPENDANCIES

		var watchField = $(divInfomatics[0]);
		var condition1  = divInfomatics[1];
		var condition2  = divInfomatics[2];
		var reveal     = divInfomatics[3];
		var condition1IsEqualTo = "";
		var condition2IsEqualTo = "";
		var eventType = 'change';
		if (window.ie7 || window.ie6) eventType = 'focus';
		
		// Format condition1
		if (condition1.indexOf("!=") > -1)
		{condition1IsEqualTo = "!=";}
		else if (condition1.indexOf(">=") > -1)
		{condition1IsEqualTo = ">=";}
		else if (condition1.indexOf("<=") > -1)
		{condition1IsEqualTo = "<=";}
		else
		{condition1IsEqualTo = "==";}
		
		// Format condition2
		if (condition2.indexOf("!=") > -1)
		{condition2IsEqualTo = "!=";}
		else if (condition2.indexOf(">=") > -1)
		{condition2IsEqualTo = ">=";}
		else if (condition2.indexOf("<=") > -1)
		{condition2IsEqualTo = "<=";}
		else
		{condition2IsEqualTo = "==";}
		
		condition1 = condition1.replace(/!/g, "").replace(/=/g, ""); // Clean
		condition1 = condition1.replace(/</g, "").replace(/>/g, ""); // Clean stage2
		condition2 = condition2.replace(/!/g, "").replace(/=/g, ""); // Clean
		condition2 = condition2.replace(/</g, "").replace(/>/g, ""); // Clean stage2
		
		if (condition1.indexOf("'") == -1)
		{
		    // Wrap in single quotes
		    condition1 = "'" + condition1 + "'";
		    // Just in case check and replace double single quotes
		    condition1 = condition1.replace(/''/g, "'");	    
		}
		if (condition2.indexOf("'") == -1)
		{
		    // Wrap in single quotes
		    condition2 = "'" + condition2 + "'";
		    // Just in case check and replace double single quotes
		    condition2 = condition2.replace(/''/g, "'");	    
		}
		// Now prefix != or == or >= or <=
		condition1 = condition1IsEqualTo + condition1;
		condition2 = condition2IsEqualTo + condition2;
	
		{
		 
		    var evalString = "if(watchField.value "+condition1+")\n\r{ revealing(reveal,true); }\n\r else if(watchField.value "+condition2+")\n\r{ revealing(reveal,true); }\n\r else\n\r { var inputs = $ES('input', reveal);\n\r inputs.each(function(input) { input.value = ''; });\n\r revealing(reveal,false);\n\r }";

			watchField.addEvent('change', function(event) {
				event = new Event(event).stop();
				eval("if(watchField.value "+condition1+") { revealing(reveal,true); } else if(watchField.value "+condition2+") { revealing(reveal,true); } else { var inputs = $ES('input', reveal); inputs.each(function(input) { input.value = ''; }); revealing(reveal,false); }");
			});
		}
	}			

	
	   

	function revealing(expose, visibility){
		if (visibility == true)
		{
			expose.style.display = 'block';
			
			//Validation return
			var fields = expose.getElements('*');
			fields.each(function(field) {
					if(field.getAttribute('validatorsStore') != null){
						var valstore = field.getAttribute('validatorsStore');
						field.removeProperty('validatorsStore');
						field.setProperty('validators',valstore);
					}
			});
			$('errors').empty();
			
			//end validation return
			
			//Fix sifr
			if (expose.tagName == "FIELDSET")
			{
				var legend = $ES('h3', expose);
				var legendText;
				if (window.ie)
				{
				    if(legend[0] != null)
				    {legendText = legend[0].getElement("span").innerHTML;}
				    else legendText = "";
				}
				else
				{
				    if(legend[0] != null)
				    {
				    var spans = legend[0].getElementsByClassName("sIFR-alternate");
				    legendText = spans[0].innerHTML;
				    }
				    else legendText = "";
				    
				}
				
				// Delete everything in expose
				var toDelete = legend.getElements('*');
				
				toDelete.each(function(element) {
					element.remove();
				});
				
				if(legend[0] != null)
				{
				legend[0].innerHTML = legendText;
				legend[0].className = "";
				
				sIFR.replaceElement(named({sSelector:"h3", sFlashSrc:"/_include/flash/castle.swf", startColour:"#C4C4C4", endColour:"#333333", sWmode:"transparent"}));
				}
			}
			// Stop fixing sifr
		} 
		else 
		{
			expose.style.display = 'none';
			//Validation remove
			var fields = expose.getElements('*');
			fields.each(function(field) 
			{
					if(field.getAttribute('validators') != null)
					{
						var valstore = field.getAttribute('validators');
						field.removeProperty('validators');
						field.setProperty('validatorsStore',valstore);
					}
			});
			$('errors').empty();
			
			//end validation remove			
		}
	}
});
	
