var protocolVersion = '1_5_0';
var scriptLocation = 'backend/r.php/';

function doLogin(name, pw, success) {
	var data = JSON.stringify({username:name, password:generate_md5(pw), protocol_version:protocolVersion});
	request("POST", 'session/authenticate', success, function(err,code) {
		if (code == 100 && onLoginFailed != null) onLoginFailed();
		else if (onError) onError(err);
	}, data);
}

function request(type, url, success, fail, data) {
	$.ajax({
		type: type,
		url: scriptLocation+url,
		data: data,
		dataType: "json",
		success: function(result) {
			success(result);
		},
		error: function (xhr, desc, exc) {
			if (!xhr || !xhr.responseText) {
				fail("Unknown error");
			} else {
				var err = JSON.parse(xhr.responseText);
				fail(err.error, err.code);
			}
		}
	});
}

