function trim(string) {
   var tmpchar, i, j, result;

   i = 0;
   tmpchar = string.charAt(i);
   while (tmpchar == ' ') {
      i ++;
      tmpchar = string.charAt(i);
   }

   j = string.length - 1;
   tmpchar = string.charAt(j);
   while (tmpchar == ' ') {
      j --;
      tmpchar = string.charAt (j);
   }
   if ( i <= j)
      result = string.substring(i,j+1);
   else
      result = "";
   return result;
}

function checkpass1(str1,len)
{
	for (var i=0; i<str1.value.length;i++) {
                if (((str1.value.charAt(i)>='0')&&(str1.value.charAt(i)<='9'))||((str1.value.charAt(i)>='A')&&(str1.value.charAt(i)<='Z'))||((str1.value.charAt(i)>='a')&&(str1.value.charAt(i)<='z'))||(str1.value.charAt(i)=='_')) {
					document.getElementById("pass2alert").innerHTML="<font color=blue>OK！</font>";
                } else{
				document.getElementById("pass2alert").innerHTML="<font color=red>密碼含有非法字符！只能是數字、字母 'a-z , A-Z' 及 '_' 為有效字符。</font>";
                        //alert("密碼含有非法字符！只能是數字、字母 'a-z , A-Z' 及 '_' 為有效字符。");
						str1.value = "";
                       // str1.focus();
                        
                        return false;
                }
        }
	 if (str1.value.length <len && str1.value.length!='') {
               document.getElementById("pass2alert").innerHTML="<font color=red>密碼應大於等於"+len+"位！</font>";
			   // alert("密碼應大於等於"+len+"位！");
				str1.value = "";
                //str1.focus();
      return false;
   }
   if(str1.value.length >len){
   		document.getElementById("pass2alert").innerHTML="<font color=blue>OK！</font>";
   }else{
	                  document.getElementById("pass2alert").innerHTML="<font color=#FF0000>*必要欄位</font>";
			   // alert("密碼應大於等於"+len+"位！");
                //str1.focus();
      		return false;
	   }
}
function checkpass2(str1,str2)
{
	
		 if (str1.value == str2.value && str1.value.length!='') {
			  document.getElementById("pass2alert").innerHTML="<font color=blue>OK！</font>";
                return true;
        }
		else if(str1.value != str2.value && str1.value.length!='')
		{
                document.getElementById("pass2alert").innerHTML="<font color=red>兩次密碼輸入不一致！請重新輸入！</font>";
				//alert("兩次密碼輸入不一致！請重新輸入！"); 
				str1.value = "";
                str2.value = "";
             //   str1.focus();
               
                return false;
        }else if(str1.value.length=='')
			{
               document.getElementById("pass2alert").innerHTML="<font color=#FF0000>*必要欄位</font>";
			   // alert("密碼應大於等於"+len+"位！");
                //str1.focus();
      		return false;
   		}		
		
        return true
}

//====================================================================================================================================
//by fkj  2008-12-15
function cleck_name_ee(string) {  //真實姓名
        var s,i,j;
        s="'\"<>";
        for (i=0; i<string.value.length; i++) {
           for(j=0;j<s.length;j++){
                if (string.value.charAt(i) == s.charAt(j))
                {
						document.getElementById("namealert").innerHTML="<font color=red>不能包含 ' < > 和 \"。</font>";
						string.value = "";						
						//alert(fieldname+"不能包含 ' < > 和 \"");
                        return false;
                 }else{
					document.getElementById("namealert").innerHTML="<font color=blue>OK！</font>";	
				}
          }
        }
   		if(string.value.length !=''){
   			document.getElementById("namealert").innerHTML="<font color=blue>OK！</font>";
   		}else{
			document.getElementById("namealert").innerHTML="<font color=#FF0000>*必要欄位</font>";
      		return false;
	   }		
		
        return true;
}

function cleck_idc_ee(string) {  //身份證字號
        var s,i,j;
        s="'\"<>";
        for (i=0; i<string.value.length; i++) {
           for(j=0;j<s.length;j++){
                if (string.value.charAt(i) == s.charAt(j))
                {
						document.getElementById("idcalert").innerHTML="<font color=red>不能包含 ' < > 和 \"。</font>";
						string.value = "";						
						//alert(fieldname+"不能包含 ' < > 和 \"");
                        return false;
                 }else{
					document.getElementById("idcalert").innerHTML="<font color=blue>OK！</font>";	
				}
          }
        }
   		if(string.value.length !=''){
   			document.getElementById("idcalert").innerHTML="<font color=blue>OK！</font>";
   		}else{
			document.getElementById("idcalert").innerHTML="<font color=#FF0000>*必要欄位</font>";
      		return false;
	   }		
		
        return true;
}

function cleck_area_ee(str){   //聯絡電話區號
        rep=new String(str.value);
        pattern=/^[0-9]+$/;

        if (rep.length==0 || (rep.match(pattern) && rep.length<=40))
		{
			document.getElementById("areaalert").innerHTML="<font color=blue>OK！</font>";
		}else {
						document.getElementById("areaalert").innerHTML="<font color=red>電話區號含有非法字符！只能是數字。</font>";
						str.value = "";						
						//alert(fieldname+"不能包含 ' < > 和 \"");
                        return false;
        }
   		if(str.value.length !=''){
   			document.getElementById("areaalert").innerHTML="<font color=blue>OK！</font>";
   		}else{
			document.getElementById("areaalert").innerHTML="<font color=#FF0000>*必要欄位</font>";
      		return false;
	   }		
        return true;		
}

function cleck_phone_ee(str){   //聯絡電話
        rep=new String(str.value);
        pattern=/^[0-9]+$/;

        if (rep.length==0 || (rep.match(pattern) && rep.length<=40))
		{
			document.getElementById("areaalert").innerHTML="<font color=blue>OK！</font>";
		}else {
						document.getElementById("areaalert").innerHTML="<font color=red>聯絡電話含有非法字符！只能是數字。</font>";
						str.value = "";						
						//alert(fieldname+"不能包含 ' < > 和 \"");
                        return false;
        }
   		if(str.value.length !=''){
   			document.getElementById("areaalert").innerHTML="<font color=blue>OK！</font>";
   		}else{
			document.getElementById("areaalert").innerHTML="<font color=#FF0000>*必要欄位</font>";
      		return false;
	   }		
        return true;		
}

function cleck_mobile_ee(str){   //行動電話
        rep=new String(str.value);
        pattern=/^[0-9]+$/;

        if (rep.length==0 || (rep.match(pattern) && rep.length<=40))
		{
			document.getElementById("mobileaalert").innerHTML="<font color=blue>OK！</font>";
		}else {
						document.getElementById("mobileaalert").innerHTML="<font color=red>行動電話含有非法字符！只能是數字。</font>";
						str.value = "";						
						//alert(fieldname+"不能包含 ' < > 和 \"");
                        return false;
        }
   		if(str.value.length !=''){
   			document.getElementById("mobileaalert").innerHTML="<font color=blue>OK！</font>";
   		}else{
			document.getElementById("mobileaalert").innerHTML="<font color=#FF0000>*必要欄位</font>";
      		return false;
	   }		
        return true;		
}

//------------------------------------------------------------------------------------------------------------------------------------

function validpassword(str1,str2,len)
{
        if (str1.value.length <len) {
                alert("密碼應大於等於"+len+"位！");
                str1.focus();
      return false;
   }
        for (var i=0; i<str1.value.length;i++) {
                if (((str1.value.charAt(i)>='0')&&(str1.value.charAt(i)<='9'))||((str1.value.charAt(i)>='A')&&(str1.value.charAt(i)<='Z'))||((str1.value.charAt(i)>='a')&&(str1.value.charAt(i)<='z'))||(str1.value.charAt(i)=='_')) {
                } else{
                        alert("密碼含有非法字符！只能是數字、字母 'a-z , A-Z' 及 '_' 為有效字符。");
                        str1.focus();
                        str1.value = "";
                        return false;
                }
        }
        if (str2.value.length <len) {
                alert("密碼應大於等於"+len+"位！");
                str2.focus();
      return false;
   }
        for (var i=0; i<str2.value.length;i++) {
                if (((str2.value.charAt(i)>='0')&&(str2.value.charAt(i)<='9'))||((str2.value.charAt(i)>='A')&&(str2.value.charAt(i)<='Z'))||((str2.value.charAt(i)>='a')&&(str2.value.charAt(i)<='z'))||(str2.value.charAt(i)=='_')) {
                } else{
                        alert("密碼含有非法字符！只能是數字、字母 'a-z , A-Z' 及 '_' 為有效字符。");
                        str2.focus();
                        str2.value = "";
                        return false;
                }
        }
        if (str1.value == str2.value) {
                return true;
        } else {
                alert("兩次密碼輸入不一致！請重新輸入！");
                str1.focus();
                str1.value = "";
                str2.value = "";
                return false;
        }
        return true
}

function validemail(string) {
        var ok=false;
        for (var i=1; i<string.value.length-3;i++) {
                if (string.value.charAt(i) =='@'){
                        ok = true;break;
                }
        }
        for (var j=0; j<string.value.length; j++) {
                if ((((string.value.charAt(j)>='0')&&(string.value.charAt(j)<='9'))||((string.value.charAt(j)>='A')&&(string.value.charAt(j)<='Z'))||((string.value.charAt(j)>='a')&&(string.value.charAt(j)<='z'))||(string.value.charAt(j)=='_')||(string.value.charAt(j)=='@')||(string.value.charAt(j)=='-')||(string.value.charAt(j)=='.'))) {
                } else {
                        ok = false;break;

                }
        }
        if (ok) {
                return true;
        } else {
                alert("非法電子郵件地址！");
           string.focus();
           return false;
        }
}

function validstring(fieldname,string) {
        var s,i,j;
        s="'!#&*\"()<>";
        for (i=0; i<string.value.length; i++) {
           for(j=0;j<s.length;j++){
                if (string.value.charAt(i) == s.charAt(j))
                {
                        alert(fieldname+"不能包含 ! ' # & * ( ) < > 和 \"");
                         string.focus();
                         return false;
                 }
          }
        }
        return true;
}

function validstring1(fieldname,string) {
        var s,i,j;
        s="'\"<>";
        for (i=0; i<string.value.length; i++) {
           for(j=0;j<s.length;j++){
                if (string.value.charAt(i) == s.charAt(j))
                {
                        alert(fieldname+"不能包含 ' < > 和 \"");
                         string.focus();
                         return false;
                 }
          }
        }
        return true;
}

function validdate(fieldname,Year,Month,Day) {
        var d,temp,s = "";
        d = new Date(Year.value,Month.value-1,Day.value);
        temp = d.getYear();
        if ( temp < 100) s +="19";
        if ( temp < 10) s +="0";
        s += temp + "-";
        s += (d.getMonth() + 1) + "-";
        s += d.getDate();
        if (s == Year.value+"-"+Month.value+"-"+Day.value) {
              return true;
        } else {
              alert(fieldname+"格式不正確！");
              Day.focus();
              return false;
        }
}

function notNull(fieldname,string)
{
   string.value=trim(string.value);
   if (string.value == "") {
      alert("請輸入"+fieldname+"！");
     // string.focus();
      return false;
                    }
  else
   return true;
  }

function notShort(fieldname,string,len)
{
        if (string.value.length == len) {
                return true;
        } else {
                alert(fieldname+"應為"+len+"位！");
                string.focus();
           return false;
        }
}

function notLong(fieldname,string,len)
{
        if (string.value.length <= len) {
                return true;
        } else {
                alert(fieldname+"最多只能輸入"+len+"個英文字母或漢字！");
                string.focus();
           return false;
        }
}

function notSelect(fieldname,string)
{
   if (string.options[0].selected) {
      alert("請選擇"+fieldname+"！");
      return false;
   }
   else
   return true;
}

function notCheck(fieldname,string)
{
   if (!(string[0].checked ||string[1].checked ||string[2].checked)) {
      alert("請選擇"+fieldname+"！");
      return false;
                    }
   else
      return true;
}

function notErrorLink(fieldname,string)
{

//************************
  return true;
//************************

   var sublink=".auctions.yahoo.co.jp/jp/";
   var tmpchar, i;

   i = 0;
   tmpchar = string.value.charAt(i);
   while ((tmpchar != '.')&&(i < string.value.length - 1)) {
      i ++;
      tmpchar = string.value.charAt(i);
   }
   if (sublink != string.value.substring(i, sublink.length+i)){
       alert(fieldname+"輸入錯誤！");
       return false;
   }else
       return true;
}

function check_int(strnote,str){
        rep=new String(str.value);
        pattern=/^[\.0-9]+$/;

        if (rep.length==0 || (rep.match(pattern) && parseFloat(str.value)>=0 && rep.length<=40)) return true;
        else {
                alert(strnote+"輸入錯誤！");
                str.focus();
                return false;
        }
}
//回饋金驗證（1.不能超過現有的  2.單次使用不能超過服務費） by ekey 2008.11.27
function check_dgf(coins,dgf,str)
{
	if(parseFloat(str.value)>parseFloat(coins))
	{
       alert("回饋金超額輸入！");
	   str.value='';
		str.focus();
		return false;
	}
	else
	{
		if(parseFloat(str.value)>parseFloat(dgf))
		{
		   alert("回饋金單次抵用不可以超過服務費哦！");
		   str.value='';
			str.focus();
			return false;
		}
		else
		{
			if(trim(str.value)=="")
			{
			return true;
			}
			else
			{
			document.order.dgf_tw.value=Math.ceil(parseFloat(dgf)-parseFloat(str.value));
			return true;
			}
		}
	}
	
}
function check_phone(strnote,str){
        rep=new String(str.value);
        pattern=/^[0-9]+$/;

        if (rep.length==0 || (rep.match(pattern) && rep.length<=40)) return true;
        else {
                alert(strnote+"輸入錯誤！");
                str.focus();
                return false;
        }
}

function validform_forget(myform)
{
    if(notNull("會員名稱",myform.user)&&validstring1("會員名稱",myform.user)&&notNull("電子郵件",myform.email)&&validstring("電子郵件",myform.email)&&validemail(myform.email)&&notNull("電話區號",myform.area)&&check_phone("電話區號",myform.area)&&notNull("聯絡電話",myform.phone)&&check_phone("聯絡電話",myform.phone))
      return true;
    return false;
}
function check_intro(user,intro)
{
   if (trim(user.value) == trim(intro.value)) 
   {
      alert("介紹人不可以填自己哦，如果沒有，請不要填寫！");
	  intro.value="";
	  intro.focus();
      return false;
   }
  else
  {
   return true;
  }
}
function validform_reg_step1(myform)
{
    if(notNull("帳號",myform.email)&&validstring("電子郵件",myform.email)&&validemail(myform.email)&&notNull("密碼",myform.pass1)&&notNull("確認密碼",myform.pass2)&&validpassword(myform.pass1,myform.pass2,4)&&notNull("真實姓名",myform.name)&&validstring1("真實姓名",myform.name)&&notNull("身份証字號",myform.idc)&&validstring1("身份証字號",myform.idc)&&notNull("電話區號",myform.area)&&check_phone("電話區號",myform.area)&&notNull("聯絡電話",myform.phone)&&check_phone("聯絡電話",myform.phone)&&notNull("行動電話",myform.mobile)&&check_phone("行動電話",myform.mobile)&&check_intro(myform.email,myform.intro))
      return true;
    return false;
}

function validform_reg_step2(myform)
{
    if(validstring1("銀行名稱",myform.bank)&&validstring1("分行名稱",myform.subbranch)&&validstring1("銀行帳號",myform.account)&&validstring1("銀行戶名",myform.depositor))
      return true;
    return false;
}

function validform_reg_submit(myform)
{
    if(notNull("會員名稱",myform.user)&&validstring1("會員名稱",myform.user))
      return true;
    return false;
}

function validform_alter(myform)
{
    if((((myform.pass1.value.length==0)&&(myform.pass2.value.length==0))||(validpassword(myform.pass1,myform.pass2,4)))&&notNull("電子郵件",myform.email)&&validstring("電子郵件",myform.email)&&validemail(myform.email)&&notNull("送貨地址",myform.address)&&validstring1("送貨地址",myform.address)&&check_int("郵政區號",myform.postcode)&&notNull("電話區號",myform.area)&&check_phone("電話區號",myform.area)&&notNull("聯絡電話",myform.phone)&&check_phone("聯絡電話",myform.phone)&&notNull("行動電話",myform.mobile)&&check_phone("行動電話",myform.mobile))
      return true;
    return false;
}
function check_pay00(payjp,wpayjp)
{
	  if(parseFloat(payjp.value)>=parseFloat(wpayjp.value)) return true;
      else{
        alert("您的底價必須比現在出價更高！");
        payjp.focus();
        return false;
      }
}
function check_pay00(payjp,wpayjp,bidnum)
{
	//alert(payjp.value);
	   if(parseFloat(bidnum.value)==0)
	  {
			if(parseFloat(payjp.value)<parseFloat(wpayjp.value))
			{
				alert("您的出價不可以低於目前出價！");
				return false;
			}
			else
			{
			return true;
			}
	  }
	  else
	  {
		   //根據目前出價 判斷增額；客人不可以出價小於（目前出價＋增額）
			if(parseFloat(wpayjp.value)<=999)
			{
			 ze=10;
			}else if(parseFloat(wpayjp.value)>=1000&&parseFloat(wpayjp.value)<=4999)
			{
			 ze=100;
			}else if(parseFloat(wpayjp.value)>=5000&&parseFloat(wpayjp.value)<=9999)
			{
			 ze=250;
			}else if(parseFloat(wpayjp.value)>=10000&&parseFloat(wpayjp.value)<=49999)
			{
			 ze=500;
			}else if(parseFloat(wpayjp.value)>=50000)
			{
			 ze=1000;
			}else
			{
			 ze=0;
			}
			//直接告知會員最低應投多少金額  2008.10.24 Ben Add
			var minbid	= parseFloat(Math.ceil(wpayjp.value*1+ze*1));
			minbid = "您的出價少於一個\"出價增額\", 無法出價成功！\n\n您最低應出價" + minbid + "日幣以上";
			
		   if(parseFloat(payjp.value*1)>=parseFloat(Math.ceil(wpayjp.value*1+ze*1))) 
		   {
			   return true; 
		   }
           else{
                alert(minbid);
                payjp.focus();
                return false;
                }
	  }
	 
}
function validform_order(myform)
{
	//notNull("標的物名稱",myform.object)&&validstring1("標的物名稱",myform.object)&&notNull("標的物網址",myform.link)&&((myform.service.value==3)||notNull("標的物ID",myform.jpnid))&&validstring1("標的物網址",myform.link)&&notErrorLink("標的物網址",myform.link)&&
	//&&notNull("委託人",myform.name)&&validstring1("委託人",myform.name)&&notNull("電話區號",myform.area)&&check_phone("電話區號",myform.area)&&notNull("聯絡電話",myform.phone)&&check_phone("聯絡電話",myform.phone)&&notNull("送貨地址",myform.address)&&validstring1("送貨地址",myform.address)&&notNull("電子郵件",myform.email)&&validstring("電子郵件",myform.email)&&validemail(myform.email)
    if(notNull("商品訊息取得失敗！ \n\n 請回商品頁面等待商品圖片出現之後再點選出價競標！",myform.object)&&notNull("商品類別無法識別",myform.type)&&notNull("中文商品名",myform.zwpm)&&notNull("最高底價",myform.maxpay_jp)&&check_int("最高底價",myform.maxpay_jp))//&&check_pay00(myform.maxpay_jp,myform.now_price,myform.xz_bidnum)
      return true;
    return false;
}
function validform_order_step3(myform)
{
	//notSelect("標的物類別",myform.type)&&notNull("標的物名稱",myform.object)&&validstring1("標的物名稱",myform.object)&&notNull("標的物網址",myform.link)&&((myform.service.value==3)||notNull("標的物ID",myform.jpnid))&&validstring1("標的物網址",myform.link)&&notErrorLink("標的物網址",myform.link)&&
	//&&notNull("委託人",myform.name)&&validstring1("委託人",myform.name)&&notNull("電話區號",myform.area)&&check_phone("電話區號",myform.area)&&notNull("聯絡電話",myform.phone)&&check_phone("聯絡電話",myform.phone)&&notNull("送貨地址",myform.address)&&validstring1("送貨地址",myform.address)&&notNull("電子郵件",myform.email)&&validstring("電子郵件",myform.email)&&validemail(myform.email)
    if(notNull("送貨地址",myform.address)&&notNull("郵政字號",myform.postcode))
      return true;
    return false;
}

function validform_order_cc(myform)
{
	if(myform.link.value.indexOf("myday")!=-1)
	{
		alert("網址輸入錯誤！\n\n請填寫投標網頁上的準確地址！\n\n如果您是透過MyDay首頁搜尋商品,\n\n請直接點選上方工具列「我要投標」按鈕！");
		return false;
	}
}

function validform_maxpay_jp(myform)
{
    if(notNull("最高底價",myform.maxpay_jp)&&check_int("最高底價",myform.maxpay_jp)&&check_pay(myform.maxpay_jp))
      return true;
    return false;
}

function validform_remit(myform)
{
    if(notNull("匯款金額(PS:我們以銀行記錄為準哦)",myform.hk)&&check_int("匯款金額",myform.hk)&&notNull("匯款帳號後5碼，方便我們盡快替您入帳",myform.acc)&&notShort("匯款帳號",myform.acc,5))
      return true;
    return false;
}
