function TestWSError(method, obj, name) {
	var error = null;

	if (method == 'wsfunction') {
		if (!obj[0] || !obj[0].RESULT) { return false; }
		if (obj[0].RESULT) { error = obj[0].RESULT; }
	}
	if (method == 'availableForRead') {
		if (!obj[0] || !obj[0].RESULT) { return false; }
		if (obj[0].RESULT < 0) { error = obj[0].RESULT; }
	}
	if (method == 'myLocation') {
		if (!obj[0] || !obj[0].RESULT) { return false; }
		if (obj[0].RESULT = -3) { return false; }
		if (obj[0].RESULT < 0) { error = obj[0].RESULT; }
	}
	if (method == 'imagesNew') { if (obj < 0) { error = obj; } }
	if (method == 'locationCheckInOut') { if (obj < 0) { error = obj; } }
	if (method == 'locationCheckInUpdate') { if (obj < 0) { error = obj; } }
	if (method == 'interestSetupNew') { if (obj < 0) { error = obj; } }
	if (method == 'interestSetupEdit') { if (obj < 0) { error = obj; } }
	if (method == 'interestSetupRead') { if (obj < 0) { error = obj; } }
	if (method == 'interestAssignmentNew') { if (obj < 0) { error = obj; } }
	if (method == 'interestAssignmentEdit') { if (obj < 0) { error = obj; } }
	if (method == 'wallNew') { if (obj < 0) { error = obj; } if (error == -4) { error = false; ShowDialogAlert('You have already liked this Item'); } }
	if (method == 'wallEdit') { if (obj < 0) { error = obj; } }

	if (error) { ShowDialogAlert(name + ' returned: ' + error); }
	return error ? true : false;
}


function WSCommonWSF(psFunction, psParam) {
	var result = false;
	var method = 'wsfunction';
	SmartWSCall(
		method + '&psFunction=' + psFunction,
		{
			'psFunction': psFunction,
			'psAccessCode': '',
			'psParam': psParam
		},
		function (data) {
			if (data.error) { return ShowDialogAlert(data.error); } // wrapper error
			if (data[method + 'Result']) {
				var obj = jQuery.parseJSON(data[method + 'Result']);
				var err = TestWSError(method, obj, psFunction);
				if (!err) { result = obj; }
			}
		}
	);
	return result;
}

function WSCommonMethod(method, params) {
	var result = false;
	SmartWSCall(
		method,
		params,
		function (data) {
			if (data.error) { return ShowDialogAlert(data.error); } // wrapper error
			if (data[method + 'Result']) {
				var obj = jQuery.parseJSON(data[method + 'Result']);
				var err = TestWSError(method, obj, method);
				if (!err) { result = obj; }
			}
		}
	);
	return result;
}


function WSPortInfoByAccId(pnAccID) {
	var acc = pnAccID ? pnAccID : accId;
	return WSCommonWSF('sm_Port_Info_By_Acc_Id', [acc]);
}

function WSGetItinerary(pnAccID) {
	var acc = pnAccID ? pnAccID : accId;
	return WSCommonWSF('sm_Get_Itinerary', [acc, acc, acc, acc, acc]);
}

function WSGetItinerary2(pnAccID) {
	var acc = pnAccID ? pnAccID : accId;
	return WSCommonWSF('sm_Get_Itinerary', [acc, acc, acc, acc, acc]);
}

function WSRestaurantList() {
	return WSCommonWSF('sm_Restaurant_List', [shipId]);
}

function WSBarList() {
	return WSCommonWSF('sm_Bar_List', [shipId]);
}

function WSVHomeScreen(pnAccID) {
	var acc = pnAccID ? pnAccID : accId;
	return WSCommonWSF('sm_V_Home_Screen', [acc]);
}

function WSVHomeScreen2(pnAccID) {
	var acc = pnAccID ? pnAccID : accId;
	return WSCommonWSF('sm_V_Home_Screen2', [acc, acc, acc, acc]);
}

function WSProfileImage(login) {
	return WSCommonWSF('sm_Profile_Image', [login]);
}

function WSLocationImages(locationCode) {
	return WSCommonWSF('sm_Location_Images', [locationCode, shipId]);
}

function WSLocationCheckinAvailable(pnAccID) {
	var acc = pnAccID ? pnAccID : accId;
	return WSCommonWSF('sm_Location_Checkin_Available', [shipId, acc]);
}

function WSGuestDetailsPrivacySettings(pnContactID) {
	var acc = pnContactID ? pnContactID : accId;
	return WSCommonWSF('sm_GuestDetails_PrivacySettings', [acc, accId, acc, accId, acc]);
}

function WSMyProfile(pnAccID) {
	var acc = pnAccID ? pnAccID : accId;
	return WSCommonWSF('sm_My_Profile', [acc, acc, acc, acc]);
}

function WSGuestProfile(pnAccID) {
	var acc = pnAccID ? pnAccID : accId;
	return WSCommonWSF('sm_Guest_Profile', [acc, acc, acc, acc, acc, acc, acc, accId, accId]);
}

function WSFriendsLocation(pnAccID) {
	var acc = pnAccID ? pnAccID : accId;
	return WSCommonWSF('sm_Friends_Location', [acc]);
//	return WSCommonWSF('sm_Friends_Location', [acc, acc, acc, acc, acc]);
}

function WSDeckplanDecklist(pnAccID) {
	var acc = pnAccID ? pnAccID : accId;
	return WSCommonWSF('sm_Deckplan_Decklist', [acc]);
}

function WSDeckplanLocations() {
	return WSCommonWSF('sm_Deck_Plan_Locations', [shipId]);
}

function WSInterest(pnAccID) {
	var acc = pnAccID ? pnAccID : accId;
	return WSCommonWSF('sm_Interest', [acc, acc]);
}

function WSWall(pnAccID) {
	var acc = pnAccID ? pnAccID : accId;
	return WSCommonWSF('sm_Wall', [acc]);
//	return WSCommonWSF('sm_Wall', [acc, acc, acc, acc, acc]);
}

function WSWallComment(wall_id) {
	return WSCommonWSF('sm_Wall_Comment', [wall_id]);
}

function WSWallTypeComment(object_type, object_id, pnAccID) {
	var acc = pnAccID ? pnAccID : accId;
	return WSCommonWSF('sm_Wall_Type_Comment', [object_type, object_id, acc]);
}

function WSPortDetails(date, pnAccID, type) {
	var acc = pnAccID ? pnAccID : accId;
	return WSCommonWSF('sm_Port_Details', [date, acc, type]);
}

function WSInterestDetails(int_setup_id, pnAccID) {
	var acc = pnAccID ? pnAccID : accId;
	return WSCommonWSF('sm_Interest_Details', [acc, int_setup_id, acc, acc, acc, acc, int_setup_id, acc]);
}

function WSFriendsNews(pnAccID, date) {
	var acc = pnAccID;
	return WSCommonWSF('sm_Friends_News', [acc, date]);
}

function WSPositionDisplay(position) {
	return WSCommonWSF('sm_Position_Display', [position]);
}

function WSLocationOpeningHours(app_id) {
	return WSCommonWSF('sm_Location_Opening_Hours', [app_id, shipId]);
}

function WSLocation(location_id, pnAccID, location_art) {
	var acc = pnAccID ? pnAccID : accId;
	return WSCommonWSF('sm_Location', [location_id, acc, location_art]);
}

function WSPhotoAlbum(pnAccID, owner) {
	var acc = pnAccID ? pnAccID : accId;
	return WSCommonWSF('sm_Photo_Album', [acc, owner]);
}

function WSPhotoAlbumDetails(pnAccID, EVENTTARGETID, EVENTTARGETTYPE) {
	var acc = pnAccID ? pnAccID : accId;
	return WSCommonWSF('sm_Photo_Album_Details', [acc, EVENTTARGETID, EVENTTARGETTYPE]);
}



function WSAvailableForRead(availableDateTime, availableInfo) {
	return WSCommonMethod('availableForRead', {
		'loginName': accLogin.toLowerCase(),
		'password': hex_md5(accPass.toLowerCase()),
		'shipid': shipId,
		'availableDateTime': availableDateTime,
		'availableInfo': availableInfo			
	});
}

function WSImagesNew(imageCode, imageType, imageURL, imageDescription, sortOrder, imageSource, imageLoginId, imageAppSetup, screenID, linkTableColumn, recordId, imageLinkComment, fileData, albumType) {
	var result = WSCommonMethod('imagesNew', {
		'loginName': accLogin.toLowerCase(),
		'passWord': hex_md5(accPass.toLowerCase()),
		'shipID': shipId,
		'imageCode': imageCode,
		'imageType': imageType,
		'imageURL': imageURL,
		'imageDescription': imageDescription,
		'sortOrder': sortOrder,
		'imageSource': imageSource,
		'imageLoginId': imageLoginId,
		'imageAppSetup': imageAppSetup,
		'screenID': screenID,
		'linkTableColumn': linkTableColumn,
		'recordId': recordId,
		'imageLinkComment': imageLinkComment,
		'fileData': fileData,
		'albumType': albumType
	});
	/*
	if (typeof result == 'string') {
		$.ajax({
			type: 'POST',
			url: 'services/notify.php',
			data: { 'image_url' : result },
			success: function(data) { if (data) { alert(data); } }
		});
	}*/
	return result;
}

function WSMyLocation() {
	return WSCommonMethod('myLocation', {
		'loginName': accLogin.toLowerCase(),
		'password': hex_md5(accPass.toLowerCase()),
		'shipid': shipId
	});
}

function WSLocationCheckInOut(chkStatus, location, comment) {
	return WSCommonMethod('locationCheckInOut', {
		'loginName': accLogin.toLowerCase(),
		'passWord': hex_md5(accPass.toLowerCase()),
		'shipID': shipId,
		'chkStatus': chkStatus,
		'location': location,
		'comment': comment
	});
}

function WSLocationCheckInUpdate(chkID, chkCol, chkValue) {
	return WSCommonMethod('locationCheckInUpdate', {
		'loginName': accLogin.toLowerCase(),
		'passWord': hex_md5(accPass.toLowerCase()),
		'shipID': shipId,
		'chkID': chkID,
		'chkCol': chkCol,
		'chkValue': chkValue
	});
}

function WSInterestSetupNew(interestSetupGroup, interestSetupType, interestSetupName, interestSetupCode, interestSetupDescription, interestSetupImageURL, interestSetupDefaultGroup, interestAssignment) {
	return WSCommonMethod('interestSetupNew', {
		'loginName': accLogin.toLowerCase(),
		'passWord': hex_md5(accPass.toLowerCase()),
		'shipID': shipId,
//		'interestSetupGroup': interestSetupGroup,
		'interestSetupType': interestSetupType,
		'interestSetupName': interestSetupName,
//		'interestSetupCode': interestSetupCode,
		'interestSetupDescription': interestSetupDescription,
		'interestSetupImageURL': interestSetupImageURL,
		'interestSetupDefaultGroup': interestSetupDefaultGroup,
		'interestAssignment': interestAssignment
	});
}

function WSInterestSetupEdit(interestSetupId, interestSetupGroup, interestSetupType, interestSetupName, interestSetupCode, interestSetupDescription, interestSetupImageUrl) {
	return WSCommonMethod('interestSetupEdit', {
		'LoginName': accLogin.toLowerCase(),
		'passWord': hex_md5(accPass.toLowerCase()),
		'shipID': shipId,
		'interestSetupId': interestSetupId,
//		'interestSetupGroup': interestSetupGroup,
		'interestSetupType': interestSetupType,
		'interestSetupName': interestSetupName,
//		'interestSetupCode': interestSetupCode,
		'interestSetupDescription': interestSetupDescription,
		'interestSetupImageUrl': interestSetupImageUrl
	});
}


function WSInterestSetupRead(setupGroup, setupType) {
	return WSCommonMethod('interestSetupRead', {
		'loginName': accLogin.toLowerCase(),
		'passWord': hex_md5(accPass.toLowerCase()),
		'shipID': shipId,
		'setupGroup': setupGroup,
		'setupType': setupType
	});
}

function WSInterestAssignmentNew(interestGroup, interestType, customGroup, interestSetupId) {
	return WSCommonMethod('interestAssignmentNew', {
		'loginName': accLogin.toLowerCase(),
		'passWord': hex_md5(accPass.toLowerCase()),
		'shipID': shipId,
//		'interestGroup': interestGroup,
		'interestType': interestType,
		'customGroup': customGroup,
		'interestSetupId': interestSetupId
	});
}


function WSInterestAssignmentEdit(interestId, interestCol, interestValue) {
	return WSCommonMethod('interestAssignmentEdit', {
		'loginName': accLogin.toLowerCase(),
		'passWord': hex_md5(accPass.toLowerCase()),
		'shipID': shipId,
		'interestId': interestId,
		'interestCol': interestCol,
		'interestValue': interestValue
	});
}

function WSWallNew(recordID, comment, likeIt, imageUrl, likeType, likeName, commentText, ownerName) {
	return WSCommonMethod('wallNew', {
		'loginName': accLogin.toLowerCase(),
		'passWord': hex_md5(accPass.toLowerCase()),
		'shipID': shipId,
		'recordID': recordID,
		'comment': comment,
		'likeIt': likeIt,
		'imageUrl': imageUrl,
		'likeType': likeType,
		'likeName': likeName,
		'commentText': commentText,
		'ownerName': ownerName
	});
}

function WSWallEdit(wallID, tableColumn, recordID, comment, likeIt, imageID, likeType, likeName, commentText) {
	return WSCommonMethod('wallEdit', {
		'loginName': accLogin.toLowerCase(),
		'passWord': hex_md5(accPass.toLowerCase()),
		'shipID': shipId,
		'wallID': wallID,
		'tableColumn': tableColumn,
		'recordID': recordID,
		'comment': comment,
		'likeIt': likeIt,
		'imageID': imageID,
		'likeType': likeType,
		'likeName': likeName,
		'commentText': commentText
	});
}






