// ZDSOFT.NET JavaScript Base Ver 040910

var ActRd;
var ActRs=new Array();

function Act(obj,str)
{
  eval(obj.type+'.'+str+'(obj)');
}
function LoadClass(name)
{
  eval(name+'=new '+name+'()');
}

function GetIndex(obj)
{
	for(var i=0; i < obj.form.elements.length; i++)
  {
	  if ( obj.form.elements[i].uniqueID == obj.uniqueID) return(i);
  }
	return(-1);
}

function GetOffset(obj)
{
	var x=0,y=0;
	if(!obj) return [x,y];
	while(obj)
  {
		x+=parseInt(obj.offsetLeft);
		y+=parseInt(obj.offsetTop);
		obj=obj.offsetParent;
	}
	return [x,y];
}

Number.prototype.Fix = function()
{
	return numFix(this)
}

Number.prototype.Int = function()
{
	return numInt(this)
}

Array.prototype.Clear = function()
{
	this.length=0;
}

Array.prototype.Max = function()
{
	var i, max = this[0];
	for (i = 1; i < this.length; i++)
  {
		if (max < this[i]) max = this[i];
  }
	return max;
}

Array.prototype.Min = function()
{
	var i, min = this[0];
	for (i = 1; i < this.length; i++)
  {
		if (min > this[i]) min = this[i];
  }
	return min;
}

Array.prototype.Sum = function()
{
	var i, sum = 0;
	for (i = 0; i < this.length; i++)
  {
		sum+=this[i];
  }
	return sum;
}

Array.prototype.Left=function(length)
{
  return this.slice(0,length);
}

Array.prototype.Mid=function(start,length)
{
  return this.slice(start,start+length);
}

Array.prototype.Right=function(length)
{
  if(length>=this.length) return this;
  return this.slice(this.length-length,this.length);
}


String.prototype.IsDate = function()
{
	var myReg = /^(\d{4})(-|\/|.)(\d{1,2})\2(\d{1,2})$/; 
	var result=this.match(myReg);
  if(result==null) return false;
	var test= new Date(result[1],result[3]-1,result[4]);
	if ((test.getFullYear()==result[1]) && (test.getMonth()+1==result[3]) && (test.getDate()==result[4]))
  {
		ActRs.Clear();
		ActRs[0]=result[1];ActRs[1]=result[3];ActRs[2]=result[4];
		return true;
	}
	else return false;
}

String.prototype.IsTime = function()
{
	var myReg = /^(\d{1,2})(:)(\d{1,2})\2(\d{1,2})$/; 
	var result=this.match(myReg);
  if(result==null) return false;
	var test= new Date(2000,1,1,result[1],result[3],result[4]);
	if ((test.getHours()==result[1]) && (test.getMinutes()==result[3]) && (test.getSeconds()==result[4]))
  {
		ActRs[3]=result[1];ActRs[4]=result[3];ActRs[5]=result[4];
		return true;
	}
	else
  {
    return false;
  }
}

String.prototype.IsDateTime = function()
{
	var myReg = this.split(" ");
	if(myReg.length!=2) return false;
	if(myReg[0].IsDate() && myReg[1].IsTime()) return true;
	return false;
}

String.prototype.IsEmail = function()
{
	var myReg = /[\u4e00-\u9fa5]/;
	if(!myReg.test(this))
  {
		myReg = /^[_a-zA-Z0-9][-._a-zA-Z0-9]*@[-._a-zA-Z0-9]+\.[-._a-zA-Z0-9]+(\.[-._a-zA-Z])*$/;
		if (myReg.test(this)) return true;
	}
  else
  {
		myReg = /^[_a-zA-Z0-9\u4e00-\u9fa5][-_.a-zA-Z0-9\u4e00-\u9fa5]*@[-._a-zA-Z0-9\u4e00-\u9fa5]+(\.[-._0-9a-zA-Z\u4e00-\u9fa5]+)*$/;
		if (myReg.test(this)) return true;
	}
	return false;
}

String.prototype.IsIdcard = function()
{
  var myReg = /^[1-9][0-9]{14}$|^[1-9][0-9]{16}[0-9a-zA-Z]$/;
	if(myReg.test(this)) return true;
  return false;
}

String.prototype.IsTelephone = function()
{
	myReg = /[(]/;
	if (!myReg.test(this))
	{
		myReg = /^[1-9][0-9]{6,7}$|^[0-9]{3,4}-[1-9][0-9]{6,7}$|^[0-9]{3,4}-[1-9][0-9]{6,7}-[0-9]{2,8}$/;
		if (myReg.test(this)) return true;
	}
	//else {
		//myReg = /^[1-9][0-9]{6,7}$|^([0-9]{3,4})[1-9][0-9]{6,7}$|^([0-9]{3,4})[1-9][0-9]{6,7}-[0-9]{2,8}$/;
		//if (myReg.test(this)) return true;
	//}
	return false;
}

String.prototype.IsNumber = function()
{
	var myReg = /^[0-9]+$/;
	if(!myReg.test(this)) return false;
	ActRd=parseInt(this)	
	return true;
}

String.prototype.IsFloat = function()
{
	var myReg = /^[0-9.]+$/;
	if(!myReg.test(this)) return false;
	var pos=this.indexOf('.')
	if(pos==-1) return false;
	if(pos!=this.lastIndexOf('.')) return false;
	if(pos==0 || (pos+1)==this.length) return false;
	ActRd=parseFloat(this)
	return true;
}

String.prototype.IsPhone = function()
{
	if(!this.IsNumber()) return false;
	if(this.length != 11 || this<13000000000 || this>13999999999) return false;
	return true;
}

String.prototype.IsDomain = function()
{
    var myReg = /^[0-9a-zA-Z\-]+$/;
    if(myReg.test(this)) return true;
    return false;
}

String.prototype.IsAvail = function()
{
	var myReg = /^[0-9a-zA-Z]+$/;
	if(myReg.test(this)) return true;
	return false;
}

String.prototype.IsEn = function()
{
    var myReg = /^[a-zA-Z]+$/;
    if(myReg.test(this)) return true;
    return false;
}

String.prototype.ConvertHtml = function()
{
	var tmp = this.replace(/\&/g, "&amp;");
  tmp = tmp.replace(/\"/g, "&quot;");
	tmp = tmp.replace(/ /g, "&nbsp;");
  tmp = tmp.replace(/</g, "&lt;");
  //tmp = tmp.replace(/\'/g, "&apos;");
  tmp = tmp.replace(/>/g, "&gt;");
  return tmp;
}

String.prototype.ConvertHtmlWithN = function()
{
	var tmp = this.replace(/\&/g, "&amp;");
  tmp = tmp.replace(/\"/g, "&quot;");
	tmp = tmp.replace(/ /g, "&nbsp;");
  tmp = tmp.replace(/</g, "&lt;");
  tmp = tmp.replace(/>/g, "&gt;");
  //tmp = tmp.replace(/\'/g, "&apos;");
  tmp = tmp.replace(/\r\n/g, "<br>");
  return tmp;
}

String.prototype.ConvertHtmlWithP = function()
{
  var tmp = this.replace(/\&/g, "&amp;");
  tmp = tmp.replace(/\"/g, "&quot;");
  tmp = tmp.replace(/ /g, "&nbsp;");
  tmp = tmp.replace(/</g, "&lt;");
  tmp = tmp.replace(/>/g, "&gt;");
  //tmp = tmp.replace(/\'/g, "&apos;");
  tmp = tmp.replace(/\r\n/g, "</p><p>");
  return tmp;
}

String.prototype.IsCn = function()
{
    var ch,temp,isCN,isTrue;
    isTrue = true;
    for(var i=0;i<this.length;i++)
    {
      ch = this.substring(i,i+1);
      temp = escape(ch);
      isCN = (temp.length == 6)? true:false;
      if(!isCN)
      {
        isTrue = false;
        break;
      }
    }
    return isTrue;
}

String.prototype.isCom_sep_char = function()
{
	var myReg = /^[-()_+*]*$/;
	if(myReg.test(this)) return true;
	return false;
}

String.prototype.IsReg1 = function()
{
	var s,i;
	for(i=0;i<this.length;i++)
  {
		s=this.charAt(i);
		if(s.IsNumber() || s.IsEn() || s.IsCn()) continue;
		return false;
	}
	return i?true:false;
}

String.prototype.IsReg12 = function()
{
	var s,i;
	for(i=0;i<this.length;i++)
  {
		s=this.charAt(i);
		if(s.IsNumber() || s.IsEn() || s.IsCn() || s.isCom_sep_char()) continue;
		return false;
	}
	return i?true:false;
}


String.prototype.Trim = function()
{
	var tmp = this.replace(/(^\s*)|(\s*$)/g, "");
	return tmp.replace(/(^\2005-9-28*)|(\　*$)/g,"");
}

String.prototype.Left = function(n)
{
	return strLeft(this,n);
}

String.prototype.Right = function(n)
{
	return strRight(this,n);
}

String.prototype.Len = function()
{
	var len=0;
	for (var i=0;i<this.length;i++)
  {
		if (this.charCodeAt(i)>255)
    {
      len+=2; 
    }
		else
    {
      len++;
    }
	}
	return len;
}

String.prototype.Text = function()
{
	var elm = document.createElement("DIV");
	elm.innerHTML = this;
	return elm.innerText;
}

String.prototype.Html = function()
{
	var elm = document.createElement("DIV");
	elm.innerText = this;
	return elm.innerHTML;
}

String.prototype.Escape = function()
{
	return escape(this);
}

String.prototype.UnEscape = function()
{
	return unescape(this);
}

String.prototype.toInt = function()
{
	return parseInt(this);
}

String.prototype.toFloat = function()
{
	return parseFloat(this);
}

String.prototype.toHex = function()
{
	if(!this.IsNumber()) return "";
	var x=parseInt(this);
  var out = "";
  var remainder;
  while (x > 0)
  {
    remainder = x % 16;
    if (remainder < 10)
    {
        out = remainder + out;
    } 
    else if (remainder == 10)
    {
        out = "a" + out;
    }
    else if (remainder == 11)
    {
        out = "b" + out;
    }
    else if (remainder == 12)
    {
        out = "c" + out;
    }
    else if (remainder == 13)
    {
        out = "d" + out;
    } 
    else if (remainder == 14)
    {
        out = "e" + out;
    } 
    else if (remainder == 15)
    {
        out = "f" + out;
    }
    x = Math.floor(x / 16);
  }
  return out;
}

String.prototype.toDate = function()
{
	ActRs.Clear();
	this.IsDate();
	return new Date(ActRs[0],ActRs[1]-1,ActRs[2]);
}

String.prototype.Hexto = function()
{
	var myReg = /^[0-9a-fA-F]+$/;
	if(!myReg.test(this)) return "0";
	ActRd=parseInt(this,16);
	return ""+ActRd;
}

String.prototype.Octto = function()
{
	var myReg = /^[0-7]+$/;
	if(!myReg.test(this)) return "0";
	ActRd=parseInt(this,8);
	return ""+ActRd;
}

String.prototype.Binto = function()
{
	var myReg = /^[0-1]+$/;
	if(!myReg.test(this)) return "0";
	ActRd=parseInt(this,2);	
	return ""+ActRd;
}

String.prototype.Repeat = function(n)
{
  if (typeof(n) != "number") return "";
  var out = this;
  for (var i = 1; i < n; i++) out += this;
  return out;
}

String.prototype.Reverse = function()
{
  var out = "";
  for (var i = this.length; i >= 0; i--)
  {
    out += this.charAt(i);
  }
  return out;
}

String.prototype.Comp = function(str,m)
{
	return Compstr(this,str,m);
}

String.prototype.addDate = function(mode,d)
{
	return addDate(mode,d,this);
}

String.prototype.Count = function()
{
  var out = new Array(256);
  for (var i = 0; i < this.length; i++)
  {
    if (out[this.charCodeAt(i)] == undefined)
    {
      out[this.charCodeAt(i)] = 1;
    } 
    else
    {
      out[this.charCodeAt(i)]++;
    }
  }
  return out;
}

function cutStr(str,strlen)
{
  var temp_i=0;
	var temp=0;
	var temp_str="";
	for(temp_i=0;temp_i<str.length;temp_i++)
  {
    if(Math.abs(str.charCodeAt(temp_i))>255)
    {
      temp+=2;
    }
    else 
    {
      temp+=1;
    }
	   
    if(temp>strlen)
    {
      temp_str=str.substr(0,temp_i)+"...";
      break;
    }
    else
    {
      temp_str=str;
    }
	}
   return temp_str;
}

/** 
 *返回查询字符串 已经用encodeURI转码过
 *输入 form对象
 *输出 用encodeURI转码过查询字符串，最前边是没有&号的，如：str1=aaa&str2=bbb
 */

String.prototype.myEncode = function()
{
	return this.replace(/\&/g, "%26").replace(/\?/g,"%3F").replace(/\#/g,"%23").replace(/\+/g,"%2B");
}

/** 
 *返回查询字符串 已经用encodeURI转码过
 *输入 form对象
 *输出 用encodeURI转码过查询字符串，最前边是没有&号的，如：str1=aaa&str2=bbb
 */
function generateQueryString(aform)
{
  var qstr = "";
	if (aform != null)
	{
	  for (var i = 0 ; i < aform.length ; i++ )
		{
			//qstr += aform[i].name + "=" + escape(aform[i].value) + "&";
			//qstr += aform[i].name + "=" + encodeURI(aform[i].value) + "&";
			if (aform[i].type == "text" || aform[i].type == "textarea")
			{
				qstr += aform[i].name + "=" + aform[i].value.myEncode() + "&";
			}
			else if (aform[i].type == "radio")
			{
				if (aform[i].checked)
				{
					qstr += aform[i].name + "=" + aform[i].value.myEncode() + "&";
				}
			}
			else if (aform[i].type == "checkbox")
			{
				if (aform[i].checked)
				{
					qstr += aform[i].name + "=" + aform[i].value.myEncode() + "&";
				}
			}			
			else if (aform[i].type == "button")
			{
			}
			else 
      {
				qstr += aform[i].name + "=" + aform[i].value.myEncode() + "&";
			}
		}
		if (qstr.length > 0 )
		{
			qstr = qstr.substring(0,qstr.length - 1);
		}
	}
	return qstr;
}

//COOKIE 函数
function SetCookie(name,value)//两个参数，一个是cookie的名子，一个是值
{
    var Days = 1; //此 cookie 将被保存 1 天
    var exp  = new Date();    //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}

function getCookie(name)//取cookies函数        
{
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
     if(arr != null) return unescape(arr[2]); return null;

}
function delCookie(name)//删除cookie
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
//查找数组中是否有此值 返回所在位置，如无，则-1
function valueInArray(s_value,to_array)
{
	if (s_value=="" || to_array.length<=0)
	{
		return -1;
	}
	var bak_i=-1;
	for (var i=0;i<to_array.length ;i++ )
	{
		if (to_array[i]==s_value)
		{
			bak_i=i+1;
			break;
		}
	}
	return bak_i;
}

function addOption(objId,optionValue,optionText)
{
	var obj=document.getElementById(objId);
	obj.options.add(new Option(optionText,optionValue));
}

function removeAll(objId)
{
  var obj=document.getElementById(objId);
  obj.options.length=0;
}

function _getId(a)
{
	return document.getElementById ? document.getElementById(a) : null;
}

function _getName(a)
{
	return document.getElementsByName ? document.getElementsByName(a) : null;
}
