// Platform Utilities

var mouseinmenu = false;
setTimeout("carousel();", 10000);

$(function(){
	// Menu drops after a few seconds
	$('.bmenu').delay(10000).animate({ "margin-top": "-290px"}, 300 );
	$('#btmenu').hover(function() {
		if($('.bmenu').css('margin-top') == "-350px"){
			$('.bmenu').stop(true, true).animate({ "margin-top": "-290px"}, 300 );
		}
	});
	// Menu hover over buttons
	$('.hover').mouseenter(function(){
		var str = $(this).attr('src');
		$(this).attr('src',str.substr(0,str.length-5)+'2'+str.substr(str.length-4,4));
	}).mouseleave(function(){
		var str = $(this).attr('src');
		$(this).attr('src',str.substr(0,str.length-5)+'1'+str.substr(str.length-4,4));
	});
	
	// bmenu dropdown
	$('#btmenu').click(function(){
		if($('.bmenu').css('margin-top') == "0px"){
			px = '-290px';
			if($('.bmenu-inner2').is(':visible')) SwitchMenuView();
		}else{
			px = '0px';
		}
		$('.bmenu').stop(true, true).animate({
			"margin-top": px
		}, 1000 );
	});
	$('.bmenu').hover(function(){ 
        mouseinmenu = true; 
    }, function(){ 
        mouseinmenu = false; 
    });
	$('#btmenu').hover(function(){ 
        mouseinmenu = true; 
    }, function(){ 
        mouseinmenu = false; 
    });

	$('body').click(function() {
		if($('.bmenu').css('margin-top') == "0px" && !mouseinmenu){
			$('.bmenu').css('margin-top', "-290px");
			if($('.bmenu-inner2').is(':visible')) SwitchMenuView();
		}
	});

	$('#dropdown').mouseleave(function(){
		$("#sign_ext").stop(true, true).slideUp("slow");
	});
	
	$('.bmenu #tab').click(function(){
		SwitchMenuView();
	});
	
	$('#submenu1-bt_left, #submenu1-bt_right').click(function() {
		$('.b-submenu1-mask1').toggle();
	});
	$('#bmenu-slider_left, #bmenu-slider_right').click(function() {
		$('.pandsmenu').toggle();
	});
	$('#fb_bt_send').click(function() {
		$.ajax({
			url: DATA.SITE+'assets/scripts/feedback.php?name='+encodeURIComponent($('#fb_name').val())+'&note='+encodeURIComponent($('#fb_note').val())+'&type='+encodeURIComponent($('#fb_type').val())+'&site='+encodeURIComponent($('#fb_site').val())
		});
		alert('Thank you for your feedback');
		$('#feedback-box').fadeOut();
		$('#fb_name, #fb_note').val('')
	});
	$('#fb_bt_cancel').click(function() {
		$('#feedback-box').fadeOut();
		$('#fb_name, #fb_note').val('');
	});
			
	
	function SwitchMenuView()
	{
		$('.b-inner').toggle();
		if($('.bmenu-inner1').is(':visible'))
		{
			$('#tab div').html('See Product &amp; Service Menu').removeClass('tabmenu');
		}
		else
		{
			$('#tab div').html('Main Menu').addClass('tabmenu');
		}
	}
});

function carousel()
{
	if ((ap+1) >= po)
	{
		bf = ap;
		ap = 0;
	}
	else
	{
		bf = ap;
		ap++;
	}
	di = 1;
	nextpic();
	setTimeout("carousel();", 8000);
}

function checkCard(ccnum,obj,res)
{
	$(obj).html('<img src="https://secure.balanceofnature.com/lp1/graphics/loader.gif" style="margin-bottom:-5px; margin-left:-20px;" width="16" height="16" alt="loading..." />');
	$.ajax({
		url: DATA.SITE_HTTPS+'assets/scripts/checkcc.php?ccnum='+ccnum,
		success: function(json)
		{
			var jsona = eval("(" + json + ")");
			if (jsona['result']=='true')
			{
				$(obj).html('<img src="https://secure.balanceofnature.com/lp1/graphics/check.gif" style="margin-bottom:-5px; margin-left:-20px;" width="19" height="19" alt="Accepted" />');
				$('.fadeout').show();
				$('#cc'+jsona['card']).hide();
				$('#card_type').val(jsona['card']);
			}
			else
			{
				$(obj).html('<img id="erroricon" style="margin-bottom:-5px; margin-left:-20px;"\n'+
								 'src="https://secure.balanceofnature.com/lp1/graphics/x.gif" width="19" height="19" \n'+
								 'alt="Error" title="'+jsona['error']+'" />');
			}
		},
		error: function()
		{
			$(obj).html('');
		}
	});
}
function ZipLookUp(zip,obj,city,state,citystate,sec)
{
	$(obj).html('<img src="https://secure.balanceofnature.com/lp1/graphics/loader.gif" style="margin-bottom:-5px; margin-left:-20px;" width="16" height="16" alt="loading..." />');
	$.ajax({
		url: DATA.SITE_HTTPS+'assets/scripts/checkzip.php?zip='+zip,
		success: function(json_string)
		{
			var jsonarray = eval("(" + json_string + ")");
			if (jsonarray['result']=='true')
			{
				$(city).val(jsonarray['city']);
				$(state).val(jsonarray['state']);
				$(citystate).html(jsonarray['city']+', '+jsonarray['state']);
				$(obj).html('<img src="https://secure.balanceofnature.com/lp1/graphics/check.gif" style="margin-bottom:-5px; margin-left:-20px;" width="19" height="19" alt="Accepted" title="'+jsonarray['city']+', '+jsonarray['state']+'" />');
			}
			else
			{
				if(typeof(sec) === 'undefined') {
					ZipLookUp(zip,obj,city,state,citystate,true);
				} else {
					$(obj).html('<img id="erroricon" style="margin-bottom:-5px; margin-left:-20px;"\n'+
								 'src="https://secure.balanceofnature.com/lp1/graphics/x.gif" width="19" height="19" \n'+
								 'alt="Error" title="Not Found" />');
				}
			}
		},
		error: function()
		{
			$(obj).html('');
		}
	});
}
function ZipLookUp2(zip,obj,city,state,citystate,sec)
{
	$(obj).html('<img src="https://secure.balanceofnature.com/lp1/graphics/loader.gif" style="margin-bottom:-5px; margin-left:-20px;" width="16" height="16" alt="loading..." />');
	$.ajax({
		url: DATA.SITE+'assets/scripts/checkzip.php?zip='+zip,
		success: function(json_string)
		{
			var jsonarray = eval("(" + json_string + ")");
			if (jsonarray['result']=='true')
			{
				$(city).val(jsonarray['city']);
				$(state).val(jsonarray['state']);
				$(citystate).html(jsonarray['city']+', '+jsonarray['state']);
				$(obj).html('<img src="https://secure.balanceofnature.com/lp1/graphics/check.gif" style="margin-bottom:-5px; margin-left:-20px;" width="19" height="19" alt="Accepted" title="'+jsonarray['city']+', '+jsonarray['state']+'" />');
			}
			else
			{
				if(typeof(sec) === 'undefined') {
					ZipLookUp(zip,obj,city,state,citystate,true);
				} else {
					$(obj).html('<img id="erroricon" style="margin-bottom:-5px; margin-left:-20px;"\n'+
								 'src="https://secure.balanceofnature.com/lp1/graphics/x.gif" width="19" height="19" \n'+
								 'alt="Error" title="Not Found" />');
				}
			}
		},
		error: function()
		{
			$(obj).html('');
		}
	});
}

function PromocodeLookup(promo,obj,check,sec)
{
	$(obj).html('<img src="https://secure.balanceofnature.com/lp1/graphics/loader.gif" style="margin-bottom:-5px; margin-right:-20px;" width="16" height="16" alt="loading..." />');
	$(check).val(0);
	$.ajax({
		url: DATA.SITE+'assets/scripts/checkpromo.php?promo='+promo,
		success: function(json_string) {
			var jsonarray = eval("(" + json_string + ")");
			if (jsonarray['result']=='true') {
				$(check).val(1);
				$(obj).html('<img src="https://secure.balanceofnature.com/lp1/graphics/check.gif" style="margin-bottom:-5px; margin-right:-20px;" width="19" height="19" alt="Accepted" title="Promo Code confirmed" />');
			}
			else
			{
				if(typeof(sec) === 'undefined') {
					PromocodeLookup(promo,obj,check,true);
				} else {
					$(check).val(0);
					$(obj).html('<img id="erroricon" style="margin-bottom:-5px; margin-right:-20px;"\n'+
								 'src="https://secure.balanceofnature.com/lp1/graphics/x.gif" width="19" height="19" \n'+
								 'alt="Error" title="Not Found" />');
				}
			}
		},
		error: function()
		{
			$(check).val(0);
			$(obj).html('');
		}
	});
}

function checkcc(cc,cccheck)
{
	if ($(cccheck).val() == 0) {
		$(cc).validationEngine('showPrompt', '<img src="https://secure.balanceofnature.com/lp1/graphics/loader.gif" width="16" height="16" /> checking credit card...', 'load', 'topRight', true);
		$.ajax({
			url: DATA.SITE_HTTPS+'assets/scripts/checkcc2.php?cc='+$(cc).val(),
			success: function(json) {
				var jsona = eval("(" + json + ")");
				if (jsona['result']=='true') {
					$(cccheck).val(1);
					$('.fadeout').show();
					$('#cc'+jsona['card']).hide();
					$('#card_type').val(jsona['card']);
					$(cc).validationEngine('showPrompt', jsona['msg'], 'pass', 'topRight', true);
					setTimeout("$('"+cc+"').validationEngine('hide');", 600);
				} else {
					$(cccheck).val(0);
					$(cc).validationEngine('showPrompt', jsona['msg'], 'error', 'topRight', true);
					return false;
				}
			},
			error: function() {
				alert("An Error Occured Please call 1-800-246-8751");
				$(cccheck).val(0);
				return false;
			}
		});
	}
}
function checkZip(zip,country,zipcheck,city,state,citystate,sec) {
	if ($(zipcheck).val() == 0) {
		$(zip).validationEngine('showPrompt', '<img src="https://secure.balanceofnature.com/lp1/graphics/loader.gif" width="16" height="16" /> checking zip code...', 'load', 'topRight', true);
		$.ajax({
			url: DATA.SITE_HTTPS+'assets/scripts/checkzip2.php?zip='+$(zip).val()+'&country='+$(country).val(),
			success: function(json) {
				var jsona = eval("(" + json + ")");
				if (jsona['result']=='true') {
					$(zipcheck).val(1);
					$(state).val(jsona['state']);
					$(citystate).html(jsona['city']+', '+jsona['state']);
					if(jsona['code'] != '2') {
						$(zip).validationEngine('showPrompt', jsona['msg'], 'pass', 'topRight', true);
						setTimeout("$('"+zip+"').validationEngine('hide');", 600);
						$(city).val(jsona['city']).removeClass('inactive');
					} else {
						$(zip).validationEngine('hide')
						$(citystate).html('Enter Zip for City and State');
						if($(city).val() == "" || $(city).val() == $(city).attr('ref')) {
							$(city).val(jsona['city']).addClass('inactive');
						}
					}
					return true;
				} else {
					if(typeof(sec) === 'undefined' && jsona['code'] == '1') {
						checkZip(zip,country,zipcheck,city,state,citystate,true);
					} else {
						$(zipcheck).val(0);
						$(citystate).html('Enter Zip for City and State');
						$(zip).validationEngine('showPrompt', jsona['msg'], 'error', 'topRight', true);
						return false;
					}
				}
			},
			error: function() {
				alert("An Error Occured Please call 1-800-246-8751");
				$(zipcheck).val(0);
				$(citystate).html('Enter Zip for City and State');
				return false;
			}
		});
	} else {
		return true;
	}
}

function checkEmail(){
	
	if ($('#emailcheck').val() == 0) {
		$('#email').validationEngine('showPrompt', '<img src="https://secure.balanceofnature.com/lp1/graphics/loader.gif" width="16" height="16" /> checking email...', 'load', 'topRight', true);
		$.ajax({
			url: DATA.SITE_HTTPS+'assets/scripts/checkemail2.php?email='+$('#email').val(),
			success: function(json) {
				var jsona = eval("(" + json + ")");
				if (jsona['result']=='true') {
					$('#emailcheck').val(1);
					$('#email').validationEngine('showPrompt', 'OK', 'pass', 'topRight', true);
					setTimeout("$('#email').validationEngine('hide');", 500);
					return true;
				} else {
					$('#emailcheck').val(0);
					$('#email').validationEngine('showPrompt', jsona['msg'], 'error', 'topRight', true);
					return false;
				}
			},
			error: function() {
				alert("An Error Occured Please call 1-800-246-8751");
				$('#emailcheck').val(0);
				return false;
			}
		});
	} else {
		return true;
	}
}
function autoscroll(timeout) {
	$('.infiniteCarousel').trigger('gotonextpage');
	setTimeout("autoscroll("+timeout+");", timeout); 
}
function addProduct() {
	if (!$("input[name='prod']").is(':checked')) {
		alert('You must select an Option and click Add To Shopping Bag.');
	} else {
		if ($('input#preferred').is(':checked')) {
			qty	= 1;
			pc	= "&pc=1";
		} else { 
			qty = $('#retail_qty').val();
			pc	= "&pc=0";
		}
			
		window.location = DATA.SITE+"shopbag/?c=add&p="+$("input[name='prod']:checked").val()+"&q="+qty+pc;
	}
}
function daysInMonth(month,year) {  // months are 1-12
	var dd = new Date(year, month, 0);
	return dd.getDate();
}
function checkAge(field, rules, i, options) { 
	var today = new Date(); 
	var d = field.val();
	if (!/\d{2}\/\d{2}\/\d{4}/.test(d)) {   // check valid format
		return "* Invalid date";
	}
	
	d = d.split("/");
	var byr = parseInt(d[2]);
	var nowyear = today.getFullYear();
	if (byr >= nowyear || byr < 1900) {  // check valid year
		return "* Invalid year";
	}
	var bmth = parseInt(d[0],10)-1;   // radix 10!
	if (bmth <0 || bmth >11) {  // check valid month 0-11
		return "* Invalid month";
	}
	var bdy = parseInt(d[1],10);   // radix 10!
	var dim = daysInMonth(bmth+1,byr);
	if (bdy <1 || bdy > dim) {  // check valid date according to month
		return "* Invalid day";
	}
	var age = nowyear - byr;
	var nowmonth = today.getMonth();
	var nowday = today.getDate();
	if (bmth > nowmonth) {age = age - 1}  // next birthday not yet reached
	else if (bmth == nowmonth && nowday < bdy) {age = age - 1}
	if (age < 18) {
		alert("Sorry, You do not qualify for this special offer.\nYou must be 18 years or older! Please call 1-800-246-8751 for further assistance.");
		return "* Unqualified";  
	}
}
function enterdate(date,event) {
	if (!$(date).hasClass('inactive')) {
		if ($(date).val().length == 2) 
			$(date).val($(date).val()+'/');
		if ($(date).val().length == 5) 
			$(date).val($(date).val()+'/');
	}
}
