/* functions_extras.js Last updated 16/10/2008 */

/* clearInputContent - remove content from an input on focus */
function clearInputContent(){
	if (!document.getElementById) return false;	
	if (typeof($$) == "undefined") return false;
	if (!$$('input.resetText').length) return false;
	var elements = $$('input.resetText');
		for (var i=0; i<elements.length; i++){
		var originalValue = elements[i].readAttribute('value');	
		elements[i].onfocus = function(){
			if (this.value == originalValue){
			this.clear();
				this.onblur = function(){
					if (this.value==""){
					this.value = originalValue;	 
					}
				}
			}
		}
	}
}


/* standardFormValidation - should be able to apply to any standards compliant form with labels. Add the class "validateForm" to the form and "validateField" to any field you want to validate*/
function standardFormValidation(){
	if(!document.forms) return false;
	if($$("form.validateForm")){

		$$("form.validateForm").each(function(vform){
			vform.onsubmit = function(){
				
				$$("span.validationMessage").each(function(em){
				em.remove();							 
				});
				$$(".validationContainerActive").each(function(vca){
				vca.removeClassName("validationContainerActive");							 
				});
				if($$('div.newWarningBlock')){
					$$('div.newWarningBlock').each(function(nwb){
					nwb.remove();							 
					});
				}
			var emailf = false;	
			var formElementCount = 0;
			var labels = vform.getElementsByTagName("label")
			
			var validField = $(vform).getElementsByClassName("validateField");

				$A(validField).each(function(vf){
												  
					$A(labels).each(function(lbl){
						if(vf.id == $(lbl).attributes['for'].nodeValue){
							if(lbl.innerHTML.toLowerCase().match("name")){
							reName = /^\D{1,}/
								if(reName.test(vf.value)){
								formElementCount++;
								}
								else{
								formElementCount = 0;
								ErrorDisplay(lbl,vf,vform,emailf);
								}
							}
							if(lbl.innerHTML.toLowerCase().match("e-mail") || lbl.innerHTML.toLowerCase().match("email") || lbl.innerHTML.toLowerCase().match("e mail")){	
							reEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
								if(reEmail.test(vf.value)){
								formElementCount++;
								}
								else{
								formElementCount = 0;
								emailf = true
								ErrorDisplay(lbl,vf,vform,emailf);
								}
							}	
							if(lbl.innerHTML.toLowerCase().match("mobile") || lbl.innerHTML.toLowerCase().match("phone") || lbl.innerHTML.toLowerCase().match("tel") || lbl.innerHTML.toLowerCase().match("home") || lbl.innerHTML.toLowerCase().match("number")){
							rePhone = /^\d{1,}/
								if(rePhone.test(vf.value)){
								formElementCount++;
								}
								else{
								formElementCount = 0;
								ErrorDisplay(lbl,vf,vform,emailf);
								}
							}	
						}
					});
				});
				if(formElementCount == validField.length){
				return true;
				}
				else{
				return false;
				}
			}
		});									 
	}
} 

/* ErrorDisplay - handles the error/warning messages */
function ErrorDisplay(label, element, standardForm, emailField){
	if($$('div.newWarningBlock').length<1){
	new Insertion.Before(standardForm, 
"<div class='jScriptHandler'><div class='newWarningBlock'><div class='newWarningBlockInner'><img class='newWarningBlockIcon' title='Alert!' alt='Alert!' src='/images/icons/warning.gif'/><div class='newWarningBlockContent'><strong>Please check these details are correct</strong><ul id='warningBlockField'></ul></div></div></div></div>");	
	}
	$(label).up().addClassName("validationContainerActive");
	if(emailField == true){
new Insertion.Before(label, '<span class="validationMessage">It appears you&#096;ve not entered your <strong>' + label.innerHTML + '</strong> address or it is in a format we don&#096;t recognise.</span>');	
	}
	else{
	new Insertion.Before(label, '<span class="validationMessage">It appears you&#096;ve not entered your <strong>' + label.innerHTML + '</strong></span>');
	}
new Insertion.Bottom($('warningBlockField'), '<li class="validationMessage">' + label.innerHTML + '</li>');
}

addLoadEvent(clearInputContent);
addLoadEvent(standardFormValidation);