///////////////////////////////////////////////////////////
function $(id, show_err){
	var o=O(id);
	if (o!==false) return o; 
	else{
		try{ o=document.getElementByName(id); }
		catch(e){
			try{ o=document.getElementsByName(id); }catch(e){o=false;}
		}
		if(o!==null) return o; else return false;
	}
}
function O(id){
	var o=document.getElementById(id);
	if(o!==null) return o; else return false;
}
///////////////////////////////////////////////////////////
var hk_ads = {
	explode: function(ele, delimiter) {
		tempArray=new Array(1);
		var count=0;
		var tmp_Str=new String(ele);
		while (tmp_Str.indexOf(delimiter)>0) {
			tempArray[count]=tmp_Str.substr(0,tmp_Str.indexOf(delimiter));
			tmp_Str=tmp_Str.substr(
				tmp_Str.indexOf(delimiter)+delimiter.length,
				tmp_Str.length-(tmp_Str.indexOf(delimiter)+delimiter.length)+1
			);
			count++;
		}
		tempArray[count]=tmp_Str;
		return tempArray;
	}, ///////////////////////////////////////////////////////////
	ads_web: '',//'http://ads.guideball.com/ads/',
	ads_idx: Array(), ads_list: Array(), web_id: '', ads_result: Array(),
	request_ok: function (rs, ele){
		if(ele=='get_ads') { this.get_ads_rs(rs); } else
		if (ele!='no_alert') alert('22||'+rs);
	},
	set_ads: function(){
		var j=0, k=this.ads_list.length;
		for (j ; j<k ; j++ ){
			this.ads_idx[this.ads_list[j]['div_id']]=j;
			this.get_ads(this.ads_list[j], 0);
		}
	},
	count_view: function(tag_id, ads_id){
		var param='PJN=view&web_id='+this.web_id+'&tag_id='+tag_id+'&ads_id='+ads_id;
		ads_ajax(this.ads_web+'ajax_ads.php', param, '', '');
	}, //// ok|fifa_game|1|10|<object
	show_ads: function(div_id, item){
		var a = this.ads_result[div_id]; var c=a.list.length; var l=a.list[item];
		if(l.cookie_mins>=0){
			if(l.noscript) $(div_id).innerHTML=l.html;
			else {
				var tdiv=$(div_id);
				tdiv.innerHTML='';
				eval(l.html);
			}
			this.count_view(div_id, l.id);
			if(l.cookie_mins>0){
				var cookie_name=div_id+'_'+l.id;
				this.Set_Cookie(cookie_name, cookie_name, l.cookie_mins, '', '', 0 );
				this.ads_result[div_id].list[item].cookie_mins=-1;
				this.ads_result[div_id].count_show++;
			}
			if(c>1){
				item++; if (item>=c) item=0;
				setTimeout('hk_ads.show_ads("'+div_id+'", '+item+')', a.list[item].show_time * 1000);
			}
		}else if(a.list.length<=a.count_show){
			if (l.default_div.length)
				$(div_id).innerHTML=$(l.default_div).innerHTML
			else if(l.redirect_url.length)
				location.href=l.redirect_url;
		}
	}, /////////////////////
	get_ads: function(ob, ads_item){
		var param='PJN=get&web_id='+this.web_id+'&ads_id='+ads_item+'&tag_name='+ob.div_id;
		ads_ajax(this.ads_web+'ajax_ads.php', param, 'get_ads', hk_ads);
	}, //// ok|fifa_game|1|10|<object
	get_ads_rs: function(rs){
		//alert(rs);
		//rs=ok|hk|fifa_game|hk|id|10|30|<object banner 1>|hk|id|20|0|<object  banner 2>
		//hk_ads.ads_list=Array({div_id:'smirnoff_cover', default_div: '', redirect_url: '2007new_ver/index.php'});
		var ar_rs=this.explode(rs, '|hk|');
		if(rmo_addr=='61.91.117.42') alert(rs);
		if(ar_rs[0]=='empty'){
			var div_id=ar_rs[1];
			var l=this.ads_list[this.ads_idx[div_id]];
			if (l.default_div)
				$(div_id).innerHTML=$(l.default_div).innerHTML
			else if(l.redirect_url)
				location.href=l.redirect_url;
		}else if (ar_rs[0]=='ok'){
			var div_id=ar_rs[1];
			this.ads_result[div_id]={count_show: 0, list: Array()};
			var c=ar_rs.length; var j=2;
			for(j; j<c; j++){
				var b=this.explode(ar_rs[j], '|');
				var cookie_name=div_id+'_'+b[0];
				var chk_cookie=parseInt(b[2])? (this.Get_Cookie(cookie_name)>''|false) : false;
				if(!chk_cookie)
					this.ads_result[div_id].list[j-2]={
						id:b[0],
						show_time: parseInt(b[1]),
						cookie_mins: parseInt(b[2]),
						noscript: b[3]!='script',
						html: (b[3]!='script'? b[3] : this.urlDecode(b[4]).replace(/\{hk\}/g, '+')),
						count_show: 0
					};
			}
			if(this.ads_result[div_id].list.length>0) this.show_ads(div_id, 0);
			else{
				var l=this.ads_list[this.ads_idx[div_id]];
				if (l.default_div)
					$(div_id).innerHTML=$(l.default_div).innerHTML
				else if(l.redirect_url)
					location.href=l.redirect_url;
			}
		}
	},
	urlEncode: function (str) {
		str = escape(str);
		str = str.replace('+', '%2B');
		str = str.replace('%20', '+');
		str = str.replace('*', '%2A');
		str = str.replace('/', '%2F');
		str = str.replace('@', '%40');
	return str;
	}, //////////////////////////////////////
	urlDecode: function (str) {
		str = str.replace(/\+/g, ' ');
		str = unescape(str);
		return str;
	}, //////////////////////////////////////
	Set_Cookie: function( name, value, expires_mins, path, domain, secure ) {
		// set time, it's in milliseconds
		var today = new Date();
		today.setTime( today.getTime() );
		/*
		if the expires variable is set, make the correct
		expires time, the current script below will set
		it for x number of days, to make it for hours,
		delete * 24, for minutes, delete * 60 * 24
		*/
		if ( expires_mins ) { expires_mins = expires_mins * 1000 * 60; }
		var expires_date = new Date( today.getTime() + (expires_mins) );

		document.cookie = name + "=" +escape( value ) +
		( ( expires_mins ) ? ";expires=" + expires_date.toGMTString() : "" ) +
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
	},
	// this fixes an issue with the old method, ambiguous values
	// with this test document.cookie.indexOf( name + "=" );
	Get_Cookie: function( check_name ) {
		// first we'll split this cookie up into name/value pairs
		// note: document.cookie only returns name=value, not the other components
		var a_all_cookies = document.cookie.split( ';' );
		var a_temp_cookie = '';
		var cookie_name = '';
		var cookie_value = '';
		var b_cookie_found = false; // set boolean t/f default f

		for ( i = 0; i < a_all_cookies.length; i++ ) {
			a_temp_cookie = a_all_cookies[i].split( '=' );
			cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
			if ( cookie_name == check_name ){
				// we need to handle case where cookie has no value but exists (no = sign, that is):
				if ( a_temp_cookie.length > 1 ){
					cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
				}
				return cookie_value;
				break;
			}
			a_temp_cookie = null; cookie_name = '';
		} return null;
	},
	// this deletes the cookie when called
	Delete_Cookie:function( name, path, domain ) {
		if ( Get_Cookie( name ) ) document.cookie = name + "=" +
		( ( path ) ? ";path=" + path : "") +
		( ( domain ) ? ";domain=" + domain : "" ) +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}
///////////////////////////////////////////////////////////
function ads_ajax(url, param, ele, callback){
	//hk_ajax_2009_02
	var http;
	if(window.XMLHttpRequest){   //IE7,   Mozilla   ,Firefox   ?????????
		http = new XMLHttpRequest();
	}else if(window.ActiveXObject){ //IE6?IE5
		try{ http = new ActiveXObject( "Msxml2.XMLHTTP ");} catch (e){ ; }
		if( http ==  null) try   { http = new ActiveXObject( "Microsoft.XMLHTTP ");}   catch   (e){; }
	}
	try {
		http.open("POST", url, false);
	} catch (e) { alert( "2: Error http.open: " + e + "\n\n"+ "Click OK to continue.\n\n"); return false; }
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", param.length);
	http.setRequestHeader("Connection", "close");
	http.send(param);
	if(http.readyState == 4)
		if(http.status == 200) { var txt=http.responseText; if(callback) callback.request_ok(txt, ele); }
	http = null;
}
/*
///////////////////////////////////////////////////////////////////////////
///////  Below is Class and Function to recieve result from this function.
///////////////////////////////////////////////////////////////////////////
var js_name={
	request_ok: function (rs, ele){
		if(ele=='get_ads') { this.get_ads_rs(rs); } else
		if (ele!='no_alert') alert('22||'+rs);
	},
}
/////////////////////////////////////////////////////////////////////*/

