var SugarRest = function() {
}
SugarRest.proxy_url = 'Rest_Proxy.php';
SugarRest.server_url = 'http://localhost/crm.andymain.co.uk/service/v2/rest.php'

SugarRest.call = function(method, data, callback, type) {

	// defult to get to cache
	if (typeof type == 'undefined') {
		type = "get"
	}

	// store the call details incase we have to login
	if (method != "login") {
		SugarRest.method = method
		SugarRest.callback = callback
		SugarRest.type = type
		SugarRest.data = data
		// add the session id in here if we have it
	}

	// only redirect the call to login if we not loggedin or trying to
	if ((typeof SugarRest.session_id == 'undefined') && method != "login") {
		SugarRest.call("login", "", SugarRest.get_session_id, "post");
		return;
	} else {
		data["session"] = SugarRest.session_id
	}

	$.ajax({
		type : type,
		url : SugarRest.proxy_url,
		data : "method=" + method
				+ "&input_type=JSON&response_type=JSON&rest_data="
				+ $.toJSON(data),
		dataType : 'JSON',
		crossDomain : false,
		success : callback
	});

}

SugarRest.get_session_id = function(return_data) {
	SugarRest.session_id = return_data.id
	SugarRest.call(SugarRest.method, SugarRest.data, SugarRest.callback,
			SugarRest.type);
}
