
//弹出页面，OPEN方式。
function TSC_OpenBrWindow(theURL,winName,iWidth,iHeight) 
{ 
    var iTop = (window.screen.availHeight-30-iHeight)/2;
    var iLeft = (window.screen.availWidth-10-iWidth)/2;
    var features = "height="+iHeight+",width="+iWidth+",top="+iTop+",left="+iLeft;
    window.open(theURL,winName,features);
}
//弹出页面，Modal方式。暂时不用。
function TSC_ShowModalDialog(theURL,winName,iWidth,iHeight)
{
    var iTop = (window.screen.availHeight-30-iHeight)/2;
    var iLeft = (window.screen.availWidth-10-iWidth)/2;
    var features = "dialogHeight:"+iHeight+"px;dialogWidth:"+iWidth+"px;dialogTop="+iTop+"px;dialogLeft="+iLeft+"px;";
    var returnobj = null;
    returnobj =  window.showModalDialog(theURL,winName,features);
    
    return returnobj;
}
//弹出页面，Modal方式。暂时不用。
function TSC_ShowModalDialog(theURL,winName,iWidth,iHeight,noScroll)
{
    var iTop = (window.screen.availHeight-30-iHeight)/2;
    var iLeft = (window.screen.availWidth-10-iWidth)/2;
    var features = "dialogHeight:"+iHeight+"px;dialogWidth:"+iWidth+"px;dialogTop="+iTop+"px;dialogLeft="+iLeft+"px;";
    if(noScroll=="1")
    {
        features = "dialogHeight:"+iHeight+"px;dialogWidth:"+iWidth+"px;dialogTop="+iTop+"px;dialogLeft="+iLeft+"px;resizable:no;scroll:no;status:no";
    }
    var returnobj = null;
    returnobj =  window.showModalDialog(theURL,winName,features);
    
    return returnobj;
}
function RefreshPage(obj)
{
    if(obj==null||obj=="undefined")
    {
        return;
    }
    if(obj=="1")
    {
        document.getElementById('btnRefresh').click();
    }
}

/////////////////////////////////////////////
//全选
function SelectAllCheckboxes(spanChk)
{
    var oItem = spanChk.children;
    var theBox= (spanChk.type=="checkbox") ? spanChk : spanChk.children.item[0];
    xState=theBox.checked;
    elm=theBox.form.elements;

    for(i=0;i<elm.length;i++)
    {
        if(elm[i].type=="checkbox" && elm[i].id!=theBox.id)
        {
            if(elm[i].checked!=xState)
                elm[i].click();
        }
    }
}

function CheckThis(checkbox, id)
{
    var IDStr = document.getElementById("fldIds");
    var NameStr = document.getElementById("fldDisplayContent");
    
    var localid = id.split(',')[0];
    var localContent = id.split(',')[1];
    pos = IDStr.value.indexOf(localid);
    if(checkbox.checked)
    {
       if(pos == -1) //not yet exist
       {
           IDStr.value += "," + localid;
           NameStr.value += "/" + localContent;
       }
    }
    else
    {
       if(pos > -1)
       {
           IDStr.value = IDStr.value.replace("," + localid, "");
           NameStr.value = NameStr.value.replace("/" + localContent,"");
       }
    }
 }
//////////////////////////////////////////////

function OpenAttachWindow(poststr)   
  {
     TSC_ShowModalDialog(poststr,"",600,340);
  } 
  
  
  
  /* Function List:
 *
 * 1 , String.trim()
 * 
 * 2 , String.isEmpty()
 * 
 * 3 , String.isInsideOf(specifyStr) //to check whether or not only include specify string
 * 
 * 4 , String.isAlpha()
 * 
 * 5 , String.isInteger()
 * 
 * 6 , String.isDouble()
 * 
 * 7 , String.isNumberInPrecision(precision,scale)
 * 
 * 8 , String.isNumberInRange(min,max) //to check the number whether or not between min and max.
 * 
 * 9 , String.isEmail()
 *
 * 10, String.isAlphanumeric()
 *
 * 11, String.replaceAll()
 * 
 * 
 */
 
 String.prototype.ISOLen = function()
 {    
	 var i,str1,str2,str3,nLen;
	 str1 = this;
	 nLen = 0;	 
	 for(i=1;i<=str1.length;i++) {
		str2=str1.substring(i-1,i)
		str3=escape(str2);
		if(str3.length>3){
			nLen = nLen + 2;
		}else {
			nLen = nLen + 1;
		}
	 }
	 return nLen;	
 }
 String.prototype.trim = function()
{
    return this == null ? "" : this.replace(/(^[\s]*)|([\s]*$)/g, "");
}
String.prototype.lTrim = function()
{
    return this == null ? "" : this.replace(/(^[\s]*)/g, "");
}
String.prototype.rTrim = function()
{
    return this == null ? "" : this.replace(/([\s]*$)/g, "");
}
String.prototype.isEmpty = function()
{
    return this == null ? true : this.trim()=="";
}
//to check whether or not only include specify string
String.prototype.isInsideOf = function(specifyStr)
{
	if (this.isEmpty()) return true;
	for (var i = 0; i < this.length; i++){
		if (specifyStr.indexOf(this.charAt(i)) == -1) return false;
	}
	return true;
}
String.prototype.isAlpha = function()
{
	/*var validAlpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	return this.isInsideOf(validAlpha);*/
	return /^[a-zA-Z]+$/g.test(this);
}
String.prototype.isInteger = function()
{
	return /^(\-?)(\d+)$/.test(this);
}
String.prototype.isDouble = function()
{  
    return this.indexOf(".") == -1 ? this.isInteger() : /^(\-?)(\d+)(.{1})(\d+)$/.test(this);
}
String.prototype.isNumberInPrecision = function(precision,scale)
{
    if(this.isEmpty()) return true;
	var integerLen = precision-scale;
	if (this.indexOf(".") == -1){
		//if (!(/^(\-?)(\d+)$/g.test(this))) return false;
		if (!(/^(\-?)(\d+)$/.test(this))) return false;
		if ((RegExp.$2).length>integerLen) return false;
	}else{
		if (scale==0) return false;
		//if (!(/^(\-?)(\d+)(.{1})(\d+)$/g.test(this))) return false;
		if (!(/^(\-?)(\d+)(.{1})(\d+)$/.test(this))) return false;
		var num1=(RegExp.$2).length;
		var num2=(RegExp.$4).length;
		if (num1>integerLen) return false;
		if (num2>scale) return false;
		if (num1+num2>precision) return false;
	}	
	return true;
}
//to check the number whether or not between min and max.
String.prototype.isNumberInRange = function(min,max)
{
    if(!this.isDouble()) return false;
	var num = parseFloat(this);
	return ((num >= min) && (num <= max));
}
String.prototype.isEmail = function()
{
	//return /^([a-zA-Zd_\.\-])+\@(([a-zA-Zd\-])+\.)+([a-zA-Zd]{2,4})+$/g.test(this);
	//return /^[\w-]+@[\w-]+\.(com|net|org|edu|mil|tv|biz|info)$/g.test(this);
	if(this.isEmpty()) return true;
	return /^(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)(;(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*))*$/.test(this);//for multi email address ,split by ';'
}
String.prototype.isAlphanumeric = function()
{
    return /^\w+$/gi.test(this);
}
String.prototype.replaceAll = function(source,target)
{
	var str2="";
	var i1=this.indexOf(source);
	var i2=0;
	for(;i1>=0;i1=this.indexOf(source,i2))
	{
		str2+=this.substring(i2,i1) + target;
		i2=i1+source.length;
	}
	str2+=this.substring(i2);
	return str2;
}



function checkDateWithFormat(dateStr,format) 
{	
	if (dateStr.isEmpty()) return true;
	format = format.toLowerCase();

	var reg = /^(\d{4})-(\d{2})-(\d{2})$/;
	if(format == "mm-dd-yyyy" || format == "dd-mm-yyyy"){
		reg = /^(\d{2})-(\d{2})-(\d{4})$/;
	}
	if (!reg.test(dateStr)) return false;  //dateStr.match(reg); //reg.exec(dateStr); //var pattern = new RegExp(pat,"gi");

	var array_str = dateStr.split("-");
	var y,m,d;
	if(format == "yyyy-mm-dd"){
		y=array_str[0];m=array_str[1];d=array_str[2];
	}else if(format == "mm-dd-yyyy"){
		y=array_str[2];m=array_str[0];d=array_str[1];
	}else if(format == "dd-mm-yyyy"){
		y=array_str[2];m=array_str[1];d=array_str[0];
	}
	var err=isValidDate(y,m,d);	
	if (err!=1) {
		return false;
	}else{
		return true;
	}
}

function isValidDate(y,m,d)
{
	if (y<1900) return -1;
	if (m<1 || m>12) return -2;
	if (d<1 || d>daysInMonth(y,m)) return -3;
	return 1;
}

function daysInMonth(year, month)
{
	if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10|| month == 12) return 31;
	if (month == 4 || month == 6 || month == 9 || month == 11) return 30;
	if (month == 2) return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

function ValidTextNUll(txtID,alertMsg)
{
    var txt = document.getElementById(txtID);
    if(txt.value.trim() == "")
     {
         alert(alertMsg);
         return false;
     }
     return true;
}
