function recipeAdd(session){
	var posts = "session="+session+"&";
	var x = document.getElementById("response");
	x.style.display="block";
	x.innerHTML = "Please select an ingredient to add.";
	var C = 0;
	for(i=0; i<document.ingredient.elements.length; i++){
		if( document.ingredient.elements[i].checked == true ){
			var n = document.ingredient.elements[i].name;
			var v = document.ingredient.elements[i].value;
			
			posts += "ingredient" + C + "=" + v + "&";
			
			x.innerHTML = "These items were successfully added to your shopping list.";
			
			C++;
		}
	}
	
	posts = removeTrailing(posts);
	//alert(posts);
	
	if( C > 0 ){
		/* call to ajax page here */
		//alert(posts);
		$.ajax({
			type: "POST",
			url: "addRecipe.php",
			data: posts,
			success: function(msg){
				//alert(msg.trim());
				var msg = msg.trim();
				var chk = msg.search('success');
				//alert(chk);
				if(chk >= 0){
					//successful transaction
				}
			}
		});
	}
	
}

function recipeLoad(id){
	var x = document.getElementById("response");
	x.style.display="none";
	
	var load = $.ajax({
				type: "POST",
				url: "recipe.php",
				data: {id: id},
				success: function(html){
					//alert(html);
					$('#recipeLoad').html(html);
				}
			});	
		
	//var setFont = $("#recipe"+id).css("font-weight", "bold");
	
}

function checkAllRecipe(){
	var x = document.getElementById("response");
	var y = document.getElementById("selectDeselect");
	for(i=0; i<document.ingredient.elements.length; i++){
		document.ingredient.elements[i].checked = true;
	}
	x.style.display="none";
	
	y.innerHTML = '<span class="lowlight" style="cursor: pointer;" onclick="uncheckAllRecipe()">Deselect all ingredients</span>';
}

function uncheckAllRecipe(){
	var x = document.getElementById("response");
	var y = document.getElementById("selectDeselect");
	for(i=0; i<document.ingredient.elements.length; i++){
		document.ingredient.elements[i].checked = false;
	}
	x.style.display="none";
	
	y.innerHTML = '<span class="lowlight" style="cursor: pointer;" onclick="checkAllRecipe()">Select all ingredients</span>';
}

function removeTrailing(fld) {
	var ln = fld.length;
	var end = ln - 1;
	return fld.substr(0,end);
}

function printList(session){
	var x = document.getElementById("response");
	x.style.display="none";
	var browser = '0';
	window.open ("shopping_list.php","mywindow","menubar=1,resizable=1,scrollbars=1,width=505,height=550");
		jQuery.each(jQuery.browser, function(i) {
			if($.browser.msie){
				browser = 'msie';
			}
		});
		
	if(browser == 'msie'){
		//child.location.reload(true);
	}
}

function clearList(){
	var clear = $.ajax({
				type: "POST",
				url: "clearList.php",
				data: "list=clear",
				success: function(msg){
					//alert(msg);
					window.close('mywindow');
				}
			});
}

function alertRewards(){
	var name = $("#name").val();
	var email = $("#email").val();
	var Address1 = $("#Address1").val();
	var City = $("#City").val();
	var state = $("#state").val();
	var subject = $("#subject").val();
	var message = $("#message").val();
	var age = $('#18_years_old').is(':checked');
	var rules = $('#Official_rules').is(':checked');

	var pass = 'Y';
	
	if( !checkmail(email) ) {
		pass = 'N';
	}
	
	if( name == ''){
		pass = 'N';
	}
	
	if( Address1 == ''){
		pass = 'N';
	}
	
	if( City == ''){
		pass = 'N';
	}
	
	if( state == ''){
		pass = 'N';
	}
	
	if( subject == ''){
		pass = 'N';
	}
	
	if( message == ''){
		pass = 'N';
	}
	
	if( age == false){
		pass = 'N';
	}
	
	if( rules == false){
		pass = 'N';
	}

	if(pass == 'Y'){
		alert('Thank You');
		//awardsForm();
		return true;
	}else{
		if( age == false ){
			alert('You must be 18 years old to participate');
		}else if (rules == false){
			alert('You must agree to the rules in order to participate');
		}else{
			alert('Please fill out required fields');
		}
		return false;
		//$("#aiContactSafe_info").css("color", "red");
	}

}

function awardsForm(){
	var posts = '';
	var name = $("#name").val();
	var email = $("#email").val();
	var phone = $("#phone").val();
	var Address1 = $("#Address1").val();
	var Address2 = $("#Address2").val();
	var City = $("#City").val();
	var state = $("#state").val();
	var zip = $("#subject").val();
	var publixZip = $("#message").val();
	var optin = $("#offers").val();
	
	posts += "optin="+optin;
	posts += "&name="+name;
	posts += "&email="+email;
	posts += "&Address1="+Address1;
	posts += "&Address2="+Address2;
	posts += "&City="+City;
	posts += "&state="+state;
	posts += "&zip="+zip;
	posts += "&publixZip="+publixZip;
	posts += "&phone="+phone;
	alert(posts);
	if(name != ''){
	/*
		var insert = $.ajax({
				type: "POST",
				url: "custom/submitCustomAwd.php",
				data: posts,
				success: function(txt){
					alert("AJAX MESSAGE:\n"+txt);
				}
			});
	*/
	}
}

/*------------------------------------------------------------------------------------------------
	CHECKS TO SEE IF A STRING IS FORMATTED AS A VALID EMAIL ADDRESS
-------------------------------------------------------------------------------------------------*/
var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

function checkmail(e){
	var returnval=emailfilter.test(e);
	return returnval;
}

function rewardsLink(){
	//window.location = 'rewards/default/display/';
	//window.location = '/rewards-coming-soon';
	//window.location = '/meal-solutions';
	window.location = '/rewards.php';
}
