function LTrim(str){ 
	var i;
	for(i=0;i < str.length;i++ ) {
		if(str.charAt(i) != " " && str.charAt(i)!= "\n" && str.charAt(i)!= "\r") break;
	}
	str = str.substring(i,str.length);
	return str;
}
function RTrim(str){
	var i;
	for(i=str.length-1;i>=0;i--){
		if(str.charAt(i) != " " && str.charAt(i)!= "\n" && str.charAt(i)!= "\r") break;
	}
	str = str.substring(0,i+1);
	return str;
}
function Trim(str){
	return LTrim(RTrim(str));
}


function checkUrlValid(obj) {
	var str = obj.value;
	if ( str == "" ) return true;
	if ( str.indexOf("http://") == -1 ) str = "http://" + str;
	var urlpatern = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/; 

	if(!urlpatern.test(str))	{
		alert("網址格式錯誤，請檢查輸入是否正確！");
		obj.focus();
		return false;
	}
	return true;
}

function checkEmail(emailObj){
	strtxt = emailObj;
	if (strtxt=="") {
		alert ("電子郵件信箱 請不要空白");
		return false;
	}
	if((!checkMouse(emailObj) || !checkDot(emailObj))){
		alert ("電子郵件信箱格式錯誤：" + strtxt);
		return false;
	}
	
	if(strtxt.charAt(strtxt.length-1)=="@"){
		alert ("電子郵件信箱的結尾不能為( @ )：" + strtxt);
		return false;
	}

	if(strtxt.charAt(0)=="@"){
		alert ("電子郵件信箱的開頭不能為( @ )：" + strtxt);
		return false;
	}

	if(strtxt.charAt(strtxt.length-1)=="."){
		alert ("電子郵件信箱的結尾不能為( . )：" + strtxt);
		return false;
	}
	
	if(strtxt.charAt(0)=="."){
		alert ("電子郵件信箱的開頭不能為( . )：" + strtxt);
		return false;
	}
	
	if(!checkSemicolon(emailObj)){
		alert ("電子郵件信箱不可含有( ; )：" + strtxt);
		return false;
	}
	
	if(!checkSpace(emailObj)){
		alert ("電子郵件信箱不可含有空白：" + strtxt);
		return false;
	}
	
	if(!checkDoubleDot(emailObj)){
		alert ("電子郵件信箱不可含有連續小數點：" + strtxt);
		return false;
	}
	
	if(!checkMouseDotOrDotMouse(emailObj)){
		alert ("電子郵件信箱中小數點不可緊鄰( @ )：" + strtxt);
		return false;
	}
	return true;
}

function checkMouse(emailObj){
 idx=0;
 count = 0;
 exist = "false";
 strtxt = emailObj;
 while (idx < strtxt.length){
	if (strtxt.charAt(idx++) == "@"){
		exist = "true";
		count++
	}
 }
 if(exist == "true" && count == 1)
 	return true;
 else
 	return false;
}

function checkDot(emailObj){
 idx=0;
 exist = "false";
 strtxt = emailObj;
 while (idx < strtxt.length){
	if (strtxt.charAt(idx++) == "."){
		exist = "true";
	}
 }
 if(exist == "true")
 	return true;
 else
 	return false;
}

function checkSemicolon(emailObj){
 idx=0;
 strtxt = emailObj;
 while (idx < strtxt.length){
	if (strtxt.charAt(idx++) == ";")
		return false;
 }
 return true;
}

function checkSpace(emailObj){
 idx=0;
 strtxt = emailObj;
 while (idx < strtxt.length){
	if (strtxt.charAt(idx++) == " ")
		return false;
 }
 return true;
}

function checkDoubleDot(emailObj){
 idx=0;
 strtxt = emailObj;
 while (idx < strtxt.length){
	if (strtxt.charAt(idx) == "." && strtxt.charAt(idx+1) == "."){
		return false;
	}
	idx++;
 }
 return true;
}

function checkMouseDotOrDotMouse(emailObj){
 idx=0;
 strtxt = emailObj;
 while (idx < strtxt.length){
	if ((strtxt.charAt(idx) == "@" && strtxt.charAt(idx+1) == ".")||(strtxt.charAt(idx) == "." && strtxt.charAt(idx+1) == "@")){
		return false;
	}
	idx++;
 }
 return true;
}