// FT.com ("FTCOM") site-specific JavaScript functions

// Global variables
var ord = Math.round(Math.random()*100000000);

var debug = "off"							// Turns debugging on or off
var co = "uk";								// Defines country; uk=United Kingdom
var siteName = "ft.com";	// Defines site name
var numAds = 0;
var tileSrc = new Array();

// browser sniffer
function Is ()
{   // convert all characters to lowercase to simplify testing
    var agt=navigator.userAgent.toLowerCase();

    // *** BROWSER VERSION ***
    // Note: On IE5, these return 4, so use is.ie5up to detect IE5.

    this.major = parseInt(navigator.appVersion);
    this.minor = parseFloat(navigator.appVersion);

    // Note: Opera and WebTV spoof Navigator.  We do strict client detection.
    // If you want to allow spoofing, take out the tests for opera and webtv.
    this.nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
                && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
                && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
    this.nav4 = (this.nav && (this.major == 4));
    this.nav4up = (this.nav && (this.major >= 4));
    this.navonly      = (this.nav && ((agt.indexOf(";nav") != -1) ||
                          (agt.indexOf("; nav") != -1)) );
    this.nav6 = (this.nav && (this.major == 5));
    this.nav6up = (this.nav && (this.major >= 5));
    this.gecko = (agt.indexOf('gecko') != -1);
    this.ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
    this.ie3    = (this.ie && (this.major < 4));
    this.ie4    = (this.ie && (this.major == 4) && (agt.indexOf("msie 5")==-1) );
    this.ie4up  = (this.ie  && (this.major >= 4));
    this.ie5    = (this.ie && (this.major == 4) && (agt.indexOf("msie 5.0")!=-1) );
    this.ie5_5  = (this.ie && (this.major == 4) && (agt.indexOf("msie 5.5") !=-1));
    this.ie6    = (this.ie && (this.major == 6) && (agt.indexOf("msie 6.0") != -1));
    this.ie5up  = (this.ie  && !this.ie3 && !this.ie4);
    this.ie5_5up =(this.ie && !this.ie3 && !this.ie4 && !this.ie5);
    this.ie6up  = (this.ie && !this.ie3 && !this.ie4 && !this.ie5 && !this.ie5_5);

    // *** PLATFORM ***
    this.win   = ( (agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1) );
    // NOTE: On Opera 3.0, the userAgent string includes "Windows 95/NT4" on all
    //        Win32, so you can't distinguish between Win95 and WinNT.
    this.win95 = ((agt.indexOf("win95")!=-1) || (agt.indexOf("windows 95")!=-1));

    // is this a 16 bit compiled version?
    this.win16 = ((agt.indexOf("win16")!=-1) ||
                 (agt.indexOf("16bit")!=-1) || (agt.indexOf("windows 3.1")!=-1) ||
                 (agt.indexOf("windows 16-bit")!=-1) );

    this.win31 = ((agt.indexOf("windows 3.1")!=-1) || (agt.indexOf("win16")!=-1) ||
                  (agt.indexOf("windows 16-bit")!=-1));

    // NOTE: Reliable detection of Win98 may not be possible. It appears that:
    //       - On Nav 4.x and before you'll get plain "Windows" in userAgent.
    //       - On Mercury client, the 32-bit version will return "Win98", but
    //         the 16-bit version running on Win98 will still return "Win95".
    this.win98 = ((agt.indexOf("win98")!=-1) || (agt.indexOf("windows 98")!=-1));
    this.winnt = ((agt.indexOf("winnt")!=-1) || (agt.indexOf("windows nt")!=-1));
    this.win32 = (this.win95 || this.winnt || this.win98 ||
                    ((this.major >= 4) && (navigator.platform == "Win32")) ||
                    (agt.indexOf("win32")!=-1) || (agt.indexOf("32bit")!=-1));

    this.winme = ((agt.indexOf("win 9x 4.90")!=-1));
    this.win2k = ((agt.indexOf("windows nt 5.0")!=-1));

    this.mac    = (agt.indexOf("mac")!=-1);

    this.mac68k = (this.mac && ((agt.indexOf("68k")!=-1) ||
                               (agt.indexOf("68000")!=-1)));
    this.macppc = (this.mac && ((agt.indexOf("ppc")!=-1) ||
                                (agt.indexOf("powerpc")!=-1)));
}

var is;
is = new Is();

function isSecure(doc) {
	return(doc.location.protocol == 'https:');
}

// Build an Img path
function ftcomImg(img){
        return(ftcomPath(iPath+img));
}

function ftcomPath(path){
  var newurl = '';

  if( isSecure(document) ){
    newurl = 'https';
  } else {
    newurl = 'http';
  }
  newurl += '://' + path;
  return(newurl);
}

// commonOnLoad
function commonOnLoad()
{
}


// Get Extra Params for Ad Server
function getAdParams(){
	var sAYSC = getCookieValue('AYSC', '');
	var params = '';
	var cDate = new Date();
	var uSec = cDate.getTime();

	params += "&13=" + getValueFromStr('_13', '', sAYSC, '_', '');
	params += "&14=" + getValueFromStr('_14', '', sAYSC, '_', '');
    params += "&17=" + getValueFromStr('_17', '', sAYSC, '_', '');
    params += "&18=" + getValueFromStr('_18', '', sAYSC, '_', '');
 	params += "&transId=" + uSec;
    return(params);
}

function getCookieValue(key, defaultVal){ return(getValueFromStr(key,defaultVal,document.cookie,';')); }

function getValueFromStr(key,defaultVal, inStr, sep, keySep ){
var value = defaultVal;
var begin;
var str;

        if( key && key.length > 0 ){
                key += arguments.length >= 5 ? keySep : '=';
                begin =  inStr.indexOf(key) > -1 ? inStr.indexOf(key) + key.length : -1;
                if( begin > -1){
                        str =  inStr.substr(begin, inStr.length);
                        value = str.substr(0, str.indexOf(sep) >= 0 ? str.indexOf(sep) : str.length);
                }
        }
        return(value);
}

//write doubleClick ad and link
function getAdHTML(pos,width,height,FTSite,FTSection,FTPage) {
    var thisDC;
	if (pos == "hp120x20") {
       thisDC = pos; // FT.com advert fixup
	} else {
	   thisDC = "doubleclick" + width;
   };

    var outStr = adImageInternal(thisDC,pos,1,width,height,FTSite,FTSection,FTPage);
    return outStr;
}

function finishPage() {
	if (is.ie) {
      document.write(getAdHTML("pop",1,1,FTSite,FTSection,FTPage));
	};

    if((is.nav4)&&(!isSecure(document))){
      for( var i = 0 ; i < tileSrc.length ; i++ ){
                  document.write(tileSrc[i]);
      }
    }
}

// Wrapper of backwards compatability
function adImage(vendor, tile){
	adImageInternal(vendor, tile, 1, -1,-1 );
}

function adImageInternal(vendor,tile,write,width,height,site,section,page) {

    var sec = "";
    var artid = "";
    var doWrite = adImageInternal.arguments.length >= 3 ? write : 1; // Default to write
    var sectionstr = '';
    var pagestr = '';


    if( typeof section != 'undefined' && section != '' ){
        sectionstr = section;
    } else {
        sectionstr = typeof FTSection != 'undefined' ? FTSection : '';
    }

    if( typeof page != 'undefined' && page != '' ){
        pagestr = page;
    } else {
        pagestr = typeof FTPage != 'undefined' ? FTPage : '';
    }

    if( sectionstr != '' && pagestr != '' ){
        vAdLink = '/site=ftcom&pos=' + tile + "&sec=" + sectionstr + '&artid=' + pagestr + getAdParams();
    }

    var outStr = "";

    if( isSecure(document) ){
        // No ad for secure documents
        outStr = '<img src="https://'+'globalelements.ft.com/Common/Wrapper/clear.gif"';
        outStr += ' width="'+width+'" height="'+height+'" border="0" />';
		if(is.ie){
			outStr += '<br>';
		}
    } else {
		if( document.layers ){
			outStr += '<a href=http://ads.ft.com/click.ng'+vAdLink+' target="_top">';
			outStr += '<img src=http://ads.ft.com/image.ng'+vAdLink+' width="'+width+'" height="'+height+'" target="_top" border=0>';
			outStr += '</a>';
		} else {
			outStr += '<iframe src="http://ads.ft.com/html.ng'+vAdLink+'" width="'+width+'" height="'+height+'" target="_top" marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no bordercolor="#000000">';
			outStr += '</iframe>';
		}
	    if (tile > numAds) { numAds=tile };
	}

    if( write == 1 ){
        document.write(outStr);
        outStr = '';
    }

    return outStr;
}

function submitForm(which){
  var form = eval("document.selectForm" + which);
  var submitStr;
  if (form.selectMenu.options[form.selectMenu.options.selectedIndex].value != "") {
    submitStr = form.selectMenu.options[form.selectMenu.options.selectedIndex].value;
    window.location = submitStr;
  }
}
