String.prototype.trim=function(){
	return this.replace(/(^\s*)|(\s*$)/g,"");
}

function isEmpty(s){  
	return s == null || s.trim().length == 0;
}

function isEmail(s){
	return /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(s);
}

function checkEmail(name){
	var o = document.getElementsByName(name);
	for(i=0; i<o.length; i++){
		if(!isEmail(o[i].value)){
			window.alert("請輸入有效的 Email 地址");
			o[i].focus();
			o[i].select();
			return false;
		}
	}
	return true;
}

function checkOptions(name,msg){
	var o = document.getElementsByName(name);
	for(i=0; i<o.length; i++){
		if(o[i].checked)
			return true;
	}
	window.alert("請選擇 ["+ msg +"]");
	return false;
}

function checkEmpty(id,msg,s){
	if(s == null)
		s = "";
	var input = null;
	if(id.value != null)
		input = id;
	if(input == null)
		input = document.getElementById(id);
	if(input == null){
		input = document.getElementsByName(id);
		input = input.length==0 ? null : input[0];
	}
	if(input!=null){
		if(input.type == "checkbox" || input.type == "radio"){
			var result = false;
			input = document.getElementsByName(id);
			for (i=0; i<input.length; i++){
				if(input[i].checked){
					result = true;
					break;
				}
			}
			if(result == false){
				window.alert("請選擇 ["+ msg +"]");
				input[0].focus();
				return false;
			}
		}
		else if(isEmpty(input.value) || input.value.trim() == s){
			if(input.type == "select")
				window.alert("請選擇 ["+ msg +"]");
			else
				window.alert("請輸入 ["+ msg +"]");
			input.focus();
			if(input.type == "text" || input.type == "password")
				input.select();
			return false;
		}
	}
	return true;
}

function checkExtName(extType,file,msg)
{
	var fileName = file.value;
	var extName = fileName.substr(fileName.lastIndexOf(".")+1);
	if(extType.indexOf(extName.toLowerCase()) == -1){ 
		alert("["+ msg +"]");
		file.focus();
		return false; 
	}
	return true;
}

function checkFCKeditor(instance,msg)
{
	var checkContent = FCKeditorAPI.GetInstance(instance).GetXHTML();
	
	if(isEmpty(checkContent)){
		var oEditor = FCKeditorAPI.GetInstance(instance);
		oEditor.Focus();
		window.alert("請輸入 ["+ msg +"]");
		return false;
	}	
	return true;
}

function checkIsMoney(input,msg)
{   
  	var moneryPattern = /^(([0-9]+\.[0-9]{0,2})|([0-9]*[1-9][0-9]*))$/ ;
  	if(input.value <= 0 || !moneryPattern.test(input.value)){
  		input.focus();
  		window.alert("["+ msg +"] 必須大於0，最多兩位小數 !");
  		return false;
  	}
  	return true;
}

function checkIsMoneyOrZero(input,msg)
{   
  	var moneryPattern = /^(0|([0-9]+\.[0-9]{0,2})|([0-9]*[1-9][0-9]*))$/ ;
  	if(!moneryPattern.test(input.value)){
  		input.focus();
  		window.alert("["+ msg +"] 必須大於等於0，最多兩位小數 !");
  		return false;
  	}
  	return true;
}

function checkMaxSize(input,msg,size){
	if(getLength(input.value) > size){
		window.alert("["+ msg +"] 長度不能大於 "+ size +" 字元 !");
		input.focus();
		input.select();
		return false;
	}
	return true;
}

function checkMinSize(input,msg,size){
	if(getLength(input.value) < size){
		window.alert("["+ msg +"] 長度不能小於 "+ size +" 字元 !");
		input.focus();
		input.select();
		return false;
	}
	return true;
}

function getLength(s){
	var length=0;
	for (var i=0; i < s.length; i++){
		if (s.charCodeAt(i) > 255)
			length += 2;
		else
			length ++;
	}
	return length;
}

function isInt(n){
  var numbers = "-1234567890";
  for (var i=0;i<n.length;i++)
  {
    if (numbers.indexOf(n.charAt(i)) == -1)
      return false;
  }
  return true;
}

function getCookieVal(offset)
{
    var endstr = document.cookie.indexOf(";",offset);
    if (endstr == -1)
    {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset,endstr));
}

function getCookie(name)
{
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i= 0;
    while (i<clen)
    {
        var j = i+alen;
            if (document.cookie.substring(i,j) == arg)
            {
                return getCookieVal(j);
            }
        i = document.cookie.indexOf(" ",i)+1;
        if(i==0) break;
    }
    return null;
}

function setCookie(name,value)
{
    var argv = setCookie.arguments;
    var argc = setCookie.arguments.length;
    var expires = (2<argc)? argv[2]:null;
    var path = (3<argc)?argv[3]:null;
    var domain = (4<argc)?argv[4]:null;
    var secure = (5<argc)?argv[5]:null;

    document.cookie = name+"="+escape(value)+((expires == null)?" ":(";expires ="+expires.toGMTString()))+((path == null)?"  ":(";path = "+path))+((domain == null)?" ":(";domain =" +domain)) +((secure==true)?";secure":" ");
}

function cleanEdmCookie()
{
	var expdate = new Date();
	expdate.setTime(expdate.getTime()-3600);
	
	setCookie("edm_amount",null,expdate,"/",null,false);
}

function chkSelectAll(obj) 
{
	var chkID = document.body.all["" + obj + ""];
	chkID.checked = "checked";
	for (var i=0; i < chkID.length; i++)
		chkID[i].checked = "checked";
}

function chkSelectAgainst(obj) 
{
	var chkID = document.body.all["" + obj + ""];
	chkID.checked = !chkID.checked;
	for (var i=0; i < chkID.length; i++) 
		chkID[i].checked = !chkID[i].checked;
}

function addOption(selectId,txt,val) 
{ 
    var objOption = new Option(txt,val); 
    document.getElementById(selectId).options.add(objOption);
}
 
function removeOption(selectId) 
{   
   document.getElementById(selectId).options.length = 0;
}

function getDate(prefix){
  var year = document.getElementsByName(prefix+'Year')[0].value.trim();
  var month = document.getElementsByName(prefix+'Month')[0].value.trim();
  var day = document.getElementsByName(prefix+"Day")[0].value.trim();
  var hour = document.getElementsByName(prefix+"Hour")[0];
  var minute = document.getElementsByName(prefix+"Minute")[0];
  hour = hour==null ? 0:hour.value.trim();
  minute = minute==null ? 0:minute.value.trim();
  
  year = parseInt(year,10);
  month = parseInt(month,10);
  day = parseInt(day,10);
  hour = parseInt(hour,10);
  minute = parseInt(minute,10);
  return new Date(year, month-1, day, hour, minute);
}

function isDate(prefix){
  var year = document.getElementsByName(prefix+'Year')[0].value.trim();
  var month = document.getElementsByName(prefix+'Month')[0].value.trim();
  var day = document.getElementsByName(prefix+"Day")[0].value.trim();
  var hour = document.getElementsByName(prefix+"Hour")[0];
  var minute = document.getElementsByName(prefix+"Minute")[0];
  hour = hour==null ? "0":hour.value.trim();
  minute = minute==null ? "0":minute.value.trim();

  year = parseInt(year,10);
  month = parseInt(month,10);
  day = parseInt(day,10);
  hour = parseInt(hour,10);
  minute = parseInt(minute,10);

  var date = new Date(year, month-1 , day, hour, minute);
  if(year != date.getFullYear() || month != date.getMonth()+1 || day != date.getDate())
	return false;
  return true;
}

function checkValidDate(prefix,msg,focusWhere){
  if(!isDate(prefix)){
    window.alert("["+ msg +"]無效，請重新選擇！");
    focusWhere.focus();
    return false;
  }
  return true;
}

// for macromedia dreamweaver
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


