/**
*
*  Javascript sprintf
*  http://www.webtoolkit.info/
*
*
**/
 function iframe_auto_height(){
    if(!this.in_site()) return;
    var iframe;
    $(parent.document).find("iframe").map(function(){ //找到自己的iframe
      if($(this).contents().get(0).location == window.location) iframe = this;
    });
    if(!iframe) return;//no parent
    var content_height = $("body").height()+50;
    content_height = content_height < 300 ? 300 : content_height; //設定最小高度
    content_height = typeof content_height == 'number' ? content_height+"px" : content_height;
    iframe.style.height = content_height;
  }
  //判斷是否在網頁的iframe之中
  function in_site(){
    if(parent != window && this.is_crosssite() == false) return(true);
    return(false);
  }
  //判斷是否跨站(可能是別人嵌入了你的網頁)
  function is_crosssite() {
    try {
      parent.location.host;
      return(false);
    }
    catch(e) {
      return(true);
    }
  }
  
sprintfWrapper = {
 
	init : function () {
 
		if (typeof arguments == "undefined") { return null; }
		if (arguments.length < 1) { return null; }
		if (typeof arguments[0] != "string") { return null; }
		if (typeof RegExp == "undefined") { return null; }
 
		var string = arguments[0];
		var exp = new RegExp(/(%([%]|(\-)?(\+|\x20)?(0)?(\d+)?(\.(\d)?)?([bcdfosxX])))/g);
		var matches = new Array();
		var strings = new Array();
		var convCount = 0;
		var stringPosStart = 0;
		var stringPosEnd = 0;
		var matchPosEnd = 0;
		var newString = '';
		var match = null;
 
		while (match = exp.exec(string)) {
			if (match[9]) { convCount += 1; }
 
			stringPosStart = matchPosEnd;
			stringPosEnd = exp.lastIndex - match[0].length;
			strings[strings.length] = string.substring(stringPosStart, stringPosEnd);
 
			matchPosEnd = exp.lastIndex;
			matches[matches.length] = {
				match: match[0],
				left: match[3] ? true : false,
				sign: match[4] || '',
				pad: match[5] || ' ',
				min: match[6] || 0,
				precision: match[8],
				code: match[9] || '%',
				negative: parseInt(arguments[convCount]) < 0 ? true : false,
				argument: String(arguments[convCount])
			};
		}
		strings[strings.length] = string.substring(matchPosEnd);
 
		if (matches.length == 0) { return string; }
		if ((arguments.length - 1) < convCount) { return null; }
 
		var code = null;
		var match = null;
		var i = null;
 
		for (i=0; i<matches.length; i++) {
 
			if (matches[i].code == '%') { substitution = '%' }
			else if (matches[i].code == 'b') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(2));
				substitution = sprintfWrapper.convert(matches[i], true);
			}
			else if (matches[i].code == 'c') {
				matches[i].argument = String(String.fromCharCode(parseInt(Math.abs(parseInt(matches[i].argument)))));
				substitution = sprintfWrapper.convert(matches[i], true);
			}
			else if (matches[i].code == 'd') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)));
				substitution = sprintfWrapper.convert(matches[i]);
			}
			else if (matches[i].code == 'f') {
				matches[i].argument = String(Math.abs(parseFloat(matches[i].argument)).toFixed(matches[i].precision ? matches[i].precision : 6));
				substitution = sprintfWrapper.convert(matches[i]);
			}
			else if (matches[i].code == 'o') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(8));
				substitution = sprintfWrapper.convert(matches[i]);
			}
			else if (matches[i].code == 's') {
				matches[i].argument = matches[i].argument.substring(0, matches[i].precision ? matches[i].precision : matches[i].argument.length)
				substitution = sprintfWrapper.convert(matches[i], true);
			}
			else if (matches[i].code == 'x') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
				substitution = sprintfWrapper.convert(matches[i]);
			}
			else if (matches[i].code == 'X') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
				substitution = sprintfWrapper.convert(matches[i]).toUpperCase();
			}
			else {
				substitution = matches[i].match;
			}
 
			newString += strings[i];
			newString += substitution;
 
		}
		newString += strings[i];
 
		return newString;
 
	},
 
	convert : function(match, nosign){
		if (nosign) {
			match.sign = '';
		} else {
			match.sign = match.negative ? '-' : match.sign;
		}
		var l = match.min - match.argument.length + 1 - match.sign.length;
		var pad = new Array(l < 0 ? 0 : l).join(match.pad);
		if (!match.left) {
			if (match.pad == "0" || nosign) {
				return match.sign + pad + match.argument;
			} else {
				return pad + match.sign + match.argument;
			}
		} else {
			if (match.pad == "0" || nosign) {
				return match.sign + match.argument + pad.replace(/0/g, ' ');
			} else {
				return match.sign + match.argument + pad;
			}
		}
	}
}
 
sprintf = sprintfWrapper.init;

// JavaScript Document 3 21 22
//==================== for zip code begin ========================= 
County = new Array("台北市","基隆市","台北縣","宜蘭縣","新竹市","新竹縣","桃園縣","苗栗縣","台中市","台中縣","彰化縣","南投縣","嘉義市","嘉義縣","雲林縣","台南市","台南縣","高雄市","高雄縣","澎湖縣","屏東縣","台東縣","花蓮縣","金門縣","連江縣","南海諸島","釣魚台列嶼","外國");//全省各縣市 
Zone = new Array(28); 
Zone[0] = new Array("中正區","大同區","中山區","松山區","大安區","萬華區","信義區","士林區","北投區","內湖區","南港區","文山區(木柵)","文山區(景美)");//台北市 
Zone[1] = new Array("仁愛區","信義區","中正區","中山區","安樂區","暖暖區","七堵區");//基隆市 
Zone[2] = new Array("萬里鄉","金山鄉","板橋市","汐止市","深坑鄉","石碇鄉","瑞芳鎮","平溪鄉","雙溪鄉","貢寮鄉","新店市","坪林鄉","烏來鄉","永和市","中和市","土城市","三峽鎮","樹林市","鶯歌鎮","三重市","新莊市","泰山鄉","林口鄉","蘆洲市","五股鄉","八里鄉","淡水鎮","三芝鄉","石門鄉");//台北縣 
Zone[3] = new Array("宜蘭市","頭城鎮","礁溪鄉","壯圍鄉","員山鄉","羅東鎮","三星鄉","大同鄉","五結鄉","冬山鄉","蘇澳鎮","南澳鄉");//宜蘭縣 
Zone[4] = new Array("");//新竹市 
Zone[5] = new Array("竹北市","湖口鄉","新豐鄉","新埔鄉","關西鎮","芎林鄉","寶山鄉","竹東鎮","五峰鄉","橫山鄉","尖石鄉","北埔鄉","峨嵋鄉");//新竹縣 
Zone[6] = new Array("中壢市","平鎮","龍潭鄉","楊梅鎮","新屋鄉","觀音鄉","桃園市","龜山鄉","八德市","大溪鎮","復興鄉","大園鄉","蘆竹鄉");//桃園縣 
Zone[7] = new Array("竹南鎮","頭份鎮","三灣鄉","南庄鄉","獅潭鄉","後龍鎮","通霄鎮","苑裡鎮","苗栗市","造橋鄉","頭屋鄉","公館鄉","大湖鄉","泰安鄉","鉰鑼鄉","三義鄉","西湖鄉","卓蘭鄉");//苗栗縣 
Zone[8] = new Array("中區","東區","南區","西區","北區","北屯區","西屯區","南屯區");//台中市 
Zone[9] = new Array("太平市","大里市","霧峰鄉","烏日鄉","豐原市","后里鄉","石岡鄉","東勢鎮","和平鄉","新社鄉","潭子鄉","大雅鄉","神岡鄉","大肚鄉","沙鹿鎮","龍井鄉","梧棲鎮","清水鎮","大甲鎮","外圃鄉","大安鄉");//台中縣 
Zone[10] = new Array("彰化市","芬園鄉","花壇鄉","秀水鄉","鹿港鎮","福興鄉","線西鄉","和美鎮","伸港鄉","員林鎮","社頭鄉","永靖鄉","埔心鄉","溪湖鎮","大村鄉","埔鹽鄉","田中鎮","北斗鎮","田尾鄉","埤頭鄉","溪州鄉","竹塘鄉","二林鎮","大城鄉","芳苑鄉","二水鄉");//彰化縣 
Zone[11] = new Array("南投市","中寮鄉","草屯鎮","國姓鄉","埔里鎮","仁愛鄉","名間鄉","集集鄉","水里鄉","魚池鄉","信義鄉","竹山鎮","鹿谷鄉");//南投縣 
Zone[12] = new Array("");//嘉義市 
Zone[13] = new Array("番路鄉","梅山鄉","竹崎鄉","阿里山鄉","中埔鄉","大埔鄉","水上鄉","鹿草鄉","太保市","朴子市","東石鄉","六腳鄉","新港鄉","民雄鄉","大林鎮","漢口鄉","義竹鄉","布袋鎮");//嘉義縣 
Zone[14] = new Array("斗南市","大埤鄉","虎尾鎮","土庫鎮","褒忠鄉","東勢鄉","台西鄉","崙背鄉","麥寮鄉","斗六市","林內鄉","古坑鄉","莿桐鄉","西螺鎮","二崙鄉","北港鎮","水林鄉","口湖鄉","四湖鄉","元長鄉");//雲林縣 
Zone[15] = new Array("中區","東區","南區","西區","北區","安平區","安南區");//台南市 
Zone[16] = new Array("永康市","歸仁鄉","新化鎮","左鎮鄉","玉井鄉","楠西鄉","南化鄉","仁德鄉","關廟鄉","龍崎鄉","官田鄉","麻豆鎮","佳里鎮","西港鄉","七股鄉","將軍鄉","學甲鎮","北門鄉","新營市","後壁鄉","白河鎮","東山鄉","六甲鄉","下營鄉","柳營鄉","鹽水鎮","善化鎮","大內鄉","山上鄉","新市鄉","安定鄉");//台南縣 
Zone[17] = new Array("新興區","前金區","苓雅區","鹽埕區","鼓山區","旗津區","前鎮區","三民區","楠梓區","小港區","左營區");//高雄市 
Zone[18] = new Array("仁武鄉","大社鄉","岡山鎮","路竹鄉","阿蓮鄉","田寮鄉","燕巢鄉","橋頭鄉","梓官鄉","彌陀鄉","永安鄉","湖內鄉","鳳山市","大寮鄉","林園鄉","鳥松鄉","大樹鄉","旗山鎮","美濃鎮","六龜鄉","內門鄉","杉林鄉","甲仙鄉","桃源鄉","三民鄉","茂林鄉","茄萣鄉");//高雄縣 
Zone[19] = new Array("馬公市","西嶼鄉","望安鄉","七美鄉","白沙鄉","湖西鄉");//澎湖縣 
Zone[20] = new Array("屏東市","三地門鄉","霧台鄉","瑪家鄉","九如鄉","里港鄉","高樹鄉","鹽埔鄉","長治鄉","麟洛鄉","竹田鄉","內埔鄉","萬丹鄉","潮州鎮","泰武鄉","來義鄉","萬巒鄉","嵌頂鄉","新埤鄉","南州鄉","林邊鄉","東港鎮","琉球鄉","佳冬鄉","新園鄉","枋寮鄉", "枋山鄉","春日鄉","獅子鄉","車城鄉","牡丹鄉","恆春鎮","滿州鄉");//屏東縣 
Zone[21] = new Array("台東市","綠島鄉","蘭嶼鄉","延平鄉","卑南鄉","鹿野鄉","關山鎮","海端鄉","池上鄉","東河鄉","成氐","長濱鄉","太麻里鄉","金峰鄉","大武鄉","達仁鄉");//台東縣 
Zone[22] = new Array("花蓮市","新城鄉","秀林鄉","吉安鄉","壽豐鄉","鳳林鎮","光復鄉","豐濱鄉","瑞穗鄉","萬榮鄉","玉里鎮","卓溪鄉","富里鄉");//花蓮縣 
Zone[23] = new Array("金沙鎮","金湖鎮","金寧鄉","金城鎮","烈嶼鄉","烏坵鄉");//金門縣 
Zone[24] = new Array("南竿鄉","北竿鄉","莒光鄉","東引");//連江縣 
Zone[25] = new Array("東沙","西沙");//南海諸島 
Zone[26] = new Array("");//釣魚台列嶼 
Zone[27] = new Array("");//外國 


ZipCode = new Array(28); 
ZipCode[0] = new Array("100","103","104","105","106","108","110","111","112","114","115","116","117");//台北市 
ZipCode[1] = new Array("200","201","202","203","204","205","206");//基隆市 
ZipCode[2] = new Array("207","208","220","221","222","223","224","226","227","228","231","232","233","234","235","236","237","238","239","241","242","243","244","247","248","249","251","252","253");//台北縣 
ZipCode[3] = new Array("260","261","262","263","264","265","266","267","268","269","270","272");//宜蘭縣 
ZipCode[4] = new Array("300");//新竹市 
ZipCode[5] = new Array("302","303","304","305","306","307","308","310","311","312","313","314","315");//新竹縣 
ZipCode[6] = new Array("320","324","325","326","327","328","330","333","334","335","336","337","338");//桃園縣 
ZipCode[7] = new Array("350","351","352","353","354","356","357","358","360","361","362","363","364","365","366","367","368","369");//苗栗縣 
ZipCode[8] = new Array("400","401","402","403","404","406","407","408");//台中市 
ZipCode[9] = new Array("411","412","413","414","420","421","422","423","424","426","427","428","429","432","433","434","435","436","437","438","439");//台中縣 
ZipCode[10] = new Array("500","502","503","504","505","506","507","508","509","510","511","5112","513","514","515","516","520","521","522","523","524","525","526","527","528","530");//彰化縣 
ZipCode[11] = new Array("540","541","542","544","545","546","551","552","553","555","556","557","558");//南投縣 
ZipCode[12] = new Array("600");//嘉義市 
ZipCode[13] = new Array("602","603","604","605","606","607","608","611","612","613","614","615","616","621","622","623","624","625");//嘉義縣 
ZipCode[14] = new Array("630","631","632","633","634","635","636","637","638","640","643","646","647","648","649","651","652","653","654","655");//雲林縣 
ZipCode[15] = new Array("700","701","702","703","704","708","709");//台南市 
ZipCode[16] = new Array("710","711","712","713","714","715","716","717","718","719","720","721","722","723","724","725","726","727","730","731","732","733","734","735","736","737","741","742","743","744","745");//台南縣 
ZipCode[17] = new Array("800","801","802","803","804","805","806","807","811","812","813");//高雄市 
ZipCode[18] = new Array("814","815","820","821","822","823","824","825","826","827","828","829","830","831","832","833","840","842","843","844","845","846","847","848","849","851","852");//高雄縣 
ZipCode[19] = new Array("880","881","882","883","884","885");//澎湖縣 
ZipCode[20] = new Array("900","901","902","903","904","905","906","907","908","909","911","912","913","920","921","922","923","924","925","926","927","928","929","931","932","940","941","942","943","944","945","946","947");//屏東縣 
ZipCode[21] = new Array("950","951","952","953","954","955","956","957","958","959","961","962","963","964","965","966"); 
ZipCode[22] = new Array("970","971","972","973","974","975","976","977","978","979","981","982","983");//花蓮縣 
ZipCode[23] = new Array("890","891","892","893","894","896");//金門縣 
ZipCode[24] = new Array("209","210","211","212");//連江縣 
ZipCode[25] = new Array("817","819","290");//南海諸島 
ZipCode[26] = new Array("290");//釣魚台列嶼 
ZipCode[27] = new Array(" ");//外國 


function initCounty(countyInput) { 
countyInput.length = County.length; 
for (i = 0; i < County.length; i++) {
 
countyInput.options[i].value = County[i]; 
countyInput.options[i].text = County[i]; 
 
} 
countyInput.selectedIndex = 0; 
} 

function initZone(countyInput, zoneInput, post){ 
changeZone(countyInput, zoneInput, post); 
} 

function changeZone(countyInput, zoneInput, post) { 
selectedCountyIndex = countyInput.selectedIndex; 

zoneInput.length = Zone[selectedCountyIndex].length; 
for (i = 0; i < Zone[selectedCountyIndex].length; i++) { 
zoneInput.options[i].value = Zone[selectedCountyIndex][i]; 
zoneInput.options[i].text = Zone[selectedCountyIndex][i]; 
if (zoneInput.options[i].text == "松山區") 
zoneInput.options[i].selected = true; 
} 
//zoneInput.selectedIndex = 0; 

showZipCode(countyInput, zoneInput, post); 
} 

function showZipCode(countyInput, zoneInput, post) { 
post.value = ZipCode[countyInput.selectedIndex][zoneInput.selectedIndex]; 
} 

function showZone(countyInput,zoneInput,post){ 
var k=0;l=0; 
for (i=0; i < ZipCode.length; i++) { 
for (j=0; j < ZipCode[i].length; j++) { 
if ( post == ZipCode[i][j] ) { 
countyInput.length = County.length; 
for (k = 0; k < countyInput.length; k++) { 
countyInput.options[k].value = County[k]; 
countyInput.options[k].text = County[k]; 
if (County[k] == County[i]) { 
countyInput.options[k].selected = true; 
} 
} 

zoneInput.length = Zone[i].length; 
for (l = 0; l < zoneInput.length; l++) { 
zoneInput.options[l].value = Zone[i][l]; 
zoneInput.options[l].text = Zone[i][l]; 
if (zoneInput.options[l].text == Zone[i][j]) { 
zoneInput.options[l].selected = true; 
} 
} 
return; 
} 
} 
} 
// alert(k+'無此郵遞區號'+l);post='105';ResetAll();return; 
} 

function showZone1(post){//郵遞區號 轉 地名 
for (i=0; i < ZipCode.length; i++) { 
for (j=0; j < ZipCode[i].length; j++) { 
if ( post == ZipCode[i][j] ) { 
document.write(County[i]+Zone[i][j]);return; 
} 
} 
} 
} 
//==================== for zip code end ========================= 

//開啟置中視窗
function centerWindow(theURL,width,height) {
    var window_width = width;
    var window_height = height;
    var window_top = (screen.height-window_height)/2;
    var window_left = (screen.width-window_width)/2;
    newWindow=window.open(''+ theURL + '','','width=' + window_width + ',height=' + window_height + ',top=' + window_top + ',left=' + window_left + ',scrollbars=yes');
    newWindow.focus();
}

function dateformat(strings) {
	
	return strings.replace(/-/ig,'/');
}

  function   DateAdd(interval,number,date)  
  {  
  /*  
    *---------------   DateAdd(interval,number,date)   -----------------  
    *   DateAdd(interval,number,date)    
    *   功能:实现VBScript的DateAdd功能.  
    *   参数:interval,字符串表达式，表示要添加的时间间隔.  
    *   参数:number,数值表达式，表示要添加的时间间隔的个数.  
    *   参数:date,时间对象.  
    *   返回:新的时间对象.  
    *   var   now   =   new   Date();  
    *   var   newDate   =   DateAdd("d",5,now);  
    *   author:wanghr100(灰豆宝宝.net)  
    *   update:2004-5-28   11:46  
    *---------------   DateAdd(interval,number,date)   -----------------  
    */  
          switch(interval)  
          {  
                  case   "y"   :   {  
                          date.setFullYear(date.getFullYear()+number);  
                          return   date;  
                          break;  
                  }  
                  case   "q"   :   {  
                          date.setMonth(date.getMonth()+number*3);  
                          return   date;  
                          break;  
                  }  
                  case   "m"   :   {  
                          date.setMonth(date.getMonth()+number);  
                          return   date;  
                          break;  
                  }  
                  case   "w"   :   {  
                          date.setDate(date.getDate()+number*7);  
                          return   date;  
                          break;  
                  }  
                  case   "d"   :   {  
                          date.setDate(date.getDate()+number);  
                          return   date;  
                          break;  
                  }  
                  case   "h"   :   {  
                          date.setHours(date.getHours()+number);  
                          return   date;  
                          break;  
                  }  
                  case   "m"   :   {  
                          date.setMinutes(date.getMinutes()+number);  
                          return   date;  
                          break;  
                  }  
                  case   "s"   :   {  
                          date.setSeconds(date.getSeconds()+number);  
                          return   date;  
                          break;  
                  }  
                  default   :   {  
                          date.setDate(d.getDate()+number);  
                          return   date;  
                          break;  
                  }  
          }  
  }   
  
 	function chkMail(id){
		var m = /^([a-zA-Z0-9])([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
		if(document.getElementById(id).value.match(m)==null){
			return false;
		}
		return true;
	}
function check_tw_id(sId){
	  var LegalID = "0123456789"
	  var fResult=true;
 
	  if(sId.length<10)
		fResult=false;
	  else{
		if((sId.charAt(0)=='A') || (sId.charAt(0)=='a')) value=10
		else if((sId.charAt(0)=='B') || (sId.charAt(0)=='b')) value=11
		else if((sId.charAt(0)=='C') || (sId.charAt(0)=='c')) value=12
		else if((sId.charAt(0)=='D') || (sId.charAt(0)=='d')) value=13
		else if((sId.charAt(0)=='E') || (sId.charAt(0)=='e')) value=14
		else if((sId.charAt(0)=='F') || (sId.charAt(0)=='f')) value=15
		else if((sId.charAt(0)=='G') || (sId.charAt(0)=='g')) value=16
		else if((sId.charAt(0)=='H') || (sId.charAt(0)=='h')) value=17
		else if((sId.charAt(0)=='J') || (sId.charAt(0)=='j')) value=18
		else if((sId.charAt(0)=='K') || (sId.charAt(0)=='k')) value=19
		else if((sId.charAt(0)=='L') || (sId.charAt(0)=='l')) value=20
		else if((sId.charAt(0)=='M') || (sId.charAt(0)=='m')) value=21
		else if((sId.charAt(0)=='N') || (sId.charAt(0)=='n')) value=22
		else if((sId.charAt(0)=='P') || (sId.charAt(0)=='p')) value=23
		else if((sId.charAt(0)=='Q') || (sId.charAt(0)=='q')) value=24
		else if((sId.charAt(0)=='R') || (sId.charAt(0)=='r')) value=25
		else if((sId.charAt(0)=='S') || (sId.charAt(0)=='s')) value=26
		else if((sId.charAt(0)=='T') || (sId.charAt(0)=='t')) value=27
		else if((sId.charAt(0)=='U') || (sId.charAt(0)=='u')) value=28
		else if((sId.charAt(0)=='V') || (sId.charAt(0)=='v')) value=29
		else if((sId.charAt(0)=='X') || (sId.charAt(0)=='x')) value=30
		else if((sId.charAt(0)=='Y') || (sId.charAt(0)=='y')) value=31
		else if((sId.charAt(0)=='W') || (sId.charAt(0)=='w')) value=32
		else if((sId.charAt(0)=='Z') || (sId.charAt(0)=='z')) value=33
		else if((sId.charAt(0)=='I') || (sId.charAt(0)=='i')) value=34
		else if((sId.charAt(0)=='O') || (sId.charAt(0)=='o')) value=35
		else fResult = false ;
	  }
	  if(fResult==true){
		value = Math.floor(value/10) + (value%10)*9 + parseInt(sId.charAt(1))*8 +
				parseInt(sId.charAt(2))*7 + parseInt(sId.charAt(3)) * 6 + parseInt(sId.charAt(4)) * 5 +
				parseInt(sId.charAt(5))*4 + parseInt(sId.charAt(6)) * 3+ parseInt(sId.charAt(7)) * 2+
				parseInt(sId.charAt(8)) + parseInt(sId.charAt(9)) ;
		value = value % 10 ;
		if(value!=0) fResult = false ;
		var i;
		var c;
		for (i = 1; i < sId.length; i++){
		  c = sId.charAt(i);
		  if (LegalID.indexOf(c) == -1) fResult = false;
		}
	  }
	  if(fResult == false)
		return false;
	  else
		return true;
	}
	
	
	
/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/
 
/* utf.js - UTF-8 <=> UTF-16 convertion
*
* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
* Version: 1.0
* LastModified: Dec 25 1999
* This library is free.  You can redistribute it and/or modify it.
*/

/*
* Interfaces:
* utf8 = utf16to8(utf16);
* utf16 = utf16to8(utf8);
*/

function utf16to8(str) {
    var out, i, len, c;

    out = "";
    len = str.length;
    for(i = 0; i < len; i++) {
        c = str.charCodeAt(i);
        if ((c >= 0x0001) && (c <= 0x007F)) {
            out += str.charAt(i);
        } else if (c > 0x07FF) {
            out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
            out += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));
            out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
        } else {
            out += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));
            out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
        }
    }
    return out;
}

function utf8to16(str) {
    var out, i, len, c;
    var char2, char3;

    out = "";
    len = str.length;
    i = 0;
    while(i < len) {
        c = str.charCodeAt(i++);
        switch(c >> 4)
        {
          case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
            // 0xxxxxxx
            out += str.charAt(i-1);
            break;
          case 12: case 13:
            // 110x xxxx   10xx xxxx
            char2 = str.charCodeAt(i++);
            out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
            break;
          case 14:
            // 1110 xxxx  10xx xxxx  10xx xxxx
            char2 = str.charCodeAt(i++);
            char3 = str.charCodeAt(i++);
            out += String.fromCharCode(((c & 0x0F) << 12) |
                                           ((char2 & 0x3F) << 6) |
                                           ((char3 & 0x3F) << 0));
            break;
        }
    }

    return out;
}

/* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
* Version: 1.0
* LastModified: Dec 25 1999
* This library is free.  You can redistribute it and/or modify it.
*/

/*
* Interfaces:
* b64 = base64encode(data);
* data = base64decode(b64);
*/


var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64DecodeChars = new Array(
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
    -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
    -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);

function base64encode(str) {
    var out, i, len;
    var c1, c2, c3;

    len = str.length;
    i = 0;
    out = "";
    while(i < len) {
        c1 = str.charCodeAt(i++) & 0xff;
        if(i == len)
        {
            out += base64EncodeChars.charAt(c1 >> 2);
            out += base64EncodeChars.charAt((c1 & 0x3) << 4);
            out += "==";
            break;
        }
        c2 = str.charCodeAt(i++);
        if(i == len)
        {
            out += base64EncodeChars.charAt(c1 >> 2);
            out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
            out += base64EncodeChars.charAt((c2 & 0xF) << 2);
            out += "=";
            break;
        }
        c3 = str.charCodeAt(i++);
        out += base64EncodeChars.charAt(c1 >> 2);
        out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
        out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
        out += base64EncodeChars.charAt(c3 & 0x3F);
    }
    return out;
}

function base64decode(str) {
    var c1, c2, c3, c4;
    var i, len, out;

    len = str.length;
    i = 0;
    out = "";
    while(i < len) {
        /* c1 */
        do {
            c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
        } while(i < len && c1 == -1);
        if(c1 == -1)
            break;

        /* c2 */
        do {
            c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
        } while(i < len && c2 == -1);
        if(c2 == -1)
            break;

        out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));

        /* c3 */
        do {
            c3 = str.charCodeAt(i++) & 0xff;
            if(c3 == 61)
                return out;
            c3 = base64DecodeChars[c3];
        } while(i < len && c3 == -1);
        if(c3 == -1)
            break;

        out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));

        /* c4 */
        do {
            c4 = str.charCodeAt(i++) & 0xff;
            if(c4 == 61)
                return out;
            c4 = base64DecodeChars[c4];
        } while(i < len && c4 == -1);
        if(c4 == -1)
            break;
        out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
    }
    return out;
}
//input base64 encode
function strdecode(str){
        return utf8to16(base64decode(str));
}

function addmyorder(pids,pname) {
 
$.ajax({
  url: '../add_myorder.php',
  data: { p_id : pids},
  type : "POST",
  dataType: "json",
  success: function(data) {
 
    if (data.message =='NEEDLOGIN') { alert('請先於右上角點選登入會員'); return;}
    if (data.code == '0') { 
	alert(pname + ' 已提交詢價');
	try {
	parent.location.href= '/index.php?module=member&flag=myorder&flag2=tktask';
	parent.$.fancybox.close();
	} catch(e) {
	window.location.href= '/index.php?module=member&flag=myorder&flag2=tktask';
	}
	return;
	} else {
	alert('發生錯誤: ' + data.message);	
	return;
	}
	
  }
});
 	
}


function addmyitin(pids) {
	
	$.ajax({
  url: '../add_myitin.php',
  data: { pid : pids},
  type : "POST",
  
  success: function(data) {

    if (data =='NEEDLOGIN') { alert('請先於右上角點選登入會員'); return;}
	
	if (data =='ADDALREADY') { alert('您一定很愛這家住宿，請按確認鍵後繼續瀏覽！'); return;}
	
    if (data != '') { 
	alert('我幫您加入到收藏夾了！');
	$.get("/rightblock.php", function(data){
  		$("#rightblock").html(data);
 	});
	return;
	}
	
  	}
	});
	
}

function Removemyitin(pids) {
	
	$.ajax({
  url: '../add_myitin.php',
  data: { removeMyitin : pids},
  type : "POST",
  
  success: function(data) {

  
  if (jQuery.trim(data) == '') { 
	alert('從收藏夾移除這間飯店！');
 	window.location.reload();
	return;
	 }
	
  	}
	});
	
}

function addmyorder2(pids,pname, ifedit ) {
 
$.ajax({
  url: '../add_myorder.php',
  data: { p_id : pids, ifedit: ifedit},
  type : "POST",
  dataType: "json",
  success: function(data) {
 
    if (data.message =='NEEDLOGIN') { alert('請先於右上角點選登入會員'); parent.$.fancybox.close(); return;}
    if (data.code == '0') { 
	 alert(pname + ' 已加入');
 
	parent.$("#tkt_ask_iframe").each(function() {
 		this.contentWindow.location.reload(true);
	});

	parent.$("#order_tkt_iframe").each(function() {
 		this.contentWindow.location.reload(true);
	});
	
	 parent.$.fancybox.close();
	//parent.location.reload();
	return;
	} else {
	alert('發生錯誤: ' + data.message);	
	return;
	}
	
  }
});
 	
}
