
// ======================================================================
// 基础类函数
var ie = ("\v"=="v") ? true : false;

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.Contains = function(v) { return (this.indexOf(v)>=0) ? true : false; }
function I2(v) { return (parseInt(v,10)<10) ? "0" + v : v; }
function C2(v) { return "," + I2(v); }
String.prototype.Untail = function()
{
	var rsl = this;
	if(rsl=="") return "";
	if(rsl.substr(rsl.length-1,1)==",") return rsl.substr(0,rsl.length-1);
	return rsl;
}

String.prototype.replaceAll2 = stringReplaceAll;
function stringReplaceAll(AFindText,ARepText)
{
	var raRegExp = new RegExp(AFindText.replace(/([\(\)\[\]\{\}\^\$\+\-\*\?\.\"\'\|\/\\])/g,"\\$1"),"ig");
	return this.replace(raRegExp,ARepText);
}

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

// ======================================================================
// 仅对input类型有效
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 getElementsById(name) 
{ 
	var returns = document.getElementsByName(name); 
	if(returns.length > 0) return returns; 
	returns = new Array(); 
	var e = document.getElementsByTagName("div"); 
	for(i = 0; i < e.length; i++) {
		if(e[i].getAttribute("id") == name) { 
			returns[returns.length] = e[i]; 
		} 
	} 
	return returns; 
}

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

function getElementsByName2(ctrltype, 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("type") == ctrltype ) {
			if(e[i].getAttribute("name") == name) { 
				returns[returns.length] = e[i]; 
			} 
		}
	} 
	return returns; 
}

// ======================================================================
// radio checkbox 后面文字点击相关的操作

function RadioAction(objname, idx)
{
	var obj = getElementsByName2("radio", objname);
	if(obj!=null) {
		if(!obj[idx].checked) obj[idx].checked = true;
	}
}

function CheckboxAction(objname, idx)
{
	var obj = getElementsByName2("checkbox", objname);
	if(obj!=null) {
		if(!obj[idx].checked) obj[idx].checked = true;
		else obj[idx].checked = false;
	}
}

// ======================================================================
// ie and Firefox 通用的Ajax代码

// =====================================
// IE & Firefox & Chrome 通用的Ajax代码

var ajaxHttp = null;
var ajaxType = -1;
var ajaxResult = "";

function RandHref(vr)
{
	var v;
	if(vr.indexOf("?")>=0) 
	{
		v = vr + "&";
	}
	else 
	{
		v = vr + "?";
	}
	v += "rand=" + Math.floor(Math.random() * 65536);
	v += Math.floor(Math.random() * 65536) + "";
	v += Math.floor(Math.random() * 65536) + "";
	return v;
}

function getAjax()
{
	if(ajaxType>=0) return;
    try
    {
        ajaxHttp = new ActiveXObject("Microsoft.XMLHTTP");
        ajaxType = 1;
    }
    catch(e)
    {
        try
        {
            ajaxHttp = new ActiveXObject("Msxml2.XMLHTTP");
            ajaxType = 1;
        }
        catch(e)
        {
            try
            {
                ajaxHttp = new XMLHttpRequest();
                ajaxType = 2;
            }
            catch(e)
            {
                ajaxHttp = null;
                ajaxType = 0;
            }
        }
    }
}

function runAjax(method, url, pars, runfunc)
{
    getAjax();
    if(ajaxType == 0)
    {
        alert("您的浏览器不支持XMLHTTP，无法完成此操作");
    }
    else
    {
		try
		{
			ajaxHttp.open(method, url, false);
			if(method == "POST")
			{
				ajaxHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			}
			else
			{
				ajaxHttp.setRequestHeader("Content-Type", "text/xml;charset=GB2312");
			}
			if(ajaxType == 1)
			{
				ajaxHttp.onreadystatechange = runfunc;
			}
			else
			{
				ajaxHttp.onload = runfunc;
				ajaxHttp.onerror = runfunc;
			}
			ajaxHttp.send(pars);
		}
		catch (err)
		{
		}
    }
}

function ajaxCallback()
{
	var htmlcode = "";
	if(ajaxType == 1)
	{
		if(ajaxHttp.readyState == 4)
		{
			htmlcode = ajaxHttp.responseText;
		}
	}
	else
	{
		htmlcode = ajaxHttp.responseText;
	}

	if(document.defaultCharset=="utf-8" && navigator.userLanguage=="zh-cn") 
	{
		ajaxResult = unescape(htmlcode);
	}
	else 
	{
		ajaxResult = htmlcode;
	}
}

// Ajax基类函数(IE Only)

function AjaxBaseIE(href,params)
{
	var result = "";
	var XMLHTTP;
	try {
	    XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
	    if(!XMLHTTP) XMLHTTP = new ActiveXObject("MSXML2.XMLHTTP");
	}
	catch(e) {
	    return "";
	}
	XMLHTTP.Open("POST",href,false);
	XMLHTTP.setRequestHeader("Content-Length", params.length );   
	XMLHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=gb2312"); 
	XMLHTTP.Send(params);
	if(XMLHTTP.readyState==4) {
		if(document.defaultCharset=="utf-8" && navigator.userLanguage=="zh-cn") {
			result = unescape(XMLHTTP.responseText);
		}
		else {
			result = XMLHTTP.responseText; 
		}
	}
	else {
		result = "";
	}
	delete XMLHTTP;
	return result.replace("\r\n","");
}

function AjaxBase(href,params)
{
	ajaxResult = "";
	if(ie) {
		ajaxResult = AjaxBaseIE(href,params);
		return ajaxResult;
	}
	else {
		runAjax("POST", href, params, ajaxCallback);
		return ajaxResult.replace("\r\n","");
	}
}

// 以XML流方式传输数据，提交给href
function AjaxXML(href,params)
{
	return AjaxBase(href,params);
}

// 返回href的结果
function AjaxPost(href)
{
	return AjaxBase(RandHref(href),"");
}

// 返回href的结果
function AjaxDown(href)
{
	return AjaxBase(href,"");
}

// 返回href的结果
function AjaxPostOnly(href)
{
	return AjaxBase(RandHref(href),"");
}

// 将href返回的结果显示到 obj 中
function AjaxGet(href,obj)
{
	obj.innerHTML = AjaxBase(RandHref(href),"");
}

// ======================================================================
// 通用

function ConfirmDelete(id,title)
{
	var url = document.location.href;
	if(url.indexOf("?")>0) url = url.substr(0,url.indexOf("?"));
	if(confirm("您确认要删除(" + title + ")?")==true) {
		location.href = url + "?cmd=delete&id=" + id;
	}
}

function ConfirmDelete2(id,title)
{
	alert("您没有权限删除别人的信息哦");
}

function PushMessage(t, a)
{
	var echo = AjaxPost("/desk/ajax/Operation.aspx?t=" + t + "&a=" + a);
}
