
function ValidationType()
{
	return ["None","Character","Number","EMail","Address","Phone","Url"];
}

function Validation(controlID, validationType, minLength, maxLength,IsAllowEmpty,messageControlID,msgPrefix){
	var objControl = document.getElementById(controlID);
	var msgControl = document.getElementById(messageControlID);
	if(objControl == null){
		msgControl.innerHTML = "Can't not Find the Control:" + controlID;
		return false;
	}
	var value = objControl.value;
	var messageBuffer = new Array();
	var checkResult = true;
	
	//not allow empty
	if(!IsAllowEmpty){
		if(value.toString().length <=0){
			checkResult = false;	
			messageBuffer.push("Please enter " + msgPrefix + ".");	
			ShowMessage(msgControl,messageBuffer);
			return checkResult;
		}	
	}
	else{ // if empty
		if(value.length == 0){
			checkResult = true;
			return checkResult;		
		}	
	}
	
	//check value type
	if(ValidationType().toString().indexOf(validationType) >= 0){
		if(validationType == ValidationType()[0]){
			checkResult = checkResult && true;
		}
		
		if(validationType == ValidationType()[1]){
			checkResult = CheckVarchar(value);
			if(!checkResult){
				messageBuffer.push(msgPrefix + " include special characters.");
			}
		}
		
		if(validationType == ValidationType()[2]){
			checkResult = CheckNumber(value);
			if(!checkResult){
				messageBuffer.push("Please enter a valid " + msgPrefix + ".");
			}
		}
		
		if(validationType == ValidationType()[3]){
			checkResult = CheckEmail(value);
			if(!checkResult){
				messageBuffer.push("Please enter a valid " + msgPrefix + ".");
			}
		}		
	}
	
	//check value length
	if(minLength != -1){
		if(value.toString().length < minLength){
			checkResult = false;
			messageBuffer.push(msgPrefix + " should be at least " + minLength + " characters.");
		}
	}
	if(maxLength != -1){
		if(value.toString().length > maxLength){
			checkResult = false;
			messageBuffer.push(msgPrefix + " should be less than " + maxLength + " characters.");
		}
	}
	ShowMessage(msgControl,messageBuffer);
	return checkResult;
}

function CheckVarchar(value){
	var reg = /^[\w]+$/;
	return reg.test(value);
}

function CheckNumber(value){
	var reg=/^\d+$/;
	return reg.test(value);
}

function CheckEmail(value){
	var reg = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	return reg.test(value);
}

function ShowMessage(objControl, msgBuffer){
	objControl.style.color = "red";
	objControl.innerHTML = "";
	for(var i=0;i<msgBuffer.length;i++){
		var obj = document.createElement("span");
		obj.innerHTML = msgBuffer[i].toString() + "<br/>";
		objControl.appendChild(obj);
	}
}

function CreateErrorMsg(obj, msg){
	var parent = obj.parentNode;
	var objLabel = document.createElement("label");
	objLabel.innerHTML = "<br/><font color=red>" + msg + "</font>";
	parent.appendChild(objLabel);
}

//--Zack
function CheckPhone(value)
{
	var reg=/^[0-9pwPW() +-]{6,30}$/;
	return reg.test(value);
}
function CheckAddress(value)
{
	var reg=/^[\w|\s|\.|\-|\,|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\+|\=|\~|\\|/]{1,50}$/;
	return reg.test(value);
}
function ReturnResult()
{
	this.Message="";
	this.Result=true;
	this.ControlId="";
	
}
function ValidationDropDownList(controlID,msgPrefix)
{
	var rst= new ReturnResult();
	var objControl = document.getElementById(controlID);
	rst.ControlId=controlID;
	if(objControl == null)
	{
		objControl.focus();
		rst.Message="Cannot find the control" + controlID;
		rst.Result=false;
		return rst;
	}
	var value = objControl.value;
	if(value.toString().length <=0)
	{
		objControl.focus();
		rst.Result = false;	
		rst.Message=" Please select a " + msgPrefix;	
		return rst;
	}
	
	rst.Result = true;	
	rst.Message="";	
	return rst;
}
function ValidationText(controlID, validationType, minLength, maxLength,Mandatory,msgPrefix)
{
	var rst= new ReturnResult();
	var objControl = document.getElementById(controlID);
	rst.ControlId=controlID;
	if(objControl == null)
	{
		objControl.focus();
		rst.Message="Can't not Find the Control:" + controlID;
		rst.Result=false;
		return rst;
	}
	var value = Trim(objControl.value);
	
	if(Mandatory)//not allow empty
	{
		if(value.toString().length <=0)
		{
			objControl.focus();
			rst.Result = false;	
			rst.Message= "Please enter " + msgPrefix;	
			return rst;
		}	
	}
	else{ // if empty
		if(value.length == 0)
		{
			rst.Result = true;	
			rst.Message="";	
			return rst;	
		}	
	}
	//check value length
	if(minLength != -1)
	{
		if(value.toString().length < minLength)
		{
			objControl.focus();
			rst.Result = false;	
			rst.Message=msgPrefix + " shoule be at least " + minLength + " characters.";	
			return rst;	
		}
	}
	if(maxLength != -1)
	{
		if(value.toString().length > maxLength)
		{
			objControl.focus();
			rst.Result = false;	
			rst.Message=msgPrefix + " should be less than " + maxLength + " characters.";
			return rst;			
		}
	}
	
	var checkResult = true;
	var messageBuffer="";	
	//check value type	
	if(ValidationType().toString().indexOf(validationType) >= 0)
	{
		if(validationType == ValidationType()[0])
		{
			checkResult = checkResult && true;
		}		
		if(validationType == ValidationType()[1])
		{
			checkResult = CheckVarchar(value);
			if(!checkResult)
			{				
				//messageBuffer=msgPrefix + " Can not include special characters.";
				messageBuffer="Please enter a valid " + msgPrefix;
			}
		}		
		if(validationType == ValidationType()[2])
		{
			checkResult = CheckNumber(value);
			if(!checkResult)
			{
				//messageBuffer=msgPrefix + " Please enter only numbers.";
				messageBuffer="Please enter a valid " + msgPrefix;
			}
		}		
		if(validationType == ValidationType()[3])
		{
			checkResult = CheckEmail(value);
			if(!checkResult)
			{
				//messageBuffer=msgPrefix + " The Email format does not match";
				messageBuffer="Please enter a valid " + msgPrefix;
			}
		}
		
		if(validationType == ValidationType()[4])
		{
			checkResult = CheckAddress(value);
			if(!checkResult)
			{
				//messageBuffer=msgPrefix + "Invalid Characters";
				messageBuffer="Please enter a valid " + msgPrefix;
			}
		}
		if(validationType==ValidationType()[5])
		{
			checkResult=CheckPhone(value);
			if(!checkResult)
			{
				//messageBuffer=msgPrefix + "Invalid Phone";
				messageBuffer="Please enter a valid " + msgPrefix;
			}
		}
	}
	if(checkResult==false)
	{
		objControl.focus();
	}
	rst.Result = checkResult;	
	rst.Message=messageBuffer;
	return rst;
}
function LTrim(str)
{
	var whitespace = new String(" \t\n\r");  
	var s = new String(str);  
	if(whitespace.indexOf(s.charAt(0))!=-1)
	{
		var   j=0,   i   =   s.length;  
		while (j < i && whitespace.indexOf(s.charAt(j))!=-1)  
		{
			j++;  
		}  
		s = s.substring(j,i);  
	}  
	return s;  
}   

function RTrim(str)  
{  
	var whitespace = new String("   \t\n\r");  
	var s = new String(str);  
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1)  
	{  
		var i = s.length - 1;  
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)  
		{  
  			i--;  
		}  
		s = s.substring(0, i+1);  
	}
	return s;  
}

function Trim(str)  
{  
	return RTrim(LTrim(str)); 
}
