var validResult = Class.create();
validResult.prototype = {
	resultCode : false,
	resultValue : null,

	initialize : function (pCode, pValue)
	{
		this.resultCode = pCode;
		this.resultValue = pValue;
	}
}

var validPattern = Class.create();
validPattern.prototype = {
	initialize : function ()
	{
	},

	require : function (pValue)
	{
		pValue = Common.trim(String(pValue));

		if (pValue == null || pValue == "") return false;

		if (pValue == "") return false;
		else return true;
	},

	integer : function(pValue)
	{
		pValue = Common.trim(String(pValue));
		if (pValue == null || pValue == "") return false;

		var pt = new RegExp("/^[\\-+]?[0-9]+$", "ig");

		return pt.test(pValue);
	},

	min_length : function (pValue, pLength)
	{
		pValue = Common.trim(String(pValue));
		if (pValue == null || pValue == "") return false;

		if (pValue == null || pLength == null) return false;
		if (!this.numeric(pLength)) return false;

		if (pValue.length < pLength) return false;

		return true;
	},

	max_length : function (pValue, pLength)
	{
		pValue = Common.trim(String(pValue));
		if (pValue == null || pValue == "") return false;

		if (pValue == null || pLength == null) return false;
		if (!this.numeric(pLength)) return false;

		if (pValue.length > pLength) return false;

		return true;
	},

	between_length : function(pValue, pMin, pMax)
	{
		pValue = Common.trim(String(pValue));

		if (pValue == null || pValue == "") return false;

		return (this.min_length(pValue, pMin) && this.max_length(pValue, pMax))?true:false;
	},

	valid_email : function(pValue)
	{
		pValue = Common.trim(String(pValue));
		if (pValue == null || pValue == "") return false;

		var supported = 0;
		if (window.RegExp)
		{
			var tempStr = "a";
			var tempReg = new RegExp(tempStr);
			if (tempReg.test(tempStr)) supported = 1;
		}
		if (!supported) return (pValue.indexOf(".") > 2) && (pValue.indexOf("@") > 0);

		var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
		var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");

		return (!r1.test(pValue) && r2.test(pValue));
	},

	valid_emails : function(pValue)
	{
		pValue = Common.trim(String(pValue));
		if (pValue == null || pValue == "") return false;

		if (pValue.indexOf(",") < 0) return this.valid_email(pValue);
		else
		{
			var mails = pValue.split(",");

			for(var i=0 ; i<mails.length ; ++i)
				if (!this.valid_email(mails[i])) return false;

			return true;
		}
	},

	phone_no_style : function(pValue)
	{
		return (this.between_length(pValue, 2, 20) && this.numeric_ext(pValue));
	},

	allphone : function(pValue)
	{
		pValue = Common.trim(String(pValue));
		if (pValue == null || pValue == "") return false;

		if (pValue.indexOf("-") > 0)
		{
			var oTemp = pValue.split("-");
			if (oTemp.length == 3)
			{
				if (oTemp[0].length > 3) return false;
				if (oTemp[1].length > 4) return false;
				if (oTemp[1].length < 3) return false;
				if (oTemp[2].length > 4) return false;
			}
			else if (oTemp.length == 2)
			{
				if (oTemp[0].length > 4) return false;
				if (oTemp[1].length > 4) return false;
			}
		}
		pValue = pValue.replace(/-/ig, "");
		if (window.RegExp)
		{
			//var reg = new RegExp("/^([\d+]{9,10,11})$/");
			var reg = null;
			var prefix = pValue.substr(0,3);
			if (prefix == "154" || prefix == "164" || prefix == "158" || prefix == "168" || prefix == "159" || prefix == "166")
				reg = new RegExp("^([0-9]{8})$", "g");
			else if (prefix == "070")
				reg = new RegExp("^([0-9]{11})$", "g");
			else if (prefix == "080")
				reg = new RegExp("^([0-9]{10,11})$", "g");
			else
				reg = new RegExp("^([0-9]{9,11})$", "g");

			return reg.test(pValue);
		}
		return false;
	},

	phone : function(pValue)
	{
		pValue = Common.trim(String(pValue));
		if (pValue == null || pValue == "") return false;

		if (window.RegExp)
		{
			var reg = new RegExp("([0-9]{2,3,4}(-)?[0-9]{3,4}(-)?[0-9]{4})$");

			return reg.test(pValue);
		}
		else return false;
	},

	cellphone : function(pValue)
	{
		pValue = Common.trim(String(pValue));
		if (pValue == null || pValue == "") return false;

		//if (pValue.length != 10 && pValue.length != 11) return false;
		var temp = pValue.replace(/-/ig, "");
		if (temp.length != 10 && temp.length != 11) return false;

		if (window.RegExp)
		{
			//var reg = new RegExp("([0-9]{3}(-)?[0-9]{3,4}(-)?[0-9]{4})$");
			var reg = /^01[016789]\d{7,8}$/;

			return reg.test(temp);
		}
		else return false;
		// return (this.numeric(pValue) && this.between_length(pValue, 10, 11));
	},

	telecom : function (pValue)
	{
		pValue = Common.trim(String(pValue));
		if (pValue == null || pValue == "") return false;

		var preFix = pValue.substring(0,3);

		if (preFix == "010" || preFix == "011" || preFix == "016" || preFix == "017" || preFix == "018" || preFix == "019") return true;

		return false;
	},

	alpha : function(pValue)
	{
		pValue = Common.trim(String(pValue));
		if (pValue == null || pValue == "") return false;

		if (window.RegExp)
		{
			var reg = new RegExp("^[a-zA-Z_]+$", "ig");

			return reg.test(pValue);
		}
		else return false;
	},

	numeric : function(pValue)
	{
		pValue = Common.trim(String(pValue));
		if (pValue == null || pValue == "") return false;

		if (window.RegExp)
		{
			var reg = new RegExp("^[0-9]+$", "g");
			return (reg.test(pValue));
		}
		else return false;
	},

	numeric_ext : function(pValue)
	{
		pValue = Common.trim(String(pValue));
		if (pValue == null || pValue == "") return false;

		if (window.RegExp)
		{
			var reg = new RegExp("^[0-9-]+$", "g");
			return (reg.test(pValue));
		}
		else return false;
	},

	repeat_pattern : function(pValue, pLength)
	{
		for(i=0 ; i<pValue.length ; ++i)
		{
			//alert(pValue.charAt(i) + " - " + pValue.charAt(i).charCodeAt());

			if (pValue.charAt(i).charCodeAt() == 92)
				var reg = new RegExp("[\\\\]{" + pLength + "}");
			else if (pValue.charAt(i).charCodeAt() == 94)
				var reg = new RegExp("[\\^]{" + pLength + "}");
			else if (pValue.charAt(i).charCodeAt() == 47)
				var reg = new RegExp("[\\/]{" + pLength + "}");
			else
				var reg = new RegExp("[" + pValue.charAt(i) + "]{" + pLength + "}");

			if (reg.test(pValue)) return false;
		}

		return true;
	},

	alpha_numeric : function(pValue)
	{
		pValue = Common.trim(String(pValue));
		if (pValue == null || pValue == "") return false;

		var reg = new RegExp("^[a-z0-9]+$", "ig");

		return reg.test(pValue);
	},

	exact_length : function(pValue, pLength)
	{
		pValue = Common.trim(String(pValue));
		if (pValue == null || pValue == "") return false;

		if (this.numeric(pLength))
			if (pValue.length == pLength) return true;

		return false;
	},

	patternCheck : function(pPattern, pValue, pMinLength, pMaxLength)
	{
		pValue = Common.trim(String(pValue));
		if (pValue == null || pValue == "") return false;

		var methods = pPattern.split("|");

		for(var i=0 ; i<methods.length ; ++i)
		{
			var funcs = eval("this." + methods[i]);
			var result = true;

			if (methods[i] == "min_length") result = funcs.call(this, pValue, pMinLength);
			else if (methods[i] == "max_length") result = funcs.call(this, pValue, pMaxLength);
			else if (methods[i] == "between_length") result = funcs.call(this, pValue, pMinLength, pMaxLength);
			else if (methods[i] == "exact_length") result = funcs.call(this, pValue, pMinLength);
			else if (methods[i] == "extension") result = funcs.call(this, pValue, pMinLength);
			else if (methods[i] == "repeat_pattern") result = funcs.call(this, pValue, pMinLength);
			else result = funcs.call(this, pValue);

			if (!result)  return false
		}

		return true;
	},

	idno : function(pIdno)
	{
		pIdno = Common.trim(pIdno);
		if (pIdno == null || pIdno == "") return false;

		if (pIdno.trim() == "") return false;

		if (pIdno.indexOf("-") > 0) pIdno = pIndo.replace("-","");

		if (pIdno.length != 13) return false;

		var a1 = pIdno.substring(0, 1);
		var a2 = pIdno.substring(1, 2);
		var a3 = pIdno.substring(2, 3);
		var a4 = pIdno.substring(3, 4);
		var a5 = pIdno.substring(4, 5);
		var a6 = pIdno.substring(5, 6);
		var a7 = pIdno.substring(6, 7);
		var a8 = pIdno.substring(7, 8);
		var a9 = pIdno.substring(8, 9);
		var a10 = pIdno.substring(9, 10);
		var a11 = pIdno.substring(10, 11);
		var a12 = pIdno.substring(11, 12);
		var a = pIdno.substring(12, 13);

		var x = a1*2 + a2*3 + a3*4 + a4*5 + a5*6 + a6*7 + a7*8 + a8*9 + a9*2 + a10*3 + a11*4 + a12*5;

		var xx = x % 11;

		if (xx == 10)  xx = 0;

		a = 11 - a;
		if (a == 11) a = 1;
		else if (a == 10)  a = 0;

		if (xx == a) return true;
		else return false;
	},

	orgno : function (pIdno)
	{
		pIdno = Common.trim(pValue);
		if (pIdno == null || pIdno == "") return false;

		if (pIdno.length == 10)
		{
			var calStr1 = "13713713", biVal = 0,tmpCal;
			var calLast = pIdno.substring(9,10);

			for (i=0; i < 8; i++)
				biVal = biVal + (parseFloat(pIdno.substring(i,i+1)) * parseFloat(calStr1.substring(i,i+1))) % 10;

			tmpCal = parseFloat(pIdno.substring(8,9)) * 5 + "0";
			chkVal = parseFloat(tmpCal.substring(0,1)) + parseFloat(tmpCal.substring(1,2));

			chkDigit = (10 - (biVal + chkVal) % 10) % 10;

			if (calLast != chkDigit)  return false;
			else	return true;
		}
		else return false;
	},

	orgno_dash : function(pIdno)
	{
		pIdno = Common.trim(pValue);
		if (pIdno == null || pIdno == "") return false;

		if(pIdno.length == 12)
		{
			var calStr1 = "137-13-713", biVal = 0,tmpCal;
			var calLast = pIdno.substring(11,12);

			for (i=0; i < 10; i++)
			{
				if (i != 3 && i != 6)
					biVal = biVal + (parseFloat(pIdno.substring(i,i+1)) * parseFloat(calStr1.substring(i,i+1))) % 10;
			}

			tmpCal = parseFloat(pIdno.substring(10,11)) * 5 + "0";
			chkVal = parseFloat(tmpCal.substring(0,1)) + parseFloat(tmpCal.substring(1,2));

			chkDigit = (10 - (biVal + chkVal) % 10) % 10;

			if (calLast != chkDigit) return false;
			else return true;
		}
		else return false;
	},

	fgnno : function(pIdno)
	{
		pIdno = Common.trim(pValue);
		if (pIdno == null || pIdno == "") return false;

		var sum = 0;
		var odd = 0;

		buf = new Array(13);
		for (i = 0; i < 13; i++) buf[i] = parseInt(pIdno.charAt(i));

		odd = buf[7]*10 + buf[8];

		if (odd%2 != 0) return false;

		if ((buf[11] != 6)&&(buf[11] != 7)&&(buf[11] != 8)&&(buf[11] != 9))
			return false;

		var multipliers = [2,3,4,5,6,7,8,9,2,3,4,5];
		for (i = 0, sum = 0; i < 12; i++) sum += (buf[i] *= multipliers[i]);

		sum=11-(sum%11);

		if (sum>=10) sum-=10;

		sum += 2;

		if (sum>=10) sum-=10;

		if ( sum != buf[12]) return false;
		else return true;
	},

	emailid : function(pValue)
	{
		var str = new String(pValue);
		str = str.trim();

		if (str == null || str == "") return false;

		if (window.RegExp)
		{
			var reg = new RegExp("^[a-z0-9\.\\-_]+$", "g");

			return reg.test(str);
		}
		else return false;
	},

	isdate : function(pValue)
	{
		pValue = Common.trim(String(pValue));
		if (pValue == null || pValue == "") return false;

		if (window.RegExp)
		{
			var reg = new RegExp("([0-9]{4}[\\-\\.][0-9]{2}[\\-\\.][0-9]{2})$");

			return reg.test(pValue);
		}
		else return false;
	},

	extension : function(pFileName, pExtension)
	{
		pFileName = Common.trim(pFileName);
		if (pFileName == null || pFileName == "") return false;

		pExtension = Common.trim(pExtension);
		if (pExtension == null || pExtension == "") return false;

		pFileName = pFileName.toUpperCase();
		pExtension = pExtension.toUpperCase();

		pFileName = Common.trim(pFileName);

		if (pFileName == "") return false;

		var temp = pFileName.split(".");
		var exeTemp = pExtension.split(",");

		for(i=0 ; i<exeTemp.length ; ++i)
			if (temp[temp.length-1] == exeTemp[i]) return true;

		return false;
	},

	validGabiaID : function(pValue)
	{
		if (pValue.charAt(0) == "_" || pValue.charAt(0) == ".")
			return false;

		return this.patternCheck("require|between_length|emailid", pValue, 6, 16);
	},

	isHex : function(pValue)
	{
		pValue = pValue.toUpperCase();
		var reg = new RegExp("[A-Z0-9]{6}");

		return reg.test(pValue);
	},

	setMessage : function(pMessageID, pObjectID, pResult, pLayerID, pMsgObj)
	{
		if (typeof(pMessageID) == "object")
		{
			if (pMessageID.resultCode == RT_ERROR)
			{
				alert(pMessageID.resultCode + " : " + pMessageID.message);
				return false;
			}
			else
			{
				if (pMessageID.layerID == "")
					alert(pMessageID.message);
				else
					$(pMessageID.layerID).innerHTML = "<p>" + pMessageID.message + "</p>";

				return true;
			}
		}
		else
		{
			if (pLayerID == null)
			{
				if (!pResult && pObjectID != "" && pObjectID != null) $(pObjectID).focus();
				if (pMsgObj == null || pMsgObj == NaN)
					alert(msg.getMessage(pMessageID));
				else
					alert(pMsgObj.getMessage(pMessageID));
				return pResult;
			}
			else
			{
				if (pMsgObj == null || pMsgObj == NaN)
					$(pLayerID).innerHTML = "<p>" + msg.getMessage(pMessageID) + "</p>";
				else
					$(pLayerID).innerHTML = "<p>" + pMsgObj.getMessage(pMessageID) + "</p>";

				if (!pResult && pObjectID != "") $(pObjectID).focus();
				return pResult;
			}
		}
	}
}

var validCheck = new validPattern();

