debug = function (log_txt) {
    if (window.console != undefined) {
        console.log(log_txt);
    }
}


jQuery(document).ready(function($){
	if ( $('#scroller').length > 0 ) {
		$('.scroll').vTicker({
			speed: 600,
			pause: 5000,
			showItems: 1,
			mousePause: true,
			height: 20,
			direction: 'up'
	    });
	}
	
	// product sort ajax function
	if ( $('#product-sort-form').length > 0 ) {
		$('#product-sort-form #product-sort').click(function() {
			$('#ajax-load').show();
			$('#productList').load('/ajax/filter_products/'+Math.random()*99999, { app: $('#app-list').val(), region: $('#region-list').val() }, function(){ $('#ajax-load').hide(); });
		});
		if (jQuery.browser.msie && jQuery.browser.version <= 8) {
		    //alert('IE 8 and below');
		} else {
			$('#product-sort-form select').selectBox();
		}
		
	}
	
	
	var reg_beforeValidation = function() {};
    var reg_onSuccess = function() {
        regCheck();
    };
    var reg_onFailure = function() {};
    var reg_afterValidation = function() {}; 
    
  $('#reg_form_container > form').ketchup(
  	{ 
  		onFailure : reg_onFailure,
        beforeValidation : reg_beforeValidation,
        onSuccess : reg_onSuccess,
        afterValidation : reg_afterValidation,
        buttonId : $('#regButton'),
        useFormSubmit : false
  	}  	
  );
  
  var login_beforeValidation = function() {};
    var login_onSuccess = function() {
        loginCheck();
    };
    var login_onFailure = function() {};
    var login_afterValidation = function() {}; 
    
  $('#login_form_container > form').ketchup(
  	{ 
  		onFailure : login_onFailure,
        beforeValidation : login_beforeValidation,
        onSuccess : login_onSuccess,
        afterValidation : login_afterValidation,
        buttonId : $('#loginButton'),
        useFormSubmit : false
  	}  	
  );
  
  
  var comment_beforeValidation = function() {};
    var comment_onSuccess = function() {
        submitComment();
    };
    var comment_onFailure = function() {};
    var comment_afterValidation = function() {}; 
    
  $('#comment_form').ketchup(
  	{ 
  		onFailure : comment_onFailure,
        beforeValidation : comment_beforeValidation,
        onSuccess : comment_onSuccess,
        afterValidation : comment_afterValidation,
        buttonId : $('#commentButton'),
        useFormSubmit : false
  	}  	
  );
  
  
  // question submission form
  var q_beforeValidation = function() {};
    var q_onSuccess = function() {
        submitQuestionForm();
    };
    var q_onFailure = function() {};
    var q_afterValidation = function() {}; 
    
  $('#questionForm').ketchup(
  	{ 
  		onFailure : q_onFailure,
        beforeValidation : q_beforeValidation,
        onSuccess : q_onSuccess,
        afterValidation : q_afterValidation,
        buttonId : $('#qButton'),
        useFormSubmit : false
  	}  	
  );
  
  
  // topic submission form
  var t_beforeValidation = function() {};
    var t_onSuccess = function() {
        submitTopicForm();
    };
    var t_onFailure = function() {};
    var t_afterValidation = function() {}; 
    
  $('#topicForm').ketchup(
  	{ 
  		onFailure : t_onFailure,
        beforeValidation : t_beforeValidation,
        onSuccess : t_onSuccess,
        afterValidation : t_afterValidation,
        buttonId : $('#tButton'),
        useFormSubmit : false
  	}  	
  );
  
  // event reg submission form
  var e_beforeValidation = function() {};
    var e_onSuccess = function() {
        submitEventRegForm();
    };
    var e_onFailure = function() {};
    var e_afterValidation = function() {}; 
    
  $('#eventRegForm').ketchup(
  	{ 
  		onFailure : e_onFailure,
        beforeValidation : e_beforeValidation,
        onSuccess : e_onSuccess,
        afterValidation : e_afterValidation,
        buttonId : $('#eButton'),
        useFormSubmit : false
  	}  	
  );
  
  $('#keywords').bind('keypress', function(e) {
	        if(e.keyCode==13){
	              doSearch();
	        }
	});
	
	
  $('#searchButton').click(function() {
		doSearch();
	})
  
    
});


function doSearch() {
	
	if ( $.trim( $('#keywords').val() ).length < 3 ) {
		$('#searchError').show();
	} else {
		$('#search').submit();
	}
}

function submitQuestionForm() {
	// run spam detection
	$.post(
		'/ajax/spamcheck/'+Math.random()*99999,
		$('#questionForm').serialize(),
		function(data) {
			if (data.spam == false) {
				// safe, continue submission
				$.post(
					$('#questionForm').attr('action'), 
					$('#questionForm').serialize()
				);
				$('#questionForm').hide();
  				$('.response').show();
			}
		}, "json"
	);
}

function submitTopicForm() {
	// run spam detection
	$.post(
		'/ajax/spamcheck/'+Math.random()*99999,
		$('#topicForm').serialize(),
		function(data) {
			if (data.spam == false) {
				// safe, continue submission
				$.post(
					$('#topicForm').attr('action'), 
					$('#topicForm').serialize()
				);
				$('#topicForm').hide();
  				$('.response').show();
			}
		}, "json"
	);
}

function submitEventRegForm() {
	
	$.post(
		$('#eventRegForm').attr('action'), 
		$('#eventRegForm').serialize()
		//function(data){ 
			//alert(data);
			
		//}
  );
  $('#eventRegForm').hide();
  $('.response').show();
}


function loadEvents(day) {
	$('#currentEvent').load( '/ajax/dynamic_cal_event/'+day+'/'+Math.random()*99999 );
}


function paginateList(obj, locationPath, offset, type) {
	$('#commentEntries').load(locationPath, {current_offset:offset} );
}
	
function clearText(field){
    if (field.defaultValue == field.value) field.value = '';
    else if (field.value == '') field.value = field.defaultValue;
}

function timedRefresh(timeoutPeriod) {
	setTimeout("location.reload(true);",timeoutPeriod);
}

function submitComment() {
	if ( $('#commentConsole').css('display', 'block') ) {
		$('#commentConsole').hide();
		$('#commentConsole').empty();	
	}
	
	$.post(
		$('#comment_form').attr('action'), 
		$('#comment_form').serialize(),
		function(data){ 
			$('#commentConsole').html(data);
			//alert(data);
			var eCode = $('#commentConsole').find('h2').text();
			if (eCode == 'Error') {
				$('#commentConsole').show();
			} else {
				$('#commentConsole').empty();
				$('#commentConsole').html('<p>Your comment has been submitted to a moderator for approval.</p>');
				$('#commentConsole').show();
				timedRefresh(1500);
			}
		}
  );
}

function slidedown(container) {
	$('#'+container).slideDown("slow");
}


function loginCheck() {
	if ( $('#loginConsole').css('display', 'block') ) {
		$('#loginConsole').hide();
		$('#loginConsole').empty();	
	}
	
	$.post(
		$('#login_form').attr('action'), 
		$('#login_form').serialize(),
		function(data){ 
			$('#loginConsole').html(data);
			//alert(data);
			var eCode = $('#loginConsole').find('h2').text();
			if (eCode == 'Error') {
				$('#loginConsole').show();
			} else {
				window.location.reload();
			}
		}
  );
}


function regCheck() {
	if ( $('#regConsole').css('display', 'block') ) {
		$('#regConsole').hide();
		$('#regConsole').empty();	
	}
	
	$.post(
		$('#reg_form').attr('action'), 
		$('#reg_form').serialize(),
		function(data){ 
			$('#regConsole').html(data);
			//alert(data);
			var eCode = $('#regConsole').find('h2').text();
			if (eCode == 'Error') {
				$('#regConsole').show();
			} else {
				$.post(
					'http://www.sunbearsoftware.com/Kemin/app/map.aspx?mRequest=Process.WebSurvey&ProcessId=1700',
					{ PageURL: $('#PageURL').val(), LandingPageId: $('#LandingPageId').val(), FirstName: $('#FirstNameReg').val(), LastName: $('#LastNameReg').val(), Email: $('#EmailReg').val(), SalesRegion: $('#SalesRegionReg').val() },
					function() {  }
				);
				window.location.reload();
			}
		}
  );
}


function disableEnterKey(e)
{
     var key;     
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox     

     return (key != 13);
}
