/******************************************************************************/
/* This JavaScript in order to check input format.                            */
/* Function:                                                                  */
/*     SBCToDBC       -- Change the Character from SBC Case to DBC Case       */
/*     chkForbidChar  -- Forbidden Character Check                            */
/*     chkDigitalChar -- Digital Input Check                                  */
/*     chkCorrectDate -- Check Input Date Format and is Correct Date          */
/*     chkEMail       -- Check Input E-Mail Address                           */
/* Variable:                                                                  */
/*     null                                                                   */
/******************************************************************************/


/* Change the Character from SBC Case to DBC Case in a String */
function SBCToDBC(txt) {
	var SBCCase = "£ãģţƣǣȣɣʣˣ̣ͣΣϣУѣңӣԣգ֣ףأ٣ڣꣁEEEۣܣݣޣߣE";
	var DBCCase = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}";	
	var i;
	var pos;
	for (i = 0; i < txt.length; i++) {
	    var chkStr = txt.substring(i, i + 1);
	    pos = SBCCase.indexOf(chkStr);
	    if (pos != -1) {
	    	txt = txt.substring(0, i) + DBCCase.substring(pos, pos + 1) + txt.substring(i + 1);
	    }
	}
	return txt;
}

/* Forbidden Character Check */
function chkForbidChar(txt, except) {
	var forbiddenCase = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}";
	var i;
	for (i = 0; i < txt.length; i++) {
		if (except.indexOf(txt.substring(i, i + 1)) == -1) {
			if (forbiddenCase.indexOf(txt.substring(i, i + 1)) != -1) {
				alert ("Forbidden Character : " + txt.substring(i, i + 1) + ", Please Check Your Input.");
				return false;
			}
		}
	}
	return true;
}
/*  Character len Check */
function chkLenth(txt,except) {
	var digitalCase = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!\"#$&()*+,-./:;<=>?@[\\]^_`{|}'";
	var i;
	for (i = 0; i < txt.length; i++) {
		if (except.indexOf(txt.substring(i, i + 1)) == -1) {
			if (digitalCase.indexOf(txt.substring(i, i + 1)) == -1) {
				alert ("Input Error : " + txt.substring(i, i + 1) + ", Please Check Your Input.");
				return false;
			}
		}
	}
	return true;
}
/* Digital Input Check */
function chkDigitalChar(txt, except) {
	var digitalCase = "0123456789";
	var i;
	for (i = 0; i < txt.length; i++) {
		if (except.indexOf(txt.substring(i, i + 1)) == -1) {
			if (digitalCase.indexOf(txt.substring(i, i + 1)) == -1) {
				alert ("Input Error : " + txt.substring(i, i + 1) + ", Please Check Your Input.");
				return false;
			}
		}
	}
	return true;
}
/* Digital Input Check */
function chkDigitalChar2(txt, except) {
	var digitalCase = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	var i;
	for (i = 0; i < txt.length; i++) {
		if (except.indexOf(txt.substring(i, i + 1)) == -1) {
			if (digitalCase.indexOf(txt.substring(i, i + 1)) == -1) {
				alert ("Input Error : " + txt.substring(i, i + 1) + ", Please Check Your Input.");
				return false;
			}
		}
	}
	return true;
}
/* Digital Input Check */
function chkDigitalChar3(txt, except) {
	var digitalCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	var i;
	for (i = 0; i < txt.length; i++) {
		if (except.indexOf(txt.substring(i, i + 1)) == -1) {
			if (digitalCase.indexOf(txt.substring(i, i + 1)) == -1) {
				alert ("Input Error : " + txt.substring(i, i + 1) + ", Please Check Your Input.");
				return false;
			}
		}
	}
	return true;
}
/* Digital Input number Check */
function checkNum(txt,intPartMax, decPartMax) {

		var intPart;
		var decPart;
		
		var txtTemp = txt.replace(".", "");
		if (txtTemp.length == txt.length) {
				intPart = txt.length;
				
				if (intPart > intPartMax){
					return false;
				} else {
					return true;
				}
		}
						
		var i;
		for(i = 0; i< txt.length; i++){
			var temp = txt.substring(i, i+1);

			if ("." == temp) {
				intPart = i;
				decPart = txt.length - i - 1;
				break;
			}
				
		}

		if (intPart > intPartMax){
			return false;
		}
		if (decPart > decPartMax){
			return false;
		}		
		return true;
}
/* Check Input Date Format and is Correct Date */
function chkCorrectDate(txt) {
	var chkDay = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var year, month, day;
	
	if (txt.indexOf("/") == -1) {
		if (txt.length == 8) {
			year  = txt.substring(0, 4);
			month = txt.substring(4, 6);
			day   = txt.substring(6, 8);
		} else if (txt.length == 6) {
			year  = txt.substring(0, 2);
			month = txt.substring(2, 4);
			day   = txt.substring(4, 6);
			if (year <= 70) {
				year = "20" + year;
			} else {
				year = "19" + year;
			}
		} else {
			alert ("Input Date is not a Correct Date Format\nPlease Format Date in YYYYMMDD or YYMMDD or YYYY/MM/DD");
			return null;
		}
	} else {
		var pos  = txt.indexOf("/");
		var pos2 = txt.indexOf("/", pos + 2);
		if (pos == -1) {
			alert ("Input Date is not a Correct Date Format\nPlease Format Date in YYYYMMDD or YYMMDD or YYYY/MM/DD");
			return null;
		}
		
		/* add by wangyong in 2004-10-20 for chack '/' count */
		var location = txt.indexOf("/");
		location = txt.indexOf("/",location + 1);
		location = txt.indexOf("/",location + 1);
		if (location != -1) {
			alert ("Input Date is not a Correct Date Format\nPlease Format Date in YYYYMMDD or YYMMDD or YYYY/MM/DD");
			return null;
		}
		/* add ended */
		year  = txt.substring(0, pos);
		month = txt.substring(pos + 1, pos2);
		day   = txt.substring(pos2 + 1);
		if (year.length == 0 || year.length == 3 || year.length > 4) {
			alert ("Input Date is not a Correct Date Format\nPlease Format Date in YYYYMMDD or YYMMDD or YYYY/MM/DD");
			return null;
		} else if (year.length == 1) {
			year = "200" + year;
		} else if (year.length == 2) {
			if (year <= 70) {
				year = "20" + year;
			} else {
				year = "19" + year;
			}
		}
		if (month.length == 1) {
			month = "0" + month;
		} else if (month.length > 2 || month.length == 0) {
			alert ("Input Date is not a Correct Date Format\nPlease Format Date in YYYYMMDD or YYMMDD or YYYY/MM/DD");
			return null;		
		}
		if (day.length == 1) {
			day = "0" + day;
		} else if (day.length > 2 || day.length == 0) {
			alert ("Input Date is not a Correct Date Format\nPlease Format Date in YYYYMMDD or YYMMDD or YYYY/MM/DD");
			return null;		
		}
	}
	
	if (year % 4 == 0) {
		if (year % 100 != 0) {
			chkDay[1] = 29;
		} else {
			if (year % 400 == 0) {
				chkDay[1] = 29;
			} else {
				chkDay[1] = 28;
			}
		}
	} else {
		chkDay[1] = 28;
	}
	
	if (year < 1971 || year > 2070 || month < 1 || month > 12 || day < 1 || day > chkDay[month - 1]) {
		alert ("Date must between 1971/01/01 - 2070/12/31");
		return null;
	}
	
	txt = year + "/" + month + "/" + day;
	return txt;
	
}
/* Check Input Date Format and is Correct Date */
function chkCorrectDate8(txt) {
	var chkDay = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var year, month, day;
	
	if (txt.indexOf("/") == -1) {
		if (txt.length == 8) {
			year  = txt.substring(0, 4);
			month = txt.substring(4, 6);
			day   = txt.substring(6, 8);

		} else {
			alert ("Input Date is not a Correct Date Format\nPlease Format Date in YYYYMMDD or YYYY/MM/DD");
			return null;
		}
	} else {
		var pos  = txt.indexOf("/");
		var pos2 = txt.indexOf("/", pos + 2);
		if (pos == -1) {
			alert ("Input Date is not a Correct Date Format\nPlease Format Date in YYYYMMDD or YYYY/MM/DD");
			return null;
		}
		
		/* add by wangyong in 2004-10-20 for chack '/' count */
		var location = txt.indexOf("/");
		location = txt.indexOf("/",location + 1);
		location = txt.indexOf("/",location + 1);
		if (location != -1) {
			alert ("Input Date is not a Correct Date Format\nPlease Format Date in YYYYMMDD or YYYY/MM/DD");
			return null;
		}
		/* add ended */
		year  = txt.substring(0, pos);
		month = txt.substring(pos + 1, pos2);
		day   = txt.substring(pos2 + 1);
		if (year.length == 0 || year.length == 3 || year.length > 4) {
			alert ("Input Date is not a Correct Date Format\nPlease Format Date in YYYYMMDD or YYYY/MM/DD");
			return null;
		} else if (year.length == 1) {
			year = "200" + year;
		} else if (year.length == 2) {
			if (year <= 70) {
				year = "20" + year;
			} else {
				year = "19" + year;
			}
		}
		if (month.length == 1) {
			month = "0" + month;
		} else if (month.length > 2 || month.length == 0) {
			alert ("Input Date is not a Correct Date Format\nPlease Format Date in YYYYMMDD or YYYY/MM/DD");
			return null;		
		}
		if (day.length == 1) {
			day = "0" + day;
		} else if (day.length > 2 || day.length == 0) {
			alert ("Input Date is not a Correct Date Format\nPlease Format Date in YYYYMMDD or YYYY/MM/DD");
			return null;		
		}
	}
	
	if (year % 4 == 0) {
		if (year % 100 != 0) {
			chkDay[1] = 29;
		} else {
			if (year % 400 == 0) {
				chkDay[1] = 29;
			} else {
				chkDay[1] = 28;
			}
		}
	} else {
		chkDay[1] = 28;
	}
	
	if (year < 1971 || year > 2070 || month < 1 || month > 12 || day < 1 || day > chkDay[month - 1]) {
		alert ("Date must between 1971/01/01 - 2070/12/31");
		return null;
	}
	
	txt = year + "/" + month + "/" + day;
	return txt;
	
}
/* Check Input E-Mail Address */
function chkEMail(mail) {
	var pos = mail.indexOf("@");
	if (pos == -1 || pos == mail.length - 1 || pos == 0) {
		return false;
	}
	return true;
}
