//javascript form validation

function validateForm(theform, valSet, errorId)	{
		
	var returnval;
	var returnError;
	var errorArray = new Array();

	var regexstring = "";

	if ( errorId !== undefined )
		div = document.getElementById(errorId);
	else
		div = document.getElementById("error-msg");
	//reset the error message output
	div.innerHTML = "";
	
	for (var i=0; i < theform.elements.length; i++) {
		
		var element = theform.elements[i].name;
		var myValue = theform.elements[i].value;
		
		
		if (valSet == "createEvent")	{
			
			//if we are selecting an EXISTING venue, we NEED to enforce that the user selects one of the options ('selectVenue' radios)
			if (theform.newLocation && theform.newLocation[0] && theform.newLocation[0].checked)	{
				//USER DID NOT CHOOSE A VENUE
				
				var oneChecked = "false";
				
				//see if select venue even exists yet
				if (theform.selectVenue)	{
					var selectVenueLength = theform.selectVenue.length;
					for(var s = 0; s < selectVenueLength; s++) {
						if(theform.selectVenue[s].checked) {
							oneChecked = "true";
							break;
						}
					}//end for
					
				}//end if select venue even exists yet

				if (oneChecked == "false")	{
					regexstring = "GENERATE ERROR";		
					returnError = "You must choose a venue by searching and selecting the appropriate item.";
				}
				//USER did everything right, so let's clear any other errors
				else	{
					regexstring = "";
					
				}
			}
			//Else if creating anew venue
			else if (theform.newLocation && theform.newLocation[1] && theform.newLocation[1].checked)	{
				if (element == "vTitle")	{
					regexstring =  "^(.{1,})$";	
					returnError = "The new venue 'name' field is manditory";
				}
				else
					regexstring = "";
					returnError = "";
			}//end else if
																							  
																							  
																						
			//else, do normal error checking
			
			else if (element){
					switch(element)	{
						case ("title"):
							regexstring =  "^(.{1,})$";			
							returnError = "Title field is manditory";
						break;
						
						case ("company"):
							regexstring = "^(.{1,})$";	 		
							returnError = "Organizer field is manditory";
						break;
						
						case ("organizer"):
							regexstring = "^(.{1,})$";		
							returnError = "Organizer field is manditory";
						break;
						
						case ("category"):
							regexstring = "^(?!Select Industry).*$";	
							returnError = "Must select an industry";
						break;	
						
						
						default:
							regexstring = "";
						break;	
				
				}//end switch
			}//end else
			
			
				
		}//end if VALSET for CREATING AN EVENT
		
		else if (valSet == "discussion")	{
			
			switch(element)	{
				
				//USER  STUFF
				case ("text"):
					regexstring =  "\s*.+";			
					returnError = "Comment text cannot be empty";
				break;
				
				default:
					regexstring = "";
				break;
				
	
			}//end switch
			
		}//end else if
		
		else if (valSet == "discussion_reply")	{
			
			div = document.getElementById("error-msg_" + theform.replyTo.value);
			div.innerHTML = "";
			
			switch(element)	{
				
				//USER  STUFF
				case ("text"):
					regexstring =  "\s*.+";			
					returnError = "Comment text cannot be empty";
				break;
				
				default:
					regexstring = "";
				break;
				
	
			}//end switch
			
		}//end else if
		
		
		else if (valSet == "blog_create")	{
			
			div = document.getElementById("error-msg_blog_post");
			div.innerHTML = "";
			
			switch(element)	{
				
				//USER  STUFF
				case ("text"):
					regexstring =  "\s*.+";			
					returnError = "Blog message cannot be empty";
					
				break;
				
				default:
					regexstring = "";
				break;
				
	
			}//end switch
			
		}//end else if
		
		
		
		
		else	{
			
			switch(element)	{
			
				//USER  STUFF
				case ("username"):
					regexstring = "^([a-zA-z0-9\-_]{4,})$"; 		
					returnError = "Username must have 4 characters or more, and contain no spaces";
				break;
				
				case ("fname"):
					//This allows you to validate first names and last names in seperate fields. Instead of validating a full name in one field.
					regexstring = "^[a-zA-Z ]+(([\'\,\.\-][a-zA-Z ])?[a-zA-Z ]*)*$"; 		
					returnError = "Enter a valid First Name";
				break;
				
				case ("lname"):
					//This allows you to validate first names and last names in seperate fields. Instead of validating a full name in one field.
					regexstring = "^[a-zA-Z ]+(([\'\,\.\-][a-zA-Z ])?[a-zA-Z ]*)*$"; //ANY alphanumeric string with spaces, commas, dashes.
					returnError = "Enter a valid Last Name";
				break;
				
				case ("email"):
					
					//regexstring = "(\w+?@\w+?\x2E.+)"; //Validates an email address
					
					if (document.getElementById("emailConfirm"))	{
						if (myValue == document.getElementById("emailConfirm").value)	{
							regexstring = "^[a-zA-Z0-9\_\-]+[a-zA-Z0-9\.\_\-]*@([a-zA-Z0-9\_\-]+\.)+([a-zA-Z]{2,4}|travel|museum)$";
							returnError = "Not a valid email address";	
						}
						
						else	{
							regexstring ="error";
							returnError = "Your email addresses do not match.";
						}
					}
					//there is no confirm address, so it is stand alone, just check for valid.
					else	{
						regexstring = "^[a-zA-Z0-9\_\-]+[a-zA-Z0-9\.\_\-]*@([a-zA-Z0-9\_\-]+\.)+([a-zA-Z]{2,4}|travel|museum)$";
						returnError = "Not a valid email address";	
					}
												
				break;
	
				case ("password"):
					
					//regexstring = "(\w+?@\w+?\x2E.+)"; //Validates an email address
					
					if (document.getElementById("passwordConfirm"))	{
						if (myValue == document.getElementById("passwordConfirm").value)	{
							regexstring = "^([a-zA-Z0-9\s]{6,})$";
							returnError = "Not a valid password.  Must have 6 characters or more";	
						}
						
						else	{
							regexstring ="error";
							returnError = "Your passwords do not match";
						}
					}
					//there is no confirm address, so it is stand alone, just check for valid.
					else	{
						regexstring = "^([a-zA-Z0-9\s]{6,})$";
						returnError = "Not a valid password.  Must have 6 characters or more";	
					}
												
				break;
	
				
				case("pic"):
				case("image_exhibitor"):
					
					//regexstring = "^([a-zA-Z].*|[0-9].*)\.(((j|J)(p|P)(g|G))|((g|G)(i|I)(f|F))|((p|P)(n|N)(g|G)))$";
					//regexstring = "^(.+(?=\.(jpg|gif|png))\.\2)$";
					regexstring = /^.+\.(jpe?g|png|gif)$/i;
					returnError = "Picture must be a JPG, GIF or PNG";
					
			
				break;
				
				
				
				case("pdf"):
					
					//regexstring = "^([a-zA-Z].*|[0-9].*)\.(((j|J)(p|P)(g|G))|((g|G)(i|I)(f|F))|((p|P)(n|N)(g|G)))$";
					//regexstring = "^(.+(?=\.(jpg|gif|png))\.\2)$";
					regexstring = "^.+\.(((p|P)(d|D)(f|F)))$";
					returnError = "File must be a PDF";
					
			
				break;
				
				
				
				
				//
				// ALL THE create a CONFERENCE STUFF
				//
				
				//confernce image
				case("image"):
					if (myValue != "")	{
						regexstring = /^.+\.(jpe?g|png|gif)$/i;
						returnError = "Picture must be a JPG, GIF or PNG";
					}
				
				break;
				
				//conference date
				case("date"):
						
					regexstring = "^[0-9]{4}[/][0-9]{2}[/][0-9]{2}([ \s0-9])*$";
					returnError = "Please select a date";
				
				break;
				
				//conference location
				case("location"):
						
					regexstring = "^(.{1,})$"; 		
					returnError = "Please enter the general location for the conference";
				
				break;
				
				//conference title
				case("title"):
					regexstring = "^(.{1,})$"; 		
					returnError = "Please enter the name/title of the conference";
				break;
	
	
				/*case ("Phone"):
				
					regexstring = "^.+$"; 		
					returnError = "Enter a Phone Number";
				
				break;
				
				case ("Address"):
				
					regexstring = "^.+$"; 		
					returnError = "Enter your Address";
				
				break;
				case ("City"):
				
					regexstring = "^.+$"; 		
					returnError = "Enter your Address";
				
				break;
				
				
				
				case ("Postal"):
				
					regexstring = "^[a-zA-Z0-9]{3}[ -]?[a-zA-Z0-9]{3}$"; 		
					returnError = "Enter a Postal Code";
				
				break;*/
				
				
				
				/*case ("vEmail"):
					//regexstring = "(\w+?@\w+?\x2E.+)"; //Validates an email address
					regexstring = "^[a-zA-Z0-9\_\-]+[a-zA-Z0-9\.\_\-]*@([a-zA-Z0-9\_\-]+\.)+([a-zA-Z]{2,4}|travel|museum)$";
					returnError = "Not a valid email address";
					break;
				
				case("dBirthDate"):
					//PHP/Perl based regular expression: find aplhanumeric only(alpha and numeric only) (string and numeric only) but it will not find the empty string as it has to find string between 1 to 11 length.
					regexstring = "[a-z0-9/s]{1,30}";
					returnError = "Not a valid Birth Date";
					break;
					
				case("dDeathDate"):
					//PHP/Perl based regular expression: find aplhanumeric only(alpha and numeric only) (string and numeric only) but it will not find the empty string as it has to find string between 1 to 30 length.
					regexstring = "[a-z0-9/s]{1,30}";
					returnError = "Not a valid Death Date";
					break;	
				
				case("imageName"):
					//PHP/Perl based regular expression: find aplhanumeric only(alpha and numeric only) (string and numeric only) but it will not find the empty string as it has to find string between 1 to 30 length.
					if (myValue != "")	{
						if (document.getElementById("vImage").value == "")	{
							regexstring = "ERROR!";
							returnError = "Please select the appropriate image";
						}
					}
					else	{
						if (document.getElementById("vImage").value != "")	{
							regexstring = "ERROR!";
							returnError = "Please enter an image name";
						}
					}
					break;
				*/
				
				
				
				default:
					regexstring = "";
				break;			
			} //end switch
		
		}//end else VALSET
		
		
		//We have a set regex, therefore, our string needs to be "matched"
		
		if (regexstring != "")	{	
			if (!myValue.match(regexstring))	{
				errorArray[errorArray.length] = returnError;
			}
			returnval = false;
		}//end if
		
	}//end for	
	

	if (errorArray < 1)	{
		returnval = true;
		//if we have a loading thing to show, then do it
		if (document.getElementById('form-loading'))
			document.getElementById('form-loading').style.visibility = 'visible';
	}
	else	{
		
		//for (var key in errorArray)	{
		for (key=0; key < errorArray.length; key++)	{
			//alert(errorArray[key]);
			if (errorArray[key] != errorArray[key -1])
				div.innerHTML += errorArray[key] + "<BR>";
		}
	}

	//We used to scroll to top of page
	//scroll(0,0);
	//Now we scroll to error message div
	div.scrollTop = div.scrollHeight;

	
	
	
	if (document.getElementById('myTime') && returnval == true)
		makeDate();
	
	return returnval;	

}//end function