// Allows console to alert in IE
if( !console ) { var console = { log: function(msg) { alert(msg); } } }


sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

/*Form*/
var getTheDate = new Date();
var getNextDate = getTheDate.getDate()+1;
var getMonthDate = getTheDate.getMonth();

function checkForNextDay(dateText){
	var getDateText = dateText.split('-');
	if (getNextDate == getDateText[1]){
		alert ('appointment must be more then 24 hours');
		$('.hasDatepicker').val("");
		$("input#recaptcha_response_field").val("");
		return false;
	}
}

function showResponse(responseText, statusText)  {
	$('.setanappointment table').hide();
	$('.setanappointment').append('<p class="notice">Thank you <strong> '+$('.fname').val()+'</strong>!  We have received your appointment.</p>');
}
function validateCaptcha(form) {
	challengeField = $("input#recaptcha_challenge_field").val();
	responseField = $("input#recaptcha_response_field").val();
	//console.log(challengeField);
	//console.log(responseField);
	var html = $.ajax({
		type: "POST",
		url: "php/ajax.recaptcha.php",
		data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
		async: false
	}).responseText;
	if(html == "success") {
		var str = $(form).serialize();
		$.ajax({
			type: 'POST',
			url: "/php/email.php",
			data: str,
			success: showResponse
		});
		return true;
	} else {
		$("#captchaStatus").html("The security code you entered did not match. Please try again.");
		Recaptcha.reload();
		return false;
	}	
}

$(document).ready(function(){
	var findboardingForm = $('body').find('#boardingForm').length;
	if (findboardingForm){
		 $("#boardingForm").validate({
			debug: true
			,messages: { captcha: "Correct captcha is required. Click the captcha to generate a new one" }
			,rules: {
				fname: "required",
				lname: "required",
				pname:  "required",
				pbreed: "required",
				datein: "required",
				dateout: "required",
				email: { required: true, email: true },
				selectpet: "required"
			}
			,submitHandler: function(form) {
				$('.hasDatepicker').each(function(i,ele){
					var dateText = $(ele).val();
					checkForNextDay(dateText);
				});
				validateCaptcha(form);
			}
			,invalidHandler: function(form, validator) {
			var errors = validator.numberOfInvalids();
				if (errors) {
					var message = errors == 1
					? 'You missed 1 field. It has been highlighted'
					: 'You missed ' + errors + ' fields. They have been highlighted';
					$("div.error span").html(message);
					$("div.error").show();
				} else {
					$("div.error").hide();
				}
			}
		});
	}

	var findgroomingForm = $('body').find('#groomingForm').length;
	if (findgroomingForm){
		 $("#groomingForm").validate({
			debug: true
			,messages: { captcha: "Correct captcha is required. Click the captcha to generate a new one" }
			,rules: {
				fname: "required",
				lname: "required",
				pname:  "required",
				pbreed: "required",
				appointmentdate: "required",
				email: { required: true, email: true },
				selectpet: "required"
			}
			,submitHandler: function(form) {
				$('.hasDatepicker').each(function(i,ele){
					var dateText = $(ele).val();
					checkForNextDay(dateText);
				});
				validateCaptcha(form);
			}
			,invalidHandler: function(form, validator) {
			var errors = validator.numberOfInvalids();
				if (errors) {
					var message = errors == 1
					? 'You missed 1 field. It has been highlighted'
					: 'You missed ' + errors + ' fields. They have been highlighted';
					$("div.error span").html(message);
					$("div.error").show();
				} else {
					$("div.error").hide();
				}
			}
		});
	}


});

