/*
 *	TypeWatch 2.0 - Original by Denny Ferrassoli / Refactored by Charles Christolini
 *
 *	Examples/Docs: www.dennydotnet.com
 *
 *  Copyright(c) 2007 Denny Ferrassoli - DennyDotNet.com
 *  Coprright(c) 2008 Charles Christolini - BinaryPie.com
 *
 *  Dual licensed under the MIT and GPL licenses:
 *  http://www.opensource.org/licenses/mit-license.php
 *  http://www.gnu.org/licenses/gpl.html
*/

(function(jQuery) {
	jQuery.fn.typeWatch = function(o){
		// Options
		var options = jQuery.extend({
			wait : 750,
			callback : function() { },
			highlight : true,
			captureLength : 2
		}, o);

		function checkElement(timer, override) {
			var elTxt = jQuery(timer.el).val();

			// Fire if text > options.captureLength AND text != saved txt OR if override AND text > options.captureLength
			if ((elTxt.length > options.captureLength && elTxt.toUpperCase() != timer.text)
			|| (override && elTxt.length > options.captureLength)) {
				timer.text = elTxt.toUpperCase();
				timer.cb(elTxt);
			}
		};

		function watchElement(elem) {
			// Must be text or textarea
			if (elem.type.toUpperCase() == "TEXT" || elem.nodeName.toUpperCase() == "TEXTAREA") {

				// Allocate timer element
				var timer = {
					timer : null,
					text : jQuery(elem).val().toUpperCase(),
					cb : options.callback,
					el : elem,
					wait : options.wait
				};

				// Set focus action (highlight)
				if (options.highlight) {
					jQuery(elem).focus(
						function() {
							this.select();
						});
				}

				// Key watcher / clear and reset the timer
				var startWatch = function(evt) {
					var timerWait = timer.wait;
					var overrideBool = false;

					if (evt.keyCode == 13 && this.type.toUpperCase() == "TEXT") {
						timerWait = 1;
						overrideBool = true;
					}

					var timerCallbackFx = function()
					{
						checkElement(timer, overrideBool)
					}

					// Clear timer
					clearTimeout(timer.timer);
					timer.timer = setTimeout(timerCallbackFx, timerWait);

				};

				jQuery(elem).keydown(startWatch);
			}
		};

		// Watch Each Element
		return this.each(function(index){
			watchElement(this);
		});

	};

})(jQuery);




function captcha(qField, ansField, first, second){



    var num1 = Math.floor(Math.random() * (first - 1 + 1)) + 1;
    var num2 = Math.floor(Math.random() * (second - 1 + 1)) + 1;
    var sum = (num1 + num2);
        $("#"+qField).html('<img src="http://stockners.com/includes/functions/captcha/functions/captcha.php?num1='+num1+'&num2='+num2+'" style="border: 2px solid #000" />'
        +'<input type="text" id="'+ansField+'" style="display:inline; height: 46px; line-height: 46px; font-size: 40px; width: 55px; top: -11px; *top: -4px;  position: relative; border: 2px solid #000; border-left: none; text-align: center; color: #FF0000; background: #fff"  />');
        $("#"+qField).css("display", "inline");

function checkAns(ans){
    if (ans == sum){
        //shake yes
        $("#"+ansField).css("color", "#00FF00");
        $("#"+ansField).animate({top:'-=20'},100);
        $("#"+ansField).animate({top:'+=40'},100);
        $("#"+ansField).animate({top:'-=40'},100);
        $("#"+ansField).animate({top:'+=40'},100);
        $("#"+ansField).animate({top:'-=40'},100);
        $("#"+ansField).animate({top:'+=20'},100);


    } else {
        $("#"+ansField).css("color", "#FF0000");
    }
}

   $("#"+ansField).keyup(function() {
        var ans = $(this).val();
        $(this).typeWatch({
            callback: checkAns(ans),
            wait: 1000,
            highlight:true,
            captureLength: 2
        })
    });





    return sum;
}
