function hasChar(str, ch)
{
	var result = 0;

	for (var i = 0; i < str.length; i++)
	{
		if (str.charAt(i) == ch) result ++;
	}
	return result;
}

//=====================================================================================

function isCharsInBag (s, bag)
{
	var i;

	for (i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) return false;
	}
	return true;
}

//=====================================================================================

function isASCII(str)
{
	var result = true;

	for (var i = 0; i < str.length; i++)
	{
		if (str.charCodeAt(i) > 127) result = false;
	}
	return result;
}

//=====================================================================================

function isNumber(str)
{
	var result = true;

	for (var i = 0; i < str.length; i++)
	{
		if ((str.charCodeAt(i) >= 49) && (str.charCodeAt(i) <= 57)) {result = false; break;}
	}
	return result;
}

//=====================================================================================

function CheckIsNull(Field, FieldName)
{
	var Field = document.getElementById(Field);

	if(Field.value == '')
	{
		alert(FieldName + "欄位留白無法接受");
		Field.focus();
		return(false);
	}
	return(true);
}

//=====================================================================================

function CheckIsSelect(Field, FieldName)
{
	var Field = document.getElementById(Field);
	
	if (Field.selectedIndex <= 0)
	{
		alert("請先選擇" + FieldName);
		Field.focus();
		return(false);
	}
	return(true);
}

//=====================================================================================

function CheckIsRadio(Field, FieldName)
{	
	if (!(IsRadioCheck(Field)))
	{
		var Field = document.getElementsByName(Field);
		alert("請先選擇" + FieldName);
		//if (!(typeof(Field) == 'undifined'))
		//	Field[0].focus();
		return(false);
	}
	return(true);
}

//=====================================================================================

function IsRadioCheck(Field)
{
	var Field = document.getElementsByName(Field);
	var blnChk = false;
	
	for (var i = 0; i < Field.length; i++)
	{
		if (Field[i].checked)
		{
			blnChk = true;
			break;
		}

	}
	return(blnChk);
}

//=====================================================================================

function CheckSize(Field, FieldName, Method, Size)
{
	var Field = document.getElementById(Field);

	if (Method == "Small")
	{
		if (Field.value.length < Size)
		{
			alert(FieldName + "欄位至少要" + Size + "個字");
			Field.focus();
			return(false);
		}
	}
	else
	{
		if (Field.value.length > Size)
		{
			alert(FieldName + "欄位至多不得超過" + Size + "個字");
			Field.focus();
			return(false);
		}
	}
	return(true);
}

//=====================================================================================

function CheckIsASCII(Field, FieldName)
{
	var Field = document.getElementById(Field);
	Field.value = Field.value.toLowerCase();
	var c = Field.value.charAt(0);

	if (!((c >= "a" && c <= "z") || (c >= "0" && c <= "9")))
	{
		alert(FieldName + "欄位格式不對，第一個字必需為英數字");
		Field.focus();
		return(false);
	}

	for (var i = 1; i < Field.value.length - 1; i++)
	{
		var c = Field.value.charAt(i);
		if (!(( c >= "0" && c <= "9") || (c >= "a" && c <= "z") || (c == "-")))
		{
			alert(FieldName + "欄位格式不對，必需為英數字或連接號");
			Field.focus();
			return(false);
		}
	}

	var c = Field.value.charAt(Field.value.length - 1);
	if (!((c >= "a" && c <= "z") || (c >= "0" && c <= "9")))
	{
		alert(FieldName + "欄位格式不對，最後一個字必需為英數字");
		Field.focus();
		return(false);
	}
	return(true);
}

//=====================================================================================

function CheckIsNumber(Field, FieldName)
{
	var Field = document.getElementById(Field);

	if (!isCharsInBag(Field.value, "0123456789"))
	{
		alert(FieldName + "欄位格式不對，必需為數字");
		Field.focus();
		return(false);
	}

	return(true);
}

//=====================================================================================

function CheckSpecialChar(Field, FieldName)
{
	var Field = document.getElementById(Field);

	if (!((hasChar(Field.value, "'") == 0)
	&& (hasChar(Field.value, "+") == 0)
	&& (hasChar(Field.value, "-") == 0)
	&& (hasChar(Field.value, "&") == 0)
	&& (hasChar(Field.value, "|") == 0)
	&& (hasChar(Field.value, "<") == 0)
	&& (hasChar(Field.value, ">") == 0)
	&& (hasChar(Field.value, "\\") == 0)))
	{
		alert(FieldName + "欄位請勿使用 ' + - & | < > \\ 等字元");
		Field.focus();
		return(false);
	}
	return(true);
}

//=====================================================================================

function CheckMail(Field)
{
	var Field = document.getElementById(Field);

	if(hasChar(Field.value, "@") == 0)
	{
		alert("電子郵件信箱欄位格式不對，未包含 @ 符號");
		Field.focus();
		return(false);
	}
	else if(hasChar(Field.value, " ") != 0)
	{
		alert("電子郵件信箱欄位格式不對，不能輸入空白鍵");
		Field.focus();
		return(false);
	}
	else if(hasChar(Field.value, ".") == 0)
	{
		alert("電子郵件信箱欄位格式不對，未包含 . 符號");
		Field.focus();
		return(false);
	}
	else if(!isASCII(Field.value))
	{
		alert("電子郵件信箱欄位格式不對，不能輸入不合法的字元");
		Field.focus();
		return(false);
	}
	return(true);
}

//=====================================================================================

function CheckIsDate(Field)
{
	var Field	= document.getElementById(Field);
	
	// value is null?
	if(Field.value == '') return(true);
	
	var datePat	= /^(\d{4})(\/)(\d{1,2})(\/)(\d{1,2})$/;
	var matchArray	= Field.value.match(datePat);

	// is the format ok?
	if (matchArray == null)
	{
		alert("請輸入正確的日期格式 (yyyy/mm/dd)");
		Field.focus();
		return(false);
	}

	year	= matchArray[1];
	month	= matchArray[3];
	day	= matchArray[5];

	// check month range
	if (month < 1 || month > 12)
	{
		alert("月份輸入不正確，月份必須介於 1∼12");
		Field.focus();
		return(false);
	}

	if (day < 1 || day > 31)
	{
		alert("日期輸入不正確，日期必須介於 1∼31");
		Field.focus();
		return(false);
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31)
	{
		alert(month + " 月份不能輸入 31 天")
		Field.focus();
		return(false);
	}

	// check for february 29th
	if (month == 2)
	{
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap))
		{
			alert(year + " 年 2 月份的日期不能是 " + day + " 天");
			Field.focus();
			return(false);
		}
	}

	// date is valid
	return(true);
}

//=====================================================================================

function CheckFileExt(Field, Format)
{
	var reOKFiles;
	
	//允許的圖片副檔名
	switch (Format)
	{
		case "doc":
			reOKFiles	= /^([a-zA-Z].*|[1-9].*)\.(doc)$/;
			break;
		case "jpg":
			reOKFiles	= /^([a-zA-Z].*|[1-9].*)\.(jpg)$/;
			break;
		case "pdf":
			reOKFiles	= /^([a-zA-Z].*|[1-9].*)\.(pdf)$/;
			break;
		case "xls":
			reOKFiles	= /^([a-zA-Z].*|[1-9].*)\.(xls)$/;
			break;
		case "xml":
			reOKFiles	= /^([a-zA-Z].*|[1-9].*)\.(xml)$/;
			break;
		case "gif":
			reOKFiles	= /^([a-zA-Z].*|[1-9].*)\.(gif)$/;
			break;
		default:
			reOKFiles	= /^([a-zA-Z].*|[1-9].*)\.(doc|pdf|jpg)$/;
			break;
	}

	var file = document.getElementById(Field).value.toLowerCase();
	if (!(reOKFiles.test(file)))
	{
		alert("上傳格式錯誤，僅能接受（*." + Format.replace(/\|/g, ' 或 *.') + "）格式！")
		return false;
	}
	
	return true;
}