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

function writeMe(myStr){

framit = myStr;
framit = framit.replace(/&amp;/gi,"&");
framitorig = framit;
start1 = framit.indexOf(".ng")+4;
end = framit.indexOf("\"",start1);
end2 = framit.indexOf("'",start1);
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);
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("cat2",targStr).toUpperCase();

target = "SITE=ONL.CL.ORE.COMMERCIAL.REALESTATE/AREA=REALESTATE.COMMERCIAL."+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,"")+"/";
}

target = target.replace(/LOCALE=/i,"LOCSTATE=").replace(/SUBURB=/i,"LOCSUBURB=");

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=CRERESIZABLE/gi,"ADSPACE=2X1");
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");
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]+"/";
}

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("hserver")!=-1)
{
 	html = html.replace(/hserver/gi,"jserver");
	html = html.substring(0,html.indexOf("http://direct"))+"http://www.essentialbaby.com.au/ffxd/intermediatoryV2.html?ad="+html.substring(html.indexOf("http://direct"),html.length);
}*/
html=html.replace(/\?ord=([\d]+)/,"acc_random=$1");
document.write(html);}
