/*****************************************************************************
* Function Name: Check Form E-Mail
* Description: Checks a form for a valid e-mail address
* Inputs: frmName (String) - The form id to be validated
*****************************************************************************/
function checkEmail(frmName) { 
	var objForm = document.getElementById(frmName);

	if (objForm.email.value == "") {
   	alert ("Please enter your e-mail address.");
      return false;
   } else if (objForm.email.value.indexOf("@") == -1) {
      alert("Please enter a valid email address.\r(You have not entered an @ sign)");
      return false;
   } else if (objForm.email.value.indexOf(".") == -1) {
      alert("Please enter a valid email address.\r(You have not entered a full stop)");
      return false;
   }
	objForm.action = "../scripts/mail_contact.php";
	return true;
}

/*****************************************************************************
* Function Name: Setup XML / HTTP Request
* Description: Initialize XML / HTTP request variable
*****************************************************************************/
function getXMLHTTPRequest(){
	try{
		req = new XMLHttpRequest();
   } catch(err1) {
      try{
      	req = new ActiveXObject("Msxml2.XMLHTTP");
      } catch(err2) {
         try{
            req = new ActiveXObject("Microsoft.XMLHTTP");
         } catch(err3) {
         	req = false;
         }
      }
   }
   return req;
}

/*****************************************************************************
* Function Name: Run HTTP Request
* Description: Run HTTP request with provided file data
* Inputs: pageURL (String) - The page to be loaded
*****************************************************************************/
function runHttpRequest(pageURL){
	var http = new getXMLHTTPRequest();
	var fileURL = "pages/" + pageURL + ".html";
    http.open('get',fileURL,true);
    http.onreadystatechange = httpCallBack;
    http.send(null);
}

/*****************************************************************************
* Function Name: HTTP Callback
* Description: HTTP callback function - load page data
*****************************************************************************/
function httpCallBack(){
	if (http.readyState == 4) {
   	if (http.status == 200) {
      	var cbText = http.responseText;
         document.getElementById('pageContent').innerHTML = cbText;
      }
   }
}

/*****************************************************************************
* Function Name: Search Recipes
* Description: Run HTTP request to search recipe database
* Inputs: form (object) - The form containing the search criteria
*****************************************************************************/
function searchRecipes(form){
	var http = new getXMLHTTPRequest();
	var fileURL = "../scripts/recipe_search.php?category=" + form.category_select.value + "&search=" + form.search_string.value;
    http.open('get',fileURL,true);
    http.onreadystatechange = function(){
	    if (http.readyState == 4 && http.status == 200){
			document.getElementById("recipeCallback").innerHTML = http.responseText;
			document.getElementById("recipeSearchAd").style.display = "block";
		}
	}
    http.send(null);
}

