// Custom label focus fixes safari and enables classes
	// passes to blurClass and focusClass
	// IE cannot use label.getAttribute('for') so use label.htmlFor instead
	function focusInput () {
		focusLabels ();
		if(!document.getElementsByTagName) return false;
		var inputs = document.getElementsByTagName("input");
		for(i=0;i<inputs.length;i++) {
			inputs[i].onfocus = function () {focusClass(this);};
			inputs[i].onblur = function () {blurClass(this)};
		}
		var texts = document.getElementsByTagName("textarea");
		for(i=0;i<texts.length;i++) {
			texts[i].onfocus = function () {focusClass(this);};
			texts[i].onblur = function () {blurClass(this)};
		}
		var selects = document.getElementsByTagName("select");
		for(i=0;i<selects.length;i++) {
			selects[i].onfocus = function () {focusClass(this);};
			selects[i].onblur = function () {blurClass(this)};
		}
	}//End of focusInput
	function focusLabels () {
		if(!document.getElementsByTagName) return false;
		var labels = document.getElementsByTagName("label");
		for(i=0;i<labels.length;i++) {
			if(!labels[i].htmlFor) continue;
			labels[i].onclick = function () {
				var field = this.htmlFor;
				var focusMe = document.getElementById(field);
				focusMe.focus();
			}
		}
	}//End of focusLabels
	function focusClass (id, nameClass){
		if (id.className=="") {
				id.className= "Hovers";
				} else {
				id.className+=" Hovers";
				}
	 }//End of focusClass
	 function blurClass (id, nameClass){
		if (id.className=="Hovers") {
				id.className= "";
				} else {
				id.className=id.className.replace(new RegExp(" Hovers\\b"), "");
				}
	 }//End of blurClass
	//window.onload = function() {
	//focusInput ();
	//}
//}

//modified from christian heilman http://www.onlinetools.org/articles/unobtrusivejavascript/chapter5.html
function checkform(of) {
	if(!document.getElementById || !document.createTextNode){return;}
	if(!document.getElementById('required')){return;}
	//setup our variables
	
	var errorID = 'errorMsg';
	var errorClass = 'showError'
	var errorMsg = 'Please enter or change the fields marked with a ';
	var errorTitle = 'This field has an error!';
	// create an array of all required inputs
	var reqfields = document.getElementById('required').value.split(',');
	//create array of all error messages
	//var 
	//alert(errorResponse);
	// Cleanup old mess
	// if there is an old errormessage field, delete it
	if(document.getElementById(errorID)) {
		var em = document.getElementById(errorID);
		em.parentNode.removeChild(em);
	}
	// remove old classes from the required fields
	for(var i = 0;i < reqfields.length;i++) {
		var f = document.getElementById(reqfields[i]);
		if (f.parentNode.className == "required") {
			
		} else {
			f.parentNode.className = f.parentNode.className.replace(new RegExp(" showError\\b"), "");
		}
	}//End of the for loop
	
	// loop over required fields
	var errorArray = new Array();
	for(var i = 0;i < reqfields.length;i++) {
		// check if required field is there
		var f = document.getElementById(reqfields[i]);
		if(!f){continue;}
		// test if the required field has an error, 
		// according to its type
		// maybe need to add radio as required element.
		//need to add more info on whats wrong...
		// use checkConfirm to match with a confirmed field
		//if fieldset we need different options.
		if(f.getElementsByTagName('input').length>0) {
			var inputArray= f.getElementsByTagName('input')
			var s=0;
			for(var p = 0;p < inputArray.length;p++) {
				if(inputArray[p].checked==true){
					s++
			}else{}
			
			}if(s==0){errorArray[i]=f.id;showErrorDiv(f,true);}
		}else {
		switch(f.type.toLowerCase()){
		
			case 'text':
				
				showErrorDiv(f,false);
				if(f.value=='' && f.id!='email' || !checkConfirm(f.id)){errorArray[i]=f.id}              
				// email is a special field and needs checking
				if(f.id=='email' && !cf_isEmailAddr(f.value) || !checkConfirm(f.id) ){errorArray[i]=f.id}              
				break;
			case 'textarea':
				showErrorDiv(f,false);
				if(f.value==''){errorArray[i]=f.id}              
				break;
			case 'checkbox':
				showErrorDiv(f,true);
				if(!f.checked){errorArray[i]=f.id}              
				break;
			case 'select-one':
				//alert('select');
				showErrorDiv(f,false);
				if(f.options[f.selectedIndex].value==''){errorArray[i]=f.id}            
				break;
			case 'password':
				//check to see wether password needs to be confirmed
				if(!checkPassword(f.value,i) || !checkConfirm(f.id) ){errorArray[i]=f.id;} 
				break;
	 }//End of the switch statement
		}
	}//End of the for loop
	
	return cf_adderr(errorArray, errorID);
}//End of checkForm
  
  function showErrorDiv(f,chkBox) {
	if (f.parentNode.className=="required") {
	}else {
		//ignore whitespace
		var thisDiv = document.createElement("div");
		thisDiv.className="required";
		if(chkBox==false){
		var thisLabel = realPreviousSibling(f);
		//use f.parentNode.insertBefore so the location 'f' is a child
		f.parentNode.insertBefore(thisDiv,f);
		thisDiv.appendChild(thisLabel);	
		thisDiv.appendChild(f);
		} else {// checkbox needs label after input
		var thisLabel = realNextSibling(f);
		f.parentNode.insertBefore(thisDiv,f);
		thisDiv.appendChild(f);
		var addBr=document.createElement('br');
		thisDiv.appendChild(thisLabel);	
		thisDiv.appendChild(addBr);
		}
  	}
  }
  
  
  function cf_isEmailAddr(str) {
      return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
  }

  
// setup global error var
var error = "";
var passwordError=""
//pass through errorArray so we can show custom message if needed.
function checkPassword (strng,p) {
		//var illegalChars = /[\W_]/; // allow only letters and numbers
	if (strng == "") {
		error = "You didn't enter a password.";
		passwordError=p;
		return false;
	} else if ((strng.length < 6) || (strng.length > 20)) {
		error = "The password is the wrong length.";
		passwordError=i;
		return false;
	//} else if (illegalChars.test(strng)) {
	//	error = "The password contains illegal characters.";alert(error);
	//	passwordError=i;
	//	return false;
	} else {
	return true
	}
}
	
	
  
function checkConfirm(str) {
	//do password check - only send it once.
	var myString = str;
	var test=myString.length;
	//test to see if confirm password2 exists
	if(myString.charAt(test-1)!='2'){
		var passwordCheck=str+"2";
		//should do error check for a login page where no confirm is required
		if(!document.getElementById(passwordCheck))return true;
			var confirms = document.getElementById(passwordCheck);
			var pass=document.getElementById(str);
	
			if (confirms.value != pass.value){
				//passwords do not match
				return false;
			} else {
				//passwords match
				return true;
			}
	}		
}

function checkadsl(form){
	var valid = false;
	if(!document.getElementById || !document.createTextNode){return;}
	var errorID = 'ADSLContainer';
	var em = document.getElementById(errorID);
	var f = document.getElementById('btnumber');
	if(f.value==''){              
		em.className = 'showError';
	}
	else{
		em.className = '';
		valid = true;
	}
	return valid;
}

	/* Tool methods */
function cf_adderr(errorArray, errorID) {
var errorResponse = document.getElementById('errorResponse').value.split(',');
	for(i=0;i<errorArray.length;i++){
		if(errorArray[i]!=null){
			o = document.getElementById(errorArray[i]);
			//alert(o)
			o.parentNode.className+=" showError";
			// Check if there is no error message
			if(!document.getElementById(errorID)) {
				var msg=document.createElement('div');
				msg.id=errorID;
				var container = document.getElementById('errorContainer');
				container.appendChild(msg);
				var errorString = 'The following errors were found, please correct them and resubmit the form';
				var errorText = document.createTextNode(errorString);
				var txtContain = document.createElement('h2');
				txtContain.appendChild(errorText);
				msg.appendChild(txtContain);
			} // End of if
			// Check if there is no error message
		    if(!document.getElementById(errorID+'List')) {
				// create errormessage ul
				var em=document.createElement('ol');
				em.id=errorID+'List';
			}
			var newLi=document.createElement('li');
			var newHref = document.createElement('a');
			if(document.createAttribute){
				var attrHREF = document.createAttribute( "href" );
				attrHREF.nodeValue = "#"+o.id;
				var thisHref=o.id;
			} else {
			
			}
			var thisFocus='focusMe(\''+thisHref+'\')';
			// if the node's attribute already exists
			// then replace its value FIX IE 5
			if(document.getAttributeNode) {
				if (newHref.getAttributeNode("onclick")) {
					//alert('ie');
					newHref.removeAttribute("onclick")
				}
			}
			
			newHref.setAttribute("onclick", thisFocus);

			//check for password error
			if (passwordError==i && passwordError!=""){
				var newText = document.createTextNode(error);
			} else {
				var newText = document.createTextNode(errorResponse[i]);
			}
			newHref.appendChild(newText);
			if (document.setAttributeNode) {
				newHref.setAttributeNode( attrHREF );
			} else {
			newHref.href="#"+o.id;
			}
			newLi.appendChild(newHref);
			em.appendChild(newLi);
			msg.appendChild(em);
		}//End of if(errorArray[i]!=null)
	}//End of the for loop
	
	if(errorArray.length>0) {
	this.focus='#errorContainer';
	this.location='#errorContainer';
	document.title='This form has errors please try again';
	return false;
	}else {return}
}//End of the function cf_adderr
  
  function focusMe(thisFocus) {
  	//Using setTimeout to let firefox focus :(
	setTimeout('document.getElementById(\''+thisFocus+'\').focus()', 100);
  }
  
  /* try and fix previousSibling having an extra text element in gecko */
function realPreviousSibling(node){
 	var tempNode=node.previousSibling;
 	while(tempNode.nodeType!=1){
  		tempNode=tempNode.previousSibling;
 	}
 	return tempNode;
}

function realNextSibling(node){
 	var tempNode=node.nextSibling;
 	while(tempNode.nodeType!=1){
  		tempNode=tempNode.nextSibling;
 	}
 	return tempNode;
}