// 補零函數
var addZero = function (str, n) {
    var strWithZero = str.toString(10);
    var nCount = parseInt(n) - strWithZero.length
    for (i = 1; i <= nCount; i++) {
        strWithZero = '0' + strWithZero;
    }
    return strWithZero;
}

// javascript 版本的 nl2br
var nl2br = function (string) {
	string = escape(string);
	if (string.indexOf('%0D%0A') > -1) {
		var re_nlchar = /%0D%0A/g ;
	} else if (string.indexOf('%0A') > -1) {
		var re_nlchar = /%0A/g ;
	} else if (string.indexOf('%0D') > -1) {
		var re_nlchar = /%0D/g ;
	}
	return unescape(string.replace(re_nlchar, '<br />'));
}

// javascript 版本的 htmlspecialchars
var htmlspecialchars = function (string, quote_style) {
   string = string.toString();

   string = string.replace(/&/g, '&amp;');
   string = string.replace(/</g, '&lt;');
   string = string.replace(/>/g, '&gt;');

   if (quote_style == 'ENT_QUOTES') {
       string = string.replace(/"/g, '&quot;');
       string = string.replace(/\'/g, '&#039;');
   } else if (quote_style != 'ENT_NOQUOTES') {
       string = string.replace(/"/g, '&quot;');
   }

   return string;
}

// 生日日期下拉式連動選單（select 年月日 name 限定 birthYear、birthMonth、birthDay）
var birthdaySelector = function (objForm, selectIndex) {

    // 取得該月天數（考慮閏年）
    timeA = new Date(objForm.birthYear.options[objForm.birthYear.selectedIndex].text, objForm.birthMonth.options[objForm.birthMonth.selectedIndex].value, 1);
    timeDifference = timeA - 86400000;
    timeB = new Date(timeDifference);
    var daysInMonth = timeB.getDate();

    // 清空原先日期所有選項
    for (var i = 0; i < objForm.birthDay.length; i++) {
        objForm.birthDay.options[0] = null;
    }

    // 預設第一項日期為「請選擇」
    objForm.birthDay.options[0] = new Option('請選擇');
    for (var i = 1; i <= daysInMonth; i++) {
        objForm.birthDay.options[i] = new Option(i);
    }

    // 預設日期選項為第一項「請選擇」
    objForm.birthDay.options[0].selected = true;
	objForm.birthDay.options[0].value = '';
};

// 預先載入圖片
var preloadImages = function (imageSrcList) {
    for (var i in imageSrcList) {
        var _img = new Image();
        _img.src = imageSrcList[i];
    }
};