﻿/*
*	Class: Login
*	Use: LoginjQuery way
*	Author: Mithilesh Kumar (http://dikshaonline.com)
*	Version: 1.0
*/

(function($) {
	$.Login = {version: '1.1'};
	$.fn.Login = function(config){
		
		config = $.extend({}, {
			imageLoader: '',
			buttonLogin: '#pxLogin',
			buttonClear: '#pxClear',
			funs:'1',
			//successOutput: 'Login Sucessfull',
			errorOutput: 'Your login attempt was not successful. Please try again.',
			userName: 'username',
			password: 'password',
			userText: 'User Name',
			passwordText: 'Password',
			inputSize: 30,
			callback: function() {
				
			}
		}, config);

		var itr = 0; 

$(config.buttonLogin).click(function(){
				$("#pxlogin_form form").each(function(){
					e = $(this);
					var id = "#" + $(e).attr("id");
					$(id).submit();

})
});







	$(config.buttonClear).click(function(){
			$("#px_display").fadeOut("slow",function(){
				$("#px_display").html("");
				$("#pxlogin_form").html("");
				itr = 0;
		//		$limit = 1;
				px.appendForm();
				//$('#px_button input').attr("disabled","disabled");
				$(this).show();
			});
		});
	

		var px = {
			init: function(e){
				var form = $(e).parents('form');
				px.formAction = $(form).attr('action');
				$(form).before(' \
					<div id="pxlogin_form"></div> \
					<div id="px_display"></div> \
					<div id="px_button"></div> \
				');
				$(config.buttonLogin+','+config.buttonClear).appendTo('#px_button');
		//		if ( $(e).attr('name') != '' ){
		//			config.inputName = $(e).attr('name');
		//		}
				if ( $(e).attr('size') != '' ){
					config.inputSize = $(e).attr('size');
				}
							

				$(form).hide();
				this.appendForm();
			},
			appendForm: function(){
			itr++;
				var formId = "pxlogin" 
				var iframeId = "pxlogin_frame";
				var inputId = "pxlogin_input";
				var contents = '<form method="post" id="'+ formId +'" action="'+ px.formAction +'"  target="'+ iframeId +'">' +
				'<input type="hidden" name="fun" value="'+ config.funs +'" id="fun" class="pxuser"  />' +
			config.userText+'<br /><input type="text" name="'+ config.userName +'" id="'+ config.userName +'" class="inputtext" onfocus="this.select()" onclick="this.select()" size="'+ config.inputSize +'px" />' +
				'<br />'+config.passwordText+'<br /><input type="password" name="'+ config.password +'" id="'+ config.password +'" class="inputtext" onfocus="this.select()" onclick="this.select()" size="'+ config.inputSize +'px" onkeypress="if(enter_key_press(event)) pxLogin.click()" />' +
				'</form><br />' + 
				'<iframe id="'+ iframeId +'" name="'+ iframeId +'" src="about:blank" style="display:none"></iframe>';
				//alert(contents);
				
				$("#pxlogin_form").append( contents );

		$("#"+iframeId ).load(function(){
		//alert($(this).contents().find("#output").text());
		var msg=$(this).contents().find("#output").text().split("|");
		if (msg[0]==-1)
		alert('Error : '+msg[1]+'\n'+config.errorOutput);
		else if(msg[0]==1)
		window.location.href=msg[1];  
		else if(msg[0]==-2)
		alert('Error : Server Link Down.\nPlease try after sometime.');
		
		// $(e).remove();
        //config.callback($(this));
        //		this.appendForm();
		});



			}
			

			}
		
		px.init(this);
		
		return this;
	}
})(jQuery);
