//ffx_swap - Will add the html to the innerHTML of the defined div
//090424 for GetMusic.com.au - dfinnecy
//090624 No domain setting, no intermediatory used

var siteString = "SITE=ONL.CH.GETMUSIC.MUSIC/AREA=MUSIC.GETMUSIC.";
if(ffxpageVars.cat != undefined && ffxpageVars.cat.length > 0) siteString += ffxpageVars.cat;
else siteString += "OTHER";
if(ffxpageVars.cat1 != undefined && ffxpageVars.cat1.length > 0) siteString += "."+ffxpageVars.cat1;
siteString += "/";

var intermediatory = "http://www.getmusic.com.au/ffxd/intermediatoryV2.html";

function fixStr(str){
  return str.toUpperCase().replace(/[/\-\&]/gi,"").replace(/[ +]/,"_");
}

function ffx_swap(elName, html) {
  var d = document;
  if (!d.getElementById) { return; }
  
  var el = d.getElementById(elName);
  if (!el) { return; }
  
  var remove = false;
  if (typeof ffxAdExclusionList != "undefined") {
    var thisPage = window.location.pathname;
    for (var i=0; i<ffxAdExclusionList.length; i++) {
      var current = ffxAdExclusionList[i];

      if (current == 'remove_all_ads'){ return; }
      else if (current == 'remove_article_ads'){
        if (thisPage.match(/\/news\/[a-zA-Z0-9-]+\/[a-zA-Z0-9-]+\/[0-9]{4}\/[0-9]{2}\/[0-9]{2}\/[0-9]+.html/) || thisPage.match(/\/articles\/.*?/)){
              remove = true;
            }
        }
      else if (current == 'remove_index_ads'){
            if  (thisPage.match(/\/index.html$/) || !thisPage.match(/.html$/)){
              remove = true;
            }
        }
      else if (current == thisPage) {
            return;
        }
      }
    }
 if (remove == true) {return;}
 el.innerHTML = html;
}

function getVal(zMSmyStr,zMStagline)
{
	zMSmyStr = zMSmyStr+"=";
	var zMSind = zMStagline.indexOf(zMSmyStr)+zMSmyStr.length;
	if(zMSind==(zMSmyStr.length-1)) //test for myStr not found
		return "";
	zMStagline += "&";
	return zMStagline.substring(zMSind,zMStagline.indexOf("&",zMSind));
}

// Testmodule - takes query string values for ad testing
function testMods(testTarg,testKey,testSize){
	testSizeMatch = 0;
	testSizeList = 0;
	if(testSize){
		testSize = testSize.split("_");
		testSizeList=1;
		}
	for(i=0;i<testSize.length;i++){
		if(testTarg.match("AAMSZ="+testSize[i])) testSizeMatch=1;
		}
	if(!testSizeList || testSizeMatch){
		testTarg = testTarg.replace(/SITE=[A-Z\.]+\//gi, "SITE=ZZ.ADTEST/");
		if(testTarg.indexOf("/KEYWORD=")!=-1) testTarg = testTarg.replace(/\/KEYWORD=[A-Z\.]*\//gi, "/KEYWORD="+testKey+"/");
		else testTarg = testTarg.replace(/SITE=ZZ.ADTEST\//gi, "SITE=ZZ.ADTEST/KEYWORD="+testKey+"/");
		}
	return(testTarg);
}


ran = Math.floor(Math.random()*100000000000000000);
var ad, x = 0;

 var currDom = document.domain, match, rootDomain;
 if(match = currDom.match(/([a-z0-9-_]+).(com|org|net|info|id|asn|biz|name)/i)){
 rootDomain = currDom.substr(currDom.indexOf(match[1]));
// try{
// if(rootDomain != currDom)
// document.domain = rootDomain;
// }
//catch(e){}
}


while(ad = ffxAds[x]) {
  if(typeof(ad.height) == "undefined")  hei = "1";
  else hei=ad.height;
  if(typeof(ad.width) == "undefined") wid = "1";
  else wid=ad.width;

  var adString = "";
  for(i in ad)
  if(i!="height"&&i!="width")
    adString += fixStr(i).toUpperCase()+"="+fixStr(ad[i]).toUpperCase()+"/";
    
  adString += "AAMSZ="+wid+"x"+hei+"/";
  	
  var pageString = siteString;
  for(i in ffxpageVars){
	pageString+= fixStr(i).toUpperCase()+"="+fixStr(ffxpageVars[i]).toUpperCase()+"/";
	}
	
  if(adString.length>0)
  	adString = adString.substring(0,adString.length-1);
  	
  var targetString = pageString+adString;
  
  targetString = targetString.toUpperCase();
  var urlStr = window.location.search.toUpperCase();
  if(urlStr.indexOf("ADTEST=TRUE")!=-1 && urlStr.indexOf("ADTESTKEYWORD=")!=-1){
  	testKeyword = getVal("ADTESTKEYWORD",urlStr);
  	testSizes = getVal("ADTESTSIZE", urlStr);
  	target = testMods(targetString,testKeyword,testSizes);
  	}
  
  var targetString = escape(targetString);
    
  //var call = intermediatory+"?w="+wid+"&h="+hei+"&ad=http://direct.fairfax.com.au/jserver/acc_random="+ran+"/";
  //if(ad.adtype == null || (ad.adtype.toLowerCase()!="panorama" && ad.adtype.toLowerCase()!="doubleisland")){
    call = "http://direct.fairfax.com.au/hserver/acc_random="+ran+"/";
    targetString=unescape(targetString);
  //}
  
  var ifrWid = wid;
  var ifrHei = hei;
  if(wid == 468 && hei == 60){
  ifrWid = 728;
  ifrHei = 90;
  }
  
  var iFrame = "<iframe id='ffxadiframe"+x+"' name='ffxadiframe"+x+"' allowtransparency='true' width="+ifrWid+" height="+ifrHei+" marginwidth=0 marginheight=0 hspace=0 VSPACE=0 frameborder=0 scrolling=no bordercolor='#000000' src='"+call+targetString+"'></iframe>";
  var holderDivId = "ffxad"+(x + 1);
  ffx_swap(holderDivId, iFrame);
  x++;
}