﻿///************************************
/// 取得對象下指定名稱的對象集合
/// 作者：limin_he(limin_he@maxense.com)
/// 日期：2005-09-11
/// 參數：sender: 要查詢的容器對象; name: 要查詢的名稱
/// 返回：Array
/// 使用說明： var ctrls = getElementByName(document.body,"textbox1");
///************************************
function getElementByName(sender, name)
{
    var result = new Array();
    for(var i=0; i < sender.childNodes.length; i++)
    {
        var node = sender.childNodes[i];
        var attrName = "";
        try{
            attrName = node.getAttribute("name");
        }catch(e){}
        if(attrName==name) result.push(node);
        else
        {
            if(node.childNodes.length>0) 
            {
                result = result.concat(getElementByName(node,name));
            }
        }
    }
    return result;
}
///************************************
/// 取得對象下所有的子對象
/// 作者：limin_he(limin_he@maxense.com)
/// 日期：2005-09-11
/// 返回： Array
///************************************
function getElementsByTagName(sender,tagName)
{
	var items = new Array();
	for(var i=0; i < sender.childNodes.length; i++)
	{
		var node = sender.childNodes[i];
		if(node.tagName!=null && node.tagName.toUpperCase() == tagName.toUpperCase())
		{
			items.push(node);
		}
		if(node.childNodes.length > 0)
		{
			var subItems = GetChildNodes(node,tagName);
			for(var j=0; j < subItems.length; j++)
			{
				items.push(subItems[j]);
			}
		}
	}
	return items;
}
///************************************
/// 等比縮放圖片
/// 作者：limin_he(limin_he@maxense.com)
/// 日期：2005-09-11
/// 參數：image要縮放的Image對象,width圖片的最大寬度,height圖片的最大高度
/// 使用說明：< img src='./images/test.jpg" onload="imageZoom(this,250,200)" />
///************************************
function imageZoom(image, width, height)
{
    var scale = image.width/image.height;
    if (image.width > width || image.height > height)
    {
      // 按比例转换
      image.width = scale * height < width ? (height * scale) : width;
      image.height = scale * height >= width ? (width / scale) : height;
    }
}
///************************************
/// Script的HTML形式編碼
/// 作者：limin_he(limin_he@maxense.com)
/// 日期：2005-09-11
/// 返回：返回HTML形式的編碼
/// 使用說明： var str = HtmlEncode(" < div>aa< /div>");
///************************************
function HtmlEncode(encodeHtml)
{
    var div = document.createElement('div');
    div.innerText = encodeHtml;
    return div.innerHTML;
}
///************************************
/// Script的HTML形式解碼
/// 作者：limin_he(limin_he@maxense.com)
/// 日期：2005-09-11
/// 返回：返回HTML形式的編碼
/// 使用說明： var html = HtmlEncode("&ltdiv&gt;aa&lt;/div&gt;");
///************************************
function HtmlEncode(encodeHtml)
{
    var div = document.createElement('div');
    div.innerText = encodeHtml;
    return div.innerHTML;
}
///************************************
/// 檢測字符串是否為空或全部是空格
/// 作者：limin_he(limin_he@maxense.com)
/// 日期：2005-09-11
/// 返回：是=true;否=false
/// 使用說明： if(checkNull("  ")) ...
///************************************
function checkNull(str)
{ 
    if(str=="") return true;
    var reg = /^[  ]+$/; 
    if(!reg.test(str))
        return false;
    return true;
}
///************************************
/// 檢測是否為有效的西元日期
/// 作者：limin_he(limin_he@maxense.com)
/// 日期：2005-09-11
/// 返回：是=true;否=false
/// 使用說明： if(checkDate("2008-10-10")) ...
///************************************
function checkDate(date)
{ 
    var reg = /^(\d{4})([\/,-])(\d{1,2})\2(\d{1,2})$/; 
    var r = date.match(reg); 
    if(r==null) return false; 
    var d= new Date(r[1], r[3]-1,r[4]); 
    var newStr=d.getFullYear()+r[2]+(d.getMonth()+1)+r[2]+d.getDate(); 
    date=r[1]+r[2]+((r[3]-1)+1)+r[2]+((r[4]-1)+1); 
    return newStr==date; 
} 
///************************************
/// 檢測兩個日期
/// 作者：limin_he(limin_he@maxense.com)
/// 日期：2005-09-11
/// 返回：0正確,1開始日期不正確,2結束日期不正確,3開始日期大於結束日期
/// 使用說明： if(checkTwoDate("2008-10-10","2008-10-11")==0) ...
///************************************
function checkTwoDate(bdate,edate)
{ 
    if(!checkDate(bdate))
        return 1;
    else if(!checkDate(edate))
        return 2;
    var tmpSD = bdate.split("-");
    var tmpED = edate.split("-");
    var sd = new Date(tmpSD[0],tmpSD[1],tmpSD[2]);
    var ed = new Date(tmpED[0],tmpED[1],tmpED[2]);
    if(ed-sd<0)
    {
        return 3;
    }
    return 0;
} 
///************************************
/// 檢測文件是否為指定的文件
/// 作者：limin_he(limin_he@maxense.com)
/// 日期：2005-09-11
/// 返回：是=true;否=false
/// 使用說明： if(checkFile("aa.txt","jpg|gif|bmp")) ...
///************************************
function checkFile(fileName, fileExt)
{
    var doPosition = fileName.lastIndexOf(".");
    if(doPosition == -1) return false;
    var ext = fileName.substring(doPosition+1,fileName.length);
    var exts = fileExt.split("|");
    for(var i=0; i < exts.length; i++)
    {
        if(exts[i].toLowerCase() == ext.toLowerCase())
        {
            return true;
        }
    }
    return false;
}
///************************************
/// 檢測字符串是否為EMAIL格式
/// 作者：limin_he(limin_he@maxense.com)
/// 日期：2005-09-11
/// 返回：是=true;否=false
/// 使用說明： if(checkEmail("aaa@maxense.com")) ...
///************************************
function checkEmail(email)
{
    //var reg = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
    var reg = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/; 
    if(!reg.test(email))
        return false;
    return true;
}
///************************************
/// 檢測字符串是否為電話格式
/// 作者：limin_he(limin_he@maxense.com)
/// 日期：2005-09-11
/// 返回：是=true;否=false
/// 使用說明： if(checkPhone("0730-23156489")) ...
///************************************
function checkPhone(phone)
{
    if(phone.length<7 || phone.length>18) return false;
    var reg = /^([0-9]|[\-])+$/;
    if(!reg.test(phone))
        return false;
    return true;
}
function checkPhoneAdv(phone) {
    if (phone.length < 7 || phone.length > 18) return false;
    var reg = /^([0-9]|[\-]|[\#])+$/;
    if (!reg.test(phone))
        return false;
    return true;
}
///************************************
/// 檢測字符串是否為中文格式
/// 作者：limin_he(limin_he@maxense.com)
/// 日期：2005-09-11
/// 返回：是=true;否=false
/// 使用說明： if(checkChinese("是否中文")) ...
///************************************
function checkChinese(chinese)
{
    var reg = /^[\u4E00-\u9FA5]+$/;
    if(!reg.test(chinese))
    {
        return false;
    }
    return true;
}
///************************************
/// 檢測字符串是否為英文格式
/// 作者：limin_he(limin_he@maxense.com)
/// 日期：2005-09-11
/// 返回：是=true;否=false
/// 使用說明： if(checkEnglish("english")) ...
///************************************
function checkEnglish(english)
{
    var reg = /^[a-zA-Z]+$/;
    if(!reg.test(chinese))
    {
        return false;
    }
    return true;
}
///************************************
/// 檢測字符串是否由英文數字和底線組件且以英文與底線開頭
/// 作者：limin_he(limin_he@maxense.com)
/// 日期：2005-09-11
/// 返回：是=true;否=false
/// 使用說明： if(checkLetter_Number("aaa_343adsf")) ...
///************************************
function checkLetter_Number(str)
{
    var reg = /^[a-zA-Z\_]+[0-9]*$/;
    if(!reg.test(str))
    {
        return false;
    }
    return true;
}
///************************************
/// 檢測字符串是否由英文數字組件
/// 作者：limin_he(limin_he@maxense.com)
/// 日期：2005-09-11
/// 返回：是=true;否=false
/// 使用說明： if(checkLetterNumber("sd34")) ...
///************************************
function checkLetterNumber(str)
{
    var reg = /^[0-9a-zA-Z]+$/;
    if(!reg.test(str))
    {
        return false;
    }
    return true;
}
///************************************
/// 檢測字符串是否由數字組件
/// 作者：limin_he(limin_he@maxense.com)
/// 日期：2005-09-11
/// 返回：是=true;否=false
/// 使用說明： if(checkLetterNumber("34")) ...
///************************************
function checkNumber(str)
{
    var reg = /^[0-9]+$/;
    if(!reg.test(str))
    {
        return false;
    }
    return true;
}
///************************************
/// 檢測字符串是否由中文英文數字組件
/// 作者：limin_he(limin_he@maxense.com)
/// 日期：2005-09-11
/// 返回：是=true;否=false
/// 使用說明： if(checkLetterNumber("sd34")) ...
///************************************
function checkChineseLetterNumber(str)
{
    var reg = /^[0-9a-zA-Z\u4E00-\u9FA5]+$/;
    if(!reg.test(str))
    {
        return false;
    }
    return true;
}
///************************************
/// 檢測字符串是否由中文英文數字底線及空格組件
/// 作者：limin_he(limin_he@maxense.com)
/// 日期：2005-09-11
/// 返回：是=true;否=false
/// 使用說明： if(checkLetterNumber("sd34")) ...
///************************************
function checkChineseLetter_Number(str)
{
    var reg = /^[0-9a-zA-Z_\u4E00-\u9FA5\s]+$/; //增加空格\s
    if(!reg.test(str))
    {
        return false;
    }
    return true;
}

///************************************
/// 驗証台灣身份証
/// 作者：jone_lu
/// 日期：2009-12-26
/// 返回：是=true;否=false
///************************************
function checkID(idStr)
{
  // 依照字母的編號排列，存入陣列備用。
  var letters = new Array('A', 'B', 'C', 'D',
      'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
      'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
      'X', 'Y', 'W', 'Z', 'I', 'O');
  // 儲存各個乘數
  var multiply = new Array(1, 9, 8, 7, 6, 5,
                           4, 3, 2, 1);
  var nums = new Array(2);
  var firstChar;
  var firstNum;
  var lastNum;
  var total = 0;
  // 撰寫「正規表達式」。第一個字為英文字母，
  // 第二個字為1或2，後面跟著8個數字，不分大小寫。
  var regExpID=/^[a-z](1|2)\d{8}$/i;
  // 使用「正規表達式」檢驗格式
  if (idStr.search(regExpID)==-1) {
    // 基本格式錯誤	
    return false;
  } else {
	// 取出第一個字元和最後一個數字。
	firstChar = idStr.charAt(0).toUpperCase();
	lastNum = idStr.charAt(9);
  }
  // 找出第一個字母對應的數字，並轉換成兩位數數字。
  for (var i=0; i<26; i++) {
	if (firstChar == letters[i]) {
	  firstNum = i + 10;
	  nums[0] = Math.floor(firstNum / 10);
	  nums[1] = firstNum - (nums[0] * 10);
	  break;
	}
  }
  // 執行加總計算
  for(var i=0; i<multiply.length; i++){
    if (i<2) {
      total += nums[i] * multiply[i];
    } else {
      total += parseInt(idStr.charAt(i-1)) *
               multiply[i];
    }
  }
  var tempLast=10-total % 10;
  if(tempLast==10)
  {
    tempLast=0;
  }
  if (tempLast!= lastNum) {
	return false;
  }
  
  return true;
}
//讓維護頁面按Esc鍵無作用
document.onkeydown = function() {
    if (event.keyCode == 27) {
        event.returnValue = null;
    }
}

