/*  checkForm.js: an object is used to check form's validation , version 1.0
 *  (c) 2007 ryan <rxm1025@gmail.com>
 *	e.g.
		var list = new Array();
		list[0] = new Array("text","admin_name","ﾐﾕﾃ・);
		list[1] = new Array("text","admin_pwd","ﾃﾜﾂ・);
		
		window.onload = function() {
			checkForm.checkAll("form",list);
		}
/*--------------------------------------------------------------------------*/

//an empty object includes some validation function
var checkForm = new Object();
//set focus and select
checkForm.setFocus = function(fieldID) {
	elField = document.getElementById(fieldID);
	elField.focus();	//used in Maxthon,only select() can't get focus in Maxthon
	elField.select();
}
//check this filed is empty or not
checkForm.isEmpty = function(fieldID) {
	elField = document.getElementById(fieldID);
	if(elField.value == "") { //empty
		return true; 
	}
	else { // not empty
		return false;
	}
}
//check empty and prompt info  
checkForm.checkEmpty = function(fieldID,promptInfo) {
	if(this.isEmpty(fieldID)) {
		alert(promptInfo + "を入力してください。");
		this.setFocus(fieldID);
		return false; //empty
	}
	else {
		return true;
	}
}
//check equal or not  
checkForm.checkEqual = function(fieldID1,fieldID2,promptInfo1,promptInfo2) {
	elField1 = document.getElementById(fieldID1);
	elField2 = document.getElementById(fieldID2);
	if(elField1.value != elField2.value) {
		alert(promptInfo1 + promptInfo2 + "");
		this.setFocus(fieldID2);
		return false; 
	}
	else {
		return true;
	}	
}
//check regExp
checkForm.checkRegExp = function(pattern,fieldID,promptInfo,required){
	if(!this.isEmpty(fieldID)) {
		if (!pattern.test(document.getElementById(fieldID).value) ) {
			alert(promptInfo + "が正しくないので、もう一回入力してください。");
			this.setFocus(fieldID);
			return false;
		}
		else {
			return true;
		}
	}
	else {
		if(required) {
			return this.checkEmpty(fieldID,promptInfo);
		}
		else {
			return true;
		}
	}
}
//check email's format whether legal
checkForm.checkEmail = function(fieldID,promptInfo,required){
	var pattern = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
	return this.checkRegExp(pattern,fieldID,promptInfo,required);
}
//check zipcode
checkForm.checkZip = function(fieldID,promptInfo,required){
	var pattern = /^\d{6}$/;
	return this.checkRegExp(pattern,fieldID,promptInfo,required);
}
//check mobile
checkForm.checkMobile = function(fieldID,promptInfo,required){
	var pattern = /^\d{11}$/;
	return this.checkRegExp(pattern,fieldID,promptInfo,required);
}
//check number
checkForm.checkNum = function(fieldID,promptInfo,required){
	var pattern = /^\d+$/;
	return this.checkRegExp(pattern,fieldID,promptInfo,required);
}
//check all fileds,use some different functions according to the parameters array 
checkForm.checkAll = function(formID,checkList){
	var elForm = document.getElementById(formID);
	//"this" points to object "checkForm" 
	elForm.obj = this;	
	elForm.onsubmit = function() {
		for(i = 0;i < checkList.length;i++) {
			switch(checkList[i][0]) {
				case "text":
					//In event handler,"this" points to the DOM element which calls event handler
					if( !this.obj.checkEmpty(checkList[i][1],checkList[i][2]) ) {
						return false;
					}					
					break;
				case "email":
					if( !this.obj.checkEmail(checkList[i][1],checkList[i][2],checkList[i][3]) ) {
						return false;
					}					
					break;
				case "mobile":
					if( !this.obj.checkMobile(checkList[i][1],checkList[i][2],checkList[i][3]) ) {
						return false;
					}					
					break;
				case "number":
					if( !this.obj.checkNum(checkList[i][1],checkList[i][2],checkList[i][3]) ) {
						return false;
					}					
					break;
				case "equal":
					if( !this.obj.checkEqual(checkList[i][1],checkList[i][2],checkList[i][3],checkList[i][4]) ) {
						return false;
					}					
					break;
				default:
					// do something
			} 
		}
		return true;
	}
}

