var WEB = WEB || {};
WEB.SOME_CONSTANT = 'foo bar';
var GROUP = GROUP || {};
GROUP.SOME_CONSTANT = 'foo bar';
var PROFILE = PROFILE || {};
PROFILE.SOME_CONSTANT = 'foo bar';
var USER = USER || {};
USER.SOME_CONSTANT = 'foo bar';
var UTIL = UTIL || {};
UTIL.SOME_CONSTANT = 'foo bar';

var profile = {};
profile.data = { info : {}};
var group = {};
group.data = { info: {}, info: {}, members : new Array() };
var user = {};
user.data = { groups : new Array(), messages : {}};
var app = {};
app.data = { inviteUser : {}, inviteEmail : {}, inviteCSV : {}, count : 0 };

var curStatus = -1;
var PATH = './../../ajax/';

WEB.emailToName = function (email, cbfunc) {
	$.ajax({
		type: 'POST',
		url: PATH + 'getUserNameWithEmail',
		data: { Email : email },
		success:
			function(response) {
				if (response != false) {
					if (app.data.inviteUser[response['Uid']] === undefined) {
						app.data.inviteUser[response['Uid']] = response;
					} else {
						response = 'duplicate';
					}
				} else {
					newemail = '\'' + email + '\'';
					if (app.data.inviteEmail[newemail] === undefined) {
						app.data.inviteEmail[newemail] = email;
					} else {
						response = 'duplicate';
					}
				}
				
				if (cbfunc && typeof (cbfunc) === 'function') { cbfunc(response); }
			}
	});
}

WEB.getSurroundingCities = function(data, cbfunc) {
	$.ajax({
		type: 'POST',
		url: PATH + 'getGroupsWithLocation',
		data: data,
		success:
			function (response) {
				app.data.cities = response;
				if (cbfunc && typeof(cbfunc) === 'function') { cbfunc(response); }
			}
	});
}

WEB.searchString = function (str, cbfunc){
	$.ajax({
		url: PATH + 'searchAll',
		type: 'POST',
		data: { 'messageText' : str },
		success:
			function(response) {
				user.data.results = response;
				if (cbfunc && typeof (cbfunc) === 'function') { cbfunc(user.data.results); }
			}
	});
}

WEB.getSearchArray = function (cbfunc) {
	$.ajax({
		type: 'POST',
		url : PATH + 'getUserContacts',
		success:
			function (response) {
				app.data.search = response;
				if (cbfunc && typeof (cbfunc) === 'function') { cbfunc(response); }
			}
	});
}

WEB.parseInput = function (line) {
	line = line.replace(/,/g,' ').replace(/;/g,' ');
	line = line.split(/\s+/g);
	if (line[line.length-1] == '') {
		line.splice(line.length-1,1);
	}
	return line;
}



WEB.listenLoad = function (cbfunc) {
	$.ajax({
			type: 'POST',
			url: PATH + 'listenLoad',
			data: {
				Gid: group.data.info.Gid
				},
			success: function(response) {
					group.data.messageLiveCount = response;
					if((group.data.messageLiveCount - group.data.info.messageCount) > 0){
						WEB.loadGroup(function () {
							renderGroupMessages();
						});						
					}
					if (cbfunc && typeof (cbfunc) === 'function') { cbfunc(); }
				}
		});
}

