//Declare Vars
var crcookieidvalue;
var crsessioncookieidvalue   ;
var newcrcookieidvalue  ;
var crcookieguidvalue ;
var crsessioncookieguidvalue  ;
var newcrcookieguidvalue ;
var guidstr    ;
var qswmctag  ;
var qskeyword  ;
var keywordqs ;
var referer   ;
var countryCode = ["ac","ad","ae","af","ag","ai","al","am","an","ao","aq","ar","as","at","au","aw","az","ax","ba","bb","bd","be","bf","bg","bh","bi","bj","bm","bn","bo","br","bs","bt","bv","bw","by","bz","ca","cc","cd","cf","cg","ch","ci","ck","cm","cn","co","cr","cs","cu","cv","cx","cy","cz","de","dj","dk","dm","do","dz","ec","ee","eg","eh","er","es","et","eu","fi","fj","fk","fm","fo","fr","ga","gb","gd","ge","gf","gg","gh","gi","gl","gm","gn","gp","gq","gr","gs","gt","gu","gw","gy","hk","hm","hn","hr","ht","hu","id","ie","il","im","in","io","iq","ir","is","it","je","jm","jo","jp","ke","kg","kh","ki","km","kn","kp","kr","kw","ky","kz","la","lb","lc","li","lk","lr","ls","lt","lu","lv","ly","ma","mc","md","mg","mh","mk","ml","mm","mn","mo","mp","mq","mr","ms","mt","mu","mv","mw","mx","my","mz","na","nc","ne","nf","ng","ni","nl","no","np","nr","nu","nz","om","pa","pe","pf","pg","ph","pk","pl","pm","pn","pr","ps","pt","pw","py","qa","re","ro","ru","rw","sa","sb","sc","sd","se","sg","sh","si","sj","sk","sl","sm","sn","so","sr","st","sv","sy","sz","tc","td","tf","tg","th","tj","tk","tl","tm","tn","to","tp","tr","tt","tv","tw","tz","ua","ug","uk","um","us","uy","uz","va","vc","ve","vg","vi","vn","vu","wf","ws","ye","yt","yu","za","zm","zw"];

//script
var scripts = document.getElementsByTagName('script');
var index = scripts.length - 1;
var myScript = scripts[index];
var queryString = myScript.src.replace(/^[^\?]+\??/,'');
var params = parseQuery( queryString );

function parseQuery ( query ) {
   var Params = new Object ();
   if ( ! query ) return Params; // return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) continue;
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}

var id= params['id'];
if(id)
{
	WMCAudit(id);
}

function generateGuid() {
    var result, i, j;
    result = '';
    for (j = 0; j < 32; j++) {
        if (j == 8 || j == 12 || j == 16 || j == 20) result = result + '-';
        i = Math.floor(Math.random() * 16).toString(16).toUpperCase();
        result = result + i;
    }
    return result ;
}
function setclickreportcookie(name, crcookievalue) {
    //Get A Expiry Date For The Cookie
    var expdate = new Date();
    expdate.setTime(expdate.getTime() + 90 * 24 * 60 * 60 * 1000);
    //Write The Cookie
    document.cookie = name + '=' + crcookievalue + '; expires=' + expdate.toGMTString() + '; path=/';
}
function setclickreportsessioncookie(name, crcookievalue) {
    //Write The Cookie
    document.cookie = name + '=' + crcookievalue + '; path=/';
}
function readclickreportcookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}
function closecrpopin() {
    document.all.crcompopin.style.display = 'none';
}
function opencrpopin() {
    document.all.crcompopin.style.display = '';
}
function clickrptcom_addbookmark() {
    clickrptcom_bookmarkurl = '' + location.protocol + '//' + location.hostname + location.pathname ;
    clickrptcom_bookmarktitle = '' + document.title;
    if (document.all) window.external.AddFavorite(clickrptcom_bookmarkurl, clickrptcom_bookmarktitle);
}
function clickrptcom_SwapOut(rollimg) {
    document.all(rollimg).src = clickrptcom_rollimage[1].src;
    return true;
}
function clickrptcom_SwapBack(rollimg) {
    document.all(rollimg).src = clickrptcom_rollimage[0].src;
    return true;
} 
function getPlainDomain(str) {
    if (str.length == 0) {
        return null;
    }
    str = str.toLowerCase();
    var i = str.indexOf("http://");
    if (i == 0) {
        str = str.substr(7);
    }
    i = str.indexOf("https://");
    if (i == 0) {
        str = str.substr(8);
    }
    i = str.indexOf("//");
    if (i == 0) {
        str = str.substr(2);
    }
    i = str.indexOf("/");
    if (i > -1) {
        str = str.substr(0, i);
    }
    i = str.indexOf("?");
    if (i > -1) {
        str = str.substr(0, i);
    }
    i = str.indexOf("www.");
    if (i == 0) {
        str = str.substr(4);
    }
    var parts = str.split('.');
    var lastpartdom = parts.length - 1
    var domstring
    if (parts.length > 2) {
        for (n = parts.length - 1; n > -1; n--) {
            var currentdompart = parts[n]
            if (istld(currentdompart) == 1) {
                lastpartdom = n;
                break;
            }
            else {
                if (in_array(currentdompart,countryCode) == 1) {
                    lastpartdom = n;
                }
            }
        }
        domstring = parts[lastpartdom - 1]
        return domstring;
    }
    else {
        return parts[0];
    }
}
function istld(str) {
    switch (str) {
    case "com":
        return 1;
    case "edu":
        return 1;
    case "gov":
        return 1;
    case "int":
        return 1;
    case "mil":
        return 1;
    case "net":
        return 1;
    case "org":
        return 1;
    case "biz":
        return 1;
    case "info":
        return 1;
    case "name":
        return 1;
    case "pro":
        return 1;
    case "aero":
        return 1;
    case "coop":
        return 1;
    case "museum":
        return 1;
    case "cat":
        return 1;
    case "jobs":
        return 1;
    case "travel":
        return 1;
    case "arpa":
        return 1;
    case "mobi":
        return 1;
    default:
        return 0
    }
}

function in_array (needle, haystack, argStrict) {
    var key = '', strict = !! argStrict;
    if (strict) 
    {
        for (key in haystack) 
        {
            if (haystack[key] === needle) 
            {                
            	return 1;
            }
        }
    } else {
        for (key in haystack) 
        {            
        	if (haystack[key] == needle) 
        	{
                return 1;
            }
        }
    } 
    return 0;
}

function WMCAudit(domid) {
    crcookieidvalue = '';
    crsessioncookieidvalue = '';
    //Create a new ID Value
    newcrcookieidvalue = (Math.round((Math.random() * 999999) + 1));
    //Read Click Report Cookie
    crcookieidvalue = readclickreportcookie('clickreportcookie');
    if (crcookieidvalue) {
        //Cookie is present reset expiry date on it
        setclickreportcookie('clickreportcookie', crcookieidvalue);
        //alert('cookie already present')
    }
    else {
        //Cookie is not present create it
        setclickreportcookie('clickreportcookie', newcrcookieidvalue);
        //alert('create cookie')
        crcookieidvalue = readclickreportcookie('clickreportcookie');
        if (crcookieidvalue) {
            //Cookie Accepted
            //alert('cookie accepted')
        }
        else {
            //alert('cookie blocked - attempt session cookie')
            crcookieidvalue = ''
            //Cookie Blocked - Attempt Session Cookie
            crsessioncookieidvalue = readclickreportcookie('clickreportsessioncookie');
            if (crsessioncookieidvalue) {
                //Session Cookie Present
                //alert('session cookie already present')
            }
            else {
                //Session Cookie Not Present - Create It
                setclickreportsessioncookie('clickreportsessioncookie', newcrcookieidvalue);
                //alert('create session cookie')
                crsessioncookieidvalue = readclickreportcookie('clickreportsessioncookie');
                if (crsessioncookieidvalue) {
                    //Session Cookie Allowed
                    //alert('session cookie allowed')
                }
                else {
                    //Session Cookie Blocked
                    //alert('session cookie blocked')
                    crsessioncookieidvalue = '';
                }
            }
        }
    }
    //
    crcookieguidvalue = '';
    //Create a new guid Value
    newcrcookieguidvalue = '' + generateGuid();
    //Create GUID Cookie Regardless of if it is set or not
    setclickreportcookie('clickreportGUIDcookie', newcrcookieguidvalue);
    //alert('create cookie')
    crcookieguidvalue = readclickreportcookie('clickreportGUIDcookie');
    if (crcookieguidvalue) {
        //Cookie Accepted
        //alert('cookie accepted')
    }
    else {
        //alert('cookie blocked - attempt session cookie')
        crcookieguidvalue = '';
        //Cookie Blocked - Attempt Session Cookie
        crcookieguidvalue = readclickreportcookie('clickreportGUIDsessioncookie');
        if (crcookieguidvalue) {
            //Session Cookie Present
            //alert('session cookie already present')
        }
        else {
            //Session Cookie Not Present - Create It
            setclickreportsessioncookie('clickreportGUIDsessioncookie', newcrcookieguidvalue);
            //alert('create session cookie')
            crcookieguidvalue = readclickreportcookie('clickreportGUIDsessioncookie');
            if (crcookieguidvalue) {
                //Session Cookie Allowed
                //alert('session cookie allowed')
            }
            else {
                //Session Cookie Blocked
                //alert('session cookie blocked')
                crcookieguidvalue = '';
            }
        }
    }
    guidstr = crcookieguidvalue;
    // Detect crtag querystring  
    qswmctag = '' + location.search;
    if (qswmctag.indexOf("?crtag=") > -1 || qswmctag.indexOf("&crtag=") > -1) {
        if (qswmctag.indexOf("&crtag=") > 0) qswmctag = qswmctag.substring(qswmctag.indexOf("&crtag="), qswmctag.length);
        if (qswmctag.indexOf("?crtag=") > 0) qswmctag = qswmctag.substring(qswmctag.indexOf("?crtag="), qswmctag.length);
        qswmctag = qswmctag.substring(7, qswmctag.length);
        if (qswmctag.indexOf("&") > 0) qswmctag = qswmctag.substring(0, qswmctag.indexOf("&"));
    }
    else {
        qswmctag = '';
    }
    var dompath = document.location.hostname + document.location.pathname;
    // Detect document referer & derive referersource
    var refererstr = '';
    refererstr = '' + document.referrer;
    referer = ''
    referer = getPlainDomain(refererstr)
    // Parse Keywords Depending on the format of the Referer
    var keywordarr = new Object();
    keywordarr["google"] = "q";
    keywordarr["googlesyndication"] = "q";
    keywordarr["groups.google"] = "q";
    keywordarr["yahoo"] = "p";
    keywordarr["search.yahoo"] = "p";
    keywordarr["overture"] = "Keywords";
    keywordarr["msn"] = "q";
    keywordarr["aol"] = "query";
    keywordarr["aolsearch.aol"] = "query";
    keywordarr["lycos"] = "query";
    keywordarr["ask"] = "q";
    keywordarr["altavista"] = "q";
    keywordarr["search"] = "q";
    keywordarr["netscape"] = "query";
    keywordarr["earthlink"] = "q";
    keywordarr["cnn"] = "query";
    keywordarr["msn"] = "q";
    keywordarr["looksmart"] = "key";
    keywordarr["about"] = "terms";
    keywordarr["excite"] = "qkw";
    keywordarr["mamma"] = "query";
    keywordarr["alltheweb"] = "q";
    keywordarr["gigablast"] = "q";
    keywordarr["voila"] = "kw";
    keywordarr["virgilio"] = "qs";
    keywordarr["teoma"] = "q";
    keywordarr["findwhat"] = "mt";
    keywordarr["miva"] = "mt";
    keywordarr["7search"] = "qu";
    keywordarr["dealtime"] = "KW";
    keywordarr["goclick"] = "SEARCH";
    keywordarr["blowsearch"] = "keywords";
    keywordarr["ah-ha"] = "Q";
    keywordarr["enhance"] = "Q";
    keywordarr["exactseek"] = "term";
    keywordarr["shopping"] = "KW";
    keywordarr["mysearch"] = "searchfor";
    keywordarr["test"] = "k";
    keywordarr["searchscout"] = "k";
    keywordarr["cyberelephant"] = "keywords";
    keywordarr["information"] = "keywords";
    keywordarr["nextag"] = "q";
    keywordarr["tiscali"] = "query";
    keywordarr["ehow"] = "term";
    keywordarr["myway"] = "searchfor";
    keywordarr["ufindus"] = "searched_on_google";
    keywordarr["mortgagecalculator"] = "t";
    keywordarr["googlesyndication"] = "ref|";
    keywordarr["doityourself"] = "query";
    keywordarr["myway"] = "searchfor";
    keywordarr["mywebsearch"] = "searchfor";
    keywordarr["avantfind"] = "keywords";
    keywordarr["ewoss"] = "k";
    keywordarr["myfrauddetection"] = "k";
    keywordarr["ntlworld"] = "q";
    keywordarr["healthline"] = "q1";
    keywordarr["optusnet"] = "p";
    keywordarr["orange"] = "q";
    keywordarr["gawwk"] = "qry";
    keywordarr["comcast"] = "q";
    keywordarr["canada"] = "q";
    keywordarr["adelphia"] = "q";
    keywordarr["lindr"] = "q";
    keywordarr["happytofind"] = "Keywords";
    keywordarr["seekmap"] = "Keywords";
    keywordarr["lhysearch"] = "terms";
    keywordarr["zingfeed"] = "keywords";
    keywordarr["bestseeker"] = "keywords";
    keywordarr["ahseek"] = "query";
    keywordarr["searchyond"] = "terms";
    keywordarr["seek55"] = "terms";
    keywordarr["friendsearchs"] = "terms";
    keywordarr["123feed"] = "keywords";
    keywordarr["homelow"] = "terms";
    keywordarr["starware"] = "qry";
    keywordarr["cooffe"] = "keywords";
    keywordarr["zdnet"] = "q";
    keywordarr["reference"] = "q";
    // Get Relevant keyword from refererstr 
    keywordqs = '' + keywordarr[referer];
    qskeyword = '' + refererstr;
    if (qskeyword.indexOf("?" + keywordqs + "=") > -1 || qskeyword.indexOf("&" + keywordqs + "=") > -1) {
        if (qskeyword.indexOf("&" + keywordqs + "=") > 0) qskeyword = qskeyword.substring(qskeyword.indexOf("&" + keywordqs + "="), qskeyword.length);
        if (qskeyword.indexOf("?" + keywordqs + "=") > 0) qskeyword = qskeyword.substring(qskeyword.indexOf("?" + keywordqs + "="), qskeyword.length);
        qskeyword = qskeyword.substring(keywordqs.length + 2, qskeyword.length);
        if (qskeyword.indexOf("&") > 0) qskeyword = qskeyword.substring(0, qskeyword.indexOf("&"));
    }
    else {
        qskeyword = '';
    }
    var host ='service.clickreport.com';
    // Output IFRAME to call track.aspx server side script
    var protocol = document.location.protocol;
    if (qswmctag.length > 0) {
        d1 = new Date();
        d = d1.getFullYear() + '/' + (d1.getMonth() + 1) + '/' + (d1.getDate()) + ' ' + d1.getHours() + ':' + d1.getMinutes() + ':' + d1.getSeconds();
        
        if (location.search.indexOf("&crdebug=true") > -1) {
            document.write('<iframe id="crtrackframe" height="400" width="900" src="' + protocol+ '//' + host + '/crtracking?crcookie=' + crcookieidvalue + '&crsession=' + crsessioncookieidvalue + '&crtag=' + escape(qswmctag) + '&crkeywords=' + escape(qskeyword) + '&crreferer=' + escape(referer) + '&cridomain=' + escape(domid) + '&cridomainpath=' + escape(dompath) + '&fullrefering=' + escape(refererstr) + '&guid=' + escape(guidstr) + '&crdebug=true"></iframe>');
        } else {
            document.write('<iframe id="crtrackframe" height="0" width="0" src="' + protocol+ '//' + host + '/crtracking?crcookie=' + crcookieidvalue + '&crsession=' + crsessioncookieidvalue + '&crtag=' + escape(qswmctag) + '&crkeywords=' + escape(qskeyword) + '&crreferer=' + escape(referer) + '&cridomain=' + escape(domid) + '&cridomainpath=' + escape(dompath) + '&fullrefering=' + escape(refererstr) + '&guid=' + escape(guidstr) + '"></iframe>');
        }
        document.write('<script language="javascript" type="text/javascript" src="' + protocol+ '//' + host + '/dynamicjs?crcookie=' + crcookieidvalue + '&crsession=' + crsessioncookieidvalue + '&crtag=' + qswmctag + '&crkeywords=' + qskeyword + '&crreferer=' + referer + '&cridomain=' + domid + '&cridomainpath=' + dompath + '&starttime=' + d + '"></script>');
    }
    //ouput iframe to verify landing page
    document.write('<script language="javascript" type="text/javascript" src="' + protocol+ '//' + host + '/dynamicjs?crcommand=verify&crtag=crv' + domid + '&cridomain=' + domid + '&cridomainpath=' + dompath + '"></script>');
}    
