// AAA Action tags

// global variables and accessors for the OO look and feel...

var _salesAmount;         //  the amount associated with the sales transaction
var _salesType;           //  the sales type (i.e. AIR, CAR, HOTEL, MEMBERSHIP)
var _protocol;            //  the protocol being used, http or https
var _serverAndPath;       //  the server and path that we are logging SEMActions to

function setSalesAmount(salesAmount) {
	_salesAmount = salesAmount;
}

function getSalesAmount() {
	return (_salesAmount == null ? "" : _salesAmount);
}

function setSalesType(salesType) {
	_salesType = salesType;
}

function getSalesType() {
	return (_salesType == null ? "" : _salesType);
}

function setProtocol(protocol) {
	_protocol = protocol;
}

function getProtocol() {
	return _protocol;
}

function setServerAndPath(serverAndPath) {
	_serverAndPath = serverAndPath;
}

function getServerAndPath() {
	return _serverAndPath;
}

function setURLBase(protocol, serverAndPath) {
    setProtocol(protocol);
    setServerAndPath(serverAndPath);
}

//  computational getter
function getURLBase() {
	return _protocol+"://"+_serverAndPath;
}

//  Setup the above for use by clubs - they will not be setting 
//  the server and path so we do it here,
//  a test server that does not exist
//setServerAndPath("theserver/action");
//  developers test
//setServerAndPath("sflatley/action");
// production
setServerAndPath("switch.atdmt.com/action");

//  set the protocol to the one being used
setProtocol("http"+(window.location.protocol.indexOf('https')==0?'s':''));
		
// private function logPage(p_action)
// private function logPage(p_action, p_clubId, p_customerId, p_type, p_salesAmount, p_quantity)
// Logs a page for the p_action and the other parameters passed.
function logPage() {

	switch(logPage.arguments.length) {
	
		case 1:
			var _url = buildURL(logPage.arguments[0]);
			requestImage(_url);
			return _url;
	
		case 6:
			var _url = buildURL(logPage.arguments[0], logPage.arguments[1], 
							logPage.arguments[2], logPage.arguments[3], 
							logPage.arguments[4], logPage.arguments[5]);
			requestImage(_url);
			setSalesAmount(logPage.arguments[5]);
			setSalesType(logPage.arguments[3]);
			return _url;
	
		default: 
			return "Error: logPage() - invalid number of arguments";
	}
}

//  private function getExtendedDataPath()
//  Define and return the extended data path 
function getExtendedDataPath() {
	return "/v3/ato.ClubID/atm1.Quantity/atm2.Sales/atc1.Type/atc2.CustomerID";
}

//  private function buildURL(p_action)
//  private function buildURL(p_action, p_clubId, p_customerId, p_type, p_quantity, p_salesAmount)
//
//  builds a URL using getURLBase(), the local variable _extendedData and the parameters passed

function buildURL() {

	switch (buildURL.arguments.length) {
	
		case 1:
			//  Note that we need so add the slash.
			//return  getURLBase()+ "/" + buildURL.arguments[0];
            return getURLBase()+"/"+buildURL.arguments[0];
            
		case 6:

			var	_extendedData = getExtendedDataPath();
	
			//  replace clubID, CustomerID, Type, Quantity and Sales literals with parameter values
			_extendedData = _extendedData.replace("ClubID", buildURL.arguments[1]);
			_extendedData = _extendedData.replace("CustomerID", handleCharString(buildURL.arguments[2]));
			_extendedData = _extendedData.replace("Type", handleCharString(buildURL.arguments[3]));
			_extendedData = _extendedData.replace("Quantity", handleNumeric(buildURL.arguments[4]));
			_extendedData = _extendedData.replace("Sales", handleNumeric(buildURL.arguments[5]));

			//  combine the base URL with the action and extended data and return it
			return getURLBase() + "/" + buildURL.arguments[0] + _extendedData;

		default: 
			return "Error:  buildURL()- invalid number of arguments";
	}
}

//  changes parameter to "0" if empty
function handleNumeric(p_numeric) {
	if (p_numeric == "") {
		return "0";	
	}
	else {
		return p_numeric;
	}
}

//  changes parameter to "NULL" if empty
function handleCharString(p_charstring) {
	if (p_charstring == "") {
		return "NULL";	
	}
	else {
		return p_charstring;
	}
}

//  private function requestImage(p_URL)
//    Write an image located at p_URL to memory.  This event is logged
//    at the p_URL's web server and should result in a http 404 error.
function requestImage(p_URL) {
    var _imgarray = new Array; 
	var _imagePointer = 0;
	_imgarray[_imagePointer] = new Image;
	_imgarray[_imagePointer].src = p_URL;
}

//  public function logHomePage()
function logHomePage() {
	return logPage("AAA_LP_Home_Page");
}

//  public function logMaps(p_clubId)
//  public function logMaps(p_clubId, p_customerId)
//      Logs the action AAA_LP_Maps_ED for the p_clubId and optional p_customerID
function logMaps() {
	switch (logMaps.arguments.length) {
		case 1:
			return logPage("AAA_LP_Maps_ED", 
					logMaps.arguments[0], "NULL", "NULL", "0", "0");
		case 2:
			return logPage("AAA_LP_Maps_ED", 
					logMaps.arguments[0], logMaps.arguments[1], "NULL", "0", "0");
		default:
			return "Error:  invalid number of arguments";
	}	
}

//  public function logQuickFinder(p_clubId) {
//     Logs the action AAA_LP_QuickFinder_ED for the p_club_id 
function logQuickFinder(p_clubId) {
	return logPage("AAA_LP_QuickFinder_ED",
					 	p_clubId, "NULL", "NULL", "0", "0");
}

//  public function logZipCodeRedirect(p_clubId) {
//     Logs the action AAA_LP_Zip_Code_ReDirect_ED for the p_club_id 
function logZipCodeRedirect(p_clubId) {
    return logPage("AAA_LP_Zip_Code_ReDirect_ED",
						p_clubId, "NULL", "NULL", "0", "0");
}

//  public function logClubVisitorRedirect(p_clubId) {
//     Logs the action AAA_LP_Club_Visitor_ReDirect_ED for the p_club_id 
function logClubVisitorRedirect(p_clubId) {
	return logPage("AAA_LP_Club_Visitor_ReDirect_ED",
					 	p_clubId, "NULL", "NULL", "0", "0");
}

//  public function logMembershipStart(p_clubId) {
//     Logs the action AAA_LP_Membership_Start_ED for the p_club_id 
function logMembershipStart(p_clubId) {
	return logPage("AAA_LP_Membership_Start_ED",
						p_clubId, "NULL", "NULL", "0", "0");
}

// public function logMembershipConfirmation(p_clubId, p_customerId, p_salesAmount) 
// public function logMembershipConfirmation(p_clubId, p_salesAmount) {
//      Logs the action AAA_LP_Membership_Confirmation_ED for the given parameters

function logMembershipConfirmation() {

	switch (logMembershipConfirmation.arguments.length) {
	
		case 2: 
			return logPage("AAA_LP_Membership_Confirmation_ED",
	    		   		logMembershipConfirmation.arguments[0], "NULL", 
	    		   		"NULL",  "0", logMembershipConfirmation.arguments[1]);
		case 3:
			return logPage("AAA_LP_Membership_Confirmation_ED",
	    		    	logMembershipConfirmation.arguments[0], logMembershipConfirmation.arguments[1], 
	    		    	"NULL", "0", logMembershipConfirmation.arguments[2]);
		default:
			return "error: logMembershipConfirmation: invalid number of arguments";
	}
}

// public function logBookingStart(p_clubId) {
//    Logs the action AAA_LP_Booking_Start_ED for the p_clubId
function logBookingStart(p_clubId) {
	return logPage("AAA_LP_Booking_Start_ED", 
						p_clubId, "NULL", "NULL", "0", "0");
}

// public function logBookingConfirmation(p_clubId, p_bookingType, p_salesAmount) 
// public function logBookingConfirmation(p_clubId, p_customerId, p_bookingType, p_salesAmount)
//    Logs the action AAA_LP_Booking_Confirmation_ED for the parameters passed
function logBookingConfirmation() {

	switch(logBookingConfirmation.arguments.length) {
	
		case 3: 
			return logPage("AAA_LP_Booking_Confirmation_ED", 
						logBookingConfirmation.arguments[0], "NULL", 
						logBookingConfirmation.arguments[1], 
						 "0", logBookingConfirmation.arguments[2]); 
		
		case 4:
			return logPage("AAA_LP_Booking_Confirmation_ED", 
						logBookingConfirmation.arguments[0], 
						logBookingConfirmation.arguments[1], 
						logBookingConfirmation.arguments[2], 
						 "0", logBookingConfirmation.arguments[3]);
		
		default:
			return "error: logBookingConfirmation: invalid number of arguments";		
	}
}

//  logBookingConfirmation convenience methods - one method for each booking type

// public function logBookingConfirmationAir(p_clubId, p_salesAmount)
// public function logBookingConfirmationAir(p_clubId, p_customerId, p_salesAmount)

function logBookingConfirmationAir() {

	switch (logBookingConfirmationAir.arguments.length) {
	
		case 2:
			return logBookingConfirmation(logBookingConfirmationAir.arguments[0], 
					"NULL", "Air", 
					logBookingConfirmationAir.arguments[1]);
		case 3:
			return logBookingConfirmation(logBookingConfirmationAir.arguments[0], 
					logBookingConfirmationAir.arguments[1], "Air", 
					logBookingConfirmationAir.arguments[2]);
		default:
			return "error: logBookingConfirmationAir: invalid number of arguments";	
	}
}

// public function logBookingConfirmationCar(p_clubId, p_salesAmount)
// public function logBookingConfirmationCar(p_clubId, p_customerId, p_salesAmount)

function logBookingConfirmationCar() {

	switch (logBookingConfirmationCar.arguments.length) {
	
		case 2:
			return logBookingConfirmation(logBookingConfirmationCar.arguments[0], 
					"NULL", "Car", 
					logBookingConfirmationCar.arguments[1]);
		case 3:
			return logBookingConfirmation(logBookingConfirmationCar.arguments[0], 
					logBookingConfirmationCar.arguments[1], "Car", 
					logBookingConfirmationCar.arguments[2]);
		default:
			return "error: logBookingConfirmationCar: invalid number of arguments";	
	}
}

// public function logBookingConfirmationHotel(p_clubId, p_salesAmount)
// public function logBookingConfirmationHotel(p_clubId, p_customerId, p_salesAmount)

function logBookingConfirmationHotel() {

	switch (logBookingConfirmationHotel.arguments.length) {
	
		case 2:
			return logBookingConfirmation(logBookingConfirmationHotel.arguments[0], 
					"NULL", "Hotel", 
					logBookingConfirmationHotel.arguments[1]);
		case 3:
			return logBookingConfirmation(logBookingConfirmationHotel.arguments[0], 
					logBookingConfirmationHotel.arguments[1], "Hotel", 
					logBookingConfirmationHotel.arguments[2]);
		default:
			return "error: logBookingConfirmationHotel: invalid number of arguments";	
	}
}


// public function logBookingConfirmationCruise(p_clubId, p_salesAmount)
// public function logBookingConfirmationCruise(p_clubId, p_customerId, p_salesAmount)

function logBookingConfirmationCruise() {

	switch (logBookingConfirmationCruise.arguments.length) {
	
		case 2:
			return logBookingConfirmation(logBookingConfirmationCruise.arguments[0], 
					"NULL", "Cruise", 
					logBookingConfirmationCruise.arguments[1]);
		case 3:
			return logBookingConfirmation(logBookingConfirmationCruise.arguments[0], 
					logBookingConfirmationCruise.arguments[1], "Cruise", 
					logBookingConfirmationCruise.arguments[2]);
		default:
			return "error: logBookingConfirmationCruise: invalid number of arguments";	
	}
}

// public function logBookingConfirmationPackage(p_clubId, p_salesAmount)
// public function logBookingConfirmationPackage(p_clubId, p_customerId, p_salesAmount)

function logBookingConfirmationPackage() {

	switch (logBookingConfirmationPackage.arguments.length) {
	
		case 2:
			return logBookingConfirmation(logBookingConfirmationPackage.arguments[0], 
					"NULL", "Package", 
					logBookingConfirmationPackage.arguments[1]);
		case 3:
			return logBookingConfirmation(logBookingConfirmationPackage.arguments[0], 
					logBookingConfirmationPackage.arguments[1], "Package", 
					logBookingConfirmationPackage.arguments[2]);
		default:
			return "error: logBookingConfirmationPackage: invalid number of arguments";	
	}
}

// -- end of booking confirmation convenience methods

//  public function logInsuranceStart(p_clubId)
//     Logs the action AAA_LP_Insurance_Start_ED for the p_clubId.
function logInsuranceStart(p_clubId) {
	return logPage("AAA_LP_Insurance_Start_ED",
						p_clubId, "NULL", "NULL", "0", "0");
}

//  public function logInsuranceConfirmation(p_clubId, p_insuranceType)
//  public function logInsuranceConfirmation(p_clubId, p_customerId, p_insuranceType)
//     Logs the action AAA_LP_Insurance_Confirmation_ED for the parameters passed.

function logInsuranceConfirmation() {
	switch (logInsuranceConfirmation.arguments.length) {
	
		case 2:
			return logPage("AAA_LP_Insurance_Confirmation_ED",
	       			 	logInsuranceConfirmation.arguments[0], "NULL", 
	       			 	logInsuranceConfirmation.arguments[1], "0", "0");
		case 3:
			return logPage("AAA_LP_Insurance_Confirmation_ED",
	       			 	logInsuranceConfirmation.arguments[0], 
	       			 	logInsuranceConfirmation.arguments[1], 
	       			 	logInsuranceConfirmation.arguments[2], "0", "0");
		default:
			return "error: logInsuranceConfirmation: invalid number of arguments";	
	}
}