function IncludeJavaScript(jsFile) {
    document.write('<script type="text/javascript" src="'
    + jsFile + '"></scr' + 'ipt>');
}

IncludeJavaScript('http://www.technospeak.co.uk/WebActivityRecorder/js/easyXDM.js');


var REMOTE = "http://www.technospeak.co.uk/WebActivityRecorder";
var serializer = {
    /**
    * Serializes a hashtable and returns it as a string
    * @param {Object} data The data to serialize
    * @returns The serialized string
    * @type {String}
    */
    stringify: function(data) {
        var message = "";
        for (var key in data) {
            if (data.hasOwnProperty(key)) {
                message += key + "=" + escape(data[key]) + "&";
            }
        }
        return message.substring(0, message.length - 1);
    },
    /**
    * Deserializes a string and returns a hashtable
    * @param {String} message The string to deserialize
    * @returns An hashtable populated with key-value pairs
    * @type {Object}
    */
    parse: function(message) {
        var data = {};
        var d = message.split("&");
        var pair, key, value;
        for (var i = 0, len = d.length; i < len; i++) {
            pair = d[i];
            key = pair.substring(0, pair.indexOf("="));
            value = pair.substring(key.length + 1);
            data[key] = unescape(value);
        }
        return data;
    }
};

var channel;

function SendData(params) {
    /**
    * When the window is finished loading start setting up the channel
    */
    channel = new easyXDM.Socket(/** The configuration */{
    /**
    * Register the url to hash.html, this must be an absolute path
    * or a path relative to the root.
    * @field
    */
    local: "../hash.html",
    /**
    * Register the url to the remote interface
    * @field
    */
    remote: REMOTE + "/remotedata.html",
    remoteHelper: REMOTE + "/../name.html",
    onReady: function() {
        /**
        * Send some data to the other side
        */
        channel.postMessage(params);
    }
});
};

//function addLoadEvent(func) {   
//   var oldonload = window.onload;   
//   if (typeof window.onload != 'function') {   
//     window.onload = func;   
//   } else {   
//     window.onload = function() {   
//       if (oldonload) {   
//         oldonload();   
//       }   
//       func();   
//     }   
//   }
//}

//addLoadEvent(waRPV)


function waRPV(waUniqueid) {

    var params = "";

    var visited = new Date();

    var pagepath = window.location.href;
    //    alert(pagepath);
    var siteName = window.location.hostname;
    var uniqueId;
    if (!get_cookie("WebActivityRecorder")) {
        uniqueId = GetUniqueid();
        var current_date = new Date;
        var cookie_year = current_date.getFullYear() + 1;
        var cookie_month = current_date.getMonth();
        var cookie_day = current_date.getDate();
        set_cookie("WebActivityRecorder", uniqueId);
    }
    else {
        uniqueId = get_cookie("WebActivityRecorder");
    }

    if (waUniqueid) {
        if (uniqueId != waUniqueid) {
            delete_cookie("WebActivityRecorder");
            set_cookie("WebActivityRecorder", waUniqueid);
            uniqueId = waUniqueid;
        }

    }


    var docType = captureDocTypeDefinition();



    var outerHTML = document.getElementsByTagName("html")[0].outerHTML;

    //alert(outerHTML);

    if (!outerHTML)
        outerHTML = "<HTML>" + document.getElementsByTagName("html")[0].innerHTML + "</HTML>";

    //alert(outerHTML);
    //    var outerHTML = document.body.parentNode.innerHTML

    params += "site=" + encodeURIComponent(siteName);
    params += "&id=" + encodeURIComponent(uniqueId);
    params += "&visited=" + encodeURIComponent(visited.toUTCString());
    params += "&pagepath=" + encodeURIComponent(pagepath);
    params += "&content=" + encodeURIComponent(docType + outerHTML);

    SendData(params);

}

function GetUniqueid() {
    var uniqueid;
    //    try
    //    {
    //    uniqueid = <%=HttpContext.Current.Request.UserHostAddress.ToString()%>;
    //    }
    //    catch (e)
    //    {
    //    uniqueid = randomID(30);
    //    }
    uniqueid = randomID(10);
    return uniqueid;

}

function set_cookie(name, value) {
    var current_date = new Date;
    var cookie_year = current_date.getFullYear() + 1;
    var cookie_month = current_date.getMonth();
    var cookie_day = current_date.getDate();
    set_cookie(name, value, cookie_year, cookie_month, cookie_day);
}

function set_cookie(name, value, exp_y, exp_m, exp_d, path, domain, secure) {
    var cookie_string = name + "=" + escape(value);

    if (exp_y) {
        var expires = new Date(exp_y, exp_m, exp_d);
        cookie_string += "; expires=" + expires.toGMTString();
    }

    if (path)
        cookie_string += "; path=" + escape(path);

    if (domain)
        cookie_string += "; domain=" + escape(domain);

    if (secure)
        cookie_string += "; secure";

    document.cookie = cookie_string;
}

function get_cookie(cookie_name) {
    var results = document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');

    if (results)
        return (unescape(results[2]));
    else
        return null;
}

function delete_cookie(cookie_name) {
    var cookie_date = new Date();  // current date & time
    cookie_date.setTime(cookie_date.getTime() - 1);
    document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function getRandomNumber(range) {
    return Math.floor(Math.random() * range);
}

function getRandomChar() {
    var chars = "0123456789abcdefghijklmnopqurstuvwxyzABCDEFGHIJKLMNOPQURSTUVWXYZ";
    return chars.substr(getRandomNumber(62), 1);
}

function randomID(size) {
    var str = "";
    for (var i = 0; i < size; i++) {
        str += getRandomChar();
    }
    return str;
}

function captureDocTypeDefinition() {
    var captureDocTypeFound = 0; /* 1 = doctype definition found in page, 0 = not found */
    var captureDoctypeLabel = ""; /* Output like: HTML */
    var captureDoctypeType = ""; /* Output like: -//W3C//DTD XHTML 1.0 Transitional//EN */
    var captureDoctypePath = ""; /* Output like: http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd */

    if (navigator.userAgent.toLowerCase().indexOf("msie") > 0) {
        for (x = 0; x < document.getElementsByTagName("!").length; x++) {
            if (parseInt(document.getElementsByTagName("!")[x].nodeType) == 8) {
                if (document.getElementsByTagName("!")[x].nodeValue.toLowerCase().indexOf("doctype") > -1) {
                    tagData = new Array(); tagData = document.getElementsByTagName("!")[x].nodeValue.split("\"");
                    captureDoctypeLabel = tagData[0].replace("DOCTYPE ", "").toUpperCase(); captureDoctypeType = tagData[1]; captureDoctypePath = tagData[3]; captureDocTypeFound = 1;
                }
            }
        }
    }
    else {
        /* All Other Browsers */
        for (x = 0; x < document.childNodes.length; x++) {
            if (parseInt(document.childNodes[x].nodeType) == 10) {
                captureDoctypeLabel = document.childNodes[x].name.toUpperCase();
                captureDoctypeType = document.childNodes[x].publicId;
                captureDoctypePath = document.childNodes[x].systemId;
                captureDocTypeFound = 1;
            }
        }
    }
    var docType = "<!DOCTYPE " + captureDoctypeLabel + " PUBLIC \"" + captureDoctypeType + "\" \"" + captureDoctypePath + "\">";


    return docType
}


