function validate( fields_to_validate ){

	var valid=true;

	//Check each field
	for( var a=0;a<fields_to_validate.length;a++ ){
		
		var field_name=fields_to_validate[a].split("|")[0];
		if( typeof fields_to_validate[a].split("|")[1]!="undefined" ){
			var field_description=fields_to_validate[a].split("|")[1];
		}
		
		//Make sure that the field exists
		if( document.getElementById(field_name)!=null ){
			
			//Highlight the field which triggered an error
			if( string_replace(" ","",document.getElementById(field_name).value)=="" ){
			
				change_element_value(field_name+"_field","className","field_highlight");
				
				//Flag
				valid=false;
			
			}
			//Otherwise, unhighlight it
			else{
			
				change_element_value(field_name+"_field","className","");
			
			}
			
			//Validate other fields
			
			//Check the email address
			if( field_name=="email_address" && document.getElementById("email_address")!=null ){
			
				var email_address=string_replace( " ", "", document.getElementById("email_address").value );
				
				//That it's not empty (which has already been checked
				if( email_address=="" ){
				
				
				}
				//That it's formatted properly
				else if( valid_email_address(email_address)==false ){
						
					var email_address_message="Please enter a valid email address.";
				
				}
				
				//If there's a problem with the email address, show the message and highlight the field
				if( email_address=="" ){
				
				
				}else if( typeof email_address_message!="undefined" ){
				
					//Flag
					valid=false;
					document.getElementById("email_address_field").className="field_highlight";
					document.getElementById("email_address_message").className="row_show";
					document.getElementById("email_address_message").innerHTML=email_address_message;
				
				}else{
				
					document.getElementById("email_address_field").className="";
					document.getElementById("email_address_message").className="row_hide";
					document.getElementById("email_address_message").innerHTML="";
				
				}
				
			}
			
			//Check the telephone number
			if( field_name=="telephone_area_code" && document.getElementById("telephone_area_code")!=null && document.getElementById("telephone_number")!=null ){
			
				var telephone_area_code=document.getElementById("telephone_area_code").value;
				var telephone_number=document.getElementById("telephone_number").value;
				
				//Area code
				if( isNaN(telephone_area_code) || telephone_area_code.length!=3 ){
					
					//Flag
					valid=false;
					document.getElementById("telephone_area_code_field").className="field_highlight";
					document.getElementById("telephone_area_code_message").className="row_show";
					document.getElementById("telephone_area_code_message").innerHTML="The area code must be 3 digits.";
					
				}else{
				
					document.getElementById("telephone_area_code_field").className="";
					document.getElementById("telephone_area_code_message").className="row_hide";
					document.getElementById("telephone_area_code_message").innerHTML="";
					
				}
				
				//Number
				if( isNaN(telephone_number) || telephone_number.length!=7 ){
					
					//Flag
					valid=false;
					document.getElementById("telephone_number_field").className="field_highlight";
					document.getElementById("telephone_number_message").className="row_show";
					document.getElementById("telephone_number_message").innerHTML="The telephone number must be 7 digits.";
					
				}else{
				
					document.getElementById("telephone_number_field").className="";
					document.getElementById("telephone_number_message").className="row_hide";
					document.getElementById("telephone_number_message").innerHTML="";
					
				}
				
			}
			
			//Check the zip code
			if( field_name=="zip_code" && document.getElementById("zip_code")!=null ){
			
				var zip_code=document.getElementById("zip_code").value;
				
				//Area code
				if( isNaN(zip_code) || zip_code.length!=5 ){
					
					//Flag
					valid=false;
					document.getElementById("zip_code_field").className="field_highlight";
					document.getElementById("zip_code_message").className="row_show";
					document.getElementById("zip_code_message").innerHTML="The zip code must be 5 digits.";
					
				}else{
				
					document.getElementById("zip_code_field").className="";
					document.getElementById("zip_code_message").className="row_hide";
					document.getElementById("zip_code_message").innerHTML="";
					
				}
						
			}
			
		}
	
	}
	
		
	return valid;

}

function toggle_states(){

	//Default field type
	if( typeof state_field_type=="undefined" ){
			
		state_field_type="select";
	
	}

	var state_text_field_html="<input type=\"text\" name=\"state\" id=\"state\">";
	
	var states="Alabama,Alaska,Arizona,Arkansas,California,Colorado,Connecticut,Delaware,District of Columbia,Florida,Georgia,Hawaii,Idaho,Illinois,Indiana,Iowa,Kansas,Kentucky,Louisiana,Maine,Maryland,Massachusetts,Michigan,Minnesota,Mississippi,Missouri,Montana,Nebraska,Nevada,New Hampshire,New Jersey,New Mexico,New York,North Carolina,North Dakota,Ohio,Oklahoma,Oregon,Pennsylvania,Rhode Island,South Carolina,South Dakota,Tennessee,Texas,Utah,Vermont,Virginia,Washington,West Virginia,Wisconsin,Wyoming";
	states=states.split(",");

	var state_dropdown_html="<select id='state' name='state'><option value=''>-Select-</option>";
	for( var a=0;a<states.length;a++ ){
	
		state_dropdown_html+="<option value='"+states[a]+"'>"+states[a]+"</option>";
	
	}
	state_dropdown_html+="</select>";

	//If the United States is selected, show the states in a dropdown
	if( document.getElementById("country").value=="United States" ){
	
		state_field_type="select";
		document.getElementById("state_field").innerHTML=state_dropdown_html;
		
	
	}else if( state_field_type=="select" ){
		
		state_field_type="text";
		state_field_text=document.getElementById("state_field").innerHTML=state_text_field_html;
	
	}
	
}

//Replace a string with another string
function string_replace(findChars,replacementChars,text){

	//Replace Characters
	var newText="";
	for(var r=0;r<text.length;r++){
		//Set char string to compare
		var charString="";
		//starting at r, create charString to compare findChars with (according to number of chars)
		for(var s=r;s<r+findChars.length&&s<text.length;s++){
			charString+=text.charAt(s);
		}
		//If the charString matches findChars
		if(charString==findChars){
			//add charString to nextText
			newText+=replacementChars;
			//and move on, starting at next unchecked char
			r=r+eval(findChars.length-1);
		}
		else{
			//Add letter to nextText
			newText+=text.charAt(r);
		}
	}
	return newText;
	
}

function toggle_visibility(id){
	
	//Check the current state
	if( document.getElementById(id).className!="hide" ){
		var action="hide";
	}else{
		var action="show";
	}
	
	//Set the state
	document.getElementById(id).className=action;
	
}

function change_element_value(id,type,value){
	//Make sure that the element exists
	if( document.getElementById(id)!=null ){
		eval("document.getElementById('"+id+"')."+type+"='"+value+"'");
		
		//Notify that the element does exist
		return true;
		
	}else{
		//Notify that the element doesn't exist
		return false;	
	}
}

function valid_email_address(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		return false
	 }

	 return true					
}

function combine_fields( target_field, source_fields ){

	//Combine the values of the source fields
	var source_fields=source_fields.split(",");
	var source_value="";
	for( var a=0; a<source_fields.length; a++ ){
	
		if( a!=0 ){
		
			source_value+=" ";
		
		}
		source_value+=document.getElementById(source_fields[a]).value;
	
	}

	change_element_value( target_field, "value", source_value );

}

function retrieve_html(){

	//Hide content section
	document.getElementById("file_content").className="hide";

	//Show loading circle
	document.getElementById("loading_circle").className="show";

	var topic=document.getElementById("topic").value;

	post( "./retrieve.php", "file="+topic, true, "document.getElementById(\"file_content\").innerHTML=result; document.getElementById(\"loading_circle\").className=\"hide\";document.getElementById(\"file_content\").className=\"show\";" );

	//document.getElementById("file_content").innerHTML=html;

}