/* 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_form_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 moreRows(textarea){
	if ($(textarea).rows<22){$(textarea).rows+=6;} return false;
}
function lessRows(textarea){
	if ($(textarea).rows>8){$(textarea).rows-=6;} return false;
}	

function hideOptionalFields(){
	if(document.getElementById('fm-intro'))
	{
		var linkContainer = document.getElementById('fm-intro');
		//var linebreak = linkContainer.appendChild(document.createElement('br'));
		var toggle = linkContainer.appendChild(document.createElement('a'));
		toggle.href = '#';
		toggle.appendChild(document.createTextNode(' Optionale Felder ausblenden?'));
		toggle.onclick = function()
		{
			var linkText = this.firstChild.nodeValue;
			this.firstChild.nodeValue = (linkText == ' Optionale Felder ausblenden?') ? ' Optionale Felder einblenden?' : ' Optionale Felder ausblenden?';
			
			// TODO: join the Arrays..but they won't because tmp+tmp2 are HTMLCollections??
			tmp = document.getElementById('fm-contact').getElementsByTagName('div');
			tmp2 = document.getElementById('fm-contact').getElementsByTagName('fieldset');
			for (var i=0;i<tmp.length;i++)
				if(tmp[i].className == 'fm-opt')
					tmp[i].style.display = (tmp[i].style.display == 'none') ? 'block' : 'none';
			for (var i=0;i<tmp2.length;i++)
				if(tmp2[i].className == 'fm-opt')
					tmp2[i].style.display = (tmp2[i].style.display == 'none') ? 'block' : 'none';
			return false;
		}
	}
}

// Contact form validation

// This could cause a problem for browser-based screenreaders
// because the validation changes label text but doesn't move the focus
// and moving the focus is pretty unfriendly anyway
// so ... to mitigate that don't run the script in detectable browser-based screenreaders
// we detect them with a focus handler on the submit button
// which works because browser-based readers (JAWS and HPR tested) don't generate focus events when tabbed to
// and with a mousedown event, which keyboard navigation doesn't generate (it does generate onclick, so we can't use that)


function validateForm(){
	var isReader = true;
	
	//elements to validate, and their regex
	var vEles = {
		'fm-firstname' : '',
		'fm-lastname' : '',
		'fm-telephone' : '',
		'fm-email' : '[^@]+@[^\.]+\.[a-zA-Z]',
		'fm-comments' : ''
		};
		
	//check contact form is there
	var contactForm = document.getElementById('fm-contact');
	if(contactForm != null)
	{
	  
		//button detection handler
		document.getElementById('fm-button').onfocus = function()
		{
			isReader = false;
		};
		document.getElementById('fm-button').onmousedown = function()
		{
			isReader = false;
		};
		
		//form onsubmit handler
		contactForm.onsubmit = function()
		{
			var isValid = true;
				
			//process the form for graphical browsers
			if(!isReader)			
			{
				
				addLabelProperties(this);
				
				
				for(var j in vEles)
				{
					//remove any existing labels
					/*
					this[j].label.innerHTML = this[j].label.innerHTML.replace('[The address you entered was invalid]', '');
					this[j].label.innerHTML = this[j].label.innerHTML.replace('[Please complete this field]', '');
					this[j].label.innerHTML = this[j].label.innerHTML.replace('(Required)', '');
	*/

					//if value is invalid, clear box and add message to label
					
					this.regex = new RegExp(vEles[j],'');
					if(!this.regex.test(this[j].value)){
						this[j].value = '';
						this[j].label.innerHTML += '<em>[The address you entered was invalid]</em>';
						isValid = false;
					}
					
					//else if field is empty, add message to label
					
					else if(this[j].value == ''){
						this[j].label.innerHTML += '<em>[Please complete this field]</em>';
						isValid = false;
					}

					
				} // end for
				
					/*
					if(!document.getElementById('contact-error')){
					var error_msg = document.createTextNode("Bitte \u00fcberpr\u00fcfen Sie Ihre Eingaben.");
					var error = document.createElement('div');
					error.id = 'contact-error';
					error.appendChild(error_msg);
					document.getElementById('fm-submit').appendChild(error);
					
				 }*/
				//return false;
			} //end if is reader
			
			
			return isValid;
	//	}
		};	// end function onsubmit
	} // end if form != null
}

/* append onload events */
if(document.getElementsByTagName){
  appendOnLoad('hideOptionalFields()');
  appendOnLoad('validateForm');
}