window.onload = function()
{
	debugging = false;
	
	formImages = optionClicker();
	formImages.flipOptionButtons();
	formImages.clickableOptionButtons();
	
	formTextBorders = inputTextBorders();
	formTextBorders.initTextBorders();
	
	formSubmitters = formSubmission();
	formSubmitters = initAnchorsSubmits();
	formSubmitters = initButtonSubmits();
	
	docDropDowns = dropDownClicker();
	docDropDowns.initDropDowns();
	docDropDowns.initAnchors();
}

optionClicker = function()
{
	this.checkbox_checked = new Image;
	this.checkbox_checked.src = '/resources/images/checkbox_checked.jpg';
	this.checkbox_unchecked = new Image;
	this.checkbox_unchecked.src = '/resources/images/checkbox_unchecked.jpg';
	this.checkbox_height = 28;
	this.checkbox_width = 28;
	
	this.radio_checked = new Image;
	this.radio_checked.src = '/resources/images/radio_checked.jpg';
	this.radio_unchecked = new Image;
	this.radio_unchecked.src = '/resources/images/radio_unchecked.jpg';
	this.radio_height = 29;
	this.radio_width = 29;
	
	this.showing_dropdown = false;

	this.flipOptionButtons = function()
	{
		var survey_form_id = document.getElementById('survey_form');
		var kids = document.getElementsByTagName('INPUT');
		
		for(var i=0; i < kids.length; i++)
		{
			if(kids[i].type == 'checkbox')
			{
				this_kid_id = document.getElementById(kids[i].id);
				var this_kid_alt = kids[i].alt;
				
				if(debugging == false)
				{
					this_kid_id.style.display = 'none';
				}

				var new_checkbox_img = document.createElement("img");
				var new_checkbox_img_id = kids[i].id + '~img_' + i;

				new_checkbox_img.setAttribute('class', 'checkbox_click');
				new_checkbox_img.setAttribute('id', new_checkbox_img_id);
				
				if(kids[i].checked == true)
				{
					new_checkbox_img.setAttribute('src', this.checkbox_checked.src);
				}
				else
				{
					new_checkbox_img.setAttribute('src', this.checkbox_unchecked.src);
				}
				
				new_checkbox_img.setAttribute('group', this_kid_alt);
				new_checkbox_img.setAttribute('height', this.checkbox_height);
				new_checkbox_img.setAttribute('width', this.checkbox_width);

				var new_checkbox_img_style = "vertical-align: middle;";
				new_checkbox_img.style.cssText = new_checkbox_img_style;

				this_kid_id.parentNode.insertBefore(new_checkbox_img, this_kid_id);
			}
			
			if(kids[i].type == 'radio')
			{
				var this_kid_id = document.getElementById(kids[i].id);
				var this_kid_name = kids[i].name;
				var this_kid_alt = kids[i].alt;
				
				if(debugging == false)
				{
					this_kid_id.style.display = 'none';
				}

				var new_radio_img = document.createElement("img");
				var new_radio_img_id = kids[i].id + '~img_' + i;

				new_radio_img.setAttribute('class', 'radio_click');
				new_radio_img.setAttribute('id', new_radio_img_id);
				
				if(kids[i].checked == true)
				{
					new_radio_img.setAttribute('src', this.radio_checked.src);
				}
				else
				{
					new_radio_img.setAttribute('src', this.radio_unchecked.src);
				}
				
				new_radio_img.setAttribute('rel', this_kid_name);
				new_radio_img.setAttribute('group', this_kid_alt);
				new_radio_img.setAttribute('height', this.radio_height);
				new_radio_img.setAttribute('width', this.radio_width);
				
				var new_radio_img_style = "vertical-align: middle;";
				new_radio_img.style.cssText = new_radio_img_style;

				this_kid_id.parentNode.insertBefore(new_radio_img, this_kid_id);
			}
		}
	}


	this.clickableOptionButtons = function()
	{
		var kids = document.getElementsByTagName('IMG');
		for(var i=0; i < kids.length; i++)
		{
			if(kids[i].getAttribute("class") == 'checkbox_click')
			{
				kids[i].onclick = this.checkboxClick;
			}
			
			if(kids[i].getAttribute("class") == 'radio_click')
			{
				kids[i].onclick = this.radioClick;
			}
		}
	}
	
	
	this.checkboxClick = function()
	{
		var checkbox_image_id = document.getElementById(this.id);
		var real_checkbox = this.id.split('~');
		var this_real_checkbox_id = document.getElementById(real_checkbox[0]);
		
		
		if(checkbox_image_id.getAttribute("group"))
		{
			var this_checkbox_parent_group = checkbox_image_id.getAttribute("group");
			var this_checkbox_parent = this_checkbox_parent_group.split('~');
			document.getElementById(this_checkbox_parent[0]).checked = true;

			var siblings = document.getElementsByTagName('IMG');
			
			for(var i=0; i < siblings.length; i++)
			{
				if((siblings[i].getAttribute("group") == this_checkbox_parent[0]) && (siblings[i].getAttribute("group") != this_checkbox_parent))
				{
					switch(siblings[i].getAttribute("class"))
					{
						case 'radio_click':
							siblings[i].src = formImages.radio_checked.src;
							break;
						case 'checkbox_click':
							siblings[i].src = formImages.checkbox_checked.src;
							break;
					}
				}
			}
		}

		switch(checkbox_image_id.src)
		{
			case formImages.checkbox_checked.src:
				checkbox_image_id.src = formImages.checkbox_unchecked.src;
				this_real_checkbox_id.checked = false;
					// alert(this_real_checkbox_id.checked);
				break;
			case formImages.checkbox_unchecked.src:
				checkbox_image_id.src = formImages.checkbox_checked.src;
				this_real_checkbox_id.checked = true;
					// alert(this_real_checkbox_id.checked);
				break;
		}
	}
	
	
	this.radioClick = function()
	{
		var radio_image_id = document.getElementById(this.id);
		var real_radio = this.id.split('~');
		var this_real_radio_id = document.getElementById(real_radio[0]);
		
		var this_radio_group = this_real_radio_id.getAttribute("name");
		var siblings = document.getElementsByTagName('IMG');

		for(var i=0; i < siblings.length; i++)
		{
			if(siblings[i].getAttribute("rel") == this_radio_group)
			{
				siblings[i].src = formImages.radio_unchecked.src;
			}
		}
		
		if(radio_image_id.getAttribute("group"))
		{
			var this_radio_parent_group = radio_image_id.getAttribute("group");
			var this_radio_parent = this_radio_parent_group.split('~');
			document.getElementById(this_radio_parent[0]).checked = true;

			for(var i=0; i < siblings.length; i++)
			{
				if((siblings[i].getAttribute("group") == this_radio_parent[0]) && (siblings[i].getAttribute("group") != this_radio_parent))
				{
					switch(siblings[i].getAttribute("class"))
					{
						case 'radio_click':
							siblings[i].src = formImages.radio_checked.src;
							break;
						case 'checkbox_click':
							siblings[i].src = formImages.checkbox_checked.src;
							break;
					}
				}
			}
		}

		switch(radio_image_id.src)
		{
			case formImages.radio_checked.src:
				radio_image_id.src = formImages.radio_unchecked.src;
				this_real_radio_id.checked = false;
				break;
			case formImages.radio_unchecked.src:
				radio_image_id.src = formImages.radio_checked.src;
				this_real_radio_id.checked = true;
				break;
		}
	}
	
	return this;
}


inputTextBorders = function()
{
	initTextBorders = function()
	{
		var kids = document.getElementsByTagName('INPUT');
		
		for(var i=0; i < kids.length; i++)
		{
			if(kids[i].type == 'text')
			{
				kids[i].onclick = this.flipBorderOn;
				kids[i].onfocus = this.flipBorderOn;
				kids[i].onblur = this.flipBorderOff;
			}
		}
		
		var kids = document.getElementsByTagName('TEXTAREA');
		
		for(var i=0; i < kids.length; i++)
		{
			kids[i].onclick = this.flipTextAreaBorderOn;
			kids[i].onfocus = this.flipTextAreaBorderOn;
			kids[i].onblur = this.flipTextAreaBorderOff;
		}
	}
	
	this.flipBorderOn = function()
	{
		this.className = 'input_text_focus';
	}
	
	this.flipBorderOff = function()
	{
		this.className = 'input_text';
	}
	
	
	this.flipTextAreaBorderOn = function()
	{
		this.className = 'input_textarea_focus';
	}
	
	this.flipTextAreaBorderOff = function()
	{
		this.className = 'input_textarea';
	}
	
	return this;
}



formSubmission = function()
{
	this.initAnchorsSubmits = function()
	{
		var kids = document.getElementsByTagName('A');
		
		for(var i=0; i < kids.length; i++)
		{
			if(kids[i].className == 'form_submitter')
			{
				kids[i].onclick = this.submitFormAnchor;
			}
		}
	}
	
	this.submitFormAnchor = function()
	{
		var survey_form_id = document.getElementById('survey_form');
		survey_form_id.action = this.getAttribute('title');

		document.getElementById('coming_from').value = this.getAttribute('rel');
		
		survey_form_id.submit();
	}
	
	this.initButtonSubmits = function()
	{
		var kids = document.getElementsByTagName('INPUT');

		for(var i=0; i < kids.length; i++)
		{
			if(kids[i].type == 'button')
			{
				kids[i].onclick = this.submitFormButton;
			}
		}
	}
	
	this.submitFormButton = function()
	{
		var survey_form_id = document.getElementById('survey_form');
		survey_form_id.action = this.getAttribute('alt');
		survey_form_id.submit();
	}

	return this;
}




dropDownClicker = function()
{
	this.initDropDowns = function()
	{
		var match_class = "input_dropdown";
		var kids = document.getElementsByTagName('INPUT');
		
		for(var i=0; i < kids.length; i++)
		{
			if(kids[i].className.search(match_class) != -1)
			{
				kids[i].onclick = this.showDropDown;
				kids[i].onfocus = this.showDropDown;
			}
		}
	}
	
	this.initAnchors = function()
	{
		var kids = document.getElementsByTagName('A');
		
		for(var i=0; i < kids.length; i++)
		{
			if(kids[i].getAttribute("rel") == 'input_dropdown')
			{
				kids[i].onclick = this.setDropDown;
			}
			
			if(kids[i].className == 'close_list')
			{
				kids[i].onclick = this.closeDropDown;
			}
		}
	}

	this.showDropDown = function()
	{
		if(docDropDowns.showing_dropdown == true)
		{
			docDropDowns.closeAllDropDowns();
		}

		var this_dropdown_id = document.getElementById(this.id);
		document.getElementById(this_dropdown_id.getAttribute("alt")).style.display = 'block';
		docDropDowns.showing_dropdown = true;
	}

	this.setDropDown = function()
	{
		var this_value = this.getAttribute("title");
		var this_dropdown = this.className;

		document.getElementById(this_dropdown).value = this_value;
		docDropDowns.hideDropDown(this_dropdown + '_list');
	}

	this.closeDropDown = function(in_list)
	{
		var this_dropdown = this.getAttribute("rel");
		document.getElementById(this_dropdown).style.display = 'none';
		docDropDowns.showing_dropdown = false;
	}
	
	this.closeAllDropDowns = function(in_list)
	{
		var kids = document.getElementsByTagName('DIV');
		
		for(var i=0; i < kids.length; i++)
		{
			if(kids[i].className == 'drop_down_list')
			{
				kids[i].style.display = 'none';
			}
		}
	}

	this.hideDropDown = function(in_list)
	{
		document.getElementById(in_list).style.display = 'none';
		docDropDowns.showing_dropdown = false;
	}
	
	return this;
}
