/**
 * This is a wrapper function of cisAuthenticate function.
 */
function cisFloatingAuth(username,password,passwordHashed,validateOnly,persistent,syncWithEmbedded){
	// hide both welcome and login form
   	$('cis_floating_welcome').hide();
   	$('cis_floating_login_form').hide();
   	$('cis_floating_ajax_loader').show();
   	
	var retVal = cisAuthenticate(username,password,passwordHashed,validateOnly,persistent);
	var res = retVal[0];
	var msg = retVal[1];
	/* login success */
	if(res){
		// hide and show appropriate html elements 
		$('cis_floating_ajax_loader').hide();
		$('cis_floating_welcome').show();
					 		
		var profileArr = msg;
		
		// display welcome message
		$$('.cis_cust_name').each(function(e){
			e.innerHTML = profileArr['first_name']+' '+profileArr['family_name'];
		});
		
		// populate a few key fields that might need to be posted to the next page
		if($('customer_id')){
			$('customer_id').value= profileArr['customer_id'];
		}
		
		// call cisEmbeddedAuth function if it's defined on the same page (this is to keep the campaign embedded login box status in sync)
	    if(typeof cisFloatingAuth == 'function' && syncWithEmbedded == true){
	    	cisEmbeddedAuth(username,password,passwordHashed,validateOnly,persistent,false);
	    }
	}
		
	/* login failed */
	else{
		// hide and show appropriate html elements 
		$('cis_floating_ajax_loader').hide();
		$('cis_floating_login_form').show();
				 		
		// show user the error message (only if user is actually loggin in)
		if(!validateOnly){
			$('cis_floating_login_msg').innerHTML = msg;
		}
	}
}

/* onclick event listener for the login button on the embedded floating login box */ 
function cisFloatingLoginOnclick(){
	cisFloatingAuth($F($('cis_floating_username')), $F($('cis_floating_password')), false, false, $F($('cis_floating_rememberme'))); 
	// hack! if user login via the floating login box instead of system login user by auth cookie, a page redirect is required! 
	if($('cis_floating_welcome').style.display != 'none'){ 
		window.location = '/intrepidmembers/customer_info.php'; 
	}
}

/**
 * This is a wrapper function of cisLogout function. it is design to be used with the
 * standard cis data capture form
 */
function cisFloatingLogout(syncWithEmbedded){
	// hide both welcome and login form
   	$('cis_floating_welcome').hide();
   	$('cis_floating_login_form').hide();
   	$('cis_floating_ajax_loader').show();
   	
	var retVal = cisLogout();
	if(retVal){
		// hide and show appropriate html elements 
		$('cis_floating_ajax_loader').hide();
		$('cis_floating_login_form').show();
		
		// reset key fields that was populated upon last successful login
		if($('customer_id')){
			$('customer_id').value= '';
		}
		
		// call cisEmbeddedLogout function if it's defined on the same page (this is to keep the campaign embedded login box status in sync)
	    if(typeof cisEmbeddedLogout == 'function' && syncWithEmbedded == true){
	    	cisEmbeddedLogout(false);
	    }
		
		// reset error msg field
		$('cis_floating_login_msg').innerHTML = '';
		
		// if the current page url is inside the member area. redirect user to the member login page
		if(window.location.href.match(/intrepidmember/)){
			// ignore all page unload javascript function
			window.onbeforeunload = null;
			// force redirect to the member login page
			window.location = '/intrepidmembers/index.php';
		}
	}
}

/**
 * This is a wrapper function of resetPassword function. it is design to be used with the
 * standard cis data capture form
 */
function cisFloatingSendPassword(username){
	$('cis_floating_send_password_ajax_loader').show();
	$('cis_floating_send_password').hide();
	setTimeout(function(){ /* delay starts */
	var retVal  = resetPassword(username);
	$('cis_floating_login_msg').innerHTML = retVal[1];
	$('cis_floating_send_password_ajax_loader').hide();
	$('cis_floating_send_password').show();
	},1000); /* delay ends */
}