
/* And all that Malarkey // Trimming form elements

Please feel free to use this JavaScript file in any way that you like, although please
keep the comments intact and a link back to http://www.stuffandnonsense.co.uk/archives/trimming_form_fields.html
would be appreciated.

If you come up with a stunning design based on this technique, it would be really nice
if you would post a comment containing a URL on 
http://www.stuffandnonsense.co.uk/archives/trimming_
_fields.html#Comments 

Thanks to Brothercake (http://www.brothercake.com/) for his help with the Javascript.
His fantastic UDM 4 fully-featured and accessible website menu system is a must!
(http://www.udm4.com/) */


function process_event(obj){
	if (obj.id == 'js_submit') {
		if (contact_input_ok()){
			document.getElementById('form_contact').submit();
		} else {
			alert("A first name, last name, and email address MUST be entered in order to submit your request.");
		}
	}else if (obj.id == 'js_clear') {
		//  USER CLICKED THE CLEAR FORM BUTTON
		//  IDENTIFY ALL INPUT FIELDS & CLEAR VALUES
		var inp_bx = document.getElementsByTagName('input');
		
		for (var i=0; i < inp_bx.length; i++){
			if ((inp_bx[i].className == "req_inp") || (inp_bx[i].className == "opt_inp")) {
				inp_bx[i].value = "";
			}
		}

		//  UNCHECK CHECKBOXES
		cb = document.getElementsByTagName('input');
		for (k=0; k < cb.length; k++) {
			if (cb[k].type == 'checkbox') {
				cb[k].checked = false;
			}
		}
		
		//  SET SELECT BOX TO DEFAULT
		select_box = document.getElementById('state');
		select_box.options[0].selected = true;

		//  CLEAR TEXTAREA
		ta = document.getElementsByTagName('textarea');
		for (m=0; m < ta.length; m++) {
			ta[m].value = "";
		}

		//  IF USER FORMERLY SUBMITTED, THERE MAY 
		//  BE ERROR MESSAGES. CLEAR THESES TOO
		p = document.getElementsByTagName('p');
		for (j=0; j < p.length; j++) {
//			alert(err_msg[j].textContent);
			if (p[j].className == 'error_message') {
				p[j].style.display = 'none';
			}
		}

	}else{
		alert("javascript problem in process_event()");
	};
}




function contact_input_ok() {
			//  ITERATE AND IDENTIFY ALL 'optional' FORM FIELDS
			var tmp = document.getElementsByTagName('input');
			bool = true;
			for (var i=0;i < tmp.length;i++){
				if(tmp[i].className == 'req_inp') {
					if (tmp[i].value == ""){
						bool = false;
						break;
					}
				}
			}
			return bool;
}



window.onload = function()
{

/*var tmp=document.getElementById('first');
tmp.style.backgroundColor="green";*/
/*obj = document.getElementById('submit');
obj.onclick = process_event(obj);
*/


	if(document.getElementById)
	{

		//  FIND ELEMENT TO LATCH ONTO BELOW WHICH DYNAMIC LINK WILL APPEAR
		var linkContainer = document.getElementById('form_contact');

		//  APPEND A LINE BREAK AND THE HYPERLINK
		var linebreak = linkContainer.appendChild(document.createElement('br'));
		var toggle = linkContainer.appendChild(document.createElement('a'));
		toggle.href = '#';
		toggle.appendChild(document.createTextNode('Display optional fields?'));
		toggle.id = "toggle";
		toggle.onclick = function()
		{
			//  GIVE VALUES TO DIFF. STATES OF HYPERLINK
			var linkText = this.firstChild.nodeValue;
			this.firstChild.nodeValue = (linkText == ' Display optional fields?') ? ' Hide optional fields?' : ' Display optional fields?' ;
			
			//  ITERATE AND IDENTIFY ALL 'optional' FORM FIELDS
			var tmp = document.getElementsByTagName('div');
			for (var i=0;i < tmp.length;i++){
				
				if(tmp[i].className == 'optional')
				{
					tmp[i].style.display = (tmp[i].style.display == 'none') ? 'block' : 'none';
				}
			}
			return false;
		}
	}else{
		alert('not a modern browser');
	}
}


