jQuery.fn.ajaxIDCheck = function(options)//***** AJAX TESTING FUNCTION
{//takes rinput, queries it the queryURL, prints status in statusLabel
	var defaults = {
		idInput:null,
		qURL:null,
		msgLabel:null,
		doButton:null,//jQuery object!
		mode:"blur", //"blur" or "click" - AJAX call on input blur or AJAX call on button click.
		customMsgCSS:false, //will use the custom CSS for messages so disable the jQuery based styling.
		msgSend:"Checking user id ..",
		msgSuccess:"Entered a valid user ID.", 
		msgFail:"User ID NOT CORRECT!",
		successFadeMsg:true, //disappear the success message after shown
		callback:null
	};
	/* SIMPLE VARIABLE for messages:
		you can put '%%ID%%' in the messages and it will be replaced everytime AJAX search is called, by the value
		in the input field.
		Also, if there is a <a rel="callback"></a> in the messages, they will be used to do callback, when clicked, to be used for like submitting a form.
	*/
	var options = $.extend(defaults,options);
	function ruserIDCheck(){
		this.userID = "";
		this.rinput = "";
		this.queryURL = "";
		this.statusLabel = "";
		this.isUser = 0;
		this.rawAJAXdata = null;
		this.customMsgCSS = false;
		this.msgSend = "Checking user id ..";
		this.msgSuccess="Entered a valid user ID.";
		this.msgFail = "User ID NOT CORRECT!";
		this.successFadeMsg = true;
		this.callback = null;
		var o = this;//did this to call and use 'o.isUser'. this will be updated as AJAX request is finished, which happens asynchronously but will be updated as o.isUser is calling by REFERENCE! ^^
		this.set = function(rinput,queryURL, statusLabel, customMsgCSS, msgSend, msgSuccess, msgFail, successFadeMsg, callback)
		{//sets the object variables.
			o.userID = rinput.attr('value');
			o.rinput = rinput;
			o.queryURL = queryURL;
			o.statusLabel = statusLabel;
			o.customMsgCSS = customMsgCSS || o.customMsgCSS;
			o.msgSend = msgSend || o.msgSend;
			o.msgSuccess = msgSuccess || o.msgSuccess;
			o.msgFail = msgFail || o.msgFail;
			o.successFadeMsg = (successFadeMsg == null) ? o.successFadeMsg : successFadeMsg;
			o.callback = callback || o.callback;
			//o.isUser = 0;
		};
		this.run = function()//just runs the AJAX request again.
		{
			o.userID = o.rinput.attr('value');
			o.isUser = 0;
			var parsemsgSend = "";
			var parsemsgSuccess = "";
			var parsemsgFail = "";
			parsemsgSend = o.msgSend.replace('%%ID%%',o.userID);//the messages get userID inserted here~	
			parsemsgSuccess = o.msgSuccess.replace('%%ID%%',o.userID);	
			parsemsgFail = o.msgFail.replace('%%ID%%',o.userID);
			//when input gets blurred,
			//1. show the message "Checking user id ..."
			if (o.userID != undefined)
			{
				var query = o.queryURL+o.userID;
				o.rinput.bind("ajaxSend", function(){
				//2. do an AJAX call the to the server for user check. - if only it's a new user.
					//o.statusLabel.text("Checking user id ..");})
				o.statusLabel.html(parsemsgSend).find('a[@rel=callback]').click(function(e){e.preventDefault();o.callback();});
				})
				.bind("ajaxSuccess",function(){
					if (o.isUser == 1)
				//3. if correct user, say correct
					{
						//o.statusLabel.text("Entered a valid user ID.").css('font-weight',"normal").css('font-size',"1em");
						o.statusLabel.html(parsemsgSuccess).find('a[@rel=callback]').click(function(e){e.preventDefault();o.callback();});
						if(!o.customMsgCSS){o.statusLabel.css('font-weight',"normal").css('font-size',"1em");}
						else{o.statusLabel.removeClass('fail').removeClass('success').addClass('success');}
						if (o.successFadeMsg){setTimeout(function(){o.statusLabel.text("");},700);}
						o.rinput.attr('value',o.userID).css('color',"black");
					}
					else if (o.isUser == 0)
				//4. if incorrect user, say "Incorrect user ID. Try again."
					{	//o.statusLabel.css('font-weight',"bold").text("User ID NOT CORRECT!");
						o.statusLabel.html(parsemsgFail).find('a[@rel=callback]').click(function(e){e.preventDefault();o.callback();});
						if(!o.customMsgCSS){o.statusLabel.css('font-weight',"bold");}
						else{o.statusLabel.removeClass('fail').removeClass('success').addClass('fail');}
						o.rinput.attr('value',o.userID).css('color',"red").focus();
					}
				})
				.bind("ajaxError",function(){
					o.statusLabel.text("AJAX ERROR! dump: "+o.rawAJAXdata);
				});
				$.getJSON(query, function(data){o.isUser = data.IsUser; o.rawAJAXdata = data;});
			}
		};
	}
	/* AJAX CALLING HERE *****/
					//AJAX userID validation check - checks and only when this passes you can go to next page.
					//variables for validation:
	var rCheck = new ruserIDCheck();
	
	options.msgLabel.text("");
	rCheck.set(options.idInput, options.qURL, options.msgLabel,options.customMsgCSS, options.msgSend, options.msgSuccess, options.msgFail, options.successFadeMsg, options.callback);//AJAX function that handles user checking and related visual effects. returns if user or not user.
	//idInput.unbind('blur');
	if (options.mode =="blur")
	{
		options.idInput.blur(function(){
			//when input gets blurred,
			//1. show the message "Checking user id ..."
			if (rCheck.userID != options.idInput.attr('value') && options.idInput.attr('value') != undefined)
			{
				rCheck.run();//AJAX check function runs..
			}
		});
	}
	else if (options.mode == "click" && options.doButton.length > 0)
	{//options.doButton is the jQuery object of the button the user clicks to perform ID check.
		options.doButton.click(function(e){
			e.preventDefault();
			rCheck.run();//AJAX check funtion runs..
		});
		
	}
	return rCheck;
	//end AJAX checking.
}
