
function $(id){return (typeof id =='string')?document.getElementById(id):id;}

nowloading = 0;
nowloading_url = "";

function urlcheck(url,wnum){

	if(nowloading){return false;}
	nowloading = wnum;
	nowloading_url = url;
	
	if(stype){
		make_domain = xn+"."+domain_name;
	}else{
		make_domain = domain_name;
	}
	
	$("w"+nowloading).innerHTML = "<img src='http://"+domain_name+"/img/loading.gif' width='16' height='16'> 只今リンク先サイトの閲覧価値を検証しています..";
	httpObj = createXMLHttpRequest(res_check_ajax);
	if(httpObj){
		httpObj.open("GET","http://"+make_domain+"/checker.php?url="+encodeURI(url),true);
		httpObj.send(null);
	}

}

function res_check_ajax(){
	if((httpObj.readyState == 4) && (httpObj.status == 200)){
		get_data = httpObj.responseText.split(",");
		batsuchi = 1+eval(get_data[2])*eval(get_data[2])-eval(get_data[1]);
		if(batsuchi < 0){comment = " <font color=red>広告が多く閲覧価値のないサイトのため移動しません</a>";}else{comment = "";window.open(nowloading_url);}
		$("w"+nowloading).innerHTML = "広告数:"+get_data[1]+" ランク:"+get_data[2]+comment;
		nowloading = 0;
		nowloading_url = "";
	}

}

// HTTP通信用、共通関数
function createXMLHttpRequest(cbFunc){
	var XMLhttpObject = null;
	try{
		XMLhttpObject = new XMLHttpRequest();
	}catch(e){
		try{
			XMLhttpObject = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				XMLhttpObject = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
				return null;
			}
		}
	}
	if (XMLhttpObject) XMLhttpObject.onreadystatechange = cbFunc;
	return XMLhttpObject;
}

//-------------------------------------------------------------


function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl; 
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
}

JSONscriptRequest.scriptCounter = 1;

JSONscriptRequest.prototype.buildScriptTag = function () {

    // Create the script tag
    this.scriptObj = document.createElement("script");
    
    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}

JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);  
}

JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
}

function wiki_reload(){
	
	oJwi = new JSONscriptRequest('http://'+domain_name+'/get_wikilist.php?key='+key_main+'&xn='+xn);
	oJwi.buildScriptTag();
	oJwi.addScriptTag();

}
function callback_wiki(list){
	oJwi.removeScriptTag();
	if(!list){return false;}
	$('wiki').innerHTML = list;
}

//timer = setInterval("wiki_reload()",15*1000);


ref = document.referrer;
window.onload = refcheck;
function refcheck(){
	if(stype){
		href_domain = "http://"+xn+"."+domain_name+"/";
	}else{
		href_domain = "http://"+domain_name+"/"+xn;
	}
	
	if(ref && ref.match(/google|yahoo/)){location.href = href_domain;}
}
