
// this function tests whether 'str' contains only characters from 'charSet'
function isWithinSet ( str, charSet )
{
	var c;
	var i;
	for( i = 0; i < str.length; i ++ )
	{
		c = str.charAt( i );
		if (charSet.indexOf( c ) < 0)
			return false;
	}
	return true;
}


// this function tests whether 'str' is a valid domain name
// 'str' does not include domain extension
function isValidDomainName( str )
{
	var allowedCharSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-";
	return ( str.length>0 && str.length <=26 && str.charAt( 0 )!='-' && str.charAt( str.length - 1 ) != '-' && isWithinSet( str, allowedCharSet ) );
}


// this function tests whether 'str' is a valid email address
function isValidEmail( str )
{
     return (str.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1);
}


// this function tests whether 'str' is a valid username for the system
function isValidUserName( str )
{
	var allowedCharSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
	//alert(str.charAt( 0 )>'9');
	//alert(str.charAt( 0 )<'0');
	return ( str.length <=15 && str.length >=5 && ( str.charAt( 0 ) > '9' || str.charAt( 0 ) < '0' ) && isWithinSet( str, allowedCharSet ) );
}


// this function tests whether 'str' is a valid phone number
function isValidPhoneNumber( str )
{
	var allowedCharSet = "1234567890-()+ ";
	return ( str.length <=20 && isWithinSet( str, allowedCharSet ) );
}


// validate mandatory field and prompt error message
function validateMandatory( field, errMsg )
{
	if( field.value == "" )
	{
		alert( errMsg );
		return false;
	}
	else
	{
		return true;
	}
}

/* 
用途：检查输入字符串是否符合金额格式 
格式定义为带小数的正数，小数点后最多2位 
输入： 
s：字符串 
返回： 
如果通过验证返回true,否则返回false 
 
*/ 
function isValidMoneyAmt( s )
{ 
	var regu = "^[0-9]+[\.]*[0-9]{0,2}$";
	var re = new RegExp(regu); 
	return re.test(s);
}

// check if a host name is valid
function isValidHost( argvalue )
{

	if (argvalue.indexOf(" ") != -1) return false;

	if (argvalue.indexOf(".") == -1)
		return false;
	else if (argvalue.indexOf(".") == 0)
		return false;
	else if (argvalue.charAt(argvalue.length - 1) == ".")
		return false;

	if (argvalue.indexOf("/") != -1) return false;
	
	var allowedCharSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-.";
	return isWithinSet( argvalue, allowedCharSet );
}

function isNumber( str )
{
	var allowedCharSet = "1234567890";
	return ( str.length > 0 && ( str.charAt( 0 ) != '0' ) && isWithinSet( str, allowedCharSet ) );
}

function isUrl(s)
{
	if (s.indexOf(" ")!=-1)
		return false;
	
	if(s==""||s==null)
		return false;
	
	if(s.charAt(s.length-1)=='.')
		return false;
	
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}

// this function tests whether a given string is a valid verify code.
function isValidVCode( str )
{
	var allowedCharSet = "1234567890";
	return ( str.length <=5 && str.length >=3 && isWithinSet( str, allowedCharSet ) );
}