//http://fdimages.fairfax.com.au/crtvs/code/domain/writeMe.js
//090520 updated for guttering - sends homepage 300x250 through intermediatory - line 146
//090605 regex updated to send HP 300x250 trhough intermediatory with correct path (absolute) - line 150 [pborbely]


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);
}

function writeMe(myStr){

framit = myStr;
framit = framit.replace(/&amp;/gi,"&");
//framit = framit.replace(/\s/gi,"_");
framit = framit.replace(/^.*adspace=domaintext&loc=1[0-8].*$/i,"");
if(document.location.toString().toLowerCase().indexOf("buyersagents")!=-1)
 framit = framit.replace(/([/&])cat=[^&'"?]*/i,"$1cat=buyersagents").replace(/([/&])usertype=[^&'"?]*/i,"$1usertype=sell");
if(framit.length>0){
framitorig = framit;
start1 = framit.indexOf(".ng")+4;

end = framit.indexOf("\"",start1);
if(framit.toLowerCase().indexOf("src='")>0)
{
	end2 = framit.toLowerCase().indexOf("'",start1);

}
else
	end2 = 9999;
end3 = framit.indexOf("?",start1);
if(end==-1)
 end=9999;
if(end2==-1)
 end2=9999;
if(end3==-1)
 end3=9999;
if(end2<end && end2<end3)
 end = end2;
else if(end3<end2 && end3<end)
 end = end3;

targStr = framit.substring(start1,end);
targStr = targStr.replace(/'/g,"").replace(/\s/gi,"_").replace(/_&_/gi,"__").replace(/_\/_/gi,"__");
variablesa= targStr.split("&");

pos = getVal("loc",targStr);
if(pos.length!=0)
  pos = "/POS="+pos.toUpperCase();

zcat = getVal("cat",targStr).toUpperCase().replace(/PUBLIC_/i,"");
if(zcat.length==0)
 zcat = "OTHER";
cat2 = getVal("usertype",targStr).toUpperCase();
if(cat2.length!=0)
zcat = cat2.toUpperCase()+"."+zcat

target = "SITE=ONL.CL.ORE.DOMAIN.REALESTATE/AREA=REALESTATE.DOMAIN."+zcat.toUpperCase()+pos+"/";


for(i in variablesa)
{
	myVar = variablesa[i]+"";
	if(myVar.indexOf("=")!=variablesa[i].length-1&&myVar.length<100&&myVar.length!=0&&myVar.indexOf(" ")==-1&&i!="site")
		target+=myVar.toUpperCase().replace(/\+/gi,"_").replace(/AREA=/gi,"LOCAREA=").replace(/(public_)|-|\$|\\|\,/gi,"").replace(/CANTERBURY\/BANKSTOWN/gi,"CANTERBURY_BANKSTOWN").replace(/MACARTHUR\/CAMDEN/gi,"MACARTHUR_CAMDEN")+"/";
}

target = target.replace(/LOCALE=/i,"LOCSTATE=").replace(/SUBURB=/i,"LOCSUBURB=");
if(target.indexOf("LOCSTATE")==-1)
 target+="LOCSTATE=NAT/";

wid=1;
hei=1;
target = target.replace(/ADSPACE=IWOS/gi,"ADSPACE=3X"+getVal("loc",targStr));
target = target.replace(/ADSPACE=DOMAINTEXT/gi,"ADSPACE=0X"+getVal("loc",targStr));
target = target.replace(/ADSPACE=DOMAINRESIZABLE/gi,"ADSPACE=2X"+getVal("loc",targStr));
target = target.replace(/ADSPACE=MMMSHOWSPONSOR/gi,"ADSPACE=330X40");
target = target.replace(/ADSPACE=NETWORKAD/gi,"ADSPACE=1X4");
target = target.replace(/ADSPACE=CLIP/gi,"ADSPACE=5X"+getVal("loc",targStr));
target = target.replace(/ADSPACE=STRIP/gi,"ADSPACE=468X30");
if(target.indexOf("CAT=SEARCHRESULTS")!=-1 && target.indexOf("CAT1=LIST")==-1)
  target = target.replace(/ADSPACE=520X70/gi,"ADSPACE=638X68");
change=target.match(/ADSPACE=([^\/]*)/i);

if(change!=null&&!/\d+x\d+/i.test(change[1]))
{

try{
	wid=framit.match(/\swidth\s?=["'\s]+([^'"\s]+)["'\s]/i)[1];
	hei=framit.match(/\sheight\s?=["'\s]+([^'"\s]+)["'\s]/i)[1];
	target += "AAMSZ="+wid+"X"+hei+"/";
	}catch(e){target += "/AAMSZ=ERROR";}

}
else if(change!=null){
	target += "AAMSZ="+change[1]+"/";
}

//test for adtest and modify target if neccessary
target = target.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(target,testKeyword,testSizes);
	}

framit=framit.replace(/((ffxcam)|(campaigns.f2))[^/]*\/html\.ng/,"direct.fairfax.com.au/hserver");
framit=framit.replace(/((ffxcam)|(campaigns.f2))[^/]*\/js\.ng/,"direct.fairfax.com.au/jserver");
document.write("<!-- "+framit.substring(0,framit.indexOf("direct.fairfax.com.au")+30)+" -->");
document.write("<!-- "+target+" -->");
document.write("<!-- "+framitorig.substring(end,framitorig.length)+" -->");
html = framit.substring(0,framit.indexOf("direct.fairfax.com.au")+30)+target+"/"+framitorig.substring(end,framitorig.length);


if(html.indexOf("SEARCHRESULTS")!=-1 && html.toUpperCase().indexOf("160X600")!=-1)

{
	html = html.replace(/hserver/gi,"jserver");
	html = html.replace(/iframe/gi,"script");
}

if(html.indexOf("CAT=HOME")!=-1 && html.indexOf("AAMSZ=300X250")!=-1){
	html = html.replace(/hserver/gi,"jserver");
	//html = html.replace(/http:/i, "ffxd/intermediatory.html?ad=http:");
	html = html.replace(/http:\/\//, "http://www.domain.com.au/ffxd/intermediatory.html?ad=http://")
}

html=html.replace(/\?ord=([\d]+)/,"acc_random=$1");
document.write(html);
}
}