/**
 * Functions for login, logout, password reset, and page permissions
 * @author Andrew Masri
 */

/*
jQuery(document).ready(function() {
    
	jQuery.each(jQuery.browser, function(i, val) {
      jQuery("<div>" + i + " : <span>" + val + "</span>").appendTo('#loginResult');
    });
	if (jQuery.browser.msie && jQuery.browser.version < 8) {
		alert("Please upgrade your browser");
	}
});
*/

////////////////////  LOGIN /////////////////////////

function logout() {
	Post.Send('', site_url('login/ajaxLogout', true));
}



//open the login form in a dialog box
function popupLoginDialog() {
	//login can't work without cookies 
	if (cookieCheck()) {
		popupUrl('Login', randomiseUrl(site_url('login', true)), true);	
	}
}



//open the login form in a dialog box
function popupforgottenPasswordDialog() {
	popupUrl('Reset Password', site_url('login/forgottenPassword', true), true);
}




function submitLogin() {
	
	if (submitDisabled()) return false;	//ignore multiple submits
	
	var username = jQuery('#username').val();
	var password = jQuery('#password').val();
	var rememberMe = (jQuery('#rememberMe').is(':checked')) ? 1 : 0;
	var randomKey = jQuery('#randomKey').val();

	if (password) {
log('sessionId', randomKey);
log('hashed password', sha1(password));
		var password = sha1(sha1(password) + randomKey);
log('encrypted password', password);
	}

	Post.Send('username='+username+'&password='+password+'&rememberMe='+rememberMe, site_url('login/ajaxLogin', true));
}



function submitForgottenPasswordRequest() {

	if (submitDisabled()) return false;		//ignore multiple submits

	var fullname = jQuery('#fullname').val();
	var email = jQuery('#email').val();
	
	Post.Send('fullname='+fullname+'&email='+email, site_url('login/resetPassword', true));
}



function adminPasswordReset(user, title) {
	if (typeof user == 'undefined' || !user.length) {
		alert('System error: the user was not specified');
	}

	if (typeof title == 'undefined') {
		title = 'Confirm Password Reset';
	}

	//create the save dialog placeholder
	if (jQuery('#confirmPasswordResetDialog').length == 0) {
		jQuery('body').append('<div id="confirmPasswordResetDialog" style="display:none" title="' + title + '"><p>Would you like to generate a new password for this user and send them their new login details?</p></div>');
	}

	jQuery("#confirmPasswordResetDialog").dialog({
		resizable: false,
		modal: true,
		buttons: {
			'Confirm': function() {
				jQuery(this).dialog('close');

				Post.Send('user=' + user, base_url + 'login/resetPassword');
			},
			'Cancel': function() {
				jQuery(this).dialog('close');
			}
		}
	});
}




//the user form field on the login dialog is greyed out until the user clicks or starts typing 
function initFormField(element) {
	jQuery(element).val('').css('color', '#554').removeAttr('onclick').removeAttr('onkeydown')
}


//takes a path and prepends the base_url. Selects the most appropriate HTTP protocol (AJAX requests cannot cross protocol)  
function site_url(path, ajax) {

	if (typeof secureControllers == 'undefined' || secureControllers == false) {
		return base_url + path;			//no secure controllers have been specified
	}

	//for ajax calls the protocol must be the same otherwise the request will fail due to same origin poilcy 
	if (typeof ajax != 'undefined' && ajax) {
		if (window.location.href.indexOf('https://') == -1) {
			return base_url+path;			//the current page is HTTP - ajax call must also use HTTP
		} else {
			return base_url.replace('http://', 'https://') + path;	//the current page is HTTPS - ajax call must also use HTTPS
		}
	}

	if (secureControllers === true) {
		return base_url.replace('http://', 'https://') + path;	//all pages on this site use HTTPS  
	}
	
	secureControllersArray = secureControllers.split(',')
	for(var i in secureControllersArray) {
    	if (path.indexOf(secureControllersArray[i]) != -1) {
			return base_url.replace('http://', 'https://') + path;	//the path relates to a secure controller  
		}
	}
	
	return base_url+path;
}


////////////////////  PAGE PERMISSIONS /////////////////////////

jQuery(document).ready(function() {
	jQuery('#permissionsButton').live('click', function() {
		jQuery('#permissionsBox').fadeIn('slow').draggable({ containment: 'window', insideParent: false }); 
		jQuery('#permissionsButton').css('display', 'none');			
	});

	jQuery('#simplePermissionsButton').live('click', function() {
		var lockImage = jQuery('#simplePermissionsButton').attr('src');
		var isPublic = (lockImage.search('lock.gif')==-1) ? 0 : 1;	//determine the public status and toggle it
		Post.Send('pageId='+pageId+'&isPublic='+isPublic, base_url+'dataserver/ajaxSavePublicAccess');
		lockImage = (isPublic) ? lockImage.replace('lock.gif', 'unlocked.gif') : lockImage.replace('unlocked.gif', 'lock.gif');
		jQuery('#simplePermissionsButton').attr('src', lockImage);
		displayResult('#permissionResult');
	});
});


