
	//************************************************************************************
	//
	//  CHECK AGE
	//
	//************************************************************************************
	
	checkCookie();
	//checkIP();
	
	function checkIP()
	{
		
		$.post("http://www.theamarulaexperiment.com/recipes/getIP/", {'str': 'IP'},
	  		function(message){
				
				if(message == "CHECK")
				{
					if (window.location.href != "/recipes") {
						
						
					}else
					{
						window.location.href = "/recipes";
					}
					 
				}else if(message == "INVALID")
				{
					window.location.href = "/maintenance"
				}
			  });
		
		
	}
	
	
	function checkCookie()
	{
		
		$.post("http://www.theamarulaexperiment.com/recipes/getCookie/", {'str': 'BDAY'},
	  		function(message){
				
				if(message == "")
				{
					window.location.href = "/landing";
					 
				}else if(message == "ofAge")
				{
					
				}
			  });
	}


$(document).ready(function() {
	
	
	function addNewRecipe()
	{


   	$("#contentWrap").load(location.href+" #contentWrap>*",function(){reloadPartialPage()});


	}
	
	

	

	
	
	//************************************************************************************
	//
	//  STORE DEFAULT VALUES FOR INPUT FIELDS
	//
	//************************************************************************************
	
	$default_firstname = $('#firstname').val();
	$default_lastname = $('#lastname').val();
	$default_barname = $('#barname').val();
	$default_baraddress = $('#baraddress').val();
	$default_barcity = $('#barcity').val();
	$default_barstate = $('#barstate').val();
	$default_barzip = $('#barzip').val();
	$default_drinkname = $('#drinkname').val();
	$default_drinkimage = $('#drinkimage').val();
	$default_drinkingredients = $('drinkingredients').val();
	$default_drinkmethod = $('#drinkmethod').val();
	$default_emails = $('#emails').val();
	
	$default_age = $('#age').val();
	$default_homephone = $('#homephone').val();
	$default_email = $('#email').val();
	$default_homeaddress = $('#homeaddress').val();
	$default_homecity = $('#homecity').val();
	$default_homestate = $('#homestate').val();
	$default_homezip = $('#homezip').val();
	
	//************************************************************************************
	//
	//  ADD METHOD TO JQUERY VALIDATOR TO CHECK AGAINST DEFAULT VALUES
	//
	//************************************************************************************
	jQuery.validator.methods.oldRequired = jQuery.validator.methods.required;
	jQuery.validator.addMethod("required", function(value, element, param) {

		if (element.id !== 'barstate') {
			
			$checkvar = eval("$default_" + element.id);
			if ($checkvar == value) {
				return false;
			}
			else 
				if (value !== $checkvar) {
					return true;
			}}

		if(element.id == 'drinkingredients')
		{
			if(value == "")
			{
				return false;
			}
		}
		
		return jQuery.validator.methods.oldRequired.call(this, value, element, param);

        },
    jQuery.validator.messages.required // use default message
    );

	$("#messageBox").fadeTo(0, 0);

	//************************************************************************************
	//
	//  UPLOADIFY FLASH FILE UPLOADER
	//
	//************************************************************************************
	$('#fileInput').uploadify({
	'uploader'  : '/js/uploadify/uploadify.swf',
	'script'    : '/js/uploadify/uploadify.php',
	'cancelImg' : '/js/uploadify/cancel.png',
	'buttonImg' : '/images/bttn_browse.gif',
	'wmode'     : 'transparent',
	'buttonText': '',
	'width'     : '54',
	'multi'     : false,
	'height'    : '20',
	'auto'      : true,
	'folder'    : 'uploads/drinks',
	onComplete  : function (evt, queueID, fileObj, response, data) {
			
		},
	onSelect    : function (evt, queueID, fileObj) {
			$('#drinkimage').attr("value", fileObj.name);
		},	
	onError    : function (evt, queueID, fileObj, errorObj) {
			
		}	
	});
	
//************************************************************************************
//
//  FADE STEP DESCRIPTIONS FUNCTION
//
//************************************************************************************
function stepFade($step)
 {
	
 	if($step == 1)
	{
		
		$("#step1").fadeTo(1, 100);
		$("#step2").fadeTo(1, .4);
		$("#step3").fadeTo(1, .4);
		$("#step4").fadeTo(1, .4);
		
	}else if($step == 2)
	{
		
		$("#step2").fadeTo(1, 100);
		$("#step1").fadeTo(1, .4);
		$("#step3").fadeTo(1, .4);
		$("#step4").fadeTo(1, .4);
		
	}else if($step == 3)
	{
		
		$("#step3").fadeTo(1, 100);
		$("#step2").fadeTo(1, .4);
		$("#step1").fadeTo(1, .4);
		$("#step4").fadeTo(1, .4);
		
	}else if($step == 4)
	{
		$("#step4").fadeTo(1, 100);
		$("#step2").fadeTo(1, .4);
		$("#step1").fadeTo(1, .4);
		$("#step3").fadeTo(1, .4);
	}
 }
 
 var formStep = 1;

//************************************************************************************
//
//  FORM WIZARD FOR DRINK SUBMIT FORM
//
//************************************************************************************


$(function(){
	$("#DrinkSubmitForm").formwizard({ 


      //form wizard settings
      historyEnabled : true, 
      formPluginEnabled: true, 
	  afterNext: function(){
	  	

		formStep++;
	 	stepFade(formStep);

	   },
	 	 
		afterBack: function(){
	  	formStep--; 
		stepFade(formStep);
		
		},
      validationEnabled : true},
     {
	 	
	 	 showErrors: function(errorMap, errorList) {
	  	if (this.numberOfInvalids() > 0) {
			$("#messageBox").fadeTo(1, 1);
			$("#messageBox").html("Oops, you forgot to fill out " +
			this.numberOfInvalids() +
			" required fields.");
		}else if(this.numberOfInvalids() == 0)
		{
			$("#messageBox").fadeTo(0, 0);
		}
						   
		this.defaultShowErrors();
		},
		
		highlight: function(element, errorClass){
			$(element).addClass(errorClass);
			
			$(element).fadeOut(function(){
				$(element).fadeIn()
			})
		}
		

		
		
     },
		 {
			// form plugin settings
			dataType: 'json',
			resetForm: false
		 }

);
			});

//************************************************************************************
//
//  SUBMIT FORM VALIDATION AND CLICK
//
//************************************************************************************

$("#DrinkSubmitForm").validate();
	
	 
	

	$('#submit').click(function () {   

		
		//get rid of 'finish' button so can't click again
		$('#fileInput').remove();
		
		//bring up submit success div
		//showSubmitSuccess();
		
		//gets all the form values and puts them into a string
		
		var recipeName = $("#drinkname").val();
		recipeName.replace("&", "\&")
		
		var recipeMethod = $('#drinkmethod').val();
		
		var str = $("#DrinkSubmitForm").serialize();
		
		var barAddress = $("#baraddress").val();
		var barCity = $("#barcity").val();
		var barState = $("#barstate").val();
		
		
		
		
		$("#recName").val(recipeName);
		$("#recMethod").val(recipeMethod);
		
		
		
		
		var barGeo = findBar(barAddress, barCity, barState, recipeName);
		
		

		//post the form values to db
		$.post("http://www.theamarulaexperiment.com/formSubmit/save/", { 'str': str },
	  		function(message){
				
			
			  }, "json");
			  
	
		});
		

//************************************************************************************
//
// FIND BAR GEO LAT AND LONG
//
//************************************************************************************	
function findBar($address, $city, $state, $recipeName)
{
	
	var geocoder;
	var map;
	var addressString = $address + " " + $city + "," + $state;

	geocoder = new google.maps.Geocoder();
	geocoder.geocode({address: addressString }, function(results, status){
	
	
		
		if(status == google.maps.GeocoderStatus.OK){

			var latLong = results[0].geometry.location;
			
			$.post("http://www.theamarulaexperiment.com/formSubmit/latLong/", { 'latlong': latLong, 'name': $recipeName},
		  		function(message){
					
				  }, "json");
			
			
			
		}else {
			
     		
    	}

	})

}



//************************************************************************************
//
// SUBMIT ANOTHER RECIPE
//
//************************************************************************************		
			
			
$('#submitAnother').click(function () { 
	
		$('#successOverlay').css("display", "none");
		window.location.href = 'http://www.theamarulaexperiment.com/#0';
		$("#step1").fadeTo(1, 100);
		$("#step2").fadeTo(1, .4);
		$("#step3").fadeTo(1, .4);
		resetForm();

		});


	function browseClick()
	{
		$('#browseFile').click();
	};
	
	function startUpload()
	{
		$('#fileInput').startFileUpload();
	};
	
	function showSubmitSuccess()
	{
		
		
		$('#successOverlay').css("display", "block");
		$('#successOverlay').show();
		$('#successOverlay').fadeTo(1,100);
		$('#submit').fadeTo(0, 0);
		$('#submit').css('display', 'none');
		$('#submitFinal').fadeTo(0, 0);
		$('#submitFinal').css('display', 'none');
		$('#skipShare').fadeTo(0, 0);
		$('#backShare').fadeTo(0, 0);
		
		
	};
	
	$("#submitFinal").click(function(){
		
		
		showSubmitSuccess();
		
		var fbPic = $("#fbPic").val();
		var fbID = $("#fbID").val();
		
		//var emailAdds = $("#emails").val();
		//var emailMessage = $("#fb_message").val();
		
		
		
		var str = $("#DrinkSubmitForm").serialize();
		
		
		//post the form values to db
		$.post("http://www.theamarulaexperiment.com/formSubmit/sendEmails/", {'str': str, 'fbPic': fbPic, 'fbID' : fbID}, 
	  		function(message){
				addNewRecipe();
			  }, "json");
		
		
	});
	
	/*$("#submitFinal").click(function(){
		
		
		showSubmitSuccess();
		
		var fbPic = $("#fbPic").val();
		var fbID = $("#fbID").val();
		
		//var emailAdds = $("#emails").val();
		//var emailMessage = $("#fb_message").val();
		
		var str = $("#DrinkSubmitForm").serialize();
		addNewRecipe();
		
		//post the form values to db
		$.post("http://amarula.planitapps.com/formSubmit/sendEmails/", {'str': str, 'fbPic': fbPic, 'fbID' : fbID}, 
	  		function(message){
				
			  }, "json");
		
		
	});*/

//************************************************************************************
//
//  RESET FORM
//
//************************************************************************************
	function resetForm()
	{
		
		$('#firstname').val($default_firstname);
		$default_lastname = $('#lastname').val( $default_lastname);
		$default_barname = $('#barname').val($default_barname);
		$default_baraddress = $('#baraddress').val($default_baraddress);
		$default_barcity = $('#barcity').val($default_barcity);
		$default_barstate = $('#barstate').val($default_barstate);
		$default_barzip = $('#barzip').val($default_barzip);
		$default_drinkname = $('#drinkname').val($default_drinkname);
		$default_drinkimage = $('#drinkimage').val($default_drinkimage);
		$default_drinkingredients = $('drinkingredients').val($default_drinkingredients);
		$default_drinkmethod = $('#drinkmethod').val($default_drinkmethod);
		
		$default_age = $('#age').val($default_age);
		$default_homephone = $('#homephone').val($default_homephone);
		$default_email = $('#email').val($default_email);
		$default_homeaddress = $('#homeaddress').val($default_homeaddress);
		$default_homecity = $('#homecity').val($default_homecity);
		$default_homestate = $('#homestate').val($default_homestate);
		$default_homezip = $('#homezip').val($default_homezip);
		
		
		$('#submit').css('display', 'block');
		$('#submit').fadeTo(.5, 100);
		$('#submitFinal').css('display', 'block');
		$('#submitFinal').fadeTo(.5, 100);
		$('#skipShare').fadeTo(.5, 100);
		$('#backShare').fadeTo(.5, 100);
		$('#successOverlay').fadeTo(1,0);
		
		
		
		
	};
	
//************************************************************************************
//
// TESTING FOR FB CONNECT
//
//************************************************************************************
	
	$('#fbButt').click(function (){
	
		
		var recName = $("#recName").val();
		var method = $("#recMethod").val();
		
		
		$.post("http://www.theamarulaexperiment.com/formSubmit/getLatestRecipe/", {
			'str': recName
		}, function(recipe){

			var urlName = recipe;
			
			var actionLinks = [{ "text": "Submit Your Own", "href": "http://www.theamarulaexperiment.com"}];
			callPublish('Check out my Drink', urlName , recName, method);

		}, "script");
	});
	
function callPublish(msg, urlname, recname, recMethod) {
	
	var desc = recMethod;
	var introString = "Announce your drink to your Facebook friends!";
	var introMessage = "Help me win fame and fortune! Follow the link below and vote for my drink. And drink more Amarula!!!";
	
	
  	FB.ensureInit(function () {


	
	var recipeURL = "http://www.theamarulaexperiment.com/recipes/single/" + recname;
	
	var actionLinks = [{ 'text': 'Submit Your Own', 'href': 'http://www.theamarulaexperiment.com'}];
	var attachments =  {'name': recname , 'href': recipeURL, 'media':[{'type':'image','src': urlname,'href': recipeURL }],'description': desc};
    FB.Connect.streamPublish(introMessage, attachments, actionLinks, null, introString , fbCallback, true);
	
  });
}

function fbCallback(post_id, eception)
{
		
		
		$.post("http://www.theamarulaexperiment.com/recipes/reloadFBUser/", {
			'str': "FB"
		}, function(recipe){

			var fbid = recipe.user.uid;
			var fbpic = recipe.user.pic_square;
			$("#fbID").val(fbid);
			$("#fbPic").val(fbpic );

		}, "json");
}

//************************************************************************************
//
//  CONTACT FORM SUBMIT
//
//************************************************************************************
	$("#contactForm").validate();
		
	$('#contactSubmit').click(function (){
		
		var submitTest = 0;
	
	//alert($("#contactName").val());
	
	if ($("#contactName").val() == "") {
	//show firstname error
	submitTest++;
	$("#contactFormErrorBox").html("<p>Name Required</p>");
	$("#contactFormErrorBox").css('display', 'block');
	
	}
	else 
		if ($("#contactEmail").val() == "") {
		
		//show email error
		submitTest++;
		$("#contactFormErrorBox").html("<p>Email Required</p>");
		$("#contactFormErrorBox").css('display', 'block');
		
		}
		else 
			if ($("#contactSubject").val() == "") {
			
			//show subject error
			submitTest++;
			$("#contactFormErrorBox").html("<p>Subject Required</p>");
			$("#contactFormErrorBox").css('display', 'block'); 
			}
			else 
				if ($("#contactMessage").val() == "") {
				
				//show message error
				submitTest++;
				$("#contactFormErrorBox").html("<p>Message Required</p>");
				$("#contactFormErrorBox").css('display', 'block');
				}
	
	if (submitTest == 0) {
		$("#contactFormErrorBox").css('display', 'none');
		var str = $("#contactForm").serialize();
		//post the form values to db
		
		$.post("http://www.theamarulaexperiment.com/formSubmit/contact/", {
			'str': str
		}, function(message){
		
		}, "json");
		
	};
		});
	
	 });
	 
	 

