﻿function Anthem_Error(result) 
{
	document.body.style.cursor='auto'; 
	window.status = ''; 			
	if(result.error!='NORESPONSE')
	{
	    alert("系统繁忙，请刷新后重试！");
	}
	//alert('Anthem Error was invoked with the following error message: ' + result.error);
}
//设置GridView中所有CheckBox是否选中
function checkAllSelect(tableID, flag)
{
    var chks = document.getElementById(tableID).getElementsByTagName("input");
    
    for(var i = 0;i < chks.length; i++) {
        if(chks[i].type == 'checkbox') {
            chks[i].checked = flag;
        }
    }
    chks = null;
}

function SetFocus(objName)
{
    var txtObj=document.getElementById(objName);
    if(txtObj!=null)
        txtObj.focus();
}

function Request(strName) 
{ 
    var strHref = window.document.location.href; 
    var intPos = strHref.indexOf("?"); 
    var strRight = strHref.substr(intPos + 1); 

    var arrTmp = strRight.split("&"); 
    for(var i = 0; i < arrTmp.length; i++) 
    { 
        var arrTemp = arrTmp[i].split("="); 

        if(arrTemp[0].toUpperCase() == strName.toUpperCase()) return arrTemp[1]; 
    } 
    return ""; 
} 
//control ComponentArt Dialog show or hide
function Toggle(objDialog)
{
	if(objDialog.get_isShowing())
	{
		objDialog.Close();
	}
	else
	{
	    var obj = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
	    objDialog.set_y(obj.scrollTop + (obj.clientHeight - parseInt(objDialog.get_height())) / 2);
	    objDialog.Show();
	}
	return false;
}

function ToggleDiv(objDiv,isShow)
{
    var obj=document.getElementById(objDiv);
	if(!isShow)
	{
		obj.style.display="none";
	}
	else
	{
	    obj.style.display="block";
	}
}

function DisableNumbers(fld, e, allowSpace)
{
	var space=''
	if(allowSpace!=null && allowSpace==true)
		space=' ';
	var strCheck = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'+space;
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13) return true;  // Enter
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1)
		return false;  // Not a valid key
	return true;
}

function DisableNonNumericCharacters(fld, e, allowSpace,isDouble)
{
	var space='';
	var strCheck;
	if(allowSpace!=null && allowSpace==true)
		space=' ';
	if (isDouble !=null && isDouble == true)
	 strCheck = '0123456789.' + space;
	else
	 strCheck = '0123456789' + space;
	 
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13) return true;  // Enter
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1)
		return false;  // Not a valid key
	return true;
}

function CapFirstLetter(fld)
{
	fld.value=fld.value.properCase();
}

//TextControl.Attributes.Add("onkeypress", "return DisableSpecialCharacters(this,event,'',false,true)")
function DisableSpecialCharacters(fld,e,allowedChars,spaceAllowed,numAllowed)
{
	var space=' ';
	var PatSWord=/^[\x00-\xff]+$/;     //匹配所有单字节长度的字符组成的字符串 
	
	//by default space is allowed
	if(spaceAllowed!=null && spaceAllowed==false)
		space='';
	//by default numbers r not allowed
	var num='';
	if(numAllowed!=null && numAllowed==true)
		num='0123456789';
		
	var strCheck = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'+space+allowedChars+num;
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13) return true;  // Enter
	key = String.fromCharCode(whichCode);  // Get key value from key code
	
	 if(!PatSWord.test(key))
	 {
	    return false;
	 }
	if (strCheck.indexOf(key) == -1)
		return false;  // Not a valid key
	return true;
}

function TrimText(txt)
{
	return txt.replace(/^\s*|\s*$/g,"");
}

function FormatNumber(nStr)
{
	//add commas to number
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function RemoveCommas(str)
{
	return str.toString().replace(new RegExp(/,/g), "");
}

function IsEmptyString(str)
{
    if (TrimText(str).length == 0)
        return true;
    else
        return false;
}

function IsEmailValid(str) 
{
        
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		  return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		  return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}

function ValidateNonNumericField(fld, e, allowSpace, isDouble, errorMessage)
{
	var space='';
	var strCheck;
	if(allowSpace!=null && allowSpace==true)
		space=' ';
	if (isDouble !=null && isDouble == true)
	 strCheck = '0123456789.' + space;
	else
	 strCheck = '0123456789' + space;
	 
	for (i = 0; i < fld.value.length; i++)
	{
		key = fld.value.charAt(i);  // Get key value from key code
		if (strCheck.indexOf(key) == -1)
		{
			alert(errorMessage);
			fld.value = '';
			fld.focus();
			return;
		}
	}
}

function ValidateSpecialCharactersField(fld,e,allowedChars,spaceAllowed,numAllowed,errorMessage)
{
	var space=' ';
	//by default space is allowed
	if(spaceAllowed!=null && spaceAllowed==false)
		space='';
	//by default numbers r not allowed
	var num='';
	if(numAllowed!=null && numAllowed==true)
		num='0123456789';
		
	var strCheck = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'+space+allowedChars+num;

	for (i = 0; i < fld.value.length; i++)
	{
		key = fld.value.charAt(i);  // Get key value from key code
		if (strCheck.indexOf(key) == -1)
		{
			alert(errorMessage);
			fld.value = '';
			fld.focus();
			return;
		}
	}
}

// var test = Round(15.1356, 2);
// Result: test = 15.14
function Round(a_Num , a_Bit)
{
    return(Math.round(a_Num * Math.pow (10 , a_Bit)) / Math.pow(10 , a_Bit));
} 

//Using Regular Expression
function checkEmail(control, showMessage) 
{
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(control.value))
    {
        return (true)
    }
    if(showMessage)
        alert("Invalid E-mail Address! Please re-enter.")

    return (false)
}

//TextControl.Attributes.Add("onkeypress", "return SpecialCharacters0(this,event)")
//-Special Character: ~ ! @ # $ % ^ & * ( ) _ + = - { [ ] } | \ , . / < > ?
function SpecialCharacters0(fld,e)
{	
	var strCheck = '~!@#$%^&*()_+=-{[]}|\,./<>?';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13) return true;  // Enter
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1)
	{
		return false;  // Not a valid key
	}
	return true;
}

//TextControl.Attributes.Add("onkeypress", "return SpecialCharacters1(this,event)")
//-Special Character # 1 : Alphanumeric characters, underscore, dash, space
function SpecialCharacters1(fld,e)
{	
	var strCheck = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_- ';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13) return true;  // Enter
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1)
	{
		return false;  // Not a valid key
	}
	return true;
}

//TextControl.Attributes.Add("onkeypress", "return SpecialCharacters2(this,event)")
//-Special Character # 2 : Alphanumeric characters and all special characters
function SpecialCharacters2(fld,e)
{	
	var strCheck = '~!@#$%^&*()_+=-{[]}|\,./<>?0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13) return true;  // Enter
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1)
	{
		return false;  // Not a valid key
	}
	return true;
}

// Delete space in user string
String.prototype.Trim = function()
{  
    return this.replace(/(^\s*)|(\s*$)/g, "");  
}

//Limit word count of text area		
function LimitWordCount(object,count)
{
    var objectValue=object.value;
    if (objectValue.length > count-1) // if too long...trim it!
            object.value=objectValue.substring(0,count-1);
}

function IsURL(str_url)
{
    var strRegex = "^((https|http|ftp|rtsp|mms)?://)" 
    + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@ 
          + "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184 
          + "|" // 允许IP和DOMAIN（域名）
          + "([0-9a-z_!~*'()-]+\.)*" // 域名- www. 
          + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名 
          + "[a-z]{2,6})" // first level domain- .com or .museum 
          + "(:[0-9]{1,4})?" // 端口- :80 
          + "((/?)|" // a slash isn't required if there is no file name 
          + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"; 
          var re=new RegExp(strRegex); 
          if (re.test(str_url)){
              return (true); 
          }else{ 
              return (false); 
          }
}
