/*
Based on the "Sexy Captcha - A New Drag-and-Drop Approach" by Rock N' Roll Louis! at http://blog.lukeblackamore.com/

No licensing in code but posed on blog
========================================
Rock N' Roll Louis! said...
Warhole,

This is licensed under the GNU GPL license. Please feel free to modify and redistribute this as you like.

Thanks!

October 15, 2009 12:08 PM
*/
$('document').ready(function() {
  $('.myCaptcha').sCaptcha('/includes/classes/captcha/captcha.process.php');
  $("#mainform").validate({
	  rules: {
		  zipcode: {
			  required: true,
			  digits: true,
			  minlength: 4,
			  maxlength: 5
		  }
	  },
	 messages: {
	   zipcode: {
		 required: "<br />*****Zip Code Required*****",
		 minlength: jQuery.format("<br />*****{0} Numbers Minimum*****"),
		 digits: "<br />*****Enter Numbers Only*****"
	   }
	 }
  });
  $('#mainform').submit(function(event) {
	   	alert("You must complete Step 2 and Step 3 before submitting!   ");
		event.preventDefault();
		return false;
  });

}); 

function getdata(){
		$.ajax({
			type: "POST",
			url: "/includes/classes/roundrobin/roundrobin.process.php",
			data: 	"zipcode=" + document.getElementById("zipcode").value + "&token=" + document.getElementById("token").value + "&line=" + document.getElementById("line").value + "&state=" + document.getElementById("state").value+ "&logid=" + document.getElementById("logid").value,
			success: function(html){
				$("#myAgent").html(html);
			}
		});
}

function writelog(action) {
    $.ajax({
			type: "POST",
			url: "/includes/classes/roundrobin/log.process.php",
			data: 	"action=" + action + "&state=" + document.getElementById("state").value + "&line=" + document.getElementById("line").value + "&logid=" + document.getElementById("logid").value + "&userid=" + document.getElementById("userid").value + "&type=" + document.getElementById("type").value,
			success: function(data){
				alert("OK");
			}
		});
}

/*
 * Based on Sexy Captcha v.0.2
 * Designed and developed by: BWM Media (bwmmedia.com)
 * Modified by: Akarin Weatherford (akarin.com)
 */
(function($) {
	$.fn.sCaptcha = function(url) {
		this.each(function() {
			$(this).load(url, { action: 'refresh' }, function() {
				$('.draggable').draggable({ containment: 'parent', snap: '.target', snapMode: 'inner', snapTolerance: 35, revert: 'invalid', opacity: 0.75,
					drag: function(event, ui) {
						if(!$("#mainform").validate().form()) {
							$('#captchaWrapper').find('.target').droppable('disable');
						} else {
							$('#captchaWrapper').find('.draggable').draggable('enable');	
							$('#captchaWrapper').find('.target').droppable('enable');
						}
					}
				});
				$('.target').droppable({ accept: '.draggable', tolerance: 'intersect' });
				
				//On drop of draggable object
				$('.target').bind('drop', function(event, ui) {
					if($("#mainform").validate().form()) {
						$('#captchaWrapper').find('.target').droppable('enable');
						$('#captchaWrapper').find('.captchaAnswer').val($(ui.draggable).attr('id'));
						$('#captchaWrapper').find('.draggable').draggable('disable');
						$('#captchaWrapper').find('.draggable').unbind('click');
						$('#captchaWrapper').find('.targetWrapper').children('.target').hide();
		
						//Check captcha answer
						$.post(url, { action: 'verify', captcha: $(ui.draggable).attr('id') }, function(data) {
							if (data.status == "success" && $("#mainform").validate().form()) {
								$('#captchaWrapper').find('.targetWrapper').addClass('captchaSuccess').hide().fadeIn('slow');
								getdata();
							} else {
								$('#captchaWrapper').find('.targetWrapper').addClass('captchaFail').hide().fadeIn('slow');
								setTimeout (function(){$('#captchaWrapper').sCaptcha(url);}, 2000 );
							}
						}, 'json');
					} else {
						$('#captchaWrapper').sCaptcha(url);	
						return false;
					}
				});
				
				//On double-click of object
				$('.draggable').bind('click', function(event, ui) {
					if(!$("#mainform").validate().form()){
						$('#captchaWrapper').find('.target').droppable('disable');
					} else {
						$('#captchaWrapper').find('.draggable').draggable('enable');
						$('#captchaWrapper').find('.target').droppable('enable');
						
						$('#captchaWrapper').find('.captchaAnswer').val($(this).attr('id'));
						$('#captchaWrapper').find('.draggable').draggable('disable');
						$('#captchaWrapper').find('.draggable').unbind('click');
						$('#captchaWrapper').find('.targetWrapper').children('.target').hide();
						$(this).removeClass('draggable');
						$(this).addClass('target');
						$('#captchaWrapper').find('.targetWrapper').html($(this));
		
						//Check captcha answer
						$.post(url, { action: 'verify', captcha: $(this).attr('id') }, function(data) {
							if (data.status == "success" && $("#mainform").validate().form()) {
								$('#captchaWrapper').find('.targetWrapper').addClass('captchaSuccess').hide().fadeIn('slow');
								getdata();
							} else {
								$('#captchaWrapper').find('.targetWrapper').addClass('captchaFail').hide().fadeIn('slow');
								setTimeout (function(){$('#captchaWrapper').sCaptcha(url);}, 2000 );
							}
						}, 'json');
						
					}
				});
			});
		});

		return this;
	};
})(jQuery);

