function clickclear(thisfield, defaulttext) {
	if (thisfield.value == defaulttext) {
		thisfield.value = "";
	}
}

function clickrecall(thisfield, defaulttext) {
	if (thisfield.value == "") {
		thisfield.value = defaulttext;
	}
}

/*function previousStage(stage) {
	document.getElementById("stage").val=stage;	
}*/
$(function() {
		$( "#datepicker" ).datepicker({
			showOn: "both",
			buttonImage: "images/calender.png",
			buttonImageOnly: true,
			minDate: "+1D",
			maxDate: "+6M", 
			dateFormat: "dd/mm/yy"
		});
});
function clearText(field){
    if (field.defaultValue == field.value) field.value = '';
    else if (field.value == '') field.value = field.defaultValue;
}
function validEmail (email) {
	reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4}|museum|travel)$/
	return reg.test (email); 
}

function PostcodeAnywhere_Interactive_Find_v1_10Begin(Key, SearchTerm, PreferredLanguage, Filter, UserName)
   {
      var scriptTag = document.getElementById("PCA6d35cfc188f1451f9cfdf1b5d751a716");
      var headTag = document.getElementsByTagName("head").item(0);
      var strUrl = "";

      //Build the url
      strUrl = "https://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/Find/v1.10/json.ws?";
      strUrl += "&Key=" + encodeURI(Key);
      strUrl += "&SearchTerm=" + encodeURI(SearchTerm);
      strUrl += "&PreferredLanguage=" + encodeURI(PreferredLanguage);
      strUrl += "&Filter=" + encodeURI(Filter);
      strUrl += "&UserName=" + encodeURI(UserName);
      strUrl += "&CallbackFunction=PostcodeAnywhere_Interactive_Find_v1_10End";

      //Make the request
      if (scriptTag) 
         {
            try
              {
                  headTag.removeChild(scriptTag);
              }
            catch (e)
              {
                  //Ignore
              }
         }
      scriptTag = document.createElement("script");
      scriptTag.src = strUrl
      scriptTag.type = "text/javascript";
      scriptTag.id = "PCA6d35cfc188f1451f9cfdf1b5d751a716";
      headTag.appendChild(scriptTag);
   }

function PostcodeAnywhere_Interactive_Find_v1_10End(response)
   {
      //Test for an error
      if (response.length==1 && typeof(response[0].Error) != 'undefined')
         {
            //Show the error message
            alert(response[0].Description);
         }
      else
         {
            //Check if there were any items found
            if (response.length==0)
               {
                  alert("Sorry, no matching items found");
               }
            else
               {
                   document.getElementById('return').style.display = '';
					document.getElementById('return').options.length = 0;
					var newElem = document.createElement("OPTION");
				   newElem.text = "Select Address";
				   newElem.value = "";
				   document.getElementById('return').options.add(newElem);
                    for (var i=0;i<response.length;i++)
			            document.getElementById('return').options.add(new Option(response[i].StreetAddress + ", " + response[i].Place, response[i].Id));
               }
         }
   }
   function PostcodeAnywhere_Interactive_RetrieveById_v1_10Begin(Key, Id, PreferredLanguage, UserName)
   {
      var scriptTag = document.getElementById("PCAa73f9bc2b60d4e4cbd595512478a3291");
      var headTag = document.getElementsByTagName("head").item(0);
      var strUrl = "";

      //Build the url
      strUrl = "https://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/RetrieveById/v1.10/json.ws?";
      strUrl += "&Key=" + encodeURI(Key);
      strUrl += "&Id=" + encodeURI(Id);
      strUrl += "&PreferredLanguage=" + encodeURI(PreferredLanguage);
      strUrl += "&UserName=" + encodeURI(UserName);
      strUrl += "&CallbackFunction=PostcodeAnywhere_Interactive_RetrieveById_v1_10End";

      //Make the request
      if (scriptTag) 
         {
            try
              {
                  headTag.removeChild(scriptTag);
              }
            catch (e)
              {
                  //Ignore
              }
         }
      scriptTag = document.createElement("script");
      scriptTag.src = strUrl
      scriptTag.type = "text/javascript";
      scriptTag.id = "PCAa73f9bc2b60d4e4cbd595512478a3291";
      headTag.appendChild(scriptTag);
   }

function PostcodeAnywhere_Interactive_RetrieveById_v1_10End(response)
   {
      //Test for an error
      if (response.length==1 && typeof(response[0].Error) != 'undefined')
         {
            //Show the error message
            alert(response[0].Description);
         }
      else
         {
            //Check if there were any items found
            if (response.length==0)
               {
                  alert("Sorry, no matching items found");
               }
            else
               {
				document.getElementById("addressline_1").value= response[0].Line1;
				document.getElementById("addressline_2").value= response[0].Line2;
				document.getElementById("address_town").value= response[0].PostTown;
				document.getElementById("address_county").value= response[0].County;
				document.getElementById("address_country").value= response[0].CountryName;
				document.getElementById("postcode").value= response[0].Postcode;
			    document.getElementById('return').style.display = 'none';
               }
         }
   }

$(document).ready(function() {
	$("form#contact").submit(function() {
		var name = $('#name').attr('value');
		var phone = $('#phone').attr('value');
		var email = $('#email').attr('value');
		var enquiry = $('#enquiry').attr('value');
		
		if(name == "" || phone == "" || email == "" || enquiry == "" || validEmail(email) == false) {
			alert("Please ensure all fields are filled in correctly and that you have entered a valid E-Mail address.");
			return false;
		} else {
			$.ajax({
				type: "POST",
				url: "http://www.bookyourtheorytestonline.co.uk/includes/submit-contact-form.php",
				timeout: 7000,
				data: "name="+name+"&phone="+phone+"&email="+email+"&enquiry="+enquiry,
				success: function(){
					//$('form#contact').fadeOut('fast');
					$('#success').fadeIn('fast');
					setTimeout("window.location = 'http://www.bookyourtheorytestonline.co.uk/?page=thank_you_contact_us'", 4000);
				}
			});
		}
		return false;
	});
	
	$("form#callback").submit(function() {
		var name = $('#name').attr('value');
		var phone = $('#phone').attr('value');
		var subject = $('#subject').attr('value');
		var callback_times = $('#callback_times').attr('value');
		
		if(name == "" || phone == "" || subject == "" || callback_times == "") {
			alert("Please ensure all fields are filled in correctly.");
			return false;
		} else {
			$.ajax({
				type: "POST",
				url: "http://www.bookyourtheorytestonline.co.uk/includes/submit-callback-form.php",
				timeout: 7000,
				data: "name="+name+"&phone="+phone+"&subject="+subject+"&callback_times="+callback_times,
				success: function(){
					//$('form#contact').fadeOut('fast');
					//$('div.success').fadeIn('slow');
					setTimeout("window.location = 'http://www.bookyourtheorytestonline.co.uk/?page=thank_you_callback'", 2000);
				}
			});
		}
		return false;
	});
});

