jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

$(document).ready(function(){
$.preloadImages("/images/ps_nav_bg_active.png", "/images/ps_nav_bg_over.png");
$("a[@rel='external']").click( function() {
window.open( $(this).attr('href') );
return false;
});

// At A Glance Show and Hide

//set links
$("#glance div").click( function(){
								 
	window.location=$(this).find("a").attr("href"); return false;
});


$("#glance div").hide();
$("#glance1").show();
var glancetotal = $("#glance div").size();
var glancenum = 1;
$("#gfwd").click( function(){
	if(glancenum >= glancetotal){
		glancenum = 1;
	}else{
		glancenum++;
	}
	$("#glance div").hide();

	$("#glance" + glancenum).show();
});
$("#gback").click( function(){
	if(glancenum <= 1){
		glancenum = 3;
	}else{
		glancenum--;
	}
	$("#glance div").hide();

	$("#glance" + glancenum).show();
});

// Auto Glance
ataglance = setInterval(function() {
	$("#glance div").hide();
	if(glancenum >= glancetotal){
		glancenum = 1;
	}else{
		glancenum++;
	}
	$("#glance" + glancenum).show();

}, 4000); 

// Stop Glance on Hover
$('#glance div, #glance .glanceheader').hover(function() {
        clearInterval(ataglance);
      }, function() {
        ataglance = setInterval(function() {
	$("#glance div").hide();
	if(glancenum >= glancetotal){
		glancenum = 1;
	}else{
		glancenum++;
	}
	$("#glance" + glancenum).show();

}, 4000); 
});

// END At A Glance 

// Coming Soon Links



var soontext = "";
var soontextnew = "Coming Soon!";
$("a[@rel='soon']").hover(function() {
	soontext = $(this).text();
	$(this).css({ textDecoration: "none"});
	$(this).text(soontextnew);
}, function() {
	$(this).text(soontext);
	soontext = "";
});
$("a[@rel='soon']").click (function() {
	return false;
});

// END Coming Soon Text




						   	$('#recipient').bind('change', function(){
																	val=$('#recipient').val();
																	if(val=='Clarendon_Dispensary@bga.bm')
																		{
																			$timing='Monday - Saturday 8:00 A.M. to 6:00 P.M. / Closed Sundays & Holidays';
																		}
																	else if(val=='CollectorsHill_Dispensary@bga.bm')
																		{
																	 		$timing='Monday - Saturday 8:00 A.M. to 8:00 P.M. / Sundays & Holidays 11:00 A.M. to 7:00 P.M.';
																		}

																	else if(val=='Paget_Dispensary@bga.bm')
																		{
																	 		$timing='Monday - Saturday 8:00 A.M. to 6:00 P.M. / Sundays & Holidays 10:00 A.M. to 6:00 P.M.';
																		}
																		
																	else if(val=='Phoenix_Dispensary@bga.bm')
																		{
																	 		$timing='Monday - Saturday 8:00 A.M. to 6:00 P.M. / Sundays & Holidays 10:00 A.M. to 6:00 P.M.';
																		}
																	else if(val=='Woodbourne_Dispensary@bga.bm')
																		{
																	 		$timing='Monday - Saturday 8:00 A.M. to 6:00 P.M. / Closed Sundays & Holidays';
																		}
																	else if(val=='jrichards@bga.bm')
																		{
																	 		$timing='Monday - Friday 8:30 A.M. to 12:30 P.M. and 1:30 P.M. to 5:30 P.M.';
																		}
																		
																	else if(val=='')
																		{
																	 		$timing='';
																		}	
																	
																	
																	$('#locationinfo').html($timing);
																	});
																	

				var options = { 

					beforeSubmit:  showRequest,  // pre-submit callback 

					success:       showResponse  // post-submit callback 

				}; 

				var options2 = { 

					beforeSubmit:  showRequest2,  // pre-submit callback 

					success:       showResponse2  // post-submit callback 

				}; 
				
				

				// bind form using 'ajaxForm' 

				$('#form_contact').ajaxForm(options); 
				$('#form_prescription').ajaxForm(options2); 
				

		});

 

	// pre-submit callback 

	function showRequest(formData, jqForm, options) { 

	     var form = jqForm[0]; 

	    var queryString = $.param(formData); 		 

		var flag=0;

		$("#name, #email, #message").removeClass('wrong');

		/*$("#message").html('');*/

		msg="";

		 if ($("#name").val()=='') 

			{ 

			$("#name").addClass('wrong');

			msg+="<li>Please enter your name</li>";

			flag=1; 

			} 

		 if ($("#email").val()=='') 

			{ 

			$("#email").addClass('wrong');

			msg+="<li>Please enter a valid email</li>";			

			flag=1; 

			} 

		 if ($("#message").val()=='') 

			{ 

			$("#message").addClass('wrong');

			msg+="<li>You cannot send us an empty message!</li>";						

			flag=1; 

			} 

		if (flag==1)

			{
			alert("All fields highlighted in red are required to be filled in");
		

			return false;

			}			
	$('#submit').attr('value', 'Sending...');
	$('#submit').attr('disabled', 'disabled');
	return true; 

	} 

	 

	// post-submit callback 

	function showResponse(responseText, statusText)  { 

		// for normal html responses, the first argument to the success callback 

		// is the XMLHttpRequest object's responseText property 

	 

		// if the ajaxForm method was passed an Options Object with the dataType 

		// property set to 'xml' then the first argument to the success callback 

		// is the XMLHttpRequest object's responseXML property 

	 

		// if the ajaxForm method was passed an Options Object with the dataType 

		// property set to 'json' then the first argument to the success callback 

		// is the json data object returned by the server 

	
		$('#submit').attr('value', 'Send Message');
		$('#submit').removeAttr('disabled');

		if(responseText=="#email")

			{
			alert('Please enter a valid email address, e.g. john.doe@gmail.com');
		//	$("#notice").html('<div class="notice" id="flashMessage"><div class="alert"><span>Unfortunately we were unable to process your request, please take the following into consideration:</span><ul><li>It looks like you have missed something out. Make sure your email is correctly formated.</li></div></div>');

//			$('#form_contact').Shake(3);			

			responseText=responseText.split(',');

			var i=0;

			while (i < responseText.length)

			 	{


			  	$(responseText[i]).addClass('wrong');

				i++;

			  	}

			}
//		if(responseText=="done")
		else
			{

			$("#name, #email, #message").removeClass('wrong');			

			$("#form_contact").clearForm();			
			$("#form_contact").hide();
			$("#form_contact").remove();
			$("#notice").html('Your email has been sent, we will try and get back to you within 24 hours.');

			return false;

			}	 			

	} 



	function showRequest2(formData, jqForm, options) { 

	     var form = jqForm[0]; 

	    var queryString = $.param(formData); 		 

		var flag=0;

		$("#rx_number, #name, #email, #phone, #filled_at, #recipient, #day, #time").removeClass('wrong');

	
		msg="";

		 if ($("#rx_number").val()=='') 

			{ 
		
			$("#rx_number").addClass('wrong');

			flag=1; 

			} 

		 if ($("#email").val()=='') 

			{ 

			$("#email").addClass('wrong');

			flag=1; 

			} 

		 if ($("#name").val()=='') 

			{ 

			$("#name").addClass('wrong');

			flag=1; 

			} 
		 if ($("#phone").val()=='') 

			{ 

			$("#phone").addClass('wrong');

			flag=1; 

			} 			
			
		 if ($("#filled_at").val()=='') 

			{ 

			$("#filled_at").addClass('wrong');

			flag=1; 

			} 						

		 if ($("#recipient").val()=='') 

			{ 

			$("#recipient").addClass('wrong');

			flag=1; 

			} 						

		if (flag==1)

			{
			alert("All fields highlighted in red are required to be filled in");
		

			return false;

			}			
	$('#submit').attr('value', 'Sending Data...');
	$('#submit').attr('disabled', 'disabled');
	return true; 

	} 

	 

	// post-submit callback 

	function showResponse2(responseText, statusText)  { 

		// for normal html responses, the first argument to the success callback 

		// is the XMLHttpRequest object's responseText property 

	 

		// if the ajaxForm method was passed an Options Object with the dataType 

		// property set to 'xml' then the first argument to the success callback 

		// is the XMLHttpRequest object's responseXML property 

	 

		// if the ajaxForm method was passed an Options Object with the dataType 

		// property set to 'json' then the first argument to the success callback 

		// is the json data object returned by the server 


		if(responseText=="#email")

			{
			alert('Please enter a valid email address, e.g. john.doe@gmail.com');
		//	$("#notice").html('<div class="notice" id="flashMessage"><div class="alert"><span>Unfortunately we were unable to process your request, please take the following into consideration:</span><ul><li>It looks like you have missed something out. Make sure your email is correctly formated.</li></div></div>');

//			$('#form_contact').Shake(3);			

			responseText=responseText.split(',');

			var i=0;

			while (i < responseText.length)

			 	{


			  	$(responseText[i]).addClass('wrong');

				i++;

			  	}

	
			$('#submit').attr('value', 'Place Request');
			$('#submit').removeAttr('disabled');

			}
//		if(responseText=="done")
		else
			{
/*
			$("#rx_number, #name, #email, #phone, #filled_at, #recipient, #day, #time").removeClass('wrong');

			$("#form_prescription").clearForm();			
			$("#form_prescription").hide();
			$("#form_prescription").remove();
			$("#notice").html('Your email has been sent, we will try and get back to you within 24 hours.');
*/
			 location.href = 'prescriptions_confirmation.php';
			return false;

			}	 			

	} 

