// JavaScript Document
var ajaxFrontEnd = "wp-content/themes/duglin/_ajax";
var ajaxProduct  = "wp-content/plugins/duglinProducts/_ajax";

$(document).ready(function(){
	$("#errorFound").hide();	
	$("#locationSideBox").css({"height" : $("#locations").height() - 20 + "px"});
	$("#locationRight").css({"height" : $("#locations").height() - 20 + "px"});
	$("#cartLoading").hide();
	$("#signupError").hide();
	
	$("#newsContentArchive ul li:first-child").addClass("selected");
		$("#newsContentArchive ul li").click(function(){
													$("#newsContentArchive ul li").removeClass("selected");
													$(this).addClass("selected");
													getNewsStory($(this).attr("id"));
													  });
	

	$(".dropdown").each(function(){								
								$(this).parent().hover(function(){
								$(".dropdown", this).slideDown('fast');
								}, function(){
								$(".dropdown", this).slideUp('fast');
								 });							
								 
													  });
	
	
	$("#secondBread").hide();
	$("li.moreBread").click(function(){
									$("#secondBread").slideToggle("fast");
									$(this).toggleClass("moreBreadUp");
									 });

	$("#product_main").height($("#mainContent").height());
	$("#prod_cats").height($("#mainContent").height() - 20);
	
	$("#leftFAQ h4.question:first").addClass("active");
	$("#leftFAQ div.answer:not(:first)").hide();

	$("#leftFAQ h4.question").click(function(){	
	
	  $(this).next("div.answer").slideToggle("fast")
	  .siblings("div.answer:visible").slideUp("fast");
	  $(this).toggleClass("active");
	  $(this).siblings("h4.question").removeClass("active");
											 });
	});

function getSpecialist(i)
{
	if(i != "null")
	{
		$.ajax({
      		type: "GET",
      		url:  ajaxFrontEnd + "/loc_ajax.php",
      		data: "viewSpecial=ok&specialist=" + i,
      		cache: false,
         	success: function(data){
           		$("#specialistInfo").html(data);	
         	}
      	});

	}
	else
		$("#specialistInfo").html("");	
}

function addToShoppingCart(val)
{
		$("#cartLoading").show('fast');
		$.ajax({
      		type: "GET",
      		url:  ajaxProduct  + "/addToCart.php",
      		data: "shoppingCart=ok&addToCart=ok&cartItem=" + val,
      		cache: false,
         	success: function(data){
  				$("#cartLoading").hide('fast');
				$("#itemsCart").html("<a href='?page_id=63'>" + data + "</a>");
         	}
      	});		
}

function removeCartItem(swId)
{
			$.ajax({
      		type: "GET",
      		url:  ajaxProduct + "/addToCart.php",
      		data: "shoppingCart=ok&removeFromCart=ok&cartItem=" + swId,
      		cache: false,
         	success: function(data){
				window.location = window.location;
         	}
      	});		
}

function checkUniqueUsername(val)
{
	$("#mem_email").css({"background-color" : "#FFFFFF" });
			$.ajax({
      		type: "GET",
      		url:  ajaxFrontEnd + "/username.php",
      		data: "checkUserName=" + val,
      		cache: false,
         	success: function(data){
				if(data == "yes")
				{
				$("#mem_email").css({"background-color" : "#E1A8A8" });
				alert("This email address is already in our system.  Please go back to the previous screen and login.  If you forgot your password, you can click on the forgot password link, and a temporary password will be mailed to you");
				 return false;
				}
				
         	}
      	});		
		
		return true;
		
}

function requestFormFieldValidate()
{
	var errorFound = false;
	
	
	//CHeck the DAte
	/*var eventDate = $("#event_date").val();
	//var dateFilter = /^(\d{4})-(\d{1,2})-(\d{1,2})/;
	
	if(!dateFilter.test(eventDate))
	{
		errorFound = true;
		$("#event_date").css({"background-color" : "#E1A8A8" });
		alert("Your event date must be in proper format (YYYY-MM-DD).  If you do not have a date in mind, simply input 2011-01-01.  Thank you.");
	}
	else
	{
		$("#event_date").css({"background-color" : "#FFFFFF" });
	}*/
	
		
	$("#mem_password2").css({"background-color" : "#FFFFFF" });
	$("#requestForm :input.required").each(
									  function(){
											 var child = $(this);
											 	child.css({"background-color" : "#FFFFFF" });
											 if(child.val() == "")
											 {
											 	child.css({"background-color" : "#E1A8A8" });
												errorFound = true;
											 }
									   }
);

	$("#requestForm :input.email").each(
							function(){
								var email = $(this);
								email.css({"background-color" : "#FFFFFF" });
								var value = email.val();
								
							
							var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
								
								if(value == "")
								{
									email.css({"background-color" : "#E1A8A8" });
									errorFound = true;
								}
								
								if(!filter.test(value))
								{
									email.css({"background-color" : "#E1A8A8" });
									errorFound = true;
								}
								
							}
);

if($("#mem_password").val() != $("#mem_password2").val())
{
		$("#mem_password2").css({"background-color" : "#E1A8A8" });
		errorFound = true;
}

if(errorFound)
{
	$("#errorFound").slideDown('fast');
	return false;
}
else
	return true;

}

function getNewsStory(id)
{
		$.ajax({
      		type: "GET",
      		url:  ajaxFrontEnd + "/newsAjax.php",
      		data: "getNewsStory=" + id,
      		cache: false,
         	success: function(data){
					$("#newsContent").fadeOut('fast', function(){
					$("#newsContent").html(data);
					$("#newsContent").fadeIn('fast');
															   });
					
         	}
      	});		
}

function checkShippingValues(box)
{
	var mems = document.member_info;
	var ship = document.orderForm;
	
	if(ship.shipping_same.checked == true)
	{
		ship.ship_name.value = mems.f_name.value + " " + mems.l_name.value;
		ship.ship_add.value = mems.mem_add.value;
		ship.ship_city.value = mems.mem_city.value;
		ship.ship_state.value  = mems.mem_state.value; // Drop Down
		ship.ship_zip.value = mems.mem_zip.value;
	}
}

