var aAdLink = new Array(new Object(),new Object(),new Object(),new Object(),new Object(),new Object(),new Object(),new Object());
aAdLink[0]['text'] = 'Speed Up Your PC - Free Scan ';
aAdLink[0]['desc'] = 'Significantly Improve PC Performance and Stability. Rated 5 Stars! ';
aAdLink[0]['lurl'] = 'http://ad.doubleclick.net/clk;212605154;28159734;e?http://www.pctools.com/registry-mechanic/?ref=display-pcw&utm_source=pcw&utm_medium=text&utm_content=util_rm_1';
aAdLink[0]['cats'] = '1494,1506,1503,1497,1498,1502,1499,1504,1505,863';
aAdLink[0]['weight'] = '1';
aAdLink[1]['text'] = 'Support Smarter with GoToAssist Express.';
aAdLink[1]['desc'] = 'Instantly view and control your customers? PCs or Macs online. Try GoToAssist Express FREE.';
aAdLink[1]['lurl'] = 'http://ad.doubleclick.net/clk;218503340;13346828;a?http://altfarm.mediaplex.com/ad/ck/9710-85449-1105-1';
aAdLink[1]['cats'] = '1427,1494,1506,1503,1497,1498,1502,1499,1504,1505,863,1443,1444,1727,1446,1445,1447,1448,1477,1484,1491,1478,1485,1486,1487,1489,1488,1490,1492,1493,1479,1480,1481,1482,1483,1468,1474,1470,1476,1471,1473,1719,1472,1475,1458,1459,1460,1462,1463,1461,1464,1449,1451,1455,1453,1456,1454,1450,1452,1433,1439,1434,1440,1438,1437,1441,1436,1442,1428,1431,1430,1429,1457,1465,1467,1466,1500,1501,1518,1508,1509';
aAdLink[2]['text'] = 'Top Download - Registry Mechanic';
aAdLink[2]['desc'] = 'Highly Awarded Software That Will Help You Improve PC Performance And Reliability! ';
aAdLink[2]['lurl'] = 'http://ad.doubleclick.net/clk;212605153;28159734;d?http://www.pctools.com/registry-mechanic/?ref=display-pcw&utm_source=pcw&utm_medium=text&utm_content=util_rm_2';
aAdLink[2]['cats'] = '1494,1506,1503,1497,1498,1502,1499,1504,1505,863';
aAdLink[2]['weight'] = '1';
aAdLink[3]['text'] = 'Find the latest Malware on your PC';
aAdLink[3]['desc'] = 'Scan your PC for the latest threats today. Free, Safe and Secure PC Scan';
aAdLink[3]['lurl'] = 'http://ad.doubleclick.net/clk;216246956;28159734;t?http://www.pctools.com/spyware-doctor/?ref=display-pcw&utm_source=pcw&utm_medium=text&utm_content=avs_sd_2';
aAdLink[3]['cats'] = '1443,1444,1727,1446,1445,1447,1448';
aAdLink[3]['weight'] = '1';
aAdLink[4]['text'] = 'Top Download - Spyware Doctor ';
aAdLink[4]['desc'] = 'Downloaded by over 100 Million People Worldwide. Find Out Why! ';
aAdLink[4]['lurl'] = 'http://ad.doubleclick.net/clk;212605156;28159734;g?http://www.pctools.com/spyware-doctor/?ref=display-pcw&utm_source=pcw&utm_medium=text&utm_content=avs_sd_1';
aAdLink[4]['cats'] = '1443,1444,1727,1446,1445,1447,1448';
aAdLink[4]['weight'] = '1';
aAdLink[5]['text'] = 'The Official SpeedUpMyPC - Free Scan Now';
aAdLink[5]['desc'] = 'Boost PC and Internet Speed with the official SpeedUpMyPC 2009!';
aAdLink[5]['lurl'] = 'http://ad.doubleclick.net/clk;216197737;13346828;r?http://www.liutilities.com/products/campaigns/ds/sp/pcworld/cpc/';
aAdLink[5]['cats'] = '1494,1506,1503,1497,1498,1502,1499,1504,1505,863,1468,1474,1470,1476,1471,1473,1719,1472,1475,1477,1484,1485,1486,1487,1488,1489,1490,1491,1492,1493,1478,1479,1481,1482,1480,1483,1458,1459,1460,1462,1463,1461,1464,1449,1451,1455,1453,1456,1454,1450,1452,1433,1439,1434,1440,1438,1437,1441,1436,1442,1428,1431,1430,1429,1457,1465,1467,1466,1507,1500,1501,1518,1508,1509';
aAdLink[6]['text'] = 'Free Driver Scan';
aAdLink[6]['desc'] = 'Click here to update all your PC\'s drivers';
aAdLink[6]['lurl'] = 'http://ad.doubleclick.net/clk;216197687;13346828;v?http://www.liutilities.com/products/campaigns/adv/ds/pcworld/cpc/';
aAdLink[6]['cats'] = '1506,1494,1503,1497,1498,1502,1499,1504,1505,863,1477,1484,1485,1486,1487,1488,1489,1490,1491,1492,1493,1478,1479,1480,1481,1482,1483,1458,1459,1460,1462,1463,1461,1464,1468,1474,1470,1476,1471,1473,1719,1475,1449,1451,1455,1453,1456,1454,1450,1452,1433,1439,1434,1440,1438,1437,1441,1442,1428,1431,1430,1429,1457,1465,1467,1466,1507,1500,1501,1518,1508,1509';
aAdLink[7]['text'] = 'Download Files From Usenet For Free';
aAdLink[7]['desc'] = 'Easily download files at blazing fast speeds. Over 800 Terabytes of uploads available';
aAdLink[7]['lurl'] = 'http://ad.doubleclick.net/clk;219966536;13346828;v?http://www.binverse.com/bnv/top-downloads2.cfm?ap_id=10281';
aAdLink[7]['cats'] = '1427,1494,1443,1477,1468,1458,1449,1433,1428,1457,1465,1507,1506,1503,1497,1498,1502,1499,1504,1505,863,1444,1727,1445,1446,1447,1448,1484,1485,1486,1487,1488,1489,1490,1491,1492,1493,1478,1479,1480,1481,1483,1482,1474,1470,1476,1471,1473,1719,1472,1475,1459,1460,1462,1463,1461,1464,1451,1455,1453,1456,1454,1450,1452,1439,1434,1440,1438,1437,1441,1436,1442,1431,1430,1429,1467,1466,1500,1501,1518,1508,1509';

var aTargetedAdeptAds=new Array();
var aWeightedAdeptAds=new Array();

function categoryMatches(aAd,pageCategory)
{
                if(aAd['cats']!=null){
                        var catList=aAd['cats'].split(",");
                        for (var i=0; i<catList.length;i++)
                        {
                                if (catList[i] == pageCategory)
                                        return true;
                        }
                }
                return false;
}

function pageTagMatches(aAd,pageTag){
        return (aAd['pgtag']==pageTag);
}

function randOrd(){
	return (Math.round(Math.random())-0.5); 
} 

function getTargetedAds(adsPerBox,id,catId,pageTag)
{
        var  adBoxArray=getObjectsByClassName(id);
        var  adLimit=adsPerBox*adBoxArray.length;

                var counter=0;
                var tries=0;
                // fill ad spots with ads target to Page or CatId first.
                for (var i=0;i<aAdLink.length;i++)
                {
                        var includeThisAd=false;
                        if (pageTag !=undefined){
                                includeThisAd=pageTagMatches(aAdLink[i],pageTag);
                        }
                        if (!includeThisAd && catId !=undefined){
                                includeThisAd=categoryMatches(aAdLink[i],catId);
                        }
                        if(includeThisAd){
                                if(aAdLink[i].weight != undefined && aAdLink[i].weight!=0){
                                        aWeightedAdeptAds.unshift(aAdLink[i]);
                                } else {
                                        aTargetedAdeptAds.push(aAdLink[i]);
                                }
                                aAdLink[i].mask=true;
                                counter++
                        }
                }
				
				//randomize order of unweighted ads
				aTargetedAdeptAds.sort(randOrd);
				
				//merge randomized ads with weighted ads
				aWeightedAdeptAds=aWeightedAdeptAds.concat(aTargetedAdeptAds);

                // fill ad spots with randomly selected ads.
        while(counter < adLimit && tries < aAdLink.length){
                rand = Math.floor(Math.random()*aAdLink.length);
                if(aAdLink[rand] !=undefined && aAdLink[rand].mask ==undefined)
                {
					if(aAdLink[rand]['pgtag']==undefined && aAdLink[rand]['cats']==undefined){
                        aWeightedAdeptAds.push(aAdLink[rand]);
                        counter++;
					}
                        aAdLink[rand].mask=true;
                }
                tries++;
        }

        var i=0;

        for(var currentBox=0;currentBox < adBoxArray.length ; currentBox++){
                var targetDiv=adBoxArray[currentBox];
		var textAdDiv=targetDiv.parentNode;
                var output='';
                        for (var j=0;i<aWeightedAdeptAds.length && j < adsPerBox;j++) {
                                output+="<a href='" + aWeightedAdeptAds[i]['lurl'] +"' class='linkInline' rel='nofollow'>";
                                output+= aWeightedAdeptAds[i]['text'];
                                output+='</a>';
                                output+='<div>';
                                output+=(aWeightedAdeptAds[i]['desc']);
                                if(aWeightedAdeptAds[i]['purl']){
                                        output+='<img width="1" height="1" alt="" src="'+aWeightedAdeptAds[i]['purl']+'" />';
                                }
                                output+='</div>';
                                i++;
                        }
						if(output.length > 0){
							targetDiv.innerHTML = output;
						} else {
							textAdDiv.parentNode.removeChild(textAdDiv);
						}
                }

}


