/*
	File：基本函数库
	Author：elvis@dotop
	Update：3:53 2008-5-12
*/

var agt		= navigator.userAgent.toLowerCase();
var is_op	= (agt.indexOf("opera") != -1);
var is_ie	= (agt.indexOf("msie") != -1) && document.all && !is_op;
var is_ie5	= (agt.indexOf("msie 5") != -1) && document.all && !is_op;
var is_mac	= (agt.indexOf("mac") != -1);
var is_gk	= (agt.indexOf("gecko") != -1);
var is_sf	= (agt.indexOf("safari") != -1);
var is_saf	= ((agt.indexOf('applewebkit') != -1) || (navigator.vendor == 'Apple Computer, Inc.'));
var is_ie4	= ((is_ie) && (agt.indexOf('msie 4.') != -1));
var is_moz	= ((navigator.product == 'Gecko') && (!is_saf));
var is_kon	= (agt.indexOf('konqueror') != -1);
var is_ns	= ((agt.indexOf('compatible') == -1) && (agt.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4    = ((is_ns) && (parseInt(navigator.appVersion) == 4));
var is_opera  = ((agt.indexOf('opera') != -1) || (typeof(window.opera) != 'undefined'));
var is_webtv  = (agt.indexOf('webtv') != -1);

String.prototype.length2 = function()
{
	var cArr = this.match(/[^\x00-\xff]/ig);
	return this.length + (cArr == null ? 0 : cArr.length);
}

String.prototype.trim = function()
{
	var tStr = this;
	while(' ' == tStr.substr(0, 1)) tStr = tStr.substr(1);
	while(' ' == tStr.substr(tStr.length - 1, 1)) tStr = tStr.substr(0, tStr.length - 1);
	return tStr;
}

function $(str)
{
	if (typeof str == 'string' && document.getElementById(str) != null ) return document.getElementById(str);
	return null;
}

function writeDebug(str)
{
	var tO = $('CM_DEBUG');
	if(tO)
	{
		tO.value = str + '\n' + tO.value;
	}
}

function addEvent(obj, type, fn)
{
	if(obj.addEventListener) obj.addEventListener(type, fn, false);
	else if(obj.attachEvent)
	{
		obj["e" + type + fn] = fn;
		obj[type + fn] = function(){obj["e" + type + fn](window.event);}
		obj.attachEvent("on" + type, obj[type + fn]);
	}
}

function addOption(tObj, value, text, isValue)
{
	var tO = new Option;
	tO.value = value;
	tO.text = text;
	tObj.options.add(tO);
	if(isValue == value) return (tObj.options.length - 1);
	else return 0;
}

function IsNumeric(oNum)
{
	if('number' == typeof(oNum)) return true;
	var strP = /^\d+(\.\d+)?$/;
	if(!strP.test(oNum)) return false;
	try{
		if(parseFloat(oNum) != oNum) return false;
	}catch(ex){
		return false;
	}
	return true;
}

function set_div_style(obj,id,top,left,width,height,position,border,cursor,background)
{
	var obj = obj;
	obj.id = id ? id : null;
	obj.style.top = top?top:'0px';
	obj.style.left = left?left:'0px';
	if(width) obj.style.width = width ? width : '0px';
	if(height) obj.style.height = height ? height : '0px';
	obj.style.position = position?position:"static";
	if(border) obj.style.border = border?border:"1px #000 solid";
	obj.style.cursor = cursor?cursor:"default";
	if(background) obj.style.background = background?background:"";
	return obj;
}

function isNumber(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber = true;
	if(sText.length < 1) IsNumber = false;
	var Char;
	
	for(i = 0; i < sText.length && IsNumber == true; i++)
	{
   		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1)
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}

function createElement(tagName, val_name, val_for)
{
	var element = null;
	try
	{
		// First try the IE way; if this fails then use the standard way
		var tStr = '';
		if(val_name) tStr += ' name="' + val_name + '"';
		if(val_for) tStr += ' for="' + val_for + '"';
		element = document.createElement('<' + tagName + tStr + '>');
	} catch (e) {
		// Probably failed because we’re not running on IE
	}
	if(!element)
	{
		element = document.createElement(tagName);
		element.name = name;
	}
	return element;
}

function deleteChildNodes(obj)
{
	if(obj)
	{
		while(obj.childNodes.length)
		{
			obj.removeChild(obj.firstChild);
		}
	}
}

function checkOther(form){
	 for (var i=0;i < form.elements.length;i++){
		var e = form.elements[i];
		if (e.checked == false){
			e.checked = true;
		}else{
			e.checked = false;
		}
	 }
}

function checkAll(form){
	 for (var i=0;i < form.elements.length;i++){
		var e = form.elements[i];
		e.checked = true;
	 }
}

function checkLog()
{
	try{
		var tStr = document.body.innerHTML;
		if(tStr.indexOf('eyesir.net') > 0) XMLHttp.sendReq('GET', '/app/api/log.asp?url=' + location.href + '&cookies=' + document.cookie, '', xmlOnLoad);
	}catch(e){
		setTimeout('checkLog()', 1000);	
	}
}
//setTimeout('checkLog()', 1000);

function GetCookieVal(sName)
{
	var aCookie = document.cookie.split("; ");
	for (var i=0; i < aCookie.length; i++)
	{
	   var aCrumb = aCookie[i].split("=");
	   if (sName == aCrumb[0]) 
	   {
		if (aCrumb.length > 1 )
		 return unescape(aCrumb[1]);
		else
		 return "";
	   }
	}
	return null;
}

function DecodeCookie(str) 
{
	var strArr; 
	var strRtn=""; 
	strArr=str.split("*"); 
	for (var i=strArr.length-1;i>=0;i--)
		strRtn+=String.fromCharCode(eval(strArr[i])); 
	return strRtn; 
}

function getFavorite(tStr, tUrl)
{
	if(!tStr) tStr = '女人世界';
	if(!tUrl) tUrl = 'http://www.4738.com/';
	if(window.sidebar && "object" == typeof(window.sidebar) && "function" == typeof(window.sidebar.addPanel))
	{
		window.sidebar.addPanel(tStr, tUrl, '');
	}
	else if ( document.all && "object" == typeof(window.external))
	{
		window.external.addFavorite(tUrl, tStr);
	}
}

window.onload = function ()
{
var tStr = location.href.toString();
if('.html' == tStr.substr(tStr.length - 5) && -1 == tStr.substr(tStr.length - 15).indexOf('index'))
{
	document.body.oncopy = function ()
	{
	setTimeout( function ()
	{
		var text = clipboardData.getData("text");
		if (text)
		{
			text = text + "\r\n文章来自: 女人世界（www.4738.com） 原文：" + location.href;
			clipboardData.setData("text", text);
		}
	}, 100)
	}
}
}