﻿
function PlacePhone(ID, HTMLPhone1, HTMLPhone2, HTMLPhone3, HTMLPhone4, HTMLPhone5, Attributes) {
    var counter;
    var Domain = getDomain();

    if (document.getElementById(ID)) {
        document.getElementById(ID).style.display='';
        
        if (getTrafficSourceNo(Domain) == 0) {
            document.getElementById(ID).innerHTML = HTMLPhone1;
        }
        else if (getTrafficSourceNo(Domain) == 1) {
            document.getElementById(ID).innerHTML = HTMLPhone2;
        }  
        else if (getTrafficSourceNo(Domain) == 2) {
            document.getElementById(ID).innerHTML = HTMLPhone3;
        }
        else if (getTrafficSourceNo(Domain) == 3) {
            document.getElementById(ID).innerHTML = HTMLPhone4;
        } 
        //else if (getTrafficSourceNo(Domain) == 4) {
        //    document.getElementById(ID).innerHTML = HTMLPhone5;
        //}                                
        else {
            document.getElementById(ID).innerHTML = "Error. Could not find Phone Number.";
        }
                
        // Split Attributes.
        var arrAttributes = Attributes.split("|");
        
        for (counter = 1; counter <= arrAttributes.length; counter++) {
            var arrAttValues = arrAttributes[counter - 1].split("=");
            
            if (getTrafficSourceNo(Domain) == 0 && arrAttValues[0] == "class1") {
                document.getElementById(ID).className = arrAttValues[1];
            }
            
            if (getTrafficSourceNo(Domain) == 1 && arrAttValues[0] == "class2") {
                document.getElementById(ID).className = arrAttValues[1];
            }
            
            if (getTrafficSourceNo(Domain) == 2 && arrAttValues[0] == "class3") {
                document.getElementById(ID).className = arrAttValues[1];
            }     
            
            if (getTrafficSourceNo(Domain) == 3 && arrAttValues[0] == "class4") {
                document.getElementById(ID).className = arrAttValues[1];
            }   
            
            //if (getTrafficSourceNo(Domain) == 4 && arrAttValues[0] == "class5") {
            //    document.getElementById(ID).className = arrAttValues[1];
            //}                             
            
        }        
        
    }
}

function getDomain() {
    var strURL = document.URL;
    
    // Allow sub domains to be part as one.
    //strURL = strURL.replace(/^http\:\/\/([^\.]+)\.(.*)$/,"http://www.$2");
    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] = "normal";
    arrTrafficSources[1] = "organic";
    arrTrafficSources[2] = "ppc";
    arrTrafficSources[3] = "other"
//    arrTrafficSources[4] = "miscellaneous"
    
    return arrTrafficSources;
}

function getTrafficSourceNo(domain) {

    var arrTrafficSources = optionTrafficSources();
    var strTrafficSource = getTrafficSource(domain);
    var i, intTrafficSourceNo
    
    for (i = 0; i < arrTrafficSources.length; i++) {
        if (arrTrafficSources[i] == 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 getTrafficSource(domain){

    var cStr = "normal";

    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.cookie.match(/^(.*)utmcmd\=\(not\%20set\)(.*)$/) || document.cookie.match(/^(.*)utmcmd\=ppc(.*)$/) ||  document.referrer.match(/^(.*)sponsoredlinks\?(.*)$/)) {
                setCookie('calltracker','ppc',365,domain); 
                cStr = "ppc";           
           }
           else {
                setCookie('calltracker','organic',365,domain); 
                cStr = "organic";          
           } 
        }
        else if (document.referrer.match(/^(.*)1stcarpetcleaning(.*)$/)) {
            setCookie('calltracker','normal',365,domain); 
            cStr = "normal";
        }
        else {
           setCookie('calltracker','other',365,domain); 
           cStr = "other";            
        }
    
    }
    else {
        setCookie('calltracker','normal',365,domain); 
        cStr = "normal";
    }
    
    return cStr.toLowerCase();
}

