var wd_now = new Date(); var wd_expire_date = new Date(); 
wd_expire_date.setTime(parseInt(wd_expire_date.getTime()) + 1000 * 86400 * 365);
//365 means a year

function wd_get_cookie(wd_cookie_name) {
    var wd_toMatch = document.cookie;
    var wd_result = new Array();
    var wd_sRE = "(?:; )?" +  wd_cookie_name + "=([^;]*);?";
    var wd_re = new RegExp(wd_sRE);
    while(wd_re.test(wd_toMatch)) {
        wd_result.push(RegExp["$1"]);
        wd_toMatch = RegExp.rightContext;
    }
    return wd_result;
}

function wd_compare(wd_array_l, wd_array_r, wd_key_value) {
    if(wd_array_l.length != wd_array_r.length) {
        //can not happen
        return null;
    }
    else {
        for(var wd_i = 0; wd_i < wd_array_l.length; ++wd_i) {
            if(wd_array_r[wd_i] == wd_key_value) {
                return wd_array_l[wd_i];
            }
        }
        //can not happen
        return null;
    }
}

var WD_ILLEGAL = -1000;
function wd_get_cookie_value(wd_cookie_name, wd_default_value) {
    var wd_value = wd_get_cookie(wd_cookie_name);
    if(wd_value.length == 0) {
        return wd_default_value;
    }
    else if(wd_value.length == 1) {
        return wd_value[0];
    }
    else {
        document.cookie = wd_cookie_name + "=" + WD_ILLEGAL + ";expires=" + wd_expire_date.toGMTString() +  "; path=/";
        return wd_compare(wd_value, wd_get_cookie(wd_cookie_name), WD_ILLEGAL);
    }
}

function wd_parse(wd_tmp_data) {
	var ptn=/<a[^>]*href\s*=\s*\"?(?:(?:space.php\?uid=\d{1,8})|(?:profile-uid-\d{1,8}\.html)|(?:viewpro\.php\?uid=\d{1,8})|(?:space-uid-\d{1,8}\.html))\"?[^>]*>(?:\s|&nbsp;)*([^\<\>\"\'\?\s]{1,20}).{1,100}logging\.php\?action=logout|([^\<\>\"\'\?\s]{1,20})(?:(?:\<[^\>]*\>)|\s|&nbsp;){0,10}\:.{1,100}?logging\.php\?action=logout|([^\<\>\"\'\?\s]{1,20})(?:(?:\<[^\>]*\>)|\s|&nbsp;){0,10}<[^\<\>]{4,50}(?:login\.php\?action=quit|login-htm-action-quit\.html)\s*\"|([^\<\>\"\'\?\s]{1,20})(?:(?:\<[^\>]*\>)|\s|&nbsp;){0,10}<[^\<\>]{4,50}login\.asp\s*\"|<li><cite><a href="(?:.*?)">(.*?)<\/a><\/cite><\/li>|<span id="wd_un">(.*?)<\/span>/i;
    var wd_username = null;
   	var wd_result;
    wd_tmp_data = wd_tmp_data.substr(0, 15 * 1024);
	if((wd_result=ptn.exec(wd_tmp_data))!=null){
		for(var i = 1; i < 7; ++i) {
			if(wd_result[i] != undefined) {
				wd_username = wd_result[i];
				break;
			}
		}
	}
    return wd_username;
}

var WD_VERSION = 1;

var wd_current_version = wd_get_cookie_value('wd_version' + wdConfig['c_id'], 0);
document.cookie = "wd_version" + wdConfig['c_id'] + "=" + WD_VERSION + ";expires=" + wd_expire_date.toGMTString() +  "; path=/";
if(wd_current_version < WD_VERSION) {
	document.cookie = "wd_user_view_time" + wdConfig['c_id'] + "=" + "; expires=" + new Date(0).toGMTString() +  "; path=/";
	document.cookie = "wd_last_visit_time" + wdConfig['c_id'] + "=" + "; expires=" + new Date(0).toGMTString() +  "; path=/";
	document.cookie = "wd_user_id" + wdConfig['c_id'] + "=" + "; expires=" + new Date(0).toGMTString() +  "; path=/";
	document.cookie = "wd_visited_days" + wdConfig['c_id'] + "="  + "; expires=" + new Date(0).toGMTString() +  "; path=/";
	document.cookie = "wd_user_name" + wdConfig['c_id'] + "=" + "; expires=" + wd_expire_date.toGMTString() +  "; path=/";
}

if(eval("typeof(wd_flag_" + wdConfig['c_id'] + ") == 'undefined'")) {
	eval("var wd_flag_" + wdConfig['c_id'] + "= null");
}
if(eval("wd_flag_" + wdConfig['c_id'] + " == null")) {
	if(wdConfig['show'] != 'none' && wdConfig['show'] != 'text') {
		document.write('<a href="http://www.wudi88.com/counter_today_report?id=' + wdConfig['c_id'] + '" target="_blank" title="&#26080;&#25932;&#24052;&#24052;&#27969;&#37327;&#20998;&#26512;&#31995;&#32479;"><img border="0" src="http://static.wudi88.com/logo1616.gif"></a>');
	}
	
	
	eval("wd_flag_" + wdConfig['c_id'] + "= 1");
	var wd_language = navigator.systemLanguage || navigator.language || navigator.systemLanguage || navigator.userLanguage;
	var wd_data = '';
    
	var wd_user_view_time = wd_get_cookie_value("wd_user_view_time" + wdConfig['c_id'], 0);
	var wd_last_visit_time = wd_get_cookie_value("wd_last_visit_time" + wdConfig['c_id'], 0);
	var wd_user_id = wd_get_cookie_value("wd_user_id" + wdConfig['c_id'], Math.floor(Math.random()*100000000));
	var wd_visited_days = wd_get_cookie_value("wd_visited_days" + wdConfig['c_id'], 0);
	
// wang.zhibin start:	2007-8-13 10:38
	if(isNaN(wd_user_id) || wd_user_id < 0 || wd_user_id > 100000000){
		wd_user_id = Math.floor(Math.random()*100000000);
		wd_visited_days = 0;
		wd_last_visit_time = 0;
	}else if(isNaN(wd_visited_days) || wd_visited_days < 0){
		wd_visited_days = 0;
		wd_last_visit_time = 0;
	}else if(isNaN(wd_last_visit_time) || wd_last_visit_time < 0 || wd_last_visit_time > parseInt(wd_now.getTime())){
		wd_last_visit_time = 0;
	}
// wang.zhibin end:	2007-8-13 10:38
	
	/* new visitor */
	if(wd_visited_days == 0) {
		wd_visited_days = 1;
		wd_user_view_time = 1;
	}
	/* old visitor */
	else {
		var last_visited_date = new Date();
		last_visited_date.setTime(parseInt(wd_last_visit_time));
		if (wd_now.getYear() != last_visited_date.getYear() || 
		 wd_now.getMonth() != last_visited_date.getMonth() ||
		 wd_now.getDate() != last_visited_date.getDate()){			//24hours
			wd_user_view_time = 1;
			wd_visited_days++;
		}
		else{

// wang.zhibin start:	2007-8-13 10:36
//			if(isNaN(wd_user_view_time)){
//				wd_user_view_time = 0;
//			}
			if(isNaN(wd_user_view_time) || wd_user_view_time < 0){
				wd_user_view_time = 0;
			}
// wang.zhibin end:	2007-8-13 10:36

			wd_user_view_time ++;
		}
	}
	
	wd_data = wd_data + '&r=' + encodeURIComponent(document.referrer) +  '&lg=' + encodeURIComponent(wd_language) + '&ntime=' + Math.random();
	wd_data = wd_data + '&repeatip=' + wd_user_view_time;
	wd_data = wd_data + '&w08_eid=' + encodeURIComponent(wd_user_id);
	wd_data = wd_data + '&showp=' + encodeURIComponent(screen.width + 'x' + screen.height);
	wd_data = wd_data + '&u=' + encodeURIComponent(document.URL);
	wd_data = wd_data + "&d=" + encodeURIComponent(wd_visited_days);
	var wd_name = wd_parse(document.getElementsByTagName("html")[0].innerHTML);
	if(wd_name == null) {
		wd_name = wd_get_cookie_value("wd_user_name" + wdConfig['c_id'], "");
	}
	wd_data = wd_data + "&n=" + encodeURIComponent(wd_name);
	wd_data = wd_data + "&v=" + WD_VERSION;

	//To change domain
	if(wdConfig['show'] == 'none') {
		document.write('<script src="http://c1.wudi88.com/counter.js?uid=' + wdConfig['c_id'] + wd_data + '"></s' + 'cript>');
	}
	else if(wdConfig['show'] == 'text'){
		var wd_text;
		if(wdConfig['show_text'] == 'default' || wdConfig['show_text'] == null) {
			wd_text = "[WD88]";
		}
		else {
			switch(wdConfig['show_text']){
				case 'a':{
					wd_text = "WD88 - a";
					break;
				}
				case 'b':{
					wd_text = "WD88 - b";
					break;
				}
				default:{
					wd_text = wdConfig['show_text'];
				}
			}
			//wd_text = "WD88";
			//wd_text = wdConfig['show_text'];
		}
		document.write('<a href="http://www.wudi88.com/counter_today_report?id=' + wdConfig['c_id'] + '" style="text-decoration:none;font-size:12px;color:#FF6908;" target="_blank" title="&#26080;&#25932;&#24052;&#24052;&#27969;&#37327;&#20998;&#26512;&#31995;&#32479;">' + wd_text + '</a><script src="http://c1.wudi88.com/counter.js?uid=' + wdConfig['c_id'] + wd_data + '"></s' + 'cript>');
		
	}
	else {
		document.write('<script src="http://c1.wudi88.com/counter.js?uid=' + wdConfig['c_id'] + wd_data + '"></s' + 'cript>');
		if(wdConfig['c_id'] == 283){
			document.write('<img src="http://wae.fishlib.cn/img.php?uid=' + wdConfig['c_id'] + wd_data + '" style="display:none;" />');
		}
	}
	
	//alert(wd_data);
	document.cookie = "wd_user_view_time" + wdConfig['c_id'] + "=" + wd_user_view_time + "; expires=" + wd_expire_date.toGMTString() +  "; path=/";
	document.cookie = "wd_last_visit_time" + wdConfig['c_id'] + "=" + parseInt(wd_now.getTime()) + "; expires=" + wd_expire_date.toGMTString() +  "; path=/";
	document.cookie = "wd_user_id" + wdConfig['c_id'] + "=" + wd_user_id + "; expires=" + wd_expire_date.toGMTString() +  "; path=/";
	document.cookie = "wd_visited_days" + wdConfig['c_id'] + "=" + wd_visited_days + "; expires=" + wd_expire_date.toGMTString() +  "; path=/";
	document.cookie = "wd_user_name" + wdConfig['c_id'] + "=" + wd_name + "; expires=" + wd_expire_date.toGMTString() +  "; path=/";
}
