//form field validator
function require(form){
	
	//special check for Pagoda ID
	/*
	var id=form.pagoda_id.value;
	
		if (id.length<6 & id.length!=0){
			alert("Pagoda IDs usually have 6 characters; three letters and three numbers e.g. ABC003. If you know for sure that your item has an ID of less than six characters then enter spaces to make the ID six characters exactly.");
			form.pagoda_id.focus();
			return false;
			}
	
	*/
	var validationSet = {
		'first_name': {
		'regexp': /^\w{2,}/,
		'error': 'Please enter your first name.'
		},
		'last_name': {
		'regexp': /^\w{2,}/,
		'error': 'Please enter your last name.'
		},
		'email': {
		'regexp': /^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/,
		'error': 'Please enter a valid email address.'
		},
		'staff_email': {
		'regexp': /^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/,
		'error': 'Please enter a valid email address.'
		},
		'username':{
		'regexp': /^\w{4,}$/,
		'error': 'Please enter your username.'
		},
		'password':{
		'regexp': /^\w{4,}$/,
		'error': 'Please enter your password.'
		},
		'newpwd1':{
		'regexp': /^\w{4,}$/,
		'error': 'Your password must be at least 4 characters long.'
		},
		'newpwd2':{
		'regexp': /^\w{4,}$/,
		'error': 'Please retype your new password.'
		},
		'url': {
		'regexp': /^http/,
		'error': 'A url must begin with http.'
		},
		'link_text': {
		'regexp': /^\w{1,}/,
		'error': 'Please enter the words that should appear for this link.'
		},
		'sort_order': {
		'regexp': /^[1-9]{1}\d{0,2}$/,
		'error': 'Please enter a number between 1 and 255.'
		}
	};
	
	var fm=form.elements
		
	for(var i=0; i<fm.length; i++){
		var field=fm[i].name;
		
		if (validationSet[field]){
			var re = validationSet[field]['regexp'];
			if(!re.test(fm[i].value)){
				var error_target=field + "_error";
				var output_error=document.getElementById(error_target);
				output_error.innerHTML=validationSet[field]['error'];
				output_error.style.display="block";
				fm[i].focus();
				return false;
				}
			}
		}
return true;
}

//checks dropDownMenu
	function dropDownMenu(form)  {
		var myindex=document.forms[0].menu.selectedIndex;
		if (myindex==0) {
			alert("\nYou must make a selection from the drop-down menu.");
			document.forms[0].menu.focus();
			}else{
			menu_selection=document.forms[0].menu.options[myindex].value;
			return true;
   			}
		}

//textarea character counter
                                                
function textCounter(field,cntfield,maxlimit) 
       { 
        if (field.value.length > maxlimit) { 
               field.value = field.value.substring(0, maxlimit); 
               }else{
               cntfield.value = maxlimit - field.value.length;
               }
         }
         
//clear error

function clearError(field){
	var error_target=field.name + "_error";
	var output_error=document.getElementById(error_target);
	if(output_error){
	output_error.innerHTML="";
	output_error.style.display="none";
	}
}

//check field match
function checkMatch(field1,field2){
	var error_target=field2.name + "_error";
	var output_error=document.getElementById(error_target);
	
	if(field1.value!=field2.value){
		output_error.innerHTML="Entries do not match.";
		output_error.style.display="block";
		field2.focus();
		return false;
	}else{
		output_error.style.display="none";
	}
}

//set focus in first form field
function setFocus(target){
							target.focus();
							}
							
//duplicate form section as needed
function addField(area,template,prefix,limit) {

	if(!document.getElementById) return; //exit if older browser
	
	var field_area = document.getElementById(area);

	var all_inputs = field_area.getElementsByTagName("div"); //Count the divisions
	if(all_inputs){
	var last_item = all_inputs.length - 1;
	var last = all_inputs[last_item].id;
	var count = Number(last.split("_")[1]) + 1; //this will be the number of the next row
	}else{
	count=1;
	}

	//If the maximum number of elements have been reached, exit the function.
	//If the given limit is lower than 0, infinite number of fields can be created.
	if(count > limit && limit > 0) return;

	var section=document.getElementById(template);
	var section_info=section.innerHTML;
	document.getElementById((prefix)+"section_count").value=count;
	//var array_marker="_"+(count);
	var array_marker="["+(count)+"]";
	
	section_info=section_info.replace(/\[0\]/g,array_marker);
	field_area.innerHTML += "<div class='form_rows' id='"+(prefix)+"section_"+(count)+"'>"+(section_info)+"</div>";
	//alert(section_info);
	
}

function removeField(area,prefix) {

	if(!document.getElementById) return; //exit if older browser
	
	var field_area = document.getElementById(area);

	var all_inputs = field_area.getElementsByTagName("div"); //Count the divisions
	if(all_inputs){
	var last_item = all_inputs.length - 1;
	var last = all_inputs[last_item].id;
	var count = Number(last.split("_")[1]) + 1; //this will be the number of the next row
	}else{
	count=1;
	}
	if(count>1){
	var last_section=document.getElementById((prefix)+"section_"+(count-1));
	field_area.removeChild(last_section);
	}
}
