
function TagObject(id, name, isMust, dataType, minLen, maxLen) 
{
	this.id = id;
	this.name = name;
	this.tagType ;
	this.isArray = false;
	this.isMust = isMust;
	this.dataType = dataType;
	this.minLen = minLen;
	this.maxLen = maxLen;
	this.obj ;

	this.setObject = function(form)
	{
		this.obj = eval("form." + this.id);
		if(this.obj.type == undefined)this.isArray = true;
		if(this.isArray){
			this.tagType = this.obj[0].type;
		}else{
			this.tagType = this.obj.type;
		}
	}


	this.getValue = function()
	{
		var value = "";
		
		if(this.tagType == "radio" || this.tagType == "checkbox") {
			if (!this.isArray) {
				if (this.obj.checked) value = this.obj.value;
			} else {
				for(var i=0; i<this.obj.length; i++) {
					if(this.obj[i].checked) value += (value==""?"":", ")+this.obj[i].value;
				}
			}
		} else {
			if (this.isArray){
				for(var i=0; i<this.obj.length; i++) {
					value += (value==""?"":", ")+this.obj[i].value;
				}
			} else {
				value = this.obj.value;
			}
		}
		
		return value;
	
	}
	
	
	this.checkValue = function()
	{
		var value = this.getValue();
		//是否必須
		if(this.isMust) {
			if(!checkEmpty(value)) {
				alert("請輸入" + this.name);
				this.focus();
				return false;
			}
		}
		
		if(value == "") return true;
		
		//資料型態
		switch(this.dataType)
		{
			case "c" :
				//alert(this.name + "不可輸入xxxxS" );
				var mask = "< > \" \' % ; ( & +。";
				if (value.indexOf("<") !=-1 || value.indexOf(">") !=-1||  value.indexOf("\"") !=-1 || value.indexOf("\'")!=-1  ||  value.indexOf("%") !=-1 || value.indexOf(";") !=-1 ||  value.indexOf("(") !=-1 || value.indexOf("\'")!=-1 ||   value.indexOf("%") !=-1 || value.indexOf("&") !=-1 || value.indexOf("+") !=-1 ||  value.indexOf("。") !=-1)   	    
				{
					alert(this.name + "不可輸入" + mask );
					this.focus();
					return false;
				}
				break;
			
			case "n" :
				if(isNaN(value))
				{
					alert(this.name + "必須為數字");
					this.focus();
					return false;
				}
				break;
			case "d" :
				if(!checkDateFormat(value))
				{
					alert(this.name + "不是正確的日期");
					this.focus();
					return false;
				}
				break;
			case "e" :
				if(!checkEmail(value))
				{
					alert(this.name + "不是正確的email");
					this.focus();
					return false;
				}
				break;
			case "id" :
				if(!idCheck(value))
				{
					alert(this.name + "不正確");
					this.focus();
					return false;
				}
				break;
			
		}
		
		//check the length
		if(this.minLen == this.maxLen && this.minLen != -1)
		{
			if(value.length != this.minLen)
			{
				alert(this.name + "長度必須為" + this.minLen);
				this.focus();
				return false;
			}
		}else
		{
			//最小長度
			if(this.minLen != -1)
			{
				if(value.length < this.minLen)
				{
					alert(this.name + "長度至少為"+ this.minLen);
					this.focus();
					return false;
				}
			}
			
			//最大長度
			if(this.maxLen != -1)
			{
				if(value.length> this.maxLen)
				{
					alert(this.name + "長度不能超過"+ this.maxLen);
					this.focus();
					return false;
				}
			}
		}
		
		
		return true;
			
	}
	
	
	this.focus = function()
	{
		if(this.isArray){
			this.obj[0].focus();
		}else{
			this.obj.focus();
		}
	}
	
}


//確認是否有空白//
function checkEmpty(value) 
{
	var field = new String(value);
	if (field.length == 0) {
		return false;
	}
	return true;
}



//檢查日期格式是否正確
function checkDateFormat(value) 
{
	var arrayDay = new String(value).split("/");
	if (arrayDay.length != 3) return false ;
	for (var i=0; i<arrayDay.length; i++) 
	{
		if(isNaN(arrayDay[i])) return false;
	}
	if (!checkDate(arrayDay[0], arrayDay[1], arrayDay[2])) return false;
	return true;
}


//檢查月日是否正確//
function checkDate(YearValue, month, day) 
{
	y = parseInt(YearValue);
	//閏年檢查
	if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
	{
		maxday = 29; 	//如果true,29天
	}
	else	//否則28天
	{
		maxday = 28;
	}

	m = parseInt(month);
	d = parseInt(day);
	if (m > 12 || m < 0) 
	{
		//alert("不正確的月份");
		return false;
	}
	if (d > 31 || d < 0) 
	{
		//alert("不正確的天數");
		return false;
	}
	switch (m) 
	{
		case 4: case 6: case 9: case 11:
			if (d > 30) 
			{
				//alert("不正確的天數");
				return false;
			} break;
		case 2:
			if (d > maxday) 
			{
				//alert("不正確的天數");
				return false;
			} break;
	}
	return true;
}


//判斷email是否正確//str.indexOf(".") > 2
function checkEmail(value)
{
	var email = new String(value);	
	if (!(email.indexOf("@") > 0) || !(email.indexOf("@") < email.length-1)  || !(email.indexOf(".") > 0) ) 
	{
		return false;
	}
	return true;
}


// 確認中華民國身份證號 
function idCheck(value)
{
	id = value.toUpperCase();

	if (id.length != 10) return false;

	var ID_Input = new Array(10);

	for (var i=0 ; i<10 ; i++)
		ID_Input[i] = id.charAt(i);


	var engstring = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	ID_Input[0] = engstring.indexOf(ID_Input[0]);

	if(ID_Input[0] == -1) return false;

	if (ID_Input[1] != 1 && ID_Input[1] != 2) return false;
	
	return true;

}


function checkForm(form, arrayTag) {

	for (var i=0; i< arrayTag.length; i++) {
		arrayTag[i].setObject(form);
		if (!arrayTag[i].checkValue()) return false;
	}
	return true;
}

function checkFileExt( extensions ,field  )
 {
	if (extensions != '') 
		var re = new RegExp("\.(" + extensions.replace(/,/gi,"|") + ")$","i"); 
	
	if(extensions != '' && !re.test(field.value)) 
	{ 
		   // alert("這個檔案不符合上傳的類型！\n只有以下的類型才允許上傳： " + extensions + "。\n請依規定選取新的檔案。"); 
		   alert( "長度不能超過" ) ;
			
			field.focus();
			return false;        
	}
	return true; 
}

	
