YSMTool = new function() {
    this.RandomArray = function(aArray) {
        var randomOrder = function(a,b){
            return (Math.round(Math.random())-0.5);
        };
        return aArray.sort(randomOrder);
    }
};

function check_ysmloaded() {	
	if (zSr == null){
		setTimeout(check_ysmloaded,10);
	}else{
			ShowListings();
	}
}

//Parsing Listing
function  ShowListings()  {
	if (zSr.length > 6){
				var  html="";
				
				html = html + '<div><ul>';

				var tmpn = 1;
				var  i=6;
				//Listing
				while  (i  <  zSr.length)  {
				var  descr  =  zSr[i++];	//  listing  description 
				var  unused1  =  zSr[i++];	//  (ignore)
				var  clickURL  =  zSr[i++];	//  listing  link 
				var  title  =  zSr[i++];	//  listing  title
				var  sitehost  =  zSr[i++];	//  advertisers  domain  name 
				var  unused2  =  zSr[i++];	//  (ignore)
				
					html=  html  +
						'<li onClick="window.open(\'' + clickURL + '\')" style="cursor:pointer;">'+
						'<a>' + title + '</a>'+
						'<div>' + descr + '</div>'+
						'<cite>' + sitehost + '</cite>'+
						'</li>';
				
				tmpn = tmpn + 1;
				}
				
				html = html + '</ul></div>';
				html = html + '<div><span class="go-sponsor"><a href="http://tw.rd.yahoo.com/referurl/search/evt=30238/*http://tw.emarketing.yahoo.com/ysm/contactus/index.html?o=TW0145&cmp=TW0145" target="_new">Ads by Yahoo</a></span></div>';
				var a = document.getElementById("articleExt_relate_ads");
				if( a !== null && a !== '')	
					document.getElementById("articleExt_relate_ads").innerHTML=html;		
		
		}
		else {
			var a = document.getElementById("articleExt_relate_ads");
			if( a !== null && a !== '')	
				document.getElementById("articleExt_relate_ads").innerHTML="";
		}
}

function loadContent(file){
  var scriptTag = document.getElementById('loadScript');
  var head = document.getElementsByTagName('head').item(0)
  if(scriptTag) head.removeChild(scriptTag);
  script = document.createElement('script');
  script.src = file;
  script.setAttribute('charset','utf-8');
	script.type = 'text/javascript';
	script.id = 'loadScript';
	head.appendChild(script)
}

//Get Listing
function GetResults(){

	var q_url = "";
	zSr = null;
	q_url = 'http://cmh.tw.yahoo.overture.com/js_flat_1_0/?config=20822950855&source=' + param_partner + '&mkt=tw&maxCount='+ ysm_maxCount + '&ctxtUrl=' + encodeURIComponent(document.location.href) +'&outputCharEnc=utf8&cb=' + Math.random() + Date.parse(new Date()) + '&ctxtId='+ param_ctxtID;
	loadContent(q_url);
	check_ysmloaded();
}

GetResults() ;

