// Javascript form validation
// By: Object freedom
// E-mail: active_object_freedom@hotmail.com
// Version: 2.0 (25-11-2006)
// http://main.boxchart.com/main/

function freedom_verifyForm(){
	this.formObj = null;
	this.size = 0;
	this.name_id = new Array();
	this.code = new Array();
	this.blank = new Array();
	var aa='';
				//alert("ddddddddddddddddddval = ");
	this.init = function( formObj ){
		this.formObj = formObj;
	}
	this.isUrl = function(s) {
		var regexp = "(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?";
		return regexp.test(s);
	}

	this.isEmail = function(tfld){
			//alert("isEmail ->" + tfld);
			var emailnull = /(.)+/;
			var email = /^[^@]+@[^@.]+\.[^@]+\w\w$/;
			if (!emailnull.test(tfld)){
				aa = 1;
				return(false);
			}
			if (!email.test(tfld)){
				aa = 2;
				return(false);
			}
//			var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/;
//			if (!email2.test(tfld)) return(false);
			return(true);
	}
	
		this.isUsername = function(tfld){
			//alert("isUsername ->" + tfld);
			var txtnull = /(.)+/;
			var txtsize = /(.){6,20}/;
			var txttest = /[\w]{6,20}/;
			if (!txtnull.test(tfld)){
				aa = 1;
				return(false);
			}
			if (!txtsize.test(tfld)){
				aa = 2;
				return(false);
			}
			if (!txttest.test(tfld)){
				aa = 3;
				return(false);
			}
			return(true);
	}
	
		this.isPassword = function(tfld){
//			alert("isPassword ->" + tfld);
			var txtnull = /(.)+/;
			var txtsize = /(.){6,20}/;
			var txttest = /[\w]{6,20}/;
			if (!txtnull.test(tfld)){
				aa = 1;
				return(false);
			}
			if (!txtsize.test(tfld)){
				aa = 2;
				return(false);
			}
			if (!txttest.test(tfld)){
				aa = 3;
				return(false);
			}
			return(true);
	}

		this.isTel = function(tfld){
			//alert("isTel ->" + tfld);
			var txtnull = /(.)+/;
			var txtsize = /[\d]+/;
			var txttest = /^[0]{1}/;
			var txttest2 = /[\d]{9,10}$/;
			if (!txtnull.test(tfld)){
				aa = 1;
				return(false);
			}
			if (!txtsize.test(tfld)){
				aa = 2;
				return(false);
			}
			if (!txttest.test(tfld)){
				aa = 3;
				return(false);
			}
			if (!txttest2.test(tfld)){
				aa = 4;
				return(false);
			}
			return(true);
	}
	
		this.isZipcode = function(tfld){
			//alert("isZipcode ->" + tfld);
			var txtnull = /(.)+/;
			var txtsize = /[\d]+/;
			var txttest2 = /[\d]{5}$/;
			if (!txtnull.test(tfld)){
				aa = 1;
				return(false);
			}
			if (!txtsize.test(tfld)){
				aa = 2;
				return(false);
			}
			if (!txttest2.test(tfld)){
				aa = 3;
				return(false);
			}
			return(true);
	}
	
		this.isCapt = function(tfld){
			//alert("isCapt ->" + tfld);
			var txtnull = /(.)+/;
			var txttest2 = /[A-Z0-9]{5}$/;
			if (!txtnull.test(tfld)){
				aa = 1;
				return(false);
			}
			if (!txttest2.test(tfld)){
				aa = 2;
				return(false);
			}
			return(true);
	}
	
		this.isPrice = function(tfld){
			//alert("isPrice ->" + tfld);
			var txtnull = /(.)+/;
			var txttest2 = /[\d\.]/;
			if (!txtnull.test(tfld)){
				aa = 1;
				return(false);
			}
			if (!txttest2.test(tfld)){
				aa = 2;
				return(false);
			}
			return(true);
	}
	
	this.isDate = function( val ){
		if(!this.chkSyn(val,"[0-9]{1,2}/[0-9]{1,2}/[2][0-9]{3}")) return false; //รูปแบบไม่ตรง
		sp_date = val.split("/");
		sp_date[0] = parseInt(sp_date[0]);
		sp_date[1] = parseInt(sp_date[1]);
		sp_date[2] = parseInt(sp_date[2]);
		if(sp_date[1]>12){
			return false;
		}else if(sp_date[1]==4 || sp_date[1]==6 || sp_date[1]==9 || sp_date[1]==11 ){
			if( sp_date[0] >30){ // ลงท้ายด้วย 'ยน' มากกว่า 30
				return false;
			}
		}
		else if ( sp_date[1]==2){		
			if ((sp_date[2]%4==0 && sp_date[2]%100!=0) || sp_date[2]%400==0){
				if (sp_date[0]>29)	{ //กุมภา มากกว่า 29
					return false;
				}
			}
			else if(sp_date[0]>28){
				return false;
			}
		}
		else if ( sp_date[0] >31 ){	//วันมากกว่า 31
			return false;
		}
		return true;
	}

	this.chkSyn = function( val, code ){
		switch ( code )
		{
		case "num":
				code="^[1-9]{1}[0-9.]{0,10}";
				break;
		case 	"username":
				//code="[0-9a-zA-Z_]{6,20}";
				return(this.isUsername( val ));
				break;
		case 	"password":
				//code="[0-9a-zA-Z_]{6,20}";
//				alert( val) ;
				return(this.isPassword( val ));
				break;
		case "email":
				return(this.isEmail( val ));
				break;
		case "url":
				return(this.isUrl( val ));
				break;
		case "date":
				return(this.isDate( val ));
				break;//isTel
		case "tel":
				return(this.isTel( val ));
				break;
		case "postcode":
				//code="[0-9]{5}";
				return(this.isZipcode( val ));
				break;
		case "capt":
				//code="[0-9]{5}";
				return(this.isCapt( val ));
				break;
		case "price":
				//code="[0-9]{5}";
				return(this.isPrice( val ));
				break;
		case "idcard":
				code="[0-9]{13}";
				break;
		case "year":
				code = "[2][0-9]{3}";//พศ
				break;
		case "*":
				if(val=="" || val.substr(0,1)==" ") return(false);
				return(true);
				break;
		}
						
		var rx = new RegExp(code);
		var mat = rx.exec(val);
		if(mat==null || mat != val ){//ตรวจสอบการทำผิดเงื่อนไขถ้า null แสดงว่าผิด
			//alert("error Sytax")
			return false;
		}
		return true;
	}
	
	this.add = function(n ,c , b){
		this.name_id[this.size] = n;
		this.code[this.size] = c;
		this.blank[this.size] = b;
		this.size++
	}
	
	this.check = function(){
		var errString="";
		error=null;
		for(i=0;i<this.size;i++){
				val = document.getElementById(this.name_id[i]).value;
				if(this.blank[i] && val==""){
					//return(true); //blank return
					continue;
				}
				//alert("check " + val + ", " + this.name_id[i] + "," +this.code[i] + "," +this.blank[i])
				if(!this.chkSyn(val, this.code[i])){
					//errString += "\n      * " + ObjForm.item(i).mm;					
					if( error==null ) error = this.name_id[i];
					break;
				}		
		}
		if( error!=null ) {
				//alert("check " + val + ", " + this.name_id[i] + "," +this.code[i] + "," +this.blank[i])
					switch ( error )
					{
					case "name":
							alert("กรุณากรอกชื่อ");
							break;
					case 	"surname":
							alert("กรุณากรอกนามสกุล");
							break;
					case "user_login":
							//alert("ชื่อที่ใช้เข้าระบบต้องเป็น ตัวอักษร a-z, A-Z, 0-9,_ เท่านั้น ความยาว 6-20 ตัวอักษร");
							if(aa == 1){
								alert("กรุณากรอกชื่อที่ใช้เข้าระบบ");
							}else if(aa == 2){
								alert("ชื่อที่ใช้เข้าระบบต้องมีความยาว 6-20 ตัวอักษร");
							}else{
								alert("ชื่อที่ใช้เข้าระบบต้องเป็น ตัวอักษร a-z, A-Z, 0-9,_ เท่านั้น ความยาว 6-20 ตัวอักษร");
							}
								break;
					case "pass_login":
							//alert("กรุณากรอกรหัสผ่าน");
							if(aa == 1){
								alert("กรุณากรอกรหัสผ่าน");
							}else if(aa == 2){
								alert("รหัสผ่านต้องมีความยาว 6-20 ตัวอักษร");
							}else{
								alert("รหัสผ่านต้องเป็น ตัวอักษร a-z, A-Z, 0-9,_ เท่านั้น ความยาว 6-20 ตัวอักษร");
							}
							break;
					case "repass_login":
							//alert("กรุณากรอกรหัสผ่าน");
							if(aa == 1){
								alert("กรุณายืนยันรหัสผ่าน");
							}else if(aa == 2){
								alert("รหัสผ่านต้องมีความยาว 6-20 ตัวอักษร");
							}else{
								alert("รหัสผ่านต้องเป็น ตัวอักษร a-z, A-Z, 0-9,_ เท่านั้น ความยาว 6-20 ตัวอักษร");
							}
							break;
					case "capt":
							if(aa == 1){
								alert("กรุณากรอกรหัสยืนยัน");
							}else if(aa == 2){
								alert("กรุณากรอกรหัสยืนยันเป็นตัวพิมพ์ใหญ่ (A-Z), 0-9 ความยาว 5 ตัวอักษร");
							}
							break;
					case "tel":
							if(aa == 1){
								alert("กรุณากรอกเบอร์โทรศัพท์");
							}else if(aa == 2){
								alert("กรอกเบอร์โทรศัพท์เป็นตัวเลขเท่านั้น");
							}else if(aa == 3){
								alert("รูปแบบเบอร์โทรศัพท์ไม่ถูกต้อง (ขึ้นต้นด้วย 0)");
							}else{
								alert("เบอร์โทรศัพท์ต้องมีความยาว 9-10 ตัว");
							}
							break;
					case "email":
							if(aa == 1){
								alert("กรุณากรอกอีเมล์");
							}else{
								alert("รูปแบบอีเมล์ไม่ถูกต้อง");
							}
							break;
					case "address":
							alert("กรุณากรอกที่อยู่");
							break;
					case "zipcode":
							if(aa == 1){
								alert("กรุณากรอกรหัสไปรษณีย์");
							}else if(aa == 2){
								alert("กรุณากรอกรหัสไปรษณีย์เป็นตัวเลข");
							}else{
								alert("รหัสไปรษณีย์ต้องมีความยาว 5 ตัว");
							}
							break;
					case "price":
							if(aa == 1){
								alert("กรุณากรอกจำนวนเิงิน");
							}else if(aa == 2){
								alert("จำนวนเงินต้องเป็นตัวเลข หรือจุดทศนิยมเท่านั้น (ไม่ต้องใส่ , [คอมม่า])");
							}
							break;
					case "startdate":
							alert("กรุณากรอกวันที่");
							break;
							
					default:alert("กรุณาตรวจสอบข้อมูลให้ถูกต้อง");
					}
				document.getElementById( error ).focus();
				return false;
		}else{
				return true;
		}
	}
	
//	this.checkdetail = function(){
//			var errString="";
//			var nameid="";
//			error=null;
//			for(i=0;i<this.size;i++){
//					val = document.getElementById(this.name_id[i]).value;
//					if(this.blank[i] && val==""){
//						//return(true); //blank return
//						continue;
//					}
//					alert("checkdetail " + val + ", " + this.name_id[i] + "," +this.code[i] + "," +this.blank[i])
//					nameid = this.name_id[i];
//					switch ( nameid )
//					{
////					case "name":
////							alert("กรุณากรอกชื่อ");
////							break;
////					case 	"surname":
////							alert("กรุณากรอกนามสกุล");
////							break;
//					case "user_login":
//							if ((val.length >20) || (val.length <6)){
//							alert("ชื่อใช้งาน(อย่างน้อย 6 ตัวอักษร แต่ไม่เกิน 20 ตัวอักษร)!!");
//							}else if(){
//								
//							}
//							
//							break;
//					case "pass_login":
//							alert("กรุณากรอกรหัสผ่าน");
//							break;
//					case "repass_login":
//							alert("กรุณายืนยันรหัสผ่าน");
//							break;
//					case "capt":
//							alert("กรุณากรอกรหัสยืนยัน");
//							break;
//					case "tel":
//							alert("กรุณากรอกเบอร์โทรศัพท์");
//							break;
//					case "email":
//							alert("กรุณากรอกอีเมล์");
//							break;
//					case "address":
//							alert("กรุณากรอกที่อยู่");
//							break;
//					case "zipcode":
//							alert("กรุณากรอกรหัสไปรษณีย์");
//							break;
//					default:alert("กรุณาตรวจสอบข้อมูลให้ถูกต้อง");
//					}
//			}
//	}
//}

}