var AJAX = {};

AJAX.ishttps = (window.location.toString().indexOf("https:") >= 0);

if(AJAX.ishttps) AJAX.URL = "https://www.lociloci.com/friendfinder/";
else AJAX.URL = "https://www.lociloci.com/friendfinder/";

AJAX.setup = function() 
{
	var url = window.location.toString();	
	if(url.indexOf("http") < 0) {
        if(AJAX.ishttps) url = "https://www.lociloci.com/start/";
        else url = "http://www.lociloci.com/start/";
	}
	url = url.substring(url.indexOf("//") + 2);	
	while(url.indexOf("/") > 0) {
		url = url.substring(0, url.indexOf("/"));
	}	

	if(AJAX.ishttps) AJAX.URL = "https://" + url + "/friendfinder/";
	else AJAX.URL = "http://" + url + "/friendfinder/";
	
//	AJAX.URL = "http://localhost:8080/web/";	
};

AJAX.getXML = function(url, onSuccess)
{
	$.ajax({
		url: url + "?cachePrevent=" + new Date().getTime(),
		type: 'GET',
		dataType: 'xml',
		error: onSuccess,
		success: onSuccess
	});	
};

AJAX.get = function(action, parameters, onSuccess) 
{
	parameters.cachePrevent = new Date().getTime();	
	var first = true;
	for(var i in parameters)
	{
		if(parameters[i])
		{
			if(first)
			{
				action += "?";
				first = false;
			}
			else
			{
				action += "&";			
			}
			action += i + "=" + parameters[i];
		}
	}
	
	$.ajax({
		url: AJAX.URL + action,
		type: 'GET',
		dataType: 'xml',
		error: onSuccess,
		success: onSuccess
	});
};

AJAX.getResponseNode = function(node_xml) 
{
	if(node_xml)
	{
		var collection_arr = node_xml.childNodes;
		for(var i = 0; i < collection_arr.length; i++) 
		{
			if(collection_arr[i].nodeName == "response" && collection_arr[i].getAttribute("ok") == "true" && collection_arr[i].childNodes[0]) 
			{	
				return collection_arr[i];
			}
		}		
	}
	
	return node_xml;	
};

LLResponse = function(data)
{
	this.success = true;					
	this.error = "";
	if(data && data.childNodes)
	{
		var collection_arr = data.childNodes;
		for(var i = 0; i < collection_arr.length; i++) 
		{
			if(collection_arr[i].nodeName == "response") 
			{			
				if(collection_arr[i].getAttribute("ok") == "false") 
				{
					this.success = false;											
					if(collection_arr[i].getAttribute("errorcode")) 
					{
						this.success = false;						
						this.error = collection_arr[i].getAttribute("errorcode");
					}
					else if(collection_arr[i].childNodes[0]) 
					{
						this.success = false;					
						this.error = collection_arr[i].childNodes[0].nodeValue;											
					    for(var j = 0; j < collection_arr[i].childNodes.length; j++) {
    					    if(collection_arr[i].childNodes[j].nodeName == "data") {
        						this.error = collection_arr[i].childNodes[j].getAttribute("value");											    					        
    					    }
					    }


					}
					if(collection_arr[i].getAttribute("errormessage")) 
					{
						this.message = collection_arr[i].getAttribute("errormessage");
					}					
				}
			}
			if(collection_arr[i].nodeName == "error") 
			{			
				this.success = false;			
				this.error = collection_arr[i].childNodes[0].nodeValue;
			}		
		}
	}
	else
	{
		this.success = false;
		this.error = "no XML result";		
	}	
};

var Encoder = {};

Encoder.encode = function(val)
{
	var len     = val.length;
	var backlen = len;
	var i       = 0;
	
	var newStr  = "";
	var frag    = "";
	var encval  = "";
	var original = val;
    for (i = 0; i < len; i++)
    {
		if(val.substring(i, i+1).charCodeAt(0) < 255)
		{
			if(Encoder.isUnsafe(val.substring(i,i+1)) == false)
			{
				newStr = newStr + val.substring(i,i+1); 
			}
            else
            {
				newStr = newStr + Encoder.convert(val.substring(i,i+1)); 
			}
		}
		else
		{
		   newStr = original; 
		   i=len;             
		 }
	}

	return newStr;
};

var hexVals = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];
var unsafeString = "\"<>%\\^[]`\+\$\,\/\:";

Encoder.isUnsafe = function(compareChar)
{
	if (unsafeString.indexOf(compareChar) == -1 && compareChar.charCodeAt(0) > 32 && compareChar.charCodeAt(0) < 123)
   	{	
		return false;
	}
	else
   	{ 
		return true; 
	}
};

Encoder.convert = function(val)
{
	return  "%" + Encoder.decToHex(val.charCodeAt(0), 16); 
};

Encoder.decToHex =function(num, radix)
{
	var hexString = "";
	while (num >= radix)
	{
		temp = num % radix;
		num = Math.floor(num / radix);
		hexString += hexVals[temp];
    }
	hexString += hexVals[num];
	return Encoder.reversal(hexString);
};

Encoder.reversal = function(s)
{
	var len = s.length;
	var trans = "";
	for (i=0; i<len; i++)
	{
		trans = trans + s.substring(len-i-1, len-i); 
	}
	s = trans;
	return s;
};

var Lociloci = new Object();
Lociloci.partnerId = 2;

Lociloci.LOGIN = "ajaxLogin!msisdnLogin.action";
Lociloci.LOGOUT = "ajaxLogin!logout.action";
Lociloci.FORGOT_PASSWORD = "ajaxLogin!ForgotPassword.action";

Lociloci.CHECK_COOKIE = "ajaxUser!check.action";
Lociloci.GET_USER = "ajaxUser!user.action";
Lociloci.REMOVE_FRIEND= "ajaxUser!removeFriend.action";
Lociloci.CREATE_MESSAGE = "ajaxUser!createMessage.action";
Lociloci.POSITION_ALL = "ajaxUser!startPositioning.action";
Lociloci.TURN_ON_FREE = "ajaxUser!turnFreePositioningOn.action";
Lociloci.TURN_ON_SAFETY = "ajaxUser!turnSafetyOn.action";
Lociloci.CHANGE_MSISDN = "ajaxUser!changeMsisdn.action";
Lociloci.CHANGE_PASSWORD = "ajaxUser!changePassword.action";
Lociloci.CHANGE_NAME = "ajaxUser!changeName.action";
Lociloci.CHANGE_EMAIL = "ajaxUser!updateEmail.action";
Lociloci.POSITION_FRIEND = "ajaxUser!positionFriend.action";
Lociloci.POSITION_USER = "ajaxUser!position.action";
Lociloci.GET_MOBILE_SMS_LINK = "ajaxUser!sendMobileLink.action";
Lociloci.GET_LOG = "ajaxUser!positionLog.action";
Lociloci.SET_POSITION = "data!putLokiPosition.action";

Lociloci.checkCookie = function(onSuccess, onError) 
{
	AJAX.get(Lociloci.CHECK_COOKIE, {currentPartnerId:2}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError, function(response, onSuccess, collection_arr)
		{
			for(var i = 0; i < collection_arr.length; i++) 
			{	
				if(collection_arr[i].nodeName == "response") 			
				{
					response.userid = collection_arr[i].getAttribute("userid");
				}
			}			
		});
	});		
};

Lociloci.login = function(msisdn, password, onSuccess, onError) 
{
	AJAX.get(Lociloci.LOGIN, {msisdn:Encoder.encode(msisdn), password:Encoder.encode(password), partnerId:Lociloci.partnerId, currentPartnerId:2}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError, function(response, onSuccess, collection_arr)
		{
			for(var i = 0; i < collection_arr.length; i++) 
			{			
				if(collection_arr[i].nodeName == "response") 
				{
					response.userid = collection_arr[i].getAttribute("userid");												
				}
			}		
		});
	});
};

Lociloci.logout = function(onSuccess, onError) 
{
	AJAX.get(Lociloci.LOGOUT, {}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});	
};

Lociloci.forgotPassword = function(msisdn, onSuccess, onError) 
{
	AJAX.get(Lociloci.FORGOT_PASSWORD, {partnerId:Lociloci.partnerId, msisdn:Encoder.encode(msisdn)}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});
};

Lociloci.getUser = function(id ,onSuccess, onError) 
{
	AJAX.get(Lociloci.GET_USER, {userId:id}, function(data)
	{	
		Lociloci.wrappedResponse(data, onSuccess, onError, function(response, onSuccess, collection_arr)
		{
			for(var i = 0; i < collection_arr.length; i++) 
			{
				if(collection_arr[i].nodeName == "user") 
				{											
					response.user = new User(collection_arr[i]);
					Lociloci.parseAdvancedLandmarks(response.user, collection_arr[i].childNodes);					
				}
				if(collection_arr[i].nodeName == "friends") 
				{
					response.user.friends_arr = new Array();
					response.user.friends = new Object();
					var subcollection_arr = collection_arr[i].childNodes;
					for(var j = 0; j < subcollection_arr.length; j++) 
					{			
						if(subcollection_arr[j].nodeName == "friend") 
						{
							var friend = new User(subcollection_arr[j]);
							Lociloci.parseAdvancedLandmarks(friend, subcollection_arr[j].childNodes);												
							if(friend.longitude != 0 && friend.longitude != undefined) 
							{
								if(response.user.longitude != 0 && response.user.longitude != undefined && response.user.hiding == "false") 
								{
									friend.distance = Lociloci.getDistance(response.user.longitude, response.user.latitude, friend.longitude, friend.latitude);
								}
								else
								{
									friend.distance = 0;
								}
							}
							else
							{
								friend.distance = "unpositioned";
							}
							
							response.user.friends_arr.push(friend);
							response.user.friends[friend.id] = friend;			
						}		
					}
				}
			}
		});			
	});
};

Lociloci.parseAdvancedLandmarks = function(user, collection_arr)
{
	user.landmarks_arr = new Array();
	user.landmarks = new Object();	
	user.landmarkincomplete = true;
	for(var i = 0; i < collection_arr.length; i++) 
	{	
		if(collection_arr[i].nodeName == "advancedlandmarks" || collection_arr[i].nodeName == "landmarks") 
		{											
			collection_arr = collection_arr[i].childNodes;
			i = 0;
		}	
		if(collection_arr[i] && collection_arr[i].nodeName == "landmark") 
		{
			var landmark = new AdvancedLandmark(collection_arr[i]);
			user.landmarks_arr.push(landmark);			
			if(!user.landmarks[landmark.level])
			{
				user.landmarks[landmark.level] = landmark;
				if(landmark.level == "4")
				{
					user.landmarkincomplete = false;	
				}
			}
		}
	}
};

Lociloci.setUserPosition = function(longitude, latitude, accuracy, onSuccess, onError)
{
	AJAX.get(Lociloci.SET_POSITION, {longitude:longitude, latitude:latitude, precision:accuracy}, function(data)
	{
		var response = new LLResponse(data);		
		var collection_arr = data.childNodes;
		for(var i = 0; i < collection_arr.length; i++) 
		{
			if(collection_arr[i].nodeName == "response") 
			{
				response.landmark = collection_arr[i].getAttribute("landmark");												
			}
		}		
		onSuccess(response);
	});	
};

Lociloci.removeFriend = function(id, onSuccess, onError) 
{
	AJAX.get(Lociloci.REMOVE_FRIEND, {friendId:id}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});
};

Lociloci.getInvites = function(onSuccess, onError)
{
	AJAX.get("ajaxInvite!pendingRequests.action", {}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError, function(response, onSuccess, collection_arr) 
		{
			response.invites = new Object();
			
			for(var i = 0; i < collection_arr.length; i++) 
			{			
				if(collection_arr[i].nodeName == "requests") 
				{		
					if(collection_arr[i].childNodes)
					{
						collection_arr = collection_arr[i].childNodes;
					}
					i = 0;
				}
				if(collection_arr[i] && collection_arr[i].nodeName == "request") 
				{
					var invite = new FriendRequest(collection_arr[i]);
					if(response.invites[invite.userId] == null) 
					{						
						response.invites[invite.userId] = invite;
					}
				}	
			}
		});
	});		
};

Lociloci.acceptInvitation = function(userId, onSuccess, onError)
{
	AJAX.get("ajaxInvite!handleRequest.action", {partnerId:Lociloci.partnerId, userId:userId, accept:"true"}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});		
};

Lociloci.rejectInvitation = function(userId, onSuccess, onError)
{
	AJAX.get("ajaxInvite!handleRequest.action", {partnerId:Lociloci.partnerId, userId:userId, accept:"false"}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});		
};

Lociloci.sendSMS = function(toid, text, onSuccess, onError) 
{
	AJAX.get(Lociloci.CREATE_MESSAGE, {to:toid, text:Encoder.encode(text), sendAsSms:"true"}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});
};

Lociloci.positionAll = function(onSuccess, onError)
{
	AJAX.get(Lociloci.POSITION_ALL, {tresholdSeconds:300}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});
};

Lociloci.turnFreePositioningOn = function(onSuccess, onError)
{
	AJAX.get(Lociloci.TURN_ON_FREE, {}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});
};

Lociloci.turnSafetyOn = function(onSuccess, onError)
{
	AJAX.get(Lociloci.TURN_ON_SAFETY, {}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});	
};

Lociloci.changeMSISDN = function(onSuccess, onError)
{
	AJAX.get(Lociloci.CHANGE_MSISDN, {}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError, function(response, onSuccess, collection_arr) 
		{		
			for(var i = 0; i < collection_arr.length; i++) 
			{	
				if(collection_arr[i].nodeName == "response")		
				{		
					response.smscode = collection_arr[i].getAttribute("smscode");
					response.codeword = collection_arr[i].getAttribute("codeword");
				}
			}
		});
	});
};

Lociloci.changePassword = function(oldPassword, newPassword, onSuccess, onError)
{
	AJAX.get(Lociloci.CHANGE_PASSWORD, {oldPassword:oldPassword, newPassword:newPassword}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});	
};

Lociloci.changeName = function(name, onSuccess, onError)
{
	AJAX.get(Lociloci.CHANGE_NAME, {newUserName:name}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});	
};

Lociloci.changeEmail = function(email, onSuccess, onError)
{
	AJAX.get(Lociloci.CHANGE_EMAIL, {email:email}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});	
};

Lociloci.positionUser = function(friendId, onSuccess, onError)
{
	if(friendId == Logic.user.id) 
	{
		Lociloci.positionSelf(onSuccess, onError);
	}
	else
	{
		AJAX.get(Lociloci.POSITION_FRIEND, {friendId:friendId}, function(data)
		{
			Lociloci.wrappedResponse(data, onSuccess, onError, function(response, onSuccess, collection_arr) 
			{
				for(var i = 0; i < collection_arr.length; i++) {
					if(collection_arr[i].nodeName == "position") {			
						response.id = collection_arr[i].getAttribute("userId");
						response.latitude = collection_arr[i].getAttribute("latitude");
						response.longitude = collection_arr[i].getAttribute("longitude");											
						response.accuracy = collection_arr[i].getAttribute("accuracy");		
					}	
					if(collection_arr[i].nodeName == "landmark" && response.id != "undefined") {
						response.landmark = collection_arr[i].getAttribute("name");
					}	
					if(collection_arr[i].nodeName == "advancedlandmarks") 
					{											
						response.landmarks_arr = new Array();
						response.landmarks = new Object();	
						collection2_arr = collection_arr[i].childNodes;
						for(var j = 0; j < collection_arr.length; j++) {
							if(collection2_arr[j] && collection2_arr[j].nodeName == "landmark") 
							{
								var landmark = new AdvancedLandmark(collection2_arr[j])
								response.landmarks_arr.push(landmark);			
								response.landmarks[landmark.level] = landmark;
							}								
						}
					}	
					
					if(collection_arr[i].nodeName == "goodlandmark")
					{
						response.goodLandmark = collection_arr[i].getAttribute("name");	
						response.goodLandmarkDistance = collection_arr[i].getAttribute("distance");						
					}
				}
			});
		});		
	}
};

Lociloci.positionSelf = function(onSuccess, onError)
{
	AJAX.get(Lociloci.POSITION_USER, {}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError, function(response, onSuccess, collection_arr) 
		{
			for(var i = 0; i < collection_arr.length; i++) {
				if(collection_arr[i].nodeName == "position") {			
					response.id = collection_arr[i].getAttribute("userId");
					response.latitude = collection_arr[i].getAttribute("latitude");
					response.longitude = collection_arr[i].getAttribute("longitude");											
					response.accuracy = collection_arr[i].getAttribute("accuracy");		
				}	
				if(collection_arr[i].nodeName == "landmark" && response.id != "undefined") {
					response.landmark = collection_arr[i].getAttribute("name");
				}	
				if(collection_arr[i].nodeName == "advancedlandmarks") 
				{											
					response.landmarks_arr = new Array();
					response.landmarks = new Object();	
					collection2_arr = collection_arr[i].childNodes;
					for(var j = 0; j < collection_arr.length; j++) {
						if(collection2_arr[j] && collection2_arr[j].nodeName == "landmark") 
						{
							var landmark = new AdvancedLandmark(collection2_arr[j])
							response.landmarks_arr.push(landmark);			
							response.landmarks[landmark.level] = landmark;
						}								
					}
				}			
				if(collection_arr[i].nodeName == "goodlandmark")
				{
					response.goodLandmark = collection_arr[i].getAttribute("name");	
					response.goodLandmarkDistance = collection_arr[i].getAttribute("distance");						
				}				
			}
		});
	});		
};

Lociloci.getMobileSMS = function(onSuccess, onError)
{
	AJAX.get(Lociloci.GET_MOBILE_SMS_LINK, {}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});	
};

Lociloci.getIPhoneSMS = function(onSuccess, onError)
{
	AJAX.get("ajaxUser!sendIPhoneLink.action", {}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});	
};

Lociloci.generateProductCode = function(countryId, msisdn, billingModelId, onSuccess, onError)
{
	AJAX.get("lociRegistration!generateValidationCode.action", {countryId:countryId, msisdn:Encoder.encode(msisdn), billingModelId:billingModelId, partnerId:2, validationType:1}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});
}

Lociloci.finalizeProductRegistration = function(msisdn, code, name, email, password, countryId, inviteEmail, inviteKey, sourceAd, sourceSite, onSuccess, onError) 
{
	AJAX.get("lociRegistration!submitCode.action", {msisdn:msisdn, code:code, name:Encoder.encode(name), password:Encoder.encode(password), email:Encoder.encode(email), inviteEmail:Encoder.encode(inviteEmail), inviteKey:inviteKey, countryId:countryId, partnerId:Lociloci.partnerId, sourceAdId:sourceAd, sourceSiteId:sourceSite, targetSiteId:"www.lociloci.com", deactivateWelcomeSms:"true"}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});	
};

Lociloci.finalizeProductRegistrationWelcome = function(msisdn, code, name, email, password, countryId, inviteEmail, inviteKey, sourceAd, sourceSite, noSMS, onSuccess, onError) 
{
	AJAX.get("lociRegistration!submitCode.action", {msisdn:msisdn, code:code, name:Encoder.encode(name), password:Encoder.encode(password), email:Encoder.encode(email), inviteEmail:Encoder.encode(inviteEmail), inviteKey:inviteKey, countryId:countryId, partnerId:Lociloci.partnerId, sourceAdId:sourceAd, sourceSiteId:sourceSite, targetSiteId:"www.lociloci.com", deactivateWelcomeSms:noSMS}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});	
};

Lociloci.turnProductOn = function(billingModelId, freedays, onSuccess, onError)
{
	AJAX.get(Lociloci.TURN_ON_FREE, {freeDays:freedays, billingModelId:billingModelId}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});
};

Lociloci.getLog = function(onSuccess, onError)
{
	AJAX.get(Lociloci.GET_LOG, {firstIndex:0, maxResults:100}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError, function(response, onSuccess, collection_arr) 
		{
			response.logg_arr = new Array();
			for(var i = 0; i < collection_arr.length; i++) 
			{
				if(collection_arr[i].nodeName == "entry") 
				{
					response.logg_arr.push(new Entry(collection_arr[i]));
				}
			}
		});
	});		
};

Lociloci.registerClick = function(sourceAd, sourceSite, onSuccess, onError)
{
	AJAX.get("data!logSourceSite.action", {sourceAdId:sourceAd, sourceSiteId:sourceSite, partnerId:2, countryId:1}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});		
};

Lociloci.putAdvancedLandmark = function(landmarks, userid, onSuccess, onError)
{
	var names = "";
	var levels = "";
	var latsA = "";
	var latsB = "";
	var longsA = "";
	var longsB = "";	
	
	for(var i in landmarks)
	{
		names += landmarks[i].name + ",";
		levels += landmarks[i].level + ",";
		latsA += landmarks[i].box.south + ",";
		latsB += landmarks[i].box.north + ",";
		longsA += landmarks[i].box.west + ",";		
		longsB += landmarks[i].box.east + ",";				
	}
	
	names = names.substring(names.lenght - 1);
	levels = levels.substring(levels.lenght - 1);
	latsA = latsA.substring(latsA.lenght - 1);
	latsB = latsB.substring(latsB.lenght - 1);	
	longsA = longsA.substring(longsA.lenght - 1);	
	longsB = longsB.substring(longsB.lenght - 1);			

	AJAX.get("ajaxAdvancedLandmark!submitLandmarks.action", {names:Encoder.encode(names), levels:levels, latsA:latsA, latsB:latsB, longsA:longsA, longsB:longsB, partnerId:2, userId:userid}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError, function(response, onSuccess, collection_arr) 
		{
			response.landmarks_arr = new Array();
			response.landmarks = new Object();	
			for(var i = 0; i < collection_arr.length; i++) 
			{	
				if(collection_arr[i].nodeName == "advancedlandmarks") 
				{											
					collection_arr = collection_arr[i].childNodes;
					i = 0;
				}	
				if(collection_arr[i] && collection_arr[i].nodeName == "landmark") 
				{
					var landmark = new AdvancedLandmark(collection_arr[i])
					response.landmarks_arr.push(landmark);			
					response.landmarks[landmark.level] = landmark;
				}
			}			
		});
	});			
};

Lociloci.batchIntvite = function(invites, smsAccept, onSuccess, onError)
{
	var names = "";
	var msisdns = "";
	var emails = "";
	var facebookIds = "";	
	var imageIds = "";		
	var imageURLs = "";			
	
	for(var i in invites)
	{
		names += (invites[i].name ? invites[i].name : "null") + ",";
		msisdns += (invites[i].msisdn ? invites[i].msisdn : "null") + ",";
		emails += (invites[i].email ? invites[i].email : "null") + ",";
		facebookIds += (invites[i].facebookId ? invites[i].facebookId : "null") + ",";
		imageIds += (invites[i].imageId ? invites[i].imageId : "null") + ",";
		imageURLs += (invites[i].imageURL ? invites[i].imageURL : "null") + ",";		
	}	
	
	names = names.substring(0, (names.length - 1));
	msisdns = msisdns.substring(0, (msisdns.length - 1));
	emails = emails.substring(0, (emails.length - 1));	
	facebookIds = facebookIds.substring(0, (facebookIds.length - 1));	
	imageIds = imageIds.substring(0, (imageIds.length - 1));	
	imageURLs = imageURLs.substring(0, (imageURLs.length - 1));	

	AJAX.get("ajaxInvite!inviteBatch.action", {targetNames:Encoder.encode(names), targetMsisdns:msisdns, targetEmails:emails, targetFacebookIds:facebookIds, targetImageIds:imageIds, targetImageLocationLinks:imageURLs, smsAccept:smsAccept}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);		
	});
};

Lociloci.wrappedResponse = function(data, onSuccess, onError, parser)
{
	var response = new LLResponse(data);		
	if(response.success) 
	{	
		data = AJAX.getResponseNode(data);	
		if(parser) 
		{
			parser(response, onSuccess, data.childNodes);
		}
	}
	if(response.success)
	{
		if(onSuccess != null)
		{
			onSuccess(response);
		}
	}
	else if(onError != null)
	{
		onError(response);		
	}
};

Lociloci.getSentInvites = function(onSuccess, onError)
{ 
	AJAX.get("ajaxInvite!unhandledInvites.action", {}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError, function(response, onSuccess, collection_arr) 
		{	
			response.invites_arr = new Array();
			response.requests_arr = new Array();			
			for(var i = 0; i < collection_arr.length; i++) 
			{			
				if(collection_arr[i].nodeName == "invites") 
				{		
					if(collection_arr[i].childNodes)
					{
						var collection2_arr = collection_arr[i].childNodes;
						for(var j = 0; j < collection2_arr.length; j++) 
						{									
							if(collection2_arr[j] && collection2_arr[j].nodeName == "invite") 
							{
								var invite = new Invite(collection2_arr[j]);
								response.invites_arr.push(invite)			
							}							
						}
					}
				}
				
				if(collection_arr[i].nodeName == "requests") 
				{
					if(collection_arr[i].childNodes)
					{
						var collection3_arr = collection_arr[i].childNodes;
						for(var k = 0; k < collection3_arr.length; k++) 
						{									
							if(collection3_arr[k] && collection3_arr[k].nodeName == "request") 
							{
								var invite2 = new FriendRequest(collection3_arr[k]);
								response.requests_arr.push(invite2);		
							}							
						}
					}
				}				
			}
		});
	});		
};

Lociloci.removeInvite = function(data, onSuccess, onError) 
{
	AJAX.get("ajaxInvite!removeSentInvite.action", {contactData:Encoder.encode(data)}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});
};

Lociloci.resendInvite = function(data, onSuccess, onError) 
{
	AJAX.get("ajaxInvite!remindSentInvite.action", {contactData:Encoder.encode(data)}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});
};

Lociloci.getAvailableDevices = function(onSuccess, onError)
{
	AJAX.get("device!getVisibleDevices.action", {}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError, function(response, onSuccess, collection_arr) 
		{
			response.own_devices = new Array();
			response.other_devices = new Array();		
			
			for(var i = 0; i < collection_arr.length; i++) 
			{
				if(collection_arr[i].nodeName == "own_devices") 
				{		
					Lociloci.parseDevices(response.own_devices, collection_arr[i].childNodes);
				}	
				if(collection_arr[i].nodeName == "other_devices") 
				{			
					Lociloci.parseDevices(response.other_devices, collection_arr[i].childNodes);
				}					
			}
		});
	});
};

Lociloci.parseDevices = function(device_arr, collection_arr)
{
	for(var i = 0; i < collection_arr.length; i++) 
	{				
		if(collection_arr[i].nodeName == "user_device") 
		{						
			var device = new Device(collection_arr[i]);
			Lociloci.parseAdvancedLandmarks(device, collection_arr[i].childNodes);																			
			
			var subcollection_arr = collection_arr[i].childNodes;
			for(var j = 0; j < subcollection_arr.length; j++) 
			{			
				if(subcollection_arr[j].nodeName == "friends") 
				{		
					subcollection_arr = subcollection_arr[j].childNodes;
					j = 0;
				}
				if(subcollection_arr[j] && subcollection_arr[j].nodeName == "friend") 
				{
					device.friends.push(subcollection_arr[j].getAttribute("id"));
				}
			}
			
			device_arr.push(device);
		}
	}	
}

Lociloci.addDevice = function(typeId, name, msisdn, code, imageId, onSuccess, onError)
{
	AJAX.get("device!addDevice.action", {deviceTypeId:typeId, deviceName:Encoder.encode(name), deviceCode:code, deviceImage:imageId, deviceMsisdn:Encoder.encode(msisdn)}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});
};

Lociloci.updateDevice = function(deviceId, typeId, name, msisdn, code, imageId, onSuccess, onError)
{
	AJAX.get("device!updateDevice.action", {userDeviceId:deviceId, deviceTypeId:typeId, deviceName:Encoder.encode(name), deviceCode:code, deviceImage:imageId, deviceMsisdn:Encoder.encode(msisdn)}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});
};

Lociloci.reinitializeDevice = function(deviceId, onSuccess, onError)
{
	AJAX.get("device!reinitialize.action", {userDeviceId:deviceId}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});
};

Lociloci.removeDevice = function(deviceId, onSuccess, onError)
{
	AJAX.get("device!removeDevice.action", {userDeviceId:deviceId}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});
};

Lociloci.activateDevice = function(deviceId, onSuccess, onError)
{
	AJAX.get("device!activate.action", {userDeviceId:deviceId}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});
};

Lociloci.deactivateDevice = function(deviceId, onSuccess, onError)
{
	AJAX.get("device!deactivate.action", {userDeviceId:deviceId}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});
};

Lociloci.addFriendToDevice = function(deviceId, friendId, onSuccess, onError)
{
	AJAX.get("device!addFriend.action", {userDeviceId:deviceId, friendId:friendId}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});
};

Lociloci.removeFriendFromDevice = function(deviceId, friendId, onSuccess, onError)
{
	AJAX.get("device!removeFriend.action", {userDeviceId:deviceId, friendId:friendId}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});
};

Lociloci.updateBatteryStatus = function(deviceId, onSuccess, onError)
{
	AJAX.get("device!updateBatteryStatus.action", {userDeviceId:deviceId}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});
};

Lociloci.getDeviceGSMPosition = function(deviceId, onSuccess, onError)
{
	AJAX.get("device!getDeviceGsmPosition.action", {userDeviceId:deviceId}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError, function(response, onSuccess, collection_arr) 
		{
			for(var i = 0; i < collection_arr.length; i++) {
				if(collection_arr[i].nodeName == "position") {			
					response.id = collection_arr[i].getAttribute("userId");
					response.latitude = collection_arr[i].getAttribute("latitude");
					response.longitude = collection_arr[i].getAttribute("longitude");											
					response.accuracy = collection_arr[i].getAttribute("accuracy");		
				}	
				if(collection_arr[i].nodeName == "landmark" && response.id != "undefined") {
					response.landmark = collection_arr[i].getAttribute("name");
				}	
				if(collection_arr[i].nodeName == "advancedlandmarks") 
				{											
					response.landmarks_arr = new Array();
					response.landmarks = new Object();	
					collection2_arr = collection_arr[i].childNodes;
					for(var j = 0; j < collection_arr.length; j++) {
						if(collection2_arr[j] && collection2_arr[j].nodeName == "landmark") 
						{
							var landmark = new AdvancedLandmark(collection2_arr[j])
							response.landmarks_arr.push(landmark);			
							response.landmarks[landmark.level] = landmark;
						}								
					}
				}	
				
				if(collection_arr[i].nodeName == "goodlandmark")
				{
					response.goodLandmark = collection_arr[i].getAttribute("name");	
					response.goodLandmarkDistance = collection_arr[i].getAttribute("distance");						
				}
			}
		});
	});
};

Lociloci.getDeviceHistory = function(deviceId, fromTime, toTime, onSuccess, onError)
{	
	AJAX.get("device!getPositions.action", {userDeviceId:deviceId, fromTime:fromTime, toTime:toTime}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError, function(response, onSuccess, collection_arr) 
		{
			response.positions = new Object();
			response.positions_arr = new Array();
			
			for(var i = 0; i < collection_arr.length; i++) 
			{				
				if(collection_arr[i].nodeName == "positions") 
				{		
					collection_arr = collection_arr[i].childNodes;
					i = 0;
				}
				if(collection_arr[i] && collection_arr[i].nodeName == "position") 
				{
					if(collection_arr[i].getAttribute("longitude") != 0.0 && collection_arr[i].getAttribute("latitude") != 0)
					{					
						var pos = new Object();
						pos.id = deviceId;
						pos.positionId = collection_arr[i].getAttribute("id");
						pos.longitude = collection_arr[i].getAttribute("longitude");
						pos.latitude = collection_arr[i].getAttribute("latitude");
						pos.timestamp = collection_arr[i].getAttribute("timestamp");						
						
						response.positions_arr.push(pos);
					}
				}
			}
			
			response.positions_arr.reverse();

			for(var i = 0; i < response.positions_arr.length; i++)
			{
				response.positions[response.positions_arr[i].positionId] = response.positions_arr[i];
			}
		});
	});	
}

Lociloci.addUserInfo = function(adress, postalCode, city, personalId, onSuccess, onError)
{
	if(personalId)
	{
		AJAX.get("ajaxUser!updateUserInfo.action", {adress:Encoder.encode(adress), postnummer:Encoder.encode(postalCode), postOrt:Encoder.encode(city), personnummer:Encoder.encode(personalId)}, function(data)
		{
			Lociloci.wrappedResponse(data, onSuccess, onError);
		});
	}
	else
	{
		AJAX.get("ajaxUser!updateUserInfo.action", {adress:Encoder.encode(adress), postnummer:Encoder.encode(postalCode), postOrt:Encoder.encode(city)}, function(data)
		{
			Lociloci.wrappedResponse(data, onSuccess, onError);
		});	
	}
};


Lociloci.getLandmark = function(lon, lat, onSuccess, onError)
{
	AJAX.get("ajaxAdvancedLandmark!getLandmarks.action", {long:lon, lat:lat}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError, function(response, onSuccess, collection_arr) 
		{
			Lociloci.parseAdvancedLandmarks(response, collection_arr);
		});
	});
}

Lociloci.getWelcomeMail = function(onSuccess, onError)
{
	AJAX.get("ajaxUser!sendWelcomeMail.action", {}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError);
	});
};


Lociloci.getActiveProducts = function(onSuccess, onError)
{
	AJAX.get("ajaxUser!getActivePaymentModels.action", {}, function(data)
	{
		Lociloci.wrappedResponse(data, onSuccess, onError, function(response, onSuccess, collection_arr) 
		{
			for(var i = 0; i < collection_arr.length; i++)
			{
				if(collection_arr[i].nodeName == "products")
				{
					collection2_arr = collection_arr[i].childNodes;
					for(var j = 0; j < collection_arr.length; j++)
					{
						if(collection2_arr[j] && collection2_arr[j].nodeName == "model") 
						{							
							response.paymentModel = new BillingModel(collection2_arr[j])							
						}
					}					
				}	
				if(collection_arr[i].nodeName == "payments")
				{
					response.paymentHistory_arr = new Array();
					
					collection2_arr = collection_arr[i].childNodes;				
					for(var j = 0; j < collection2_arr.length; j++)
					{
						if(collection2_arr[j] && collection2_arr[j].nodeName == "payment") 
						{													
							response.paymentHistory_arr.push(new PaymentEntry(collection2_arr[j]));									
						}
					}		
				}
				
			}
		});
	});		
}

Lociloci.replaceBillingModel = function(old_id, new_id, freeDays, onSuccess, onError)
{
    AJAX.get("ajaxUser!replaceBillingModel.action", {toReplaceId:old_id, billingModelId:new_id, freeDays:freeDays}, function(data)
    {
        Lociloci.wrappedResponse(data, onSuccess, onError);
    });     
};

Lociloci.initPayexPayment = function(successUrl, failUrl, modelId, description, onSuccess, onError)
{
    AJAX.get("payex!initiateAutoPay.action", {successUrl:Encoder.encode(successUrl), failUrl:Encoder.encode(failUrl), billingModelId:modelId, description:Encoder.encode(description)}, function(data)
    {
        Lociloci.wrappedResponse(data, onSuccess, onError, function(response, onSuccess, collection_arr) 
        {
            for(var i = 0; i < collection_arr.length; i++)
            {
                if(collection_arr[i].nodeName == "redirect")
                {
                    response.redirect = collection_arr[i].getAttribute("redirectUrl");
                }
            }
        });
    });     
};

Lociloci.getKlarnaAddress = function(social_sec_no, onSuccess, onError) {
    AJAX.get("klarna!getAddress.action", {socialSecurityNr:Encoder.encode(social_sec_no)}, function(data) {
        Lociloci.wrappedResponse(data, onSuccess, onError, function(response, onSuccess, collection_arr) {
            response.address = new Object();
            for(var i = 0; i < collection_arr.length; i++) {
				if(collection_arr[i].nodeName == "user") {		
					collection_arr = collection_arr[i].childNodes;
					i = 0;
				}                
                if(collection_arr[i].nodeName == "data") {
                    response.address[collection_arr[i].getAttribute("field")] = collection_arr[i].getAttribute("value");
                }
            }
        });
    });
};

Lociloci.initiateKlarna = function(billingModelId, freeDays, onSuccess, onError) {
    AJAX.get("klarna!reserveAmount.action", {billingModelId:billingModelId, freeDays:freeDays, test:"true", testResult:"1"}, function(data) {
        Lociloci.wrappedResponse(data, onSuccess, onError, function(response, onSuccess, collection_arr) {
            for(var i = 0; i < collection_arr.length; i++) {
				if(collection_arr[i].nodeName == "order") {		
					collection_arr = collection_arr[i].childNodes;
					i = 0;
				}                            
                if(collection_arr[i].nodeName == "data") {
                    response[collection_arr[i].getAttribute("field")] = collection_arr[i].getAttribute("value");
                }            
            }
        });
    });
};

Lociloci.initiateKlarnaFirstLastName = function(billingModelId, freeDays, firstName, lastName, onSuccess, onError) {
    AJAX.get("klarna!reserveAmount.action", {billingModelId:billingModelId, freeDays:freeDays, firstName:firstName, lastName:lastName, test:"false", testResult:"1"}, function(data) {
        Lociloci.wrappedResponse(data, onSuccess, onError, function(response, onSuccess, collection_arr) {
            for(var i = 0; i < collection_arr.length; i++) {
				if(collection_arr[i].nodeName == "order") {		
					collection_arr = collection_arr[i].childNodes;
					i = 0;
				}                            
                if(collection_arr[i].nodeName == "data") {
                    response[collection_arr[i].getAttribute("field")] = collection_arr[i].getAttribute("value");
                }            
            }
        });
    });
};

Lociloci.unregister = function(onSuccess, onError) {
	AJAX.get("ajaxUser!removeUser.action", {}, function(data) {
		Lociloci.wrappedResponse(data, onSuccess, onError);	
	});		
};


Lociloci.getDistance = function(lon1, lat1, lon2, lat2) 
{
	if(lon1 && lat1 && lon2 && lat2) 
	{	
		var R = 6371; // km
		var dLat = (lat2-lat1) * Math.PI / 180;
		var dLon = (lon2-lon1) * Math.PI / 180; 
		var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) * Math.sin(dLon/2) * Math.sin(dLon/2); 
		var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
		var d = R * c * 1000;		
	
		return parseInt(d);
	}
	else
	{
		return 0;	
	}
};

//Entities
function Invite(node_xml) 
{
	if(node_xml) 
	{
		this.name = node_xml.getAttribute("name");
		this.msisdn = node_xml.getAttribute("msisdn");
		this.email = node_xml.getAttribute("email");
		this.facebookid = node_xml.getAttribute("facebookid");		
		this.imagelocationlink = node_xml.getAttribute("imagelocationlink");
		this.imageid = node_xml.getAttribute("imageid");		
		this.retries = node_xml.getAttribute("retries");				
	}
}

function FriendRequest(node_xml) 
{
	if(node_xml) 
	{
		this.name = node_xml.getAttribute("requestorname");
		this.userId = node_xml.getAttribute("requestoruserid");
		this.msisdn = node_xml.getAttribute("requestormsisdn");
		this.email = node_xml.getAttribute("requestoremail");		
		this.imageid = node_xml.getAttribute("requestorimageid");
		
		
		this.targetname = node_xml.getAttribute("targetname");		
		this.targetemail = node_xml.getAttribute("targetemail");						
		this.targetmsisdn = node_xml.getAttribute("targetmsisdn");				
		this.targetimageid = node_xml.getAttribute("targetimageid");
	}
}

function AdvancedLandmark(node_xml)
{
	if(node_xml) 
	{
		this.id = node_xml.getAttribute("id");
		this.name = node_xml.getAttribute("name");
		this.parentid = node_xml.getAttribute("parentid");
		this.level = node_xml.getAttribute("level");
		this.leaf = node_xml.getAttribute("leaf") == true;
		this.topleft_long = parseFloat(node_xml.getAttribute("topleft_long"));
		this.topleft_lat = parseFloat(node_xml.getAttribute("topleft_lat"));
		this.downright_long = parseFloat(node_xml.getAttribute("downright_long"));
		this.downright_lat = parseFloat(node_xml.getAttribute("downright_lat"));
		this.size = parseInt(node_xml.getAttribute("size"));
	}
}
			
function Device(node_xml) 
{
	if(node_xml) 
	{
		this.id = node_xml.getAttribute("id");
		this.userId = node_xml.getAttribute("user_id");
		this.typeId = node_xml.getAttribute("device_type_id");
		this.battery = node_xml.getAttribute("battery");
		this.lastPositioningTime = parseInt(node_xml.getAttribute("last_positioning_time"));
		this.name = node_xml.getAttribute("name");
		this.longitude = node_xml.getAttribute("longitude");
		this.latitude = node_xml.getAttribute("latitude");
		this.landmark = node_xml.getAttribute("landmark");
		this.active = node_xml.getAttribute("active");
		this.image = node_xml.getAttribute("image");
		this.velocity = node_xml.getAttribute("velocity");
		this.positionUpdateInterval = node_xml.getAttribute("position_update_interval");
		this.positionUpdateCount = node_xml.getAttribute("position_update_count");		
		this.msisdn = node_xml.getAttribute("msisdn");
		this.status = node_xml.getAttribute("status");		
		this.positionCount = node_xml.getAttribute("nb_positions");
		this.totalDistance = node_xml.getAttribute("total_distance");				
		this.batteryTimestamp = parseInt(node_xml.getAttribute("batteryStatusUpdateTime"));								
		this.oldPositioningTime = parseInt(node_xml.getAttribute("old_positioning_time"));		
		this.oldLongitude = node_xml.getAttribute("old_longitude");
		this.oldLatitude = node_xml.getAttribute("old_latitude");	
		this.latestGSMPosition = parseInt(node_xml.getAttribute("latest_gsm_position"));					
	}
	
	this.landmarks = new Array();
	this.friends = new Array();		
}


function User(node_xml) 
{
	if(node_xml) 
	{
		this.id = node_xml.getAttribute("userId");
		this.name = node_xml.getAttribute("name");
		this.msisdn = node_xml.getAttribute("msisdn");
		this.hiding = node_xml.getAttribute("hiding");
		this.description = node_xml.getAttribute("description");
		this.imageid = node_xml.getAttribute("imageid");	
		this.fromtime = node_xml.getAttribute("fromtime");			
		this.totime = node_xml.getAttribute("totime");
		this.email = node_xml.getAttribute("email");	
		this.safetypackage = node_xml.getAttribute("safetypackage");	
		this.freepositions = node_xml.getAttribute("freepositions");	
		this.hasfree = node_xml.getAttribute("hasfree");
		this.hasrss = node_xml.getAttribute("hasrss");
		this.hasloki = node_xml.getAttribute("hasloki");
		this.ignoreloki = node_xml.getAttribute("ignoreloki");	
		this.friendsCount = node_xml.getAttribute("friends");
		this.adminlevel = node_xml.getAttribute("adminlevel");		
		
		if(node_xml.getAttribute("longitude"))
		{
			this.longitude = node_xml.getAttribute("longitude");				
			this.latitude = node_xml.getAttribute("latitude");				
			this.accuracy = node_xml.getAttribute("accuracy");				
			this.lastpositioning = node_xml.getAttribute("lastpositioning");				
			this.lastattempt = node_xml.getAttribute("lastattempt");				
			this.landmark = node_xml.getAttribute("landmark");
		}
		if(this.hiding == "false")
		{
			var collection_arr = node_xml.childNodes;
			for(var i = 0; i < collection_arr.length; i++) 
			{
				if(collection_arr[i].nodeName == "position") 			
				{
					this.longitude = collection_arr[i].getAttribute("longitude");				
					this.latitude = collection_arr[i].getAttribute("latitude");				
					this.accuracy = collection_arr[i].getAttribute("accuracy");				
					this.lastpositioning = collection_arr[i].getAttribute("lastpositioning");				
					this.lastattempt = collection_arr[i].getAttribute("lastattempt");				
					this.landmark = collection_arr[i].getAttribute("landmark");								
				}
				if(collection_arr[i].nodeName == "goodlandmark")
				{
					this.goodLandmark = collection_arr[i].getAttribute("name");	
					this.goodLandmarkDistance = collection_arr[i].getAttribute("distance");						
				}
			}
		}
		
		this.pro = this.hasfree != "false";
	}
	this.friends = new Object();
	this.friends_arr = new Array();	
	this.friends_friends = new Object();
	this.connections_arr = new Array();
	this.connectionFriends = new Object();
	this.landmarks = new Array();
	
	if(this.longitude && this.longitude != 0.0)
	{
		this.time =  parseInt(this.lastpositioning / 100) / 10;
	}	
}


User.prototype.swapVisibillity = function(onSuccess) 
{
	if(this.hiding == "false") 
	{
		this.hiding = "true";
		AJAX.get("ajaxUser!hide.action", {}, onSuccess);
	}
	else 
	{
		this.hiding = "false";
		AJAX.get("ajaxUser!unhide.action", {}, onSuccess);
	}	
}


function Entry(node_xml) 
{
	
	if(node_xml) 
	{
		this.positionerId = node_xml.getAttribute("positionerId");
		this.positionerName = node_xml.getAttribute("positionerName");
		this.created = node_xml.getAttribute("created");		
		this.longitude = parseFloat(node_xml.getAttribute("longitude"));
		this.latitude = parseFloat(node_xml.getAttribute("latitude"));		
		this.reason = node_xml.getAttribute("reason");				
		this.landmark = node_xml.getAttribute("landmark");						
	}
}

function BillingModel(node_xml) 
{
	if(node_xml) 
	{
		this.activated = node_xml.getAttribute("activated");
		this.payment_type = node_xml.getAttribute("payment_type");
		this.billing_model_id = node_xml.getAttribute("billing_model_id");
		this.user_product_id = node_xml.getAttribute("user_product_id");
		this.cost_per_costinterval = node_xml.getAttribute("cost_per_costinterval");
		this.costinterval_daymonth = node_xml.getAttribute("costinterval_daymonth");
		this.costinterval_units = node_xml.getAttribute("costinterval_units");
		this.paymentinterval_daymonth = node_xml.getAttribute("paymentinterval_daymonth");
		this.paymentinterval_units = node_xml.getAttribute("paymentinterval_units");
		this.next_billing = node_xml.getAttribute("next_billing");
	}		
}

function PaymentEntry(node_xml) 
{
	if(node_xml) 
	{
		this.cost = node_xml.getAttribute("cost");
		this.status = node_xml.getAttribute("status");
		this.type = node_xml.getAttribute("type");
		this.date = node_xml.getAttribute("date");
	}		
}
