/*
 * validation.js
 * Script that takes in the ajax form with all of the serialized inputs 
 * and sends them to a PHP regular expression matching file to sanitize form
 * inputs. If the field is required or incorrect the textarea will be changed 
 * with jquery/css to red to indicate to the user that the form field is 
 * not valid 
 *
 * -Laura 08/19/09
 */
$(document).ready(function(){
	$("form#theForm").submit(function(){
  	  //alert("In validate function. AJAX POST call to validate script -> ");
			validate();
			
			return false;
	});
	return false;
});

//Main AJAX validation function 

function validate(){
			
			//make sure that all HTML input fields are 'name'd for the serialize function
			var str = $('#theForm :input').serialize();
			$.ajax({
      	type: "POST",
      	url: "formvalidation.php",
      	data: str,
				dataType: "text",
      	success: function(result) { 
					var test = result.substr(0,5);
					var cleanString = result.replace(/error,/,"");
					if (test == 'error') { 
							var myList = cleanString.split(",");
							//Iterates myList and adds the css attributes
							for(var i in myList){
								    var listItem = myList[i];
										var fieldID ="#" + listItem + "";
										$(fieldID).css('background-color','#FF9999'); 
							}	
					} 
					else
					{
						var html = "not set";
						$.ajax({
      				type: "POST",
      				url: "content/appThankYou.php",
      				data: null,
							/* We need this to return before executing the "#form".show 
							 * function below. So this AJAX call must be syncrhonous. */
							async: false,
							dataType: "text",
      				success: function(result) 
							{ 
								html = result;
							}
						});
						
						$("#form").hide(250);
							//this takes the div and refreshes it via ajax when the form is submitted
							// var tmpHTMLHello = "<h2>Thank you!</h2>\n<p>Your request for more information has been received.  A Saunders College of Business Executive Programs representative will contact you soon regarding your request.</p>\n<h2>For Further Information...</h2>\n<p>Executive MBA Program<br>\nE. Philip Saunders College of Business<br>\nRochester Institute of Technology<br>\n107 Lomb Memorial Drive<br>\nRochester, NY 14623</p>\n<p>Telephone: 585-475-7435<br>\nFax: 585- 475-6441<br>\nE-mail: <a href=\"mailto:embamail@rit.edu\">embamail@rit.edu</a></p>\n<h2>Attend an Executive Briefing Session</h2>\n<p>To learn more about the Executive MBA Program at RIT, please join us at an upcoming <a href=\"/resources/infosessions.php\">Information Session</a>.</p>";
						$("#form").html(html).show(500);
					}
				}
		});
}//validate		



