//utility function to include other files
function IncludeJavaScript(jsFile)
{
  document.write('<script type="text/javascript" src="'
    + jsFile + '"></scr' + 'ipt>'); 
}

//include prototype
IncludeJavaScript('http://iris.ecec.com/js/prototype.js');
IncludeJavaScript('http://iris.ecec.com/js/effects.js');

var _Iris = {
	required_elements: null,
  	getMessage: function (){
		//check if there has been a submission
		var result_code = _Iris.urlParam('iris_code');
		
		if(result_code != '')
		{
			var response_text = '';
			var response_class = 'iris';
			
			if(result_code == 0)
			{
				response_text = 'Your form has successfully been submitted. Thank you.';
			}
			else if(result_code == 2)
			{
				response_text = 'This form is no longer active and cannot be submitted.';
				response_class = 'iris_error';
			}
			else if(result_code = 3)
			{
				response_text = 'Characters in the image must match the text';
				response_class = 'iris_error';
			}
			//check what it is
			document.write('<div class="' + response_class +'">' + response_text + '</div>');
		}
	}, 
	checkForm: function(){
		var result = true; 		//assume it will pass
		
		//go through each of the required elements
		_Iris.required_elements.each(function(element){
			//print the name		
			if(!element.empty() && result)
			{
				//get the element
				var domElement = $(element);
				
				//get the element type
				var tempArray = element.split("_");
				
				if(tempArray[1] == 'text' || tempArray[1] == 'textarea'){
					//text elements are easy
					if(domElement.value.blank())
					{
						result = false;
						
						domElement.focus();
						
						alert('Please fill in all required fields');
					}
				}
				else if(tempArray[1] == 'captcha')
				{
					if(domElement.value.blank()){
						result = false;
						
						domElement.focus();
						
						alert('Text must match image text');
					}
				}
				else if(tempArray[1] == 'radio')
				{
					//check if any of these radio elements are selected
					var i = 0;
					var foundResult = false
					
					domElement = $(element + '_' + i);
					
					while(domElement != null && !foundResult)
					{
						if(domElement.checked)
						{
							foundResult = true;
						}
						
						i ++;
						
						domElement = $(element + '_' + i);
					}
					
					//if we went through all of them and none of them were checked
					if(!foundResult)
					{
						result = false;
						
						alert('Please complete all required items');
						
						Effect.Pulsate(element , { pulses: 3, duration: 1.5 });
					}
				}
				else if(tempArray[1] == 'checkbox')
				{
					//check if any of these boxes are checked
					var i = 0;
					var foundResult = false
					
					domElement = $(element + '_' + i);
					
					while(domElement != null && !foundResult)
					{
						if(domElement.checked)
						{
							foundResult = true;
						}
						
						i ++;
						
						domElement = $(element + '_' + i);
					}
					
					//if we went through all of them and none of them were checked
					if(!foundResult)
					{
						result = false;
						
						alert('Please complete all required items');
						
						Effect.Pulsate(element , { pulses: 3, duration: 1.5 });
					}
				}
			}
		});
		
		return result;
	},
	loadRequired: function(s){
		_Iris.required_elements = s.split(",");
	},
	setup: function(s){
		//get the name of all the elements in the form
		
	},
	urlParam: function( name )
	{  
		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
		var regexS = "[\\?&]"+name+"=([^&#]*)";  
		var regex = new RegExp( regexS );  
		var results = regex.exec( window.location.href );  
		if( results == null )   { 
			return "";  
		}
		else{    
			return results[1];
		}
	}
}