﻿
function expandDiv(ctrl) {
	var ctl = eval(ctrl);
	if (ctl.style.display == "none") 
		ctl.style.display = "";
	else
		ctl.style.display = "none";
}

function IsNumber(number)	{	
	if(number.length == 0)
		return false;
	for(i = 0; i < number.length; i++) {
		if(number.charAt(i) < '0' || number.charAt(i) > '9')
			return false;
	}
	return true;
}


function LTrim(str) {
	if (str == null) {
		return "";
	}	
	var len = 0;
	while(str.charAt(len) == " ")
		len++;
	return str.substring(len);
}


function RTrim(str) {
	if (str == null) {
		return "";
	}
	var len = str.length;
	while(str.charAt(len-1) == " ")
		len--;
	return str.substring(0,len);
}	


function Trim(str) {		
	return LTrim(RTrim(str));
}

function IsBeginChar(str)	{	
	if(str.length == 0)
		return false;		
	if((str.charAt(0) >= 'A' && str.charAt(0) <= 'Z') || (str.charAt(0) >= 'a' && str.charAt(0) <= 'z'))
			return true;		
	return false;
}

function IsEmail(email) {
	var patrn=/^\w[0-9a-zA-Z-_\.]*@(\w[0-9a-zA-Z-]*\.)+\w{2,}$/;
	if (!patrn.exec(email)) 
		return false;
	return true;
} 

function IsTel(tel) {
	if(tel.length < 7 || tel.length > 20)
		return false;
	for(i = 0;i<tel.length;i++)
	{
		if((tel.charAt(i)>='0' && tel.charAt(i)<='9') || tel.charAt(i) == '-') {
			if(tel.charAt(i) == '-' && (i <=2 || i >= (tel.length -3)))
				return false;
		}
		else			
			return false;
	}
	return true;

} 

function IsMobile(mobile) {
	if (mobile.length = 11 && IsNumber(mobile)) {
		return true;
	}
	return false;
} 

function IsPostCode(postCode)	{	
	if(postCode.length != 6 || !IsNumber(postCode))
		return false;		
	return true;
}

//日期验证
function IsDate(date) {
	var tempArr = new Array("true","");
	
	if(!(/^([0-9]){4,4}-([0-9]){2,2}-([0-9]){2,2}$/.test(date)))
	{
		tempArr[0] = "false";
        tempArr[1] = "无效的日期，请按格式 \"YYYY-MM-DD\"输入！";
		return tempArr;
	}
	var arr = date.match(/\d+/g);
	year = Number(arr[0]);
	month = Number(arr[1]);
	day = Number(arr[2]);
	var monthDay = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	if(year%400==0||(year%4==0&&year%100!=0))	monthDay[1] = 29;
	if(year<0 || month<0 || month>12 || day>31 ||day>monthDay[month-1])
	{
		tempArr[0] = "false";
		tempArr[1] = "无效的日期，请重新输入！";
		return tempArr;
	}
	return tempArr;
}

function GetParastr(strname) {
	var hrefstr
	hrefstr = window.location.href;		
	return GetParastrByUrlAndParaName(hrefstr,strname);
}


function GetParastrByUrlAndParaName(hrefstr,strname) {
	var pos,parastr,para,tempstr;		
	pos = hrefstr.indexOf("?")
	parastr = hrefstr.substring(pos+1);
	para = parastr.split("&");
	tempstr="";
	for(i=0;i<para.length;i++) {
		tempstr = para[i];
		pos = tempstr.indexOf("=");
		if(tempstr.substring(0,pos) == strname)
			return tempstr.substring(pos+1);
	}
	return null;
}	

// Find element x,y location
function findPosition( oLink ) {
	var posX = null;
	var posY = null;
	if( oLink.offsetParent ) {
	for( posX = 0, posY = 0; oLink.offsetParent; oLink = oLink.offsetParent ) {
		posX += oLink.offsetLeft;
		posY += oLink.offsetTop;
	}
	return [ posX, posY ];
	} else {
	posX = oLink.x;
	posY = oLink.y;
	return [ posX, posY ];
	}
}

function locationAddParam(url, paramName, paramValue) {
	paramValue = encodeURIComponent(paramValue)
	if (url == "" || url.length == 0) {
		url = window.location.href;
	}
	if (url.substring(url.length-1,url.length) == "#") {
		url = url.substring(0,url.length-1);
	}
	var pos = url.indexOf("&"+paramName+"=");
	if (pos == -1) {
		pos = url.indexOf("?"+paramName+"=");
	}
	if (pos != -1) {
		if (url.indexOf("&",pos+1) == -1) {
			url = url.substring(0,url.indexOf("=",pos+1)+1)+paramValue;
		}
		else {
			var tempStr = url.substring(url.indexOf("&",pos+1),url.length);
			url = url.substring(0,url.indexOf("=",pos+1)+1)+paramValue+tempStr;
		}
	}
	else {
		if (url.indexOf("?") != -1) {
			url = url+"&"+paramName+"="+paramValue;
		}
		else {
			url = url+"?"+paramName+"="+paramValue;
		}
	}
	return url;
}

function locationDelParam(url, paramName) {
	if (url == "" || url.length == 0) {
		url = window.location.href;
	}
	var pos = url.indexOf("&"+paramName+"=");
	if (pos == -1) {
		pos = url.indexOf("?"+paramName+"=");
	}
	if (pos != -1) {
		if (url.indexOf("&",pos+1) == -1) {
			url = url.substring(0,pos);
		}
		else {
			var tempStr = url.substring(url.indexOf("&",pos+1)+1,url.length);
			url = url.substring(0,pos+1)+tempStr;
		}
	}
	return url;
}

function ReadCookie(name)
{
	var mycookie = document.cookie; 
	var start1 = mycookie.indexOf(name + "=");
	if (start1== -1)
		return "";
	else
	{
		start=mycookie.indexOf("=",start1)+1; 
		var end = mycookie.indexOf(";",start);
		if (end==-1)
		{
			end=mycookie.length;
		}
		var value=unescape(mycookie.substring(start,end));
		return value;
	}
}

function isNull(obj){
	if (typeof(obj) == "undefined")
	  return true;
	  
	if (obj == undefined)
	  return true;
	  
	if (obj == null)
	  return true;
	 
	return false;
}

function replaceAll(str1,str2,str3) {
		
	if(str1 == null || str1 == "")
		return null;
	while(str1.indexOf(str2) != -1) {
		str1 = str1.replace(str2,str3);
	}
	return str1;    
}

function CheckGroup(elemname,msg)
{
   var count=0;
	var sexs=document.getElementsByName(elemname);
	for(var i=0;i<sexs.length;i++)
	{
		if(sexs[i].checked==true)
		{
			count++;
		}
	}
	if(count==0)
	{
		alert(msg);
		return false;
	}
	else
	{
		return true;
	}
}

function XMLHttpFactory() {
	var Http = null;
        try { 
	        Http = new ActiveXObject("Msxml2.XMLHTTP"); 
        } 
        catch(e) { 
	        try { 
		        Http = new ActiveXObject("Microsoft.XMLHTTP"); 
	        }
	        catch(oc) { 
			        Http = null; 
	        }
        }
        if (!Http && window.XMLHttpRequest) { 
	        Http = new XMLHttpRequest();
        }
        return Http;
}