// JavaScript Document

/*======= Testimonials Page Javascript ======== BEGIN */
if (typeof testim != "undefined" && testim)
	testim_ld();

function testim_ld(){
	$("#testim").load("/db/testim_load.php", {});
}
/*======= Testimonials Page Javascript ======== END */


/*======= Contact Us Page Javascript ========== BEGIN */
jQuery("#submit").click(validate_form); //Listens for the "submit" button clicked on the contact us page

function validate_form(){
	if (valid_form(document.contact_us)){
		if(valid_recaptcha(document.contact_us)){
			snd_mail();
		}
	}
}

function valid_recaptcha(passForm){
	if (passForm.recaptcha_response_field.value == "")
	{
		alert("Please enter the validation text");
		passForm.recaptcha_response_field.focus();
		return false
	} else {
		var prvkey = "6LfrcrwSAAAAAHIGSYU_yT_rpBRCSDwg2hVThuS9"
		var remIP = $("#clientIP").val();
		var recap_challenge = $("#recaptcha_challenge_field").val();
		var recap_response = $("#recaptcha_response_field").val();
		jQuery.post("http://api-verify.recaptcha.net/verify", {privatekey:prvkey, remoteip:remIP, challenge:recap_challenge, response:recap_response}, 
					function(data){
						if(data.search("true")){
							//Do nothing and allow the function to finish
							//return true;
						} else {
							return false;
							alert("Error with recaptcha\n" + data.value);
						}}, "text");
		return true;
	}
}

function valid_form(passForm){
	var cnt = -1
	for (i = 0; i<passForm.contact.length; i++) {
		if (passForm.contact[i].checked) {
			cnt = i
		}
	}
	if (passForm.full_name.value == "")
	{
		alert("Please enter your name.");
		passForm.full_name.focus();
		return false;
	}
	if (passForm.email_address.value == "")
	{
		alert("Please enter your email address.");
		passForm.email_address.focus();
		return false;
	}
	if (passForm.comment.value == "")
	{
		alert("Please let us know how we can help.");
		passForm.comment.focus();
		return false;
	}
	if (cnt == -1) {
		alert("Please choose a way for us to contact you.")
		return false
	}
	if ($("input[name='contact']:checked").val() == "phone") {
		if (passForm.contact_time.value == "") 
			{
				alert("Please let us know what time to contact you.");
				passForm.contact_time.focus();
				return false;
			}
	}
	return true;
}

function snd_mail() {
	var email = $("input[name='email_address']").val();
	var name = $("input[name='full_name']").val();
	var comment = $("textarea[name='comment']").val();
	var contact = $("input[name='contact']:checked").val();
	var contact_time = $("textarea[name='contact_time']").val();
	jQuery.post("send_mail.php", {full_name:name, email_address:email, comment:comment, contact:contact, contact_time:contact_time}, 
				function(data){
					if(data.result == "success"){
						$(':input', '#contact_us')
							.not(':button, :submit, :reset, :hidden')
							.val('')
							.removeAttr('checked')
							.removeAttr('selected');
						Recaptcha.reload();
						$('#email_address').focus();
						alert("Thank you, your message has been sent.");
					} else {
						alert("We're sorry, your message did not get sent, please try again.\n"+result.error)
						Recaptcha.reload();
						$("#email_address").focus();
					}
				}, "json");
	
	
	
	
}

/*======= Contact Us Page Javascript ========== END */

function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=0,width=480,height=500,left = 402.5,top = 150');");
}

/*function openVideo(URL, name) {
	var loc = URL;
	var id = name;
	window.open(loc, id, "menubar=0,resizeable=0,width=480,height=360,left=100,top=100");
}
	*/
