//////////////////////////////////////////////////////////////////////////////
// This cookie stores information on the search engine and keywords used to
// find this Anglian site through a search. No other information is stored.
//////////////////////////////////////////////////////////////////////////////

$(function()
    {
//SetUtn();
    })
   

var vAffiliates = new Array();

// ------------- affiliates -------------
vAffiliates['0100'] = 'vibrant';
vAffiliates['0116'] = 'espotting';
vAffiliates['0128'] = 'c4_homes';
vAffiliates['0141'] = 'mirago';
vAffiliates['0156'] = 'yahoo_paid';
vAffiliates['0160'] = 'comp_online';
vAffiliates['0173'] = 'google_st';
vAffiliates['0185'] = 'propertyfind';
vAffiliates['0195'] = 'yell_display';
vAffiliates['0200'] = 'more_network';
vAffiliates['0221'] = 'prime';
vAffiliates['0237'] = 'google_paid';
vAffiliates['0242'] = 'ideal_home';
vAffiliates['0250'] = 'touch_local';
vAffiliates['0260'] = 'findaprop';
vAffiliates['0281'] = 'webfinder';
vAffiliates['0300'] = 'oridian';
vAffiliates['0339'] = 'upmystreet';
vAffiliates['0343'] = 'woman_home';
vAffiliates['0348'] = 'msn_paid';
vAffiliates['0350'] = 'yell_com';
vAffiliates['0369'] = 'miva';
vAffiliates['0380'] = 'edr';
vAffiliates['0374'] = 'more_netw_red';
vAffiliates['0394'] = 'media_brokers';
vAffiliates['0410'] = 'ad_revenue';
vAffiliates['0420'] = 'loot';
vAffiliates['0432'] = 'media_brk_bhv';
vAffiliates['0440'] = 'yahoo_prop';
vAffiliates['0445'] = 'otp';
vAffiliates['0492'] = 'ad2one';
vAffiliates['0520'] = 'scoot';
vAffiliates['0531'] = 'adv_com';
vAffiliates['0545'] = 'home_garden';
vAffiliates['0556'] = 'msn_prop';
vAffiliates['0570'] = 'dgm_bhv';
vAffiliates['0590'] = 'adcom_bhv';
vAffiliates['0605'] = 'yahoo_ssp';
vAffiliates['0610'] = 'locallife';
vAffiliates['0622'] = 'eyeconomy';
vAffiliates['0646'] = 'living_etc';
vAffiliates['0676'] = 'dgm';
vAffiliates['0715'] = 'etype';
vAffiliates['0747'] = 'rightmove';
vAffiliates['0829'] = 'trade_doub';
vAffiliates['0848'] = 'uktv_style';
vAffiliates['0933'] = 'vc_med';
vAffiliates['0949'] = 'discover_rt';
vAffiliates['0963'] = 'trading_flr';
vAffiliates['0980'] = 'trusttrader';
vAffiliates['1000'] = 'adjug';
vAffiliates['1010'] = 'affilwindow';
vAffiliates['1020'] = 'buyat';
vAffiliates['1030'] = 'yahoo_bhv';
vAffiliates['1040'] = 'utarget';
vAffiliates['1050'] = 'si';
vAffiliates['1060'] = 'thompsonlcl';
vAffiliates['1070'] = 'yell_organic';
vAffiliates['1080'] = 'msn_dr';
vAffiliates['1090'] = 'aol';
vAffiliates['1100'] = 'and';
vAffiliates['1110'] = 'yell_sponsored';
vAffiliates['1130'] = 'btex';
vAffiliates['1140'] = 'cc';
vAffiliates['1150'] = 'seek';
vAffiliates['1160'] = 'ggle_cntnt_net';
vAffiliates['1170'] = 'ahi_auto_rspns';
vAffiliates['1180'] = 'mypropertyspy';
vAffiliates['1190'] = 'yell_orgnc_cns';
vAffiliates['1200'] = 'build-co-uk';
vAffiliates['1210'] = 'central-index';
vAffiliates['1220'] = 'ggle_plc_dspl';
vAffiliates['1230'] = 'biggreenswitch';
vAffiliates['1240'] = 'esnews';
vAffiliates['1250'] = 'facebook';
vAffiliates['1260'] = 'mailtrack';
vAffiliates['1300'] = 'ppc-competitor';
vAffiliates['1310'] = 'build-couk-hpg';
vAffiliates['1320'] = 'ind-advertorial';
vAffiliates['1330'] = 'british-info';
vAffiliates['1340'] = 'unanimis-netwk';
vAffiliates['1350'] = 'adlink-media-nt';
vAffiliates['1360'] = 'ad-pepper-netw';
vAffiliates['1370'] = 'spec-media-net';
vAffiliates['1380'] = 'netmovers';
vAffiliates['1390'] = 'fish4homes';
vAffiliates['1400'] = 'myvouchercode';
vAffiliates['1410'] = 'dpg-ploc-fprop';
vAffiliates['1420'] = 'dpg-homesprop';
vAffiliates['1430'] = 'ebldershome';
vAffiliates['1440'] = 'gglpanex';
vAffiliates['1450'] = 'telegraph';
vAffiliates['1460'] = 'times-online';
vAffiliates['1470'] = 'house-to-home';
vAffiliates['1480'] = 'thomplclpaid';
vAffiliates['1490'] = 'yahoo_dr';
vAffiliates['1500'] = 'realhomes';
vAffiliates['1510'] = 'homebld-renov';
vAffiliates['1520'] = 'adconion';
vAffiliates['1530'] = 'exchangelab';
vAffiliates['1540'] = 'zoopla';

// ------------- affiliates -------------


//---------------Added by sandeep--------start----------         

//----Media Code by Affiliated Name----------


var MediaCodeFromAffiliatedName = new Array();

MediaCodeFromAffiliatedName['google_paid'] = 'THEGOOP';
MediaCodeFromAffiliatedName['yahoo_paid'] = 'THEYAHP';
MediaCodeFromAffiliatedName['bing_paid'] = 'THEBINP';
MediaCodeFromAffiliatedName['facebook'] = 'THEFAC';
MediaCodeFromAffiliatedName['OTHER'] = 'THEOTH';

//----Media Code----------




//----Media Code by domain Name----------


var MediaCodeFromDomainName = new Array();

MediaCodeFromDomainName['www.google.'] = 'THEGOOG';
MediaCodeFromDomainName['search.yahoo.'] = 'THEYAH';
MediaCodeFromDomainName['www.bing.'] = 'THEBIN';


//----Media Code----------



//----Media Code for Direct traffic----------


var MediaCodeFromDirect = new Array();

MediaCodeFromDirect['direct'] = 'THEDIR';



//----Media Code for Direct traffic----------



//---------------Added by sandeep--------End----------  

var THSReferrer="";


function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  
  if (endstr == -1)
    endstr = document.cookie.length;
  
  return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) 
{
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  
  while (i < clen) 
  {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return "";
}


function SetCookie (name,value,expires,path,domain,secure) 
{
  procQueryString();
    //alert(qsParm);
  var THSMediaCode="";
  
  if(GetCookie(name)!="")
  {
 // when referrer cookie exit

        var cookReferrer = GetCookie(name);
        THSMediaCode=getAffiliatedMediaCode(cookReferrer);
         THSReferrer= escape(cookReferrer);
     
         if(THSMediaCode !="")
         {
         // media code here
    
    setContentToHiddenVariable(THSReferrer,THSMediaCode);
         }
         else
         {
         
          // check media code by domain
              var BrowserDomain=unescape(unescape(cookReferrer));  //getReferrerDomain(cookReferrer);
             THSMediaCode=getMediaCodeByDomain(BrowserDomain); 
                if(THSMediaCode !="")
                {
                
                     setContentToHiddenVariable(THSReferrer,THSMediaCode);
                }
                else
                {
                // Here direct landing
              
                THSMediaCode=MediaCodeFromDirect['direct'] ;
                  setContentToHiddenVariable("",THSMediaCode);
                
                }
         
         
         }
 
  	return;	
  }
  else
  {
  
   // when referrer cookie is not present exit
    var BrowserUrl="";
    var tempfromValue="";
    
  if(value !="")
  {
  BrowserUrl=value; 
   THSReferrer= escape(escape(value));
  }
  else
  {
      BrowserUrl=getBrowserURL();
      tempfromValue =getFromValue();
      if(tempfromValue !="")
      {
        THSReferrer = escape(escape(BrowserUrl));
      }
      else
      { 
       THSReferrer= "";
       }
  }
  
  // THSReferrer= escape(BrowserUrl);
   
     if(BrowserUrl !="")
     {
        if(value !="" && tempfromValue != "")
        {
            createCookie(name, escape(value)+escape(getBrowserURL()), expires, path, domain, secure);
        }
        else
        {
            createCookie(name, escape(BrowserUrl), expires, path, domain, secure);
        }
    
     // createCookie(name, escape (value) + escape(BrowserUrl), expires, path, domain, secure);
    
   
        THSMediaCode=getAffiliatedMediaCode(BrowserUrl);
 
       
       
        if(THSMediaCode !="")
         {
         
            setContentToHiddenVariable(THSReferrer,THSMediaCode);
         }
         else
         {
         
          // check media code by domain
              var BrowserDomain=unescape(unescape(BrowserUrl)); //getReferrerDomain(BrowserUrl);
             THSMediaCode=getMediaCodeByDomain(BrowserDomain);
                if(THSMediaCode !="")
                {
                  
                     setContentToHiddenVariable(THSReferrer,THSMediaCode);
                }
                else
                {
                // Here direct landing
                THSMediaCode=MediaCodeFromDirect['direct'] ;
                
                  setContentToHiddenVariable(THSReferrer,THSMediaCode);
                }
         
         
         }
      
     
     }
     
     
  
  }
  
  
  
//  var referrerDomain = getReferrerDomain(value);
//  var from = getFromValue();


//  if(from!="")
//    referrerDomain = 'affiliate'
//  

//  if(referrerDomain!="" && referrerDomain!=document.domain)
//	createCookie(name, escape (value) + escape(from), expires, path, domain, secure);
	
}

function getFromValue()
{  
    var fromPos = window.location.href.toUpperCase().indexOf("?FROM=");
    if (fromPos != -1)
    {
        var sAff = window.location.href.substr(fromPos+6,4);
      
        return window.location.href.substring(fromPos).toUpperCase().replace(sAff, vAffiliates[sAff]);
       
    }
    return "";    
}

function getReferrerDomain(value)
{
    var referrer;
    if(value.indexOf("?",1)==-1)
	    referrer = value;
    else
	    referrer = value.substring(0,value.indexOf("?",1));	
	    
    return referrer.substring(7,referrer.indexOf("/",7))
}

function createCookie(name,value,expires,path,domain,secure) 
{
    var expiryDate  = new Date();
    
    // Use 30 days from now to expire the cookie
    var noOfHoursInSetDays = 30*24;
    expiryDate.setHours(noOfHoursInSetDays); 
    
	document.cookie = name + "=" + escape (value) +
	  ((expiryDate) ? "; expires=" + expiryDate.toGMTString() : "") +
	  ((path) ? "; path=" + path : "") +
	  ((domain) ? "; domain=" + domain : "") +
	  ((secure) ? "; secure" : "");  
}


// ------------- UTN -------------


function SetUtn()
{    
//    createCookie('ahTelNo','',0,'/');
//    createCookie('referrer','',0,'/');
    var cookReferrer = GetCookie('referrer');
    var cookTelNo = GetCookie('ahTelNo');
//    alert(cookReferrer);
//    alert(cookTelNo);

    // If there isn't a referrer cookie set
    // then parse the current QueryString 
    // and determine number
    if(cookReferrer=="")
    {
        var from = getFromValue();
        if(from != "")
            setPPCTelephoneNumber(from);
        else
            setReferralTelephoneNumber(getReferrerDomain(document.referrer));    
    }        
    else
    {
        if(cookTelNo=="")
            parseReferrerCookie(cookReferrer)
        else
            procQueryString();
            setTelephoneClass(cookTelNo);
    }
}


function parseReferrerCookie(sCookieValue)
{
    if(sCookieValue.indexOf("FROM=")!=-1)
        setPPCTelephoneNumber(sCookieValue)
    else
        setReferralTelephoneNumber(sCookieValue);
}

// Sets the telephone for Paid Per Click referalls
function setPPCTelephoneNumber(sFrom)
{    
    if(sFrom=="")
        return ;
    switch(sFrom.substr(6))
    {
        case 'google_paid':
            setTelephoneClass("tel-08009541202"); // 0237 
            break;
        default:
            setTelephoneClass("tel-08009541204"); // Search PPC - other 
            break;
    }
}

function setReferralTelephoneNumber(sFrom)
{
    if(sFrom.indexOf('www.google')!=-1)
        setTelephoneClass("tel-08009541203");
    else
        setTelephoneClass("tel-0800500600");   // Default number if no match
}

function setTelephoneClass(sClassName)
{
    try
    {
        if(sClassName!="")
        {
            createCookie('ahTelNo',sClassName,0,'/');
            //alert(sClassName);
           // document.getElementById('phone-number-container').className = sClassName;            
        }
    }
    catch(e)
    {
        return;
    }
}

// ------------- Customer Anglian Requirements -------------

function showSecurityPoint(pointId, imgUrl)
{
    for(var i=0;i<4;i++)
    {
        var point = document.getElementById('point'+i)
        if(point!=null)
            point.className = "securityHidden";
        
    }
 //   document.getElementById(pointId).className ="securityShown";
  //  document.getElementById('securityMain').className ="securityHidden";
  //  document.getElementById('securityImage').src = imgUrl;
}


function hideSecurityPoint(pointId, imgUrl)
{
 //   document.getElementById(pointId).className = "securityHidden";
   // document.getElementById('securityMain').className = "securityShown";
   // document.getElementById('securityImage').src = imgUrl;
}



function fillAddress(ctrlThis, ctrlHouseNo, ctrlAddr1, ctrlAddr2)          
{
    if (ctrlThis.selectedIndex>0)            
    {              
        var vParts = ctrlThis.options[ctrlThis.selectedIndex].value.split('||');              
        if (vParts.length>1)              
        {   
            try
            { 
                if(vParts[1].length==0)       
                {
                    var spc = vParts[0].indexOf(' ');
                    if(spc>-1)
                    {
                        ctrlHouseNo.value = vParts[0].substr(0,spc);
                        ctrlAddr1.value = vParts[0].substr(spc+1);  
                        ctrlAddr2.value = '';
                    }
                }   
                else
                { 
                    ctrlHouseNo.value = vParts[0];  
                    var csv = vParts[1].indexOf(',');              

                    if(csv>-1)
                    {
                        ctrlAddr1.value = vParts[1].substr(0,csv);
                        ctrlAddr2.value = vParts[1].substr(csv+1);    
                    }
                    else
                    {
                        ctrlAddr1.value = vParts[1];
                        ctrlAddr2.value = '';
                    }
                    
                }
            }
            catch(e)
            {
                ctrlHouseNo.value = vParts[0]
                ctrlAddr1.value = vParts[1];
                ctrlAddr2.value = '';
                return;
            }                                                                   
        }     
    }
}

// Remove phone number for Affiliate links.

var qsParm = new Array();


function procQueryString() {
    qs();

    if (qsParm['affNoTel'] == 'true') {
      //  document.getElementById('phone-number-container').style.visibility = 'hidden';
      //  document.getElementById('phone-call-times').style.visibility = 'hidden';
      //  //document.getElementById('phone-number-container').style.backgroundImage = 'none';
    }
    else {
     //   document.getElementById('phone-number-container').style.visibility = 'visible';
     //   document.getElementById('phone-call-times').style.visibility = 'visible';
    }
}


function qs() {
    var query = window.location.search.substring(1);
    var parms = query.split('&');
    for (var i = 0; i < parms.length; i++) {
        var pos = parms[i].indexOf('=');
        if (pos > 0) {
            var key = parms[i].substring(0, pos);
            var val = parms[i].substring(pos + 1);
            qsParm[key] = val;
        }
    }
}

// end Remove phone number for Affiliate links. 

//---------------Added by sandeep--------start----------         
function getBrowserURL()
{  
return window.location.href;  
}



function getMediaCodeByDomain(forContent)
{
var sMediaCode="";
var referrerDomain=getReferrerDomain(forContent);
//THSReferrer=referrerDomain;
    if (referrerDomain.toUpperCase().indexOf("www.google.".toUpperCase()) != -1)
    {
    sMediaCode=MediaCodeFromDomainName['www.google.'];
    }
    else if (referrerDomain.toUpperCase().indexOf("search.yahoo.".toUpperCase()) != -1)
    {
    sMediaCode= MediaCodeFromDomainName['search.yahoo.'];
    }
    else if (referrerDomain.toUpperCase().indexOf("www.bing.".toUpperCase()) != -1)
    {
     sMediaCode= MediaCodeFromDomainName['www.bing.'];
    }
return sMediaCode;
}

function getAffiliatedMediaCode(Content)
{
var TempTHSMediaCode="";
//var tempContent=unescape(Content);
var tempContent=unescape(unescape(Content));
 var fromPos = tempContent.toUpperCase().indexOf("?FROM=");

    if (fromPos != -1)
    {
      var sAff = tempContent.substr(fromPos+6,4);
  
       // return window.location.href.substring(fromPos).toUpperCase().replace(sAff, vAffiliates[sAff]);
      if(vAffiliates[sAff]!= null || vAffiliates[sAff]!='')
      {          
       //THSReferrer= vAffiliates[sAff];
    
        TempTHSMediaCode=getMediaCodeByAffilatedName(vAffiliates[sAff]);
      
      }
      else
      {
      TempTHSMediaCode= MediaCodeFromAffiliatedName['OTHER'];
      }
    
    }
  return TempTHSMediaCode;
}



function getMediaCodeByAffilatedName(forAffiliatedName)
{
 
    var retMediaCode=MediaCodeFromAffiliatedName['OTHER'];
    if(MediaCodeFromAffiliatedName[forAffiliatedName] != null)
    {   
     retMediaCode=MediaCodeFromAffiliatedName[forAffiliatedName];
    }
    else
    {
     retMediaCode=MediaCodeFromAffiliatedName['OTHER'];
    }

return retMediaCode;

}

function setContentToHiddenVariable(referrer,MediaCode)
{
 document.getElementById('txtReferrer').value =referrer;
 document.getElementById('txtMediaCode').value =MediaCode;
 document.getElementById('txtCategory').value ="AHIFeedbackProduct";

}
//---------------Added by sandeep--------End----------                                                 