/**
 * user login Form Validation library
 *
 * json library is used here
*/

// This validation function for header_login validation
function ValidateHeaderLogin(){
	var counter=0;
	
	var pass=$.trim($('#header_password').val());
	if($.trim($('#header_email').val())=='')
	{
		//alert("Enter Your Email Address1111111111");
		$.blockUI({ 
			message: "Enter Your Email Address",  
					  css: { 
            border: 'none', 
            padding: '15px', 
            backgroundColor: 'RED', 
            '-webkit-border-radius': '10px', 
            '-moz-border-radius': '10px', 
            opacity: '.5', 
            color: '#fff' 
				} }); 
			setTimeout($.unblockUI, 5000); 
		$('#header_email').focus();
		return false;
	}else if(!validateEmail($.trim($('#header_email').val()))){
		//alert("Enter Valid Email Address");
		$.blockUI({ 
			message: "Enter Valid Email Address",  
					  css: { 
            border: 'none', 
            padding: '15px', 
            backgroundColor: 'RED', 
            '-webkit-border-radius': '10px', 
            '-moz-border-radius': '10px', 
            opacity: '.5', 
            color: '#fff' 
				} }); 
			setTimeout($.unblockUI, 5000);
		$('#header_email').focus();
		return false;
	}
	
	if($.trim($('#header_password').val())=='')
	{
		//alert("Enter Your Password");
		$.blockUI({ 
			message: "Enter Your Password",  
					  css: { 
            border: 'none', 
            padding: '15px', 
            backgroundColor: 'RED', 
            '-webkit-border-radius': '10px', 
            '-moz-border-radius': '10px', 
            opacity: '.5', 
            color: '#fff' 
				} }); 
			setTimeout($.unblockUI, 5000);
		$('#header_password').focus();
		return false;
	}else if(pass.length<5)
	{
		//alert("Password length must be atleast 5");
		$.blockUI({ 
			message: "Password length must be atleast 5",  
					  css: { 
            border: 'none', 
            padding: '15px', 
            backgroundColor: 'RED', 
            '-webkit-border-radius': '10px', 
            '-moz-border-radius': '10px', 
            opacity: '.5', 
            color: '#fff' 
				} }); 
			setTimeout($.unblockUI, 5000);
		$('#header_password').focus();
		return false;
	}	
	return true;	
}

// This validation function for header_login1 validation
function ValidateHeaderLogin2(){
	var counter=0;
	
	var pass=$.trim($('#header_password1').val());
	if($.trim($('#header_email1').val())=='')
	{
		//alert("Enter Your Email Address");
		$.blockUI({ 
			message: "Enter Your Email Address",  
					  css: { 
            border: 'none', 
            padding: '15px', 
            backgroundColor: 'RED', 
            '-webkit-border-radius': '10px', 
            '-moz-border-radius': '10px', 
            opacity: '.5', 
            color: '#fff' 
				} }); 
			setTimeout($.unblockUI, 5000);
		$('#header_email1').focus();
		return false;
	}else if(!validateEmail($.trim($('#header_email1').val()))){
		//alert("Enter Valid Email Address");
		$.blockUI({ 
			message: "Enter Valid Email Address",  
					  css: { 
            border: 'none', 
            padding: '15px', 
            backgroundColor: 'RED', 
            '-webkit-border-radius': '10px', 
            '-moz-border-radius': '10px', 
            opacity: '.5', 
            color: '#fff' 
				} }); 
			setTimeout($.unblockUI, 5000);
		$('#header_email1').focus();
		return false;
	}
	
	if($.trim($('#header_password1').val())=='')
	{
		//alert("Enter Your Password");
		$.blockUI({ 
			message: "Enter Your Password",  
					  css: { 
            border: 'none', 
            padding: '15px', 
            backgroundColor: 'RED', 
            '-webkit-border-radius': '10px', 
            '-moz-border-radius': '10px', 
            opacity: '.5', 
            color: '#fff' 
				} }); 
			setTimeout($.unblockUI, 5000);
		$('#header_password1').focus();
		return false;
	}else if(pass.length<5)
	{
		//alert("Password length must be atleast 5");
		$.blockUI({ 
			message: "Password length must be atleast 5",  
					  css: { 
            border: 'none', 
            padding: '15px', 
            backgroundColor: 'RED', 
            '-webkit-border-radius': '10px', 
            '-moz-border-radius': '10px', 
            opacity: '.5', 
            color: '#fff' 
				} }); 
			setTimeout($.unblockUI, 5000);
		$('#header_password1').focus();
		return false;
	}	
	return true;	
}
//==============
function setupAjaxFormHeaderLogin(form_id, form_validations){
	var form = '#' + form_id;
	var form_message = 'server-message';
	// en/disable submit button
	var disableSubmit = function(val){
		$(form + ' input[type=image]').attr('disabled', val);
	};
	// setup loading message
	$(form).ajaxSend(function(){
		//$(form_message).removeClass().addClass('loading').html('Loading...').fadeIn();
	});
	// setup jQuery Plugin 'ajaxForm' 	
	var options = {
		dataType:  'json',
		beforeSubmit: function(){
			// run form validations if they exist
			if(!ValidateHeaderLogin()){
				return false;	
			}
			if(typeof form_validations == "function" && !form_validations()) {
				// this will prevent the form from being subitted
				return false;
			}
			disableSubmit(true);
		},
		success: function(json){
			//$('#server-message').hide();
			//$('#server-message').removeClass().addClass(json.type).html(json.message).fadeIn('slow');
			$.blockUI({ 
			message: json.message,  
					  css: { 
            border: 'none', 
            padding: '15px', 
            backgroundColor: 'RED', 
            '-webkit-border-radius': '10px', 
            '-moz-border-radius': '10px', 
            opacity: '.5', 
            color: '#fff' 
				} }); 
			setTimeout($.unblockUI, 5000); 
			disableSubmit(false);
			if(json.type == 'success'){				
				if(json.goHere != '')
				window.location.href=json.goHere;
				else
				window.location.href=base_url+'welcome';
				//$(form).clearForm();
			}
		}
	};
	$(form).ajaxForm(options);
}
function setupAjaxFormHeaderLogin2(form_id, form_validations){
	var form = '#' + form_id;
	var form_message = 'server-message';
	// en/disable submit button
	var disableSubmit = function(val){
		$(form + ' input[type=image]').attr('disabled', val);
	};
	// setup loading message
	$(form).ajaxSend(function(){
		//$(form_message).removeClass().addClass('loading').html('Loading...').fadeIn();
	});
	// setup jQuery Plugin 'ajaxForm' 	
	var options = {
		dataType:  'json',
		beforeSubmit: function(){
			// run form validations if they exist
			if(!ValidateHeaderLogin2()){
				return false;	
			}
			if(typeof form_validations == "function" && !form_validations()) {
				// this will prevent the form from being subitted
				return false;
			}
			disableSubmit(true);
		},
		success: function(json){
			//$('#server-message').hide();
			//$('#server-message').removeClass().addClass(json.type).html(json.message).fadeIn('slow');
			$.blockUI({ 
			message: json.message,  
					  css: { 
            border: 'none', 
            padding: '15px', 
            backgroundColor: 'RED', 
            '-webkit-border-radius': '10px', 
            '-moz-border-radius': '10px', 
            opacity: '.5', 
            color: '#fff' 
				} }); 
			setTimeout($.unblockUI, 5000); 
			disableSubmit(false);
			if(json.type == 'success'){				
				if(json.goHere != '')
				window.location.href=json.goHere;
				else
				window.location.href=base_url+'welcome';
				//$(form).clearForm();
			}
		}
	};
	$(form).ajaxForm(options);
}
//==============
$(document).ready(function() {  
    new setupAjaxFormHeaderLogin('header_login');
});

$(document).ready(function() {
    new setupAjaxFormHeaderLogin2('header_login1');
});
//==============Email Validation
function validateEmail(email){
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
}//end function

////======================================= Logout

function logout(){

			$.blockUI({ 
			message: 'You have successfully logged out.',  
					  css: { 
            border: 'none', 
            padding: '15px', 
            backgroundColor: 'RED', 
            '-webkit-border-radius': '10px', 
            '-moz-border-radius': '10px', 
            opacity: '.5', 
            color: '#fff' 
				} }); 
			window.location.href=base_url+'welcome/logout';
			setTimeout($.unblockUI, 5000); 
			
			
}

