// JavaScript Document
$(document).ready(function() {
	if (document.getElementById("testcontent")) {
		$("p.test").hide();	
		testimonials=new Array();
		testimonials = $("p.test");
		selectTest();
	}
	if (document.getElementById("tbgfaq")) {
		//set all contained H3 headers to links and hide sub paragraphs
		$("h3 + p").hide();		//Hide all paras next to H3 tags
		var headers = $("h3");
		$(headers).each(function(i){	//add click event to each header
			var header = headers[i];			// This header
			$(header).wrap("<a href='#'></a>");  //add link
		});
		var headerlinks = $("#tbgfaq > a");
		
		$(headerlinks).click(function(i){
			var $nextP = $(this).next();
			$nextP.slideDown('fast');
			var $visibleSiblings = $nextP.siblings('p:visible');
			$visibleSiblings.hide();
			return false;
		});
	}	
	
	//search for contact links and add in function click
	if (document.getElementById("contentForm") || document.getElementById("cont") ) {
		$(".popForm").click(function(i){
			$("#TB_overlay").show();
			$("#formOverlay").animate({
   				opacity: 'show'
 				}, 1000);
			return false;
		});
	}	
	
	if (document.getElementById("formOverlay")) {
		$("#formOverlay").css({marginLeft: '-' + parseInt((630 / 2),10) + 'px', width: 630 + 'px'});
		if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
			$("#formOverlay").css({marginTop: '-' + parseInt((530 / 2),10) + 'px'});
		}
	}
	
 });
		
function closeForm() {
	$("#formOverlay").hide();
	$("#TB_overlay").hide();
}

function showForm() {
	$("#formdata").show();
}

function selectTest(jp) {
	var j = Math.floor( Math.random() * ( 6 + 1 ) );
	if (j == jp) {
		j = j+1;
	}
	for (i = 0; i < testimonials.length; i++) {
		$(testimonials[i]).hide();
	}
	if (testimonials[j] == "") {
		$(testimonials[1]).animate({
	   opacity: 'show'
	 }, 1000);
	}
	else {
		$(testimonials[j]).animate({
	   opacity: 'show'
	 }, 1000);
	}
	var delay = setTimeout("selectTest(" + j + ")",10000)
}

function validate(thisform,referrer) {
	with (thisform) {
		if ($("#contactform")) {
			//check all form values completed
			$("input.val").each(function(i){
				if ($(this).attr("id") == "email") { 
					if (! validateEmail(this)) {
						$("~ span", this).show(); 
						$(this).css({ border: "1px solid #f00" });
						$("#errMess").show();
						this.focus();
						subfrm = 'false';
						return false;
					}
				}
				if (this.value == ""){ 
					$("~ span", this).show(); 
					$(this).css({ border: "1px solid #f00" });
					$("#errMess").show();
					this.focus();
					subfrm = 'false';
					return false;
				} else {
					$("~ span", this).hide();
					$("#errMess").hide();
					$(this).css({ border: "1px solid #CCC" });
					subfrm = 'true';
				}
			});
		}
		if (subfrm == 'false') {
			return false;
		}else{
			$("#formdata").hide();
			$("#feedback").append("<p class=center>Please wait, sending data<br><img src=i/ajax-loader.gif /></p>");
			var frmData = $("form").serialize();
			$.ajax({
			   type: "POST",
			   url: "http://www.petmates.com.au/mailer.php",
			  data: frmData,
			  
			   timeout: 1000,
				//error: function(msg){
//					$("#feedback > p").remove();
//				 	$("#feedback").append("<h3>Thank you" + msg + "your information has been received</h3><p><a href='#' onclick='closeForm()'>You can now close this form</a></p>");
//				},
//			   success: function(msg){
//				 $("#feedback > p").remove();
//				 if (referrer == "contact") {
//					 $("#feedback").append("<h3><span class='hlight'>Thank you" + msg + "your information has been received</span></h3><p><strong>We will be in contact with you very soon</strong></p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>");
//					 return false;
//				 }else {
//				 	$("#feedback").append("<h3>Thank you" + msg + "your information has been received</h3><p><a href='#' onclick='closeForm()'>You can now close this form</a></p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>");
//					return false;
//				 }
//			   }
error: function(msg){
					$("#feedback > p").remove();
				 	$("#feedback").append(msg);
				},
			   success: function(msg){
				 $("#feedback > p").remove();
				 $("#feedback").append(msg);
			   }
			 });
			return false;
		}		
	}
 }
 
 function resetForm() {
	$("#formdata").show();
	$("#feedback > p").remove();
	$("#feedback > h3").remove();
}

 function validateEmail(field) {
    //Validating the email field
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    if (! field.value.match(re)) {
        return (false);
    }
    return(true);
}