// === Copyright (C) 2001	Ryuji Oyama. All rights reserved. ===

function isDigit(c) { return( (c>='0' && c<='9') ); }
function isLower(c) { return( (c>='a' && c<='z') ); }
function isUpper(c) { return( (c>='A' && c<='Z') ); }
function isAlpha(c) { return( isLower(c) || isUpper(c) ); }
function isAlNum(c) { return( isAlpha(c) || isDigit(c) ); }

function isChar(c,str)
{
	for ( var i=0 ; i<str.length ; i++ ) {
		if ( str.charAt(i) == c ) return(true);
	}
	return(false);
}
function isAllChar(c,str)
{
	if(str.length==0)
	return(false);
	for ( var i=0 ; i<str.length ; i++ ) {
		if ( str.charAt(i) != c ) return(false);
	}
	return(true);
}

function isAscii(str)
{
	for( var i=0 ; i<str.length ; i++ ) {
		if ( 
(str.charCodeAt(i)) == 3 ) return(false);
	}
	return(true);
}

function isNumString(str)
{
	if ( str.length == 1 && isChar(str.charAt(0),'-.') ) return(false);
	for ( var i=0 ; i<str.length ; i++ ) {
		var c = str.charAt(i);
		if ( !isDigit(c) ) {
			// １バイト目に「-」がある、または、「.」の場合はOK
			if ( (i == 0 && c == '-') || c == '.' ) continue;
			return(false);
		}
	}
	if(str.indexOf('.')!=str.lastIndexOf('.'))
	{
		return(false);
	}
	return(true);
}

function isFiveBitNumString(str)
{
	if ( str.length == 1 && isChar(str.charAt(0),'-.') ) return(false);
	for ( var i=0 ; i<str.length ; i++ ) {
		var c = str.charAt(i);
		if ( !isDigit(c) ) {
			// １バイト目に「-」がある、または、「.」の場合はOK
			//if ( (i == 0 && c == '-') || c == '.' ) continue;
			return(false);
		}
	}
	if(parseFloat(str)==0)
	{
		return (false);
	}
	return(true);
}

function isAlphaString( str )
{
	for ( var i=0 ; i<str.length ; i++ ) {
		if ( !isAlpha(str.charAt(i)) ) return(false);
	}
	return(true);
}

function isAlNumString( str )
{
	for ( var i=0 ; i<str.length ; i++ ) {
		if ( !isAlNum(str.charAt(i)) ) return(false);
	}
	return(true);
}
//==add by liaoyl 2005-8-4==
function isKanaString(str)
{
	//ShowMsg(str.charCodeAt(0));
	//return;
	for (var i=0; i<str.length; i++)
	{
		
		if( !((str.charCodeAt(i)>65376) && (str.charCodeAt(i)< 65440))) return (false);		
	}
	return (true);
}
//===add by liaoyl 2005-8-11==
//全角スペースの入力を許可する
function isDKanaString(str)
{
	//ShowMsg(str.charCodeAt(0));
	//return;
	for (var i=0; i<str.length; i++)
	{
		
		if( !(((str.charCodeAt(i)>=0x30a0) && (str.charCodeAt(i)<= 0x30ff))
			||str.charCodeAt(i)== 0x3000)) return (false);		
	}
	return (true);
}
//UPDATE BY LIAO YL 　2006-6-7
//「ひらがな」の入力チェックで、ひらがなと全角スペースの入力を許可する。
function isHKanaString(str)
{
	for (var i=0; i<str.length; i++)
	{
		if( !(((str.charCodeAt(i)>=0x3040) && (str.charCodeAt(i)<= 0x309f))
			||str.charCodeAt(i)== 0x3000)) return (false);		
	}
	return (true);
}
//
function isFullString(str)
{
	
	for( var i=0 ; i<str.length ; i++ ) {
		if ( getCharSize(str.charCodeAt(i)) == 1 ) return(false);
	}
	return(true);
}

function isHalfString(str)
{
	
	for( var i=0 ; i<str.length ; i++ ) {
		if ( getCharSize(str.charCodeAt(i)) == 2 ) return(false);
	}
	return(true);
}


function isGTMin( str, min )
{
	// 数値文字列チェック
	if ( !isNumString(str) ) return(false);
	// 最小値以上かどうかのチェック
	return( ( parseFloat(str) >= min ) );
}

function isLTMax( str, max )
{
	// 数値文字列チェック
	if ( !isNumString(str) ) return(false);
	// 最大値以下どうかのチェック
	return( ( parseFloat(str) <= max ) );
}

function isRange(str,min,max)
{
	// 範囲内どうかのチェック
	return( (isGTMin(str,min) && isLTMax(str,max)) );
}

/*
function isTELString(str)
{
	for ( var i=0 ; i<str.length ; i++ ) {
		var c = str.charAt(i);
		// cが数値または、「(」「)」「-」以外の場合はエラー
		if ( !isDigit(c) && !isChar(c,'()-') ) return(false);
	}
	return(true);
}
*/

function isTELString(str)
{
	// 文字列長が１２以外の場合はエラー
	var len = str.length;
	if ( len != 0) {
		if ( !(len == 12) ) return(false);

		var occurrence1 = str.indexOf('-');
		var occurrence2 = str.indexOf('-',occurrence1+1);
		var occurrence3 = str.indexOf('-',occurrence2+1);
		if (occurrence3!=-1) return(false);

		if (!((occurrence1==6 && occurrence2==11) || (occurrence1==4 && occurrence2==7) || (occurrence1==2 && occurrence2==7)))
			return(false);

		for ( var i=0 ; i<len ; i++ ) {
			var c = str.charAt(i);
			if ( c != '-' && !isDigit(c) ) return(false);
		}
	}

	return(true);
}

function isZIPString(str)
{
	// 文字列長が４、６、８以外の場合はエラー
	var len = str.length;
	if ( !(len == 4 || len == 6 || len == 8) ) return(false);

	for ( var i=0 ; i<len ; i++ ) {
		var c = str.charAt(i);
		// ４バイト目が「-」
		if ( i == 3 ) {
			if ( c != '-' ) return(false);
		}
		else {
			// 数値チェック
			if ( !isDigit(c) ) return(false);
		}
	}
	return(true);
}

/*
function isZIPString(str)
{
	// 文字列長が３、６、８以外の場合はエラー
	var len = str.length;
	var ntimes = 0 ;
	if ( !(len == 3 || len == 6 || len == 8) ) return(false);

	for ( var i=0 ; i<len ; i++ ) {
		var c = str.charAt(i);
		if ( c == '-' ) {
			ntimes = ntimes + 1 ;
		} else {
			// 数値チェック
			if ( !isDigit(c) ) return(false);
		}
	}
	if (ntimes != 1) return(false);
	return(true);
}
*/

/*
function isEMailString(str)
{
	if ( str.length != 0 &&
		(str.indexOf('@') == -1 || str.indexOf('.') == -1) ) return(false);

//	var allow_str = '-_@.';
	//RFC2822参照。「()<>,;:\"[]」は入力エラーとする。
	var allow_str = '-_.!@#$%^&*+=|?/\'{}~`';
	for ( var i=0 ; i<str.length ; i++ ) {
		var c = str.charAt(i);
		if ( !(isAlNum(c) || isChar(c,allow_str)) ) return(false);
	}
	return(true);
}
*/

//Rewrite by Maoyq 20020829 -->
function isEMailString(str)
{
	if ( str.length != 0 &&
		(str.indexOf('@') == -1 || str.indexOf('.') == -1) ) return(false);

	if(str.indexOf('..') != -1) return(false);

	//var allow_str = '-_.';
//	var allow_str = '-_.!@#$%^&*()+=|\\?/><,:;\"\'[]{}~`';
	//RFC2822参照。「()<>,;:\"[]」は入力エラーとする。
	var allow_str = '-_.!@#$%^&*+=|?/\'{}~`';
	var nCount = 0;
	for ( var i=0 ; i<str.length ; i++ ) {
		var c = str.charAt(i);
		if (i == 0) {
			//if (isDigit(c)) return(false);
			if (c == '@') return(false);
		}
		if (c == '@') {
			nCount++;
			if(nCount > 1) return(false);
		}
		else if ( !(isAlNum(c) || isChar(c,allow_str)) ) return(false);
	}
	return(true);
}
//<-- Rewrite by Maoyq 20020829
function isEMailStr(str)
{
	if(str.indexOf('..') != -1) return(false);

//	var allow_str = '-_.@';
	//RFC2822参照。「()<>,;:\"[]」は入力エラーとする。
	var allow_str = '-_.!@#$%^&*+=|?/\'{}~`';
	var nCount = 0;
	for ( var i=0 ; i<str.length ; i++ ) {
		var c = str.charAt(i);
		if ( !(isAlNum(c) || isChar(c,allow_str)) ) return(false);
	}
	return(true);
}

function isDateFormat(y,m,d)
{
	// 年月日がすべて未設定の場合は、1を返却
	// 一応、エラーにしない場合もあるので。
	if ( y.length == 0 && m.length == 0 && d.length == 0 ) return(1);
	// 年月日のいずれかが設定されていない場合は、-1を返却
	if ( !(y.length != 0 && m.length != 0 && d.length != 0) ) return(-1);
	// 年の値チェック (数値以外:-2,範囲外:-3を返却)
	if ( !isNumString(y) ) return(-2);
	if ( !isRange(y,1000,9999) ) return(-3);
	// 月の値チェック (数値以外:-4,範囲外:-5を返却)
	if ( !isNumString(m) ) return(-4);
	if ( !isRange(m,1,12) ) return(-5);
	// 日の値チェック (数値以外:-6,範囲外:-7を返却)
	if ( !isNumString(d) ) return(-6);
	if ( !isRange(d,1,getMaxDay(y,m)) ) return(-7);
	// 正常の場合、0を返却
	return(0);
}

function getMaxDay(year,month)
{
	var n_days = new Array( 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var m = parseInt(month,10);
	//修正（バグ修正） by 沈宇峰＠ＮＥＣＳＩ上海 2002/04/28 -->
	var l_flag = (m == 2 && leapYear(year)) ? 1 : 0;
	//修正（バグ修正） by 沈宇峰＠ＮＥＣＳＩ上海 2002/04/28 <--
	return( n_days[m]+l_flag );
}

function leapYear(year)
{
	var y = parseInt(year,10);
	/*
	return( !(y%4 != 0 || (y%4 == 0 && y%100 != 0)) );
	*/
	return((y%400==0) || ((y%4==0) && (y%100!=0)));
}

function getCharSize(c)
{
	return ((c<128) || (c>65376 && c<65440)) ? 1 : 2 ;
}

function getLength(str)
{
	var len=0;
	for( var i=0 ; i<str.length ; i++ )
	{
		len += getCharSize(str.charCodeAt(i));
	}
	return(len);
}

function trim(str)
{
	if( str == null || str.length == 0 ) return('');

	var remove_str = ' 　';
	var i=0;
	while ( isChar(str.charAt(i),remove_str) ) { i++; }
	var j = str.length-1;
	while( (j>=0 && isChar(str.charAt(j),remove_str)) ) { j--; }
	return( ( j<0 ) ? '' : str.substring(i,j+1) );
}

function removeChar(str,removeStr)
{
	if( str == null || str.length == 0 ) return('');

	var rtnStr = str;
	for ( var i=0 ; i<removeStr.length ; i++ ) {
		var rc = removeStr.charAt(i);
		var tmpStr = '';
		for ( var j=0 ; j<rtnStr.length ; j++ ) {
			var sc = rtnStr.charAt(j);
			if ( rc == sc ) continue;
			tmpStr += sc;
		}
		rtnStr = tmpStr;
	}
	return(rtnStr);
}

function strcmp(s1,s2)
{
	var i=0;
	while ( i < Math.max(s1.length,s2.length) )
	{
		var c1 = s1.charAt(i);
		var c2 = s2.charAt(i);
		if ( c1>c2 || ( c1!='' && c2=='' ) ) return(1);
		else
		if ( c1<c2 || ( c1=='' && c2!='' ) ) return(-1);
		i++;
	}
	return(0);
}

function numcmp(n1,n2)
{
	return(n1-n2);
}

function isDigitString(str)
{
	for ( var i=0 ; i<str.length ; i++ ) {
		var c = str.charAt(i);
		if ( !isDigit(c) ) {
			return(false);
		}
	}
	return(true);
}