// JavaScript Document

$(document).ready(function(){
	$("#login").dialog({
		bgiframe: true,
		height: 400,
		width: 250,
		modal: true,
		draggable: false,
		resizable: false,
		autoOpen: false,
		title: 'Login',
		buttons: {
			'Login': function() {
				$("#loginForm").submit();
				//$(this).dialog('close');
			},
			'Cancel': function() {
				$(this).dialog('close');
			}
		}
	});
});

function openLogin() {
	$("#login").dialog('open');
}

function closeLogin() {
	$("#login").dialog('close');
}

function logoutUser() {
	var offset = new Date();
	var url = '/assets/com/v1/login.cfc?method=logoutSFUser&returnformat=json&offset=' + offset.getTime(); //prevents caching
	$.getJSON(url,
        function(data){
          //alert(data);
		   window.location.href = '/';
		  });
}

// prepare the form when the DOM is ready 
$(document).ready(function() { 
    var options = { 
        // target:        '#errorstring',   // target element(s) to be updated with server response 
        // beforeSubmit:  showRequest,  // pre-submit callback 
        success:       loginResponse,  // post-submit callback 
		url:		   '/assets/com/v1/login.cfc', // the url to login.cfc 
		dataType:	   'json'
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
 
    // bind form using 'ajaxForm' 
    $('#loginForm').ajaxForm(options); 
}); 
 
// pre-submit callback 
function showRequest(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 
    alert('About to submit: \n\n' + queryString); 
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
} 
 
// post-submit callback 
function loginResponse(responseText, statusText)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
	
	if (statusText == 'success') {
		if (responseText == -1) {
			$("#errorstring").html("We have found that user, but the password is incorrect.");
		} else if (responseText == 0) {
			$("#errorstring").html("No user exists with that email address.");
		} else {
			$("#errorstring").html("Login OK. Refreshing the page. Please wait.");
			
			
			//alert($('#rememberme').is(':checked'));
			// set the rememberme cookie
			if ($('#rememberme').is(':checked')) {
				setCookie("REMEMBERME","1");
				//alert('rememberme is checked');
			} else {
				setCookie("REMEMBERME","0");
			}
			// set the keeploggedin cookie
			if ($('#keeploggedin').is(':checked')) {
				setCookie("KEEPLOGGEDIN","1");
				//alert('keeploggedin is checked');
			} else {
				setCookie("KEEPLOGGEDIN","0");
			}
			
			//window.location.reload(true);
		    window.location.reload(true);
			$('#login').dialog('close');
			//window.location.href = "/";
		}	
	} else {
		$("#errorstring").html("Error submitting form. Please try again.");
	}
	
 	/*
    alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
        '\n\nThe output div should have already been updated with the responseText.'); 
	*/
}

