﻿//trim()
String.prototype.trim = function() {
    return this.replace(/(^\s+)|(\s+$)/g, "");
}

//open
function openWin(url,width,height,resizable){
	var width = parseInt(width);
	var height = parseInt(height);
	var screenWidth  = screen.width;
	var screenHeight = screen.height;
	var centerX = (screenWidth-width)/2;
	var centerY = (screenHeight-height)/2;
	var resizable = resizable;
	if(resizable==false || resizable == null){
	    resizable = "no";
	}else{
		resizable = "yes";
	}
	var winConf = "toolbar=no,location=no,resizable="+resizable+",menubar=no,width="+
					width+",height="+height+",top="+centerY+",left="+centerX;
	var subwin = window.open(url,'download',winConf);
	subwin.focus();
}

//判斷全形，有全形回傳 true，沒有回傳 false
function checkCode(value) {
    var re = /[\sａ-ｚＡ-Ｚ０-９　]/;

    if (re.test(value)) {
        return true;
    } else {
        return false;
    }
}

function checkEmpty(value){

	if (value.trim().length == 0){
           //alert(str+"尚未填寫資料");
           return false;
	}
	return true;
}

function checkSpace(value) {
        for(i=0;i<value.length;i++) {
          if(value.charAt(i)==" ") {
            return false;
          }
        }
        return true;
}

function checkSpace(value){
	if( value.indexOf(" ") != -1 ){
		//alert(str+"請勿空白");
		return false;
	}
	return true;
}

function checkNum(value){
	if( isNaN(value) ){
		//alert(str+"需為數字");
		return false;
	}
	return true;
}

function CheckEng(value){
	var pattern = /[a-zA-Z]/
	if(!pattern.test(value)) {
	  //alert(str+"需為英文");
	  return false;
	}
	return true;
}

function CheckEngNum(value){
	var pattern = /[a-zA-Z0-9]/
	if(!pattern.test(value)) {
	  //alert(str+"需為英數字");
	  return false;
	}
	return true;
}

function email_check(email){
	reg=/^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$/i
	if(!email.match(reg)){
		//alert("Email格式不符");
		return false;
	}
	return true;
}

function checkFile(filename){
    var ext = filename.substring(filename.lastIndexOf(".")+1).toLowerCase();
    if (ext!="" && ext != "gif" && ext !="jpg" && ext !="jpeg"){
        //alert("圖檔格式不符");
        return false;
    }
    return true;
}

function checkContentLength(content,len){
	var chinesepattern = /[^\x00-\xff]/g;
	var contentValue = content.value;
	if(chinesepattern.test(contentValue)){
		if( contentValue.replace(/[^\x00-\xff]/g,'**').length > (len/2) ){
			//content.value = contentValue.substring(0,len);
			return false;
		}
	}else{
		if( contentValue.replace(/[^\x00-\xff]/g,'**').length > len ){
			//content.value = contentValue.substring(0,len);
			return false;
		}
	}
	return true;
}

function getRadioValue(object){
	var object = object;
	var len = object.length;
	for(var i=0; i<len; i++){
		if(object[i].checked){
			return object[i].value;
		}
	}
	return "";
}

//================================================
//功用：判斷checkbox是否有勾選
//================================================
function checkCheckBox(chk_obj){
    for(var i=0; i<chk_obj.length; i++){
        if(chk_obj[i].checked == true){
            return true;
        }
    }
    return false;
}

//================================================
//功用：判斷select是否有選
//================================================
function checkSelect(sel_object){
    //alert(sel_object.options.length);
    for(var i=0; i<sel_object.options.length; i++){
        //alert(i +":"+ sel_object.options[i].selected);
        //alert(sel_object.options[i].value);
    	if(sel_object.options[i].selected == true && sel_object.options[i].value == ""){
            //alert(i);
            return false;
    	}

    }
    return true;
}

//================================================
//功用：只能填寫中文字
//================================================
function IsValidName(namestr) {
  namestr = namestr.replace(/[ ]/g,'');
	for (var i = 0; i < namestr.length; i++) {
  		if ((namestr.charCodeAt(i) > 0 && namestr.charCodeAt(i) < 126) && !CheckEng(namestr.charAt(i))) {
			return false;
  		}
	}
	return true;
}

//================================================
//功用：地址格式
//================================================
function IsValidAddr(addrstr) {
  var tmp = addrstr.replace(/[ ]/g,'');//都是空白的情況
  tmp = tmp.replace(/[　]/g,'');//全形空白也不行
  //alert(tmp);
  //alert(tmp.length);
  if(tmp.length ==0 ){
  	return false;
	}

	for (var i = 0; i < addrstr.length; i++) {
  		strtype=false;
  		if (addrstr.charCodeAt(i) < 58 || addrstr.charCodeAt(i) > 126) {
			strtype=true;
  		}
		//alert(addrstr.charCodeAt(i));
	}
	return strtype;
}

//================================================
//功用：Email格式
//================================================
function IsValidEmail(email)
{
	re = new RegExp("^.+@.+\\..+$","i");  //Create regular expression object.
	r = email.match(re);
	if(r==null)
	{
	  return false;
	}
      return true;
}

//================================================
//功用：廣告點閱率
//================================================
function adClickCount(sno,textadNo,showPos){
	location.href="/adClickCount.jsp?sno="+sno+"&textadNo="+textadNo+"&showPos="+showPos;
	return true;
}

//================================================
//功用：檢查日期是否正確
//================================================
function JSIsDate(yea,mon,da)
{       
	   mon = parseInt(mon) + 1;

     if( ((parseInt(mon) == 1) || (parseInt(mon) == 3) || (parseInt(mon) == 5) || (parseInt(mon) == 7) || 
          (parseInt(mon) == 8) || (parseInt(mon) == 10) || (parseInt(mon) == 12)) && (parseInt(da) > 31) )              
     {
       return false;     
     }   
     
     if( ((parseInt(mon) == 2) || (parseInt(mon) == 4) || (parseInt(mon) == 6) || (parseInt(mon) == 9) || (parseInt(mon) == 11) ) && (parseInt(da) > 30) )              
     {
       return false;     
     }   
     
     if(parseInt(mon) == 2)
     {             
       if(( (parseInt(yea) % 4) == 0) && (parseInt(da) > 29) )
       {        
          return false;          
       }
       else
         if((parseInt(yea) % 4 != 0) && (parseInt(da) > 28))
         {                        
          return false;           
         }
     }            
     
     return true;
	}

