//http://econsultancy.com/uk/blog/5387-how-to-use-google-analytics-to-track-telephone-leads-part-iii-4

function callTrackerPT(ID, Attributes, numberArray) {
    var counter, trafficSourceNo;
    var Domain = getDomain();
    trafficSourceNo = getTrafficSourceNo(Domain);

    if (numberArray.length >= trafficSourceNo) {
        for (var number in numberArray) {
//alert(trafficSourceNo+" test "+number);
            if (number == trafficSourceNo) {
//                alert(ID);
                $(ID).text(numberArray[number]);
                $('a'+ID+'Link').attr('href', 'tel:'+numberArray[number]);
//                    document.getElementById(ID).innerHTML = numberArray[number];
                break;
            }
        }
    } else {
        $(ID).text(numberArray[0]);
//        document.getElementById(ID).innerHTML = "skräp";//add hardcoded backup telephone number e.g. 01903 247788
//        alert('test');
    }
}


function getDomain() {
    var strURL = document.URL;

    // Remove Blog Entry
    strURL = strURL.replace('blog.','www.');

    if (strURL != "") {
        return strURL.replace(/^http\:\/\/(www\.|)([^\/]+)(.*)$/,"$2");
    }
    else {
        return "";
    }
}


function optionTrafficSources() {
    // Enter the traffic resource names    
    var arrTrafficSources = new Array();
    arrTrafficSources[0] = "direct";
    arrTrafficSources[1] = "other";
    arrTrafficSources[2] = "adwords";
    arrTrafficSources[3] = "googleOther";
    arrTrafficSources[4] = "facebook";
    return arrTrafficSources;
}

function getTrafficSourceNo(domain) {
    var arrTrafficSources = optionTrafficSources();
    var strTrafficSource = getTrafficSource(domain);
    var i, intTrafficSourceNo;
//alert(strTrafficSource);

    for (i = 0; i < arrTrafficSources.length; i++) {
        if (arrTrafficSources[i].toLowerCase() == strTrafficSource) {
            intTrafficSourceNo = i;
            break;
        }
    }
    return intTrafficSourceNo;   
}

function setCookie(c_name,value,expiredays,domain) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+";expires="+exdate.toGMTString()+";domain="+domain;
}

function getCookie(c_name) {
    if (document.cookie.length>0) {
        c_start=document.cookie.indexOf(c_name + "=");

        if (c_start!=-1) { 
            c_start=c_start + c_name.length+1; 
            c_end=document.cookie.indexOf(";",c_start);
                if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        } 
    }
    return "";
}

function querySt(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i=0;i<gy.length;i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}

function getTrafficSource(domain) {
    var cStr = "";
    
    if (getCookie('calltracker') != "" && getCookie('calltracker') != null) {
        cStr = document.cookie.replace(/^(.*)calltracker=([^\;]+)(.*)$/,"$2");
    }
    
    if ((getDomain() == "ljwpctest.unicorn.se" || getDomain() == "phonecafe.se") && cStr == "") {
        if (cStr == "") {
            if (getCookie('calltracker') != "" && getCookie('calltracker') != null) {
                cStr = document.cookie.replace(/^(.*)calltracker=([^\;]+)(.*)$/,"$2");
            }
            else if (document.referrer != "") {
                if (document.referrer.match(/^(.*)google(.*)$/)) {
                    if (document.URL.match(/^(.*)\?gclid\=(.*)$/) || document.URL.match(/^(.*)(\?|\&)utm\_term\=ppc(.*)$/) || document.cookie.match(/^(.*)utmcmd \=\(not\%20set\)(.*)$/) || document.cookie.match(/^(.*)utmcmd\=ppc(.*)$/) ||  document.referrer.match(/^(.*)sponsoredlinks\?(.*)$/)) {
                        setCookie('calltracker','adwords',365,domain);
                        cStr = "adwords";
                    }
                    else {
                        setCookie('calltracker','googleOther',365,domain);
                        cStr = "googleOther";
                    }
                }
                else if (document.referrer.match(/^(.*)facebook(.*)$/)) {
                    setCookie('calltracker','facebook',365,domain);
                    cStr = "facebook";
                }
                else if (document.referrer.match(new RegExp("(.*)" + domain + "(.*)$"))) {
                    setCookie('calltracker','direct',365,domain);
                    cStr = "direct";
                }
                else {
                    setCookie('calltracker','other',365,domain);
                    cStr = "other";
                }
            }
            else {
                setCookie('calltracker','other',365,domain);
                cStr = "other";
            }
        }
    }
    return cStr.toLowerCase();
}

