
String.prototype.Trim = function() { return this.replace(/(^\s*)|(\s*$)/g,""); }
String.prototype.replaceAll = function(s1,s2) { return this.replace(new RegExp(s1,"gm"),s2); }
String.prototype.pureNumber = function() { return this.replace(/[^0-9]/ig,""); }
String.prototype.pureFloat = function() { return this.replace(/[^0-9.]/ig,""); }

function I2(v) { return (v<10) ? "0" + v : v; }

var isIE = !!window.ActiveXObject;
var isIE6 = isIE&&!window.XMLHttpRequest;

var imgOk = "<img src=\"/Images/right.gif\"> ";
var imgErr = "<img src=\"/Images/error.gif\"> ";

function SingleLine(str)
{
	str = "" + str;
	return str.replaceAll("'","&#39;").replaceAll("<","&lt;").replaceAll(">","&gt;");
}

function NullCheck(obj)
{
	if(obj.value == "" || obj.value == null)
	{
		$("#check_" + obj.id).html(imgErr + " ");
		return false;
	}
	else
	{
		$("#check_" + obj.id).html(imgOk);
		CheckClear();
		return true;
	}
}

function StringCheck(obj)
{
	obj.value = SingleLine(obj.value);
	if(obj.value.length > 0) {
		$("#check_" + obj.id).html(imgOk);
	}
	else {
		$("#check_" + obj.id).html(imgErr);
	}
	CheckClear();
	return true;
}

function LengthCheck(obj, n1, n2)
{
	obj.value = SingleLine(obj.value);
	if(obj.value.length >=n1 && obj.value.length <=n2)
	{
		$("#check_" + obj.id).html(imgOk);
		CheckClear();
		return true;
	}
	else
	{
		$("#check_" + obj.id).html(imgErr + " string length " + n1 + " .. " + n2 + "");
		return false;
	}
}

function SameCheck(obj, last)
{
	obj.value = SingleLine(obj.value);
	if(obj.value == $("#" + last).val())
	{
		$("#check_" + obj.id).html(imgOk);
		CheckClear();
		return true;
	}
	else
	{
		$("#check_" + obj.id).html(imgErr + " not same");
		return false;
	}
}

function CheckClear()
{
	$("#total_echo").html("");
}

function AbsolutePosition(node)
{
	var x = y = 0;
	do{
		x+=node.offsetLeft;
		y+=node.offsetTop;
	}
	while(node=node.offsetParent);   
	return{ "x":x, "y":y };         
}

function Sound()
{
	var html = "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" ";
	html += "codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" ";
	html += "width=\"1\" height=\"1\">";
	html += "<param name=\"movie\" value=\"/images/music/1.swf\">";
	html += "<param name=\"wmode\" value=\"transparent\">";
	html += "<embed src=\"/images/music/1.swf?foo=/images/music/1.mp3\" width=\"1\" height=\"1\" quality=\"high\" ";
	html += "pluginspage=\"http://www.macromedia.com/go/getflashplayer\" ";
	html += "type=\"application/x-shockwave-flash\" ";
	html += "wmode=\"transparent\">";
	html += "</embed>";
	html += "</object>";
	$("#sound_pad").html(html);
}

function getElementsByName(name) 
{ 
	var returns = document.getElementsByName(name); 
	if(returns.length > 0) return returns; 
	returns = new Array(); 
	var e = document.getElementsByTagName("input"); 
	for(i = 0; i < e.length; i++) {
		if(e[i].getAttribute("name") == name) { 
			returns[returns.length] = e[i]; 
		} 
	} 
	return returns; 
}

function FindIcons() 
{ 
	var returns = new Array(); 
	var e = document.getElementsByTagName("IMG"); 
	for(i = 0; i < e.length; i++) {
		if(e[i].id == "user_icon") { 
			returns[returns.length] = e[i]; 
		} 
	} 
	var e = document.getElementsByTagName("A"); 
	for(i = 0; i < e.length; i++) {
		if(e[i].id == "user_icon") { 
			returns[returns.length] = e[i]; 
		} 
	}
	return returns; 
}

function MinutesToHms(atime)
{
	var hours = Math.floor(atime / 60.0);
	var minutes = Math.floor(atime - hours * 60.0);
	var seconds = Math.floor((atime - hours * 60.0 - minutes) * 60.0);

	return { hours : hours, minutes : minutes, seconds : seconds };
}


function RadioOne(obj)
{
	var all = getElementsByName(obj.id);
	for(var i=0; i<all.length; i++) all[i].className = "radio2";
	obj.className = "radio1";
}

function RadioAction()
{
	var all = getElementsByName(this.id);
	for(var i=0; i<all.length; i++) all[i].className = "radio2";
	this.className = "radio1";
	document.getElementById(this.id.replace("_radio","")).value = this.getAttribute("value");
}

function CheckboxOne(obj)
{
	if(obj.className == "checkbox1") obj.className = "checkbox2";
	else obj.className = "checkbox1";
}

function CheckboxAction()
{
	if(this.className == "checkbox1") this.className = "checkbox2";
	else this.className = "checkbox1";
	var val = "";
	var all = getElementsByName(this.id);
	for(var i=0; i<all.length; i++) {
		if(all[i].className == "checkbox1") {
			val += all[i].getAttribute("value") + ", ";
		}
		else {
			val += ", ";
		}
	}
	document.getElementById(this.id.replace("_checkbox","")).value = val;
}

function Bind()
{
	var all = document.getElementsByTagName("DIV");  
	for(var i=0;i<all.length;i++)  
	{  
		if(all[i].className == "radio1" || all[i].className == "radio2")
		{
			all[i].onclick = RadioAction;
		}
		if(all[i].className == "checkbox1" || all[i].className == "checkbox2")
		{
			all[i].onclick = CheckboxAction;
		}
	}
}

function IconAction()
{
	var icons = FindIcons();
	for(var i=0; i<icons.length; i++) {
		icons[i].onmouseover = function() { 
			var id = this.getAttribute("tips");
			if(id!=null) {
				if(id.substr(0,1)=="1") {
					DropAjax(this,this,300,190,"/Utility/UserInfo.aspx?id=" + id,"ajax","middle"); 
				}
			}
		}
		icons[i].onmousemove = function() { hide_switch = false; }
		icons[i].onmouseout = function() { hide_switch = true; DropIconClear(); }
	}
}

function ShowSummary()
{
	setInterval("GetFresh()", 2000);
	$.get("/Utility/UserSummery.aspx", { rnd : Math.random() }, function(result) {
		if(result.length > 0) {
			$("#blog_c_right").html(result);
		}
	});
}

function ShowSummary2(id)
{
	setInterval("GetFresh()", 2000);
	$.get("/Utility/UserSummery.aspx", { rnd : Math.random(), iduser : id }, function(result) {
		document.getElementById("blog_c_right").innerHTML = result;
	});
}


function ProjectSummary(id)
{
	$.get("/Utility/ProjectSummery.aspx", { rnd : Math.random(), id : id }, function(result) {
		if(result.length > 0) {
			$("#blog_c_right").html(result);
		}
	});
}

function DepartmentSummary(id)
{
	$.get("/Utility/DepartmentSummery.aspx", { rnd : Math.random(), id : id }, function(result) {
		if(result.length > 0) {
			$("#blog_c_right").html(result);
		}
	});
}

function GetList(objname,cmd)
{
	$.get("/Utility/BlogList.aspx", { rnd : Math.random(), cmd : cmd, page : page_index }, function(result) {
		if(result.length>1) {
			$(objname).html(result);
			IconAction();
			ThumbsAction();
		}
	});
}

function ShowDefaultImage(obj)
{
	obj.src = "/Images/noname.gif";
}

function ShowErrorImage(obj)
{
	var org = obj.src;
	obj.src = "/Images/img-error.jpg";
	obj.parentNode.onclick = null;
	obj.onclick = function() { 
		if (confirm("无法加载图片\n" + org + "\n尝试在新窗口打开该图片吗？") == true)
		{
			window.open(org);
		}
	};
}

function ThumbsAction() 
{
	$("div#one_image").each(function(i,obj) { 
		obj.onmousemove = MoveImageAction; 
		obj.onmouseout = function() { this.scrollTop = 0; this.scrollLeft = 0; }
	});
}

function MoveAction(event)
{
	event = event || window.event;
	var doc = document.documentElement, body = document.body;
	var y = event.clientY + (doc && doc.scrollTop  || body && body.scrollTop  || 0) - (doc && doc.clientTop  || body && body.clientTop  || 0);
	var p = AbsolutePosition(this);

	if(this.scrollHeight > this.clientHeight) {
		y -= p.y;
	
		var y2 = parseInt((this.scrollHeight - this.clientHeight) * (y-20) / (this.clientHeight-40), 10);
		if(y2<0) y2=0;
		if(y2>(this.scrollHeight-20)) y2=this.scrollHeight; 
		this.scrollTop = y2;
	}
}

function position_fixed(el, eltop)
{
	if( !window.XMLHttpRequest ){
		el.style.position = "absolute";
		window.attachEvent("onload", function(){
			el.style.left = "0px";
			el.style.top = ( document.documentElement.scrollTop + eltop )+"px";
		});
		window.attachEvent("onscroll", function(){
			el.style.left = "0px";
			el.style.top = ( document.documentElement.scrollTop + eltop )+"px";
		});
	}
	else{
		el.style.position = "fixed";
	}
}

function ShowWriter(op)
{
	if($("#blog_writer").length == 0) return;

	if($("#blog_writer").css("display") != "none") {
		$("#blog_writer").css("display", "none");
	}
	else {
		$("#blog_writer").css("display", "");
	}
	if(op) $("#blog_writer").css("display", ""); 
}

function HideDateWriter()
{
	$("#predate").animate({width:0},"fast");
	$("#predate").html("");
}

function ShowDateWriter(dv)
{
	ShowWriter(true);
	$("#predate").animate({width:0},"fast");
	$("#predate").html("预约:"+dv);
	$("#predate").animate({width:100},"fast");
}
