﻿
// Used to contact the webservice
var xmlhttp = createRequestObject();
// debug mode
var debugMode = 0;
// URL RB. Dont lower case the url here!
var URL = window.location.toString();

function RecordAgentActivity(aat, sActivity, sReference)
{
    // This is no longer required
    var notSupported = 1;
}

// aat = Created or SiteSpecific or ClientSide or PageView or NewsView or EventView or CompetitionView or CompetitionEntry or UserSignUp or UserEditProfile or Login
function RecordAgentActivity2(aat, sActivity, sReference)
{
    var xmlToSend = "<?xml version='1.0' encoding='utf-8'?>"
    xmlToSend += "<soap12:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap12='http://www.w3.org/2003/05/soap-envelope'>"
    xmlToSend += "<soap12:Body>"
    xmlToSend += "<RecordAgentActivity xmlns='http://tempuri.org/'>"
    xmlToSend += "<aat>" + aat + "</aat>"
    xmlToSend += "<sActivity>" + sActivity + "</sActivity>"
    xmlToSend += "<sReference>" + sReference + "</sReference>"
    xmlToSend += "</RecordAgentActivity>"
    xmlToSend += "</soap12:Body>"
    xmlToSend += "</soap12:Envelope>"

    var xmldoc = getXML(xmlToSend);
    xmlhttp.onreadystatechange = state_Change;
    xmlhttp.open("POST", "/webservice/Referrers.asmx", false);
    xmlhttp.setRequestHeader ("SOAPAction", "http://tempuri.org/RecordAgentActivity");
    xmlhttp.setRequestHeader ("Content-Type", "text/xml");
    xmlhttp.send(xmldoc);
}

// Get the xml generation object
function getXML(xmlToSend)
{
    try //Internet Explorer
    {
        xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async="false";
        xmlDoc.loadXML(xmlToSend);
        return xmlDoc;
    }
    catch(e)
    {
        // Everything else
        parser=new DOMParser();
        xmlDoc=parser.parseFromString(xmlToSend,"text/xml");
        return xmlDoc;
    }
}

// Handle state changes
function state_Change()
{
    // if xmlhttp shows "loaded"
    if (xmlhttp.readyState==4)
    {
        // if "OK"
        if (xmlhttp.status==200)
        {
            if (debugMode == 1) alert("UJT - Called OKAY");
        }
        else
        {
            if (debugMode == 1) alert("UJT - Problem retrieving XML data");
        }
    }
} 

// Create xml connection object
function createRequestObject() {        
    if (window.XMLHttpRequest) {                
        // Everything else
        return xmlhttprequest = new XMLHttpRequest();         
    }       
    else if (window.ActiveXObject)       
    {              
        // Micro$oft 
        return xmlhttprequest = new ActiveXObject("Microsoft.XMLHTTP");       
    } 
}

// Query string replace
if (!document.location.search == '') {
    if (debugMode == 1) alert("Replacing query string");
    if (debugMode == 1) alert(document.location.search);
    URL = URL.replace(document.location.search, '');
}

// Get the filename only
if (debugMode == 1) alert("URL=" + URL);
URL = file_name_only(URL, 1);
if (debugMode == 1) alert("FILE=" + URL);
URL = URL.toLowerCase();

// Get a filename off a URL
function file_name_only(str, bExtention) {
    var slash = '/';
    if (str.match(/\\/)) {
        // online offline path matching
        slash = '\\';
    }
    if (bExtention == 1) {
        //Extention
        return str.substring(str.lastIndexOf(slash) + 1, str.length);
    } else {
        //No Extention
        return str.substring(str.lastIndexOf(slash) + 1, str.lastIndexOf('.'));
    }
}

switch (URL) {
    case 'airhotelavailability.aspx':
        // Air Hotel Availability
        RecordAgentActivity2("SiteSpecific","MARS SEARCH RESULTS",URL);
        break;
    case 'hotelextras.aspx':
        // Hotel Extras
        RecordAgentActivity2("SiteSpecific","HOTEL EXTRAS",URL);
        break;
    case 'passengersdetails.aspx':
        // Passenger Details
        RecordAgentActivity2("SiteSpecific","PASSENGER DETAILS",URL);
        break;
    case 'insurance.aspx':
        // Insurance
        RecordAgentActivity2("SiteSpecific","INSURANCE DETAILS",URL);
        break;
    case 'productavailability.aspx':
        // Product Availability
        RecordAgentActivity2("SiteSpecific","PRODUCT AVAILABILITY",URL);
        break;
    case 'specialrequirements.aspx':
        // Special Requirements
        RecordAgentActivity2("SiteSpecific","SPECIAL REQUESTS",URL);
        break;
    case 'customerdetails2.aspx':
        // Customer Details
        RecordAgentActivity2("SiteSpecific","CUSTOMER DETAILS",URL);
        break;
    case 'termsandconditions.aspx':
        // Terms and conditions
        RecordAgentActivity2("SiteSpecific","TERMS AND CONDITIONS",URL);
        break;
    case 'tourpackages2.aspx':
        RecordAgentActivity2("SiteSpecific","COACH SEARCH RESULTS",URL);
        break;
    case 'skipacks.aspx':
        RecordAgentActivity2("SiteSpecific","SKI PACKS PAGE",URL);
        break;
    case 'searchcriteria.aspx':                                                                                 
        RecordAgentActivity2("SiteSpecific","MARS SEARCH RESULTS",URL);
        break;
    case 'bookingconfirmation.aspx':
        RecordAgentActivity2("SiteSpecific","BOOKING CONFIRMATION PAGE",URL);
        break;
    case 'default.aspx':
        RecordAgentActivity2("SiteSpecific","CUSTOMER LOGIN PAGE",URL);
        break;
    case 'flydrivepaxdetails.aspx':
        RecordAgentActivity2("SiteSpecific","FLY DRIVE PASSENGER DETAILS",URL);
        break;
    case 'login.aspx':
        RecordAgentActivity2("SiteSpecific","AGENTS LOGIN PAGE",URL);
        break;
    case 'pudopoints.aspx':
        RecordAgentActivity2("SiteSpecific","PICKUP DROPOFF PAGE",URL);
        break;
    default:
        RecordAgentActivity2("SiteSpecific",URL,URL);
        break;
}
