/*
	history
	2003.11.23 created by cho
*/

//check item valid functions

/*
 check if is positive number include in float number
 0 -> true
 -12 -> false
 34 -> true
 34.23 -> true
*/
function isPosNumber(val)
{
	if( !isNumber(val) )
		return false;
	return val >= 0 ;
}

/*
	check if is number
	0 -> true	23.43 -> true -23.23 -> true 23 -> true
	asdd -> false #$% -> false 134s -> false
*/
function isNumber(val)
{
	return !isNaN(val);
}

/*
	check if is integer value
	0 -> true , 23 -> true -23 -> true
	23.23 -> false -23.23 -> false
	afe23 -> false
*/
function isInteger(val)
{
	if( !isNumber(val) )
		return false;
	return val.indexOf('.') == -1;
}

 /* 0~9 and a-Z : Not include '.' '-' 
   2343ASBfe -> true
   23.34 -> false
   -234 -> false
 */
function isNumberAndAlpha(val)
{
	
	if( val == null ) return false;
	for( i=0 ; i<val.length ; i++ )
	{
		ch = val.charAt(i);
		if( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <='Z') || (ch >='0' && ch <='9') )
			continue;
		else
			return false;
	} 
	return true; 
}

/*
	check scale
	isScaleNumber("123.45",4) -> true
	isScaleNumber("123.45",3) -> true
	isScaleNumber("123.45",2) -> false
	isScaleNumber("123",4) -> true
	isScaleNumber("123",3) -> true
	isScaleNumber("123",2) -> false
*/
function isScaleNumber(val,size)
{
	if( !isNumber(val) )
		return false;
	pos = val.indexOf('.');
	if( pos == -1 )
		return val.length <= size ;
	else
		return pos <= size;
}



/*** With Length ***/
/*
	checkLength("abcde",6) -> false
	checkLength("abcde",5) -> true
	checkLength("abcde",4) -> true
	checkLength("abcde",6,3) -> false
	checkLength("abcde",5,3) -> true
	checkLength("ab",4,3) -> false
	checkLength("ab",4,2) -> true
	checkLength("ab",null,3) -> false
	checkLength("ab",null,2) -> true
*/
function checkLength(val,maxLength,minLength)
{
	var result = true;
	if( maxLength != null )
		if( val.length > maxLength )
			result = false;
	if( minLength != null )
		if( val.length < minLength )
			result = false;
	return result;
}

/*** With Size ***/
/*
	checkSize("999",999) -> false
	checkSize("999",1000) -> false
	checkSize("999",998) -> true
	checkSize("0",998,0) -> false
	checkSize("1",998,0) -> true
	checkSize("1",null,0) -> true
*/
function checkSize(val,maxSize,minSize)
{
	var result = true;
	if( maxSize != null )
		if( val >= maxSize )
			result = false;
	if( minSize != null )
		if( val <= minSize )
			result = false;
	return result;
}

// do not check for full-word character
function hasBlank(val)
{
	if( val == null ) return false;
	return val.indexOf(" ") != -1;
}

/*
	check if first character is alphabet
	isFirstAlphabet("a234") -> true
	isFirstAlphabet("234adf") -> false
	isFirstAlphabet("#asd") -> false
*/
function isFirstAlphabet(val)
{
	// if val is null , invalid data.
	if( val == null ) return false;
	temp = val.charAt(0);

	return isConsistChar( temp , null , true , null );
}

/* usage : isConsistChar("test23432%%$",true,true,"%$") => true */
function isConsistChar(val,isNum,isAlpha,specialChar)
{
	alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	num = "0123456789";
	if( val == null ) return false;
	var checkString="";
	
	if( isNum )
		checkString += num;
	if( isAlpha )
		checkString += alpha;
	if( specialChar != null )
		checkString += specialChar;
	for( i=0 ; i < val.length ; i++ )
		if( checkString.indexOf( val.charAt( i ) ) == -1 )
			return false;
	
	return true;
}

/** hasNotChar("1234%^&","%") => false 
    hasNotChar("1234%^&","!@") => true **/
function hasNotChar(val,charList)
{
	if( val == null )
		return false;

	for( i=0 ; i<charList.length ; i++ )
	{
		if( val.indexOf(charList.charAt(i)) != -1 )
			return false;
	}
	return true;
}

/*
	isNumAndHyphen("asdf-ssdf") -> false
	isNumAndHyphen("234-11") -> true
	isNumAndHyphen("23411") -> true
*/
function isNumAndHyphen(val)
{
	return isConsistChar(val,true,false,"-");
}

/*
	isNumberStr("2342") -> true
	isNumberStr("2342.234") -> false
	isNumberStr("-2342") -> false
	isNumberStr("afd") -> false
*/
function isNumberStr(val)
{
	return isConsistChar(val,true,false,null);
}

function isNumberAndHyphen(val)
{
	return isConsistChar(val,true,false,"-");
}


/** usage : isHyphenPatten("345-2343","xxx-xxxx") ***/
function isHyphenPatten(val,patten)
{
	if( val == null ) return false;
	if( patten == null ) return true;
	if( val.length != patten.length ) return false;
	
	for( i=0 ; i<patten.length ; i++ )
	{
		ch = patten.charAt(i);
		if( ch == '-' )
			if( val.charAt(i) != '-' )
				return false;
			else
				continue;
		else if( ch == 'x' )
			if( val.charAt(i) >='0' && val.charAt(i) <='9' )		
				continue;
			else
				return false;
		else
			return false;
	}
	return true;
}

/*  Check : YYYYMMDD 
	isYYYYMMDD("11111111") -> true
	isYYYYMMDD("20031301") -> false 13 is not month value
	isYYYYMMDD("20031232") -> false 32 is not month value
	isYYYYMMDD("20030229") -> false 2003/02 has not '29' date
	isYYYYMMDD("20040229") -> true 2004/02 has '29' date
	isYYYYMMDD("00000000") -> false not allow 0000 - year , 00 - month , 00 - date
*/
function isYYYYMMDD(val)
{
	if( val == null ) return false;
	
	if( val.length != 8 )
		return false;

	if( !isNumberStr(val) )
		return false;

	year = val.substring(0,4);
	month = val.substring(4,6);
	date = val.substring(6,8);
	if( year == "0000" )
		return false;
	
	if(  month > 12 || month == "00")
		return false;
	d = new Date( year , month );
	maxDate = d.getUTCDate();
	if( date > maxDate || date == "00" )
		return false;
	return true;
}

/* Check : HHMM 
HH : 0 ~ 23
MI : 0 ~ 59
*/
function isHH24MI(val)
{
	if( val == null ) return false;
	
	if( val.length != 4 )
		return false;
	if( !isNumberStr(val) )
		return false;
		
	hour = val.substring(0,2);
	minute = val.substring(2,4);
	return hour <= 23 && minute <= 59;
}

/*
	check if has '&' or '<'
*/
function isXMLTextStr(val)
{
	return hasNotChar(val,"&<");
}

/*
	platform : "window" or "unix" or "linux"
	only checking for absolute path

	isFilePathStr("c:\aa\aa","window") -> true
	isFilePathStr("c:\aa\aa\aa?<>bb","window") -> false
	isFilePathStr("c\aa\aa","window") -> false
	isFilePathStr("aa\aa","window") -> false

	isFilePathStr("/aa/aa","unix") -> true
	isFilePathStr("/aa/aa/aa<>;aa","unix") -> false
	isFilePathStr("aa/aa/","unix") -> false
*/
function isFilePathStr(val,platform)
{
	if( val == null || platform == null )
		return false;
	
	if( platform == "window" )
	{
		if( val.indexOf(":") != 2 )
			return false;
		files = val.split("\\");
		for( i=1 ; i<files.length ; i++ )
		{
			if( !isFileNameStr(files[i],platform) )
				return false;
		}
		return true;
	}else if( platform == "unix" || platform == "linux" )
	{
		if( val.charAt(0) != "/" )
			return false;
		files = val.split("/");
		for( i=1 ; i<files.length ; i++ )
		{
			if( !isFileNameStr(files[i],platform) )
				return false;
		}
		return true;
	}else
		return true;
}

/*
	platform : "window" or "unix" or "linux"
	checking characteres for window - "\\/:*?\"<>|"
	checking characteres for unix and linux - &()|/`;\"'><

	isFileNameStr("abcd#$","window") -> true
	isFileNameStr("abcd/:","window") -> false
	isFileNameStr("c:\aa","window") -> false

	isFileNameStr("asfc#$","unix") -> true
	isFileNameStr("asdf()","unix") -> false
	isFileNameStr("/asdf","unix") -> false

*/
function isFileNameStr(val,platform)
{
	if( val == null || platform == null )
		return false;
		
	if( platform == "window" )
	{
		return hasNotChar( val , "\\/:*?\"<>|" );
	}else if( platform == "unix" || platform == "linux" )
	{
		return hasNotChar( val , "&()|/`;\"'><");
	}
}

/* Add in 2005/09/04 */

function checkIsZenkaku(value) 
{
    for (var i = 0; i < value.length; ++i) 
	{
        var c = value.charCodeAt(i);
        if (c < 256 || (c >= 0xff61 && c <= 0xff9f)) 
		{
          return false;
        }
    }
    return true;
}

/* Zengaku => Hangaku Change  

function z2h_word(src) {
  return src.replace(/([‚`-‚y‚-‚š‚O-‚XQ])/g,
    function ($0) {
      return String.fromCharCode($0.charCodeAt(0) - 65248);
    });
}
*/
/* Hangaku => Zengaku Change 

function h2z_word(src) {
  return src.replace(/(\w)/g,
    function ($0) {
      return String.fromCharCode($0.charCodeAt(0) + 65248);
    });
}

function isZengakuNumberAndAlpha(val)
{
	filter = /^[‚`-‚y‚-‚š‚O-‚XA-Za-z0-9]/
	for (var i = 0; i < val.length; ++i) 
	{
		var c = val.charAt(i);
		if( !filter.test(c) )
			return false;
	}
	return true;
}*/

function checkLengthWithZengaku(val,maxLength,minLength)
{
	var len = 0;
	for (var i = 0; i < val.length; ++i) 
	{
        var c = val.charCodeAt(i);
        if (c < 256 || (c >= 0xff61 && c <= 0xff9f)) 
			len +=1;
		else
			len +=2;
	}
	var result = true;
	if( maxLength != null )
		if( len > maxLength )
			result = false;
	if( minLength != null )
		if( len < minLength )
			result = false;
	return result;
}

function isEmail(val)
{
	var idIndex = val.indexOf('@');
	if( idIndex == -1 || idIndex == 0 || idIndex+1 == val.length )
		return false;

	var domain = val.substring(idIndex+1);
	var dotIndex = domain.indexOf('.');
	if( dotIndex == -1 || dotIndex == 0 || dotIndex+1 == domain.length )
		return false;
	
	return true;
}