function trim(string){
	string = string.replace(/^\s*|\s*$/g,"");
	return string;
}

function validateIsPhone(obj){
	string = trim(obj.value);
	if(string == "")
		return true;
	/*
	//canadian or american number
	var expression = "";
	expression += "^";//must start with
	expression += "(\\()?";//optional bracket
	expression += "[0-9]{3}";//3 digits
	expression += "((\\)(\\s)?)|\\-|\\s)?";//optional bracket, dash or space
	expression += "[0-9]{3}";//3 digits
	expression += "(\\-|\\s)?";//optional dash or space
	expression += "[0-9]{4}";//4 digits
	expression += "$";//must end with
	*/
	var expression = "";
	expression += "^";//must start with
	expression += "(\\+)?";//option plus sign
	expression += "([0-9]|\\-|\\(|\\)|\\s|\\.)*";
	expression += "$";//must end with
	
	var regex = new RegExp(expression);
	
	if(!regex.test(string)){
		validateHighlight(obj);
		return false;
	}
	
	validateUnHighlight(obj);
	return true;
}

function validateIsEmail(obj){
	string = trim(obj.value);
	if(string == "")
		return true;
	
	var expression	= "";
	expression += "^";
	expression += "[a-zA-Z0-9]+";//alphanumeric
	expression += "(";
	expression +=	"(\\.|\\-|(_)+)";
	expression += 	"[a-zA-Z0-9]+";//one or more
	expression += ")*";//optional clause
	expression += "@";//@ sign
	expression += "[a-zA-Z0-9]+";//alphanumeric
	expression += "(";
	expression += 	"(\\.|\\-|(_)+)";//one of the 3
	expression += 	"[a-zA-Z0-9]+";//one or more
	expression += ")*";//optional clause
	expression += "\\.";//last dot
	expression += "[a-zA-Z]{2,4}"; //alphabet, 2~4 characters
	expression += "$";
	
	var regex = new RegExp(expression);
	
	if(!regex.test(string)){
		validateHighlight(obj);
		return false;
	}
	
	validateUnHighlight(obj);
	return true;
}

function validateIsCreditCard(obj){
	string = trim(obj.value);
	if(string == "")
		return true;
		
	var expression = "";
	expression += "^";
	expression += "([0-9]|\\s)+";
	expression += "$";
	
	var regex = new RegExp(expression);
	
	if(!regex.test(string)){
		validateHighlight(obj);
		return false;
	}
	
	validateUnHighlight(obj);
	return true;
}

function validateMaxLength(obj, length){
	var string = trim(obj.value);
	if(string.length > length){
		validateHighlight(obj);
		return false;
	}
	
	validateUnHighlight(obj);
	return true;
}

function validateMinLength(obj, length){
	var string = trim(obj.value);
	if(string.length < length){
		validateHighlight(obj);
		return false;
	}
	
	validateUnHighlight(obj);
	return true;	
}

function validateIsEmpty(obj){
	var isEmpty = false;
	
	if(obj.type=="text" || obj.type=="textarea" || obj.type=="password"){//text or textarea
		var value = trim(obj.value);
		if(value == "")
			isEmpty = true;
	}
	else if(obj.type == "select-one"){//dropdown
		if(obj.selectedIndex==0)
			isEmpty = true;
	}
	else if(obj.type == "select-multiple"){//multiple selects
		var selected = false;
		for(_i=0; _i<obj.length; _i++){
			if(obj.options[_i].selected == true){
				selected = true;
				break;
			}	
		}
		
		if(selected == false)
			isEmpty = true;
	}
	else if(obj.length != null){
		if(obj[0].type == "radio"){//radio button
			var checked = false;
			for(_i=0; _i<obj.length; _i++){
				if(obj[_i].checked == true){
					checked = true;
					break;
				}	
			}
			
			if(checked == false)
				isEmpty = true;
		}
		else if(obj[0].type == "checkbox"){//checkbox
			var checked = false;
			for(_i=0; _i<obj.length; _i++){
				if(obj[_i].checked == true){
					checked = true;
					break;
				}	
			}
			
			if(checked == false)
				isEmpty = true;
		}
	}
	if(isEmpty == true){
		validateHighlight(obj);
		return true;
	}
	
	validateUnHighlight(obj);
	return false;
}

function validateIsNumber(obj){
	var num = trim(obj.value);
	if(num =="")
		return true;
	
	num = Number(num);
	
	if(isNaN(num)){
		validateHighlight(obj);
		return false;
	}
	
	validateUnHighlight(obj);
	return true;
}

function validateIsString(obj){
	var string = trim(obj.value);
	if(string == "")
		return true;
	
	if(!isString(string)){
		validateHighlight(obj);
		return false;
	}
	
	validateUnHighlight(obj);
	return true;
}


function validateHighlight(obj){
	if(
		obj.type == "text" || 
		obj.type == "textarea" || 
		obj.type == "password" ||
		obj.type == "select-one" ||
		obj.type == "select-multiple"
	){
		obj.style.backgroundColor = "#F6C8C8";
		return;
	}
	else if(obj.length != null){
		return;
	}
	alert("missed an object type!, "+obj.type);
}

function validateUnHighlight(obj){
	if(
		obj.type == "text" || 
		obj.type == "textarea" || 
		obj.type == "password" ||
		obj.type == "select-one" ||
		obj.type == "select-multiple"
	){
		obj.style.backgroundColor = "transparent";
		return;
	}
	else if(obj.length != null){
		return;
	}
	alert("missed an object type!, "+obj.type);
}



