﻿/*/====================================================================================
/// Ray 6/30/2006 
///		- validate string , integer ,or what you want
/// Ray 11/2/2006 
///		- `name` allow space
///		- add `password`
///		- add `ident` ,no allow space
///Sophie 12/5/2007 #9777 : nting - Invoice Tool => add "rate" between 0~100
/// Ray Chou 1/22/2008
///		- Modify "double"
///====================================================================================*/
function is_string_validate(strValue,strType){
	var strRegx   ="";
	var strTmp	  = "";
	var isSuccess = false;
	//---------------------
	if(!strValue){
		//alert("Wrong parameter!");
		//return false;
		isSuccess =true;
	}
	if(strType){
		strType = strType.toLowerCase();
	}else{
		strType = "";
	}

	//---------------------
	if(strValue){
		switch (strType){
			case "null":
				isSuccess = true;
				break;
			case "int":
				strRegex=/\d+/;
				strTmp=strValue.match(strRegex);
				//if(strValue == strTmp)isSuccess = true;
				if(strValue == strTmp && strValue >=0 && strValue <= 4294967295) isSuccess = true;
				break;			
			case "tinyint":
				/// Sophie 7/11/2006
				/// when datatype = tinyint & length = 3
				strRegex=/\d+/;
				strTmp=strValue.match(strRegex);
				if(strValue == strTmp && strValue >=0 && strValue <= 255) isSuccess = true;
				break;
			case "smallint":
				/// Sophie 7/12/2006
				strRegex=/\d+/;
				strTmp=strValue.match(strRegex);
				if(strValue == strTmp && strValue >=0 && strValue <= 65535) isSuccess = true;
				break;
			case "rate":
				/// Sophie 12/5/2007 #9777
				strRegex=/\d+\.\d+|\d+/;
				strTmp=strValue.match(strRegex);
				if(strValue == strTmp && strValue >=0 && strValue <= 100) isSuccess = true;
				break;
			case "mediumint":
				/// Sophie 7/12/2006
				strRegex=/\d+/;
				strTmp=strValue.match(strRegex);
				if(strValue == strTmp && strValue >=0 && strValue <= 16777215) isSuccess = true;
				break;
			case "bigint":
				/// Sophie 7/12/2006
				strRegex=/\d+/;
				strTmp=strValue.match(strRegex);
				if(strValue == strTmp && strValue >=0 && strValue <= 18446744073709551615) isSuccess = true;
				break;
			case "double":
				/// Sophie 7/12/2006
				strRegex=/\d+\.\d+|\d+/;
				strTmp=strValue.match(strRegex);
				if((strValue == strTmp || (0-strValue) == strTmp) && strValue >=-9999999999 && strValue <= 9999999999 ) isSuccess = true;
				break;		
			case "text":
				isSuccess = true;
				break;
			case "float":
				strRegex=/\d+\.\d+|\d+/;
				strTmp=strValue.match(strRegex);
				if(strValue == strTmp)isSuccess = true;
				break;
			case "name":
				strRegex =/\w+[-_]\w+|\w+/;
				isSuccess = true;
				var arrTmp = strValue.split(",");
				for(key in arrTmp){
					if(arrTmp[key]){
						try{strTmp=strValue.match(arrTmp[key]);}catch(e){}
						if(strValue != strTmp)isSuccess = false;
						if(isSuccess == false)break;
					}
				}

				break;
			case "ident":
				strRegex =/\w+[-_]\w+|\w+/;
				strTmp=strValue.match(strRegex);
				if(strValue == strTmp)isSuccess = true;
				break;
			
			case "email":
				/*
				var arrTmp = strValue.split("@");
				var intCountSuccess = 0;
					if(arrTmp.length==2 && arrTmp[1]){
						var strA = arrTmp[0];		// string before @
						var strB = arrTmp[1];		// string after @
						var strResult = strA.match(/\w+\.\w+|\w+/);
						if(strA == strResult){
							intCountSuccess++;
						}
						strResult = strB.match(/\w+\.\w+\.\w+|\w+\.\w+/);
						if(strB == strResult){
							intCountSuccess++;
						}
					}
					if(intCountSuccess==2)isSuccess = true;
				*/	
				
				if( strValue.match( /^[a-z].*\w+@\w+\.[a-z]{2,3}/i) != null ) 
					isSuccess = true;
					
				break;
			case "phone":
				///Ray 8/10/2006

				// 1. check first char
				strRegex = /^[+]|^\d/;

				if(strValue.match(strRegex)!=null){
					//2 . check others
					strRegex = /[ \-()\#]/;
					//# cut first char
					strValue=strValue.substr(1,strValue.length);
					for(var i=0;i<=10;i++){
						strValue  = strValue.replace(strRegex,"");
					}
					//3 . check string is [0-9]
					strRegex = /\d+/;
					strTmp = strValue.match(strRegex);
					if(strTmp == strValue)isSuccess = true;
				}
				break;
			case "password":
				strRegex = /^\w+\w+\w$/;
				strTmp=strValue.match(strRegex);
				if(strValue == strTmp)isSuccess = true;
				break;
			case "specialchars":
				///Ray 8/10/2006
				strRegex=/[\"\'\\]/;
				if(strValue.search(strRegex)==-1){
					isSuccess = true;
				}
				break;
			case "notEmpty":
				//Chris 4/17/2008
				strTmp = strValue.toString().trim();
				if( !strTmp )
					isSuccess = false;
				else
					isSuccess = true;
				break;
			default:
				strRegex = /\w+/;
				strTmp = strValue.match(strRegex);
				if(strValue == strTmp)isSuccess = true;
		}
	}
	return isSuccess;
}

