var theRequest = false;

function insertPageHit(){
    var theReferrer = document.referrer;

    if (theRequest && theRequest != null && theRequest.readyState != 0 && theRequest.readyState != 4){
        theRequest.abort();
    }
    theRequest = false;

    try {
        theRequest = new XMLHttpRequest();
    } catch(err) {
        try {
            theRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
            theRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (theRequest.overrideMimeType) {
        theRequest.overrideMimeType('text/xml');
    }

    if (!theRequest) {
        setHitID(0);
        return false;
    }

    var hitRecorder = "../hitRecorder.asp?referrer=" + theReferrer;
    theRequest.onreadystatechange = setHitID;
    theRequest.open('GET', hitRecorder, true);
    theRequest.send(null);
}

function setHitID(errCode){
    var msg = "";
    if (errCode == 0){
        msg = "Unable to record the hit."
    } else {
        if (theRequest.readyState == 4 && theRequest.status == 200) {
            var returnVal = theRequest.responseXML;
            if (returnVal.getElementsByTagName("nodata")[0].childNodes[0].nodeValue == "XXemptyXX"){
                msg = "Unable to record the hit.";
            } else {

                tempVal = returnVal.getElementsByTagName("hitID")[0].childNodes[0].nodeValue;
                if (tempVal == "!!")
                    tempVal = "";
                document.getElementById("hitID").value = tempVal;
            } // end if 'empty' return value
        } // end if valid status and state
    } // end if error creating the object
}


// set the clickThrough field to true in the DB
function recordClickThrough(){
    theHit = document.getElementById("hitID").value;
    if (theHit > 0){
        if (theRequest && theRequest != null && theRequest.readyState != 0 && theRequest.readyState != 4){
            theRequest.abort();
        }
        theRequest = false;

        try {
            theRequest = new XMLHttpRequest();
        } catch(err) {
            try {
                theRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                theRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        var hitRecorder = "../clickThrough.asp?theHit=" + theHit;
        theRequest.open('GET', hitRecorder, true);
        theRequest.send(null);
        window.location = "http://shops3.directedje.com/motorcyclecards/catalog.asp";
    } // end if a valid hit to record to
}