/*****************************************************************************************************/
/*                                                                                                   */
/*                                        'LOGIN PANEL' CLASS                                        */          
/*                                                                                                   */
/*****************************************************************************************************/

function LOGIN(){
	
	var JSObject = this;
	
	this.type = "login"; 
	this.arr_inputs = [];
						
	this.form;
	this.DOMDoc;
	
	this.send_btn;
	
	this.sendingValues = false;

	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION INIT                                                */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	
	this.init = function(){
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                          INFORMATION                                              */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		
		this.send_btn = $('#'+this.type+'_send_btn',this.DOMDoc).get(0);
		
		this.form = this.DOMDoc.getElementById(this.type+'_email').form;
		$("#"+this.form.id,this.DOMDoc).bind("submit",function(){return false;});

		this.initCreate();
		this.enableButton(this.send_btn);

	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                  FUNCTION CREATE PANEL                                            */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initCreate = function(){
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'EMAIL' ACTIONS                                 	     */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var $Email = $('#'+this.type+'_email',this.DOMDoc);
		$Email.inputvalidate({
					SubmitFunction:		function(){ JSObject.validate(); },
					DOMDoc: 			JSObject.DOMDoc
					
		});
		
			
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                      INPUT 'PASSWORD' ACTIONS                                     */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var $Password= $('#'+this.type+'_password',this.DOMDoc);
		$Password.inputvalidate({
					SubmitFunction:		function(){ JSObject.validate(); },
					DOMDoc: 			JSObject.DOMDoc
					
		});
			
				
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                    INPUT BUTTON 'SEND' ACTIONS                                    */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var send_mouseDown = false;
		$(this.send_btn).mousedown(function(){										
										if (JSObject.sendValues == true) return;
										
										send_mouseDown = true;
										$(JSObject.DOMDoc.body).mouseup(function(){
																		 
											 if (JSObject.sendValues == true) return;
											 
											 if (send_mouseDown == true){
												send_mouseDown = false;
												$(JSObject.send_btn).unbind("mouseleave");
												$(JSObject.DOMDoc.body).unbind("mouseup");
												JSObject.sendValues = true;
												JSObject.validate();
											 }
										})
									});
		
		$(this.send_btn).mouseup(function(){
										if (JSObject.sendValues == true) return;
										
										if (send_mouseDown == true){
											send_mouseDown = false;
											$(this).unbind("mouseleave");
											$(JSObject.DOMDoc.body).unbind("mouseup");
											JSObject.sendValues = true;
											JSObject.validate();
										}
									});
		
		
		
	}
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                 FUNCTION ENABLE BUTTON                                            */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.enableButton = function(btn){
		JSObject.sendValues = false;
		
//		$(btn).css('cursor','pointer');
//		$(btn).animate({opacity:1},100);
	}
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                 FUNCTION DISABLE BUTTON                                           */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.disableButton = function(btn){
		JSObject.sendValues = true;
		
//		$(btn).animate({opacity:0.4},100);
//		$(btn).css('cursor','default');
	}

	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                 FUNCTION VALIDATE INFORMATION                                     */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.validate = function(){

		var $Email = $('#'+this.type+'_email',this.DOMDoc);
		var $Password= $('#'+this.type+'_password',this.DOMDoc);

		$.ajax({
			   type: "POST",
			   url: JSObject.form.action,
			   cache: false,
			   async: true,
			   data: "email="+$Email.val()+
					 "&password="+$Password.val(),
			   success: function(response){
					
			   		 var response = Boolean(Number(String(response)));
	
					 if (response == true){
						
		 				// ************************** DISPLAY MESSAGE *************************** //
			
						var message = 'You have successfully logged in';
						JSInterface.Loader.display({message: message});
						
						//postpone redirect
		 				setTimeout(
		 				
		 					function(){
		 						
								//redirect
								document.location.href = JSInterface.localpath+"users/user/accountform";
		 						
		 					},1000
		 				
		 				);
						
						// ********************************************************************** //
					 }
					 else{
					 						 
					 	//display error message
					 	//alert("Invalid email or password. Please try again!");
						 var message = 'Invalid email or password. Please try again!';
						 JSInterface.Loader.display({message: message});
					 	
						//enable buttons
						JSObject.enableButton(JSObject.send_btn);
					 }
					  
			   },
			   error: function(){
					alert("Our server is busy. Please try again in a few seconds!");
			   }
		});
		
	}

}
