//<![CDATA[
/* Dreamwraver */
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_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_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];}
}

/* email check */
function email_check(email) {
	var at="@";
	var dot=".";
	var lat=email.indexOf(at);
	var lstr=email.length;
	var ldot=email.indexOf(dot);
	if (!checkInputValue(email)) return false;
	if (email.indexOf(at)==-1) return false;
	if (email.indexOf(at)==-1 || email.indexOf(at)==0 || email.indexOf(at)==lstr) return false;
	if (email.indexOf(dot)==-1 || email.indexOf(dot)==0 || email.indexOf(dot)==lstr) return false;
	if (email.indexOf(at,(lat+1))!=-1) return false;
	if (email.substring(lat-1,lat)==dot || email.substring(lat+1,lat+2)==dot) return false;
	if (email.indexOf(dot,(lat+2))==-1) return false;
	if (email.indexOf(" ")!=-1) return false;

	return true;
}
function selectAllFormItems(e, form) {
	var ListForm = form;
	if(e.checked==true) {
		for (var i=0; i<ListForm.length; i++)
		{
			if (ListForm.elements[i].checked==false) {
				ListForm.elements[i].checked = true;
			}
		}
	} else {
		for (var i=0; i<ListForm.length; i++)
		{
			if (ListForm.elements[i].checked==true) {
				ListForm.elements[i].checked = false;
			}
		}
	}
}
function checkFormValue(element) {
	if (document.getElementById(element)==undefined || document.getElementById(element)==null)
	{
		alert("項目未選擇或是沒有設定。");
		return false;
	} else {
		var v = document.getElementById(element).value;
		if ( v.replace(/(^\s*)|(\s*$)/gi,"")!='' && v!=0) return true;
			return false;
	}
}

function checkInputValue(v) {
	if ( v.replace(/(^\s*)|(\s*$)/gi,"")!='' && v!=0) return true;
		return false;
}
function shift_this_tab2(e, id) {
	try {
		var parentUL = e.parentNode;
		for(var i=0; i< parentUL.childNodes.length; i++) {
			if(parentUL.childNodes[i].nodeName=="LI" && parentUL.childNodes[i].className=='tab-selected-on') {
				parentUL.childNodes[i].className = 'tab-selected-off';
			}
		}
		e.className = 'tab-selected-on';
		
		for(var i=0; i<4; i++) {
			var tabs = document.getElementById('tab-selected-'+parseInt(i));
			tabs.style.display = 'none';
		}
		tabs = document.getElementById('tab-selected-'+parseInt(id));
		tabs.style.display = 'block';
	} catch(e) {}
}
function shift_this_tab(e) {
	/* fixed with FXCK IE */
	if (e.style.backgroundImage=="" || e.style.backgroundImage=="url(../images/channel-header-out-bg.png)" || e.getAttribute("class")=="tab-out") {
		e.style.backgroundImage="url(../images/channel-header-in-bg.png)";
		e.setAttribute("class", "tab-in");
	} else if (e.style.backgroundImage=="url(../images/channel-header-in-bg.png)" || e.getAttribute("class")=="tab-in") {
		e.style.backgroundImage="url(../images/channel-header-out-bg.png)";
		e.setAttribute("class", "tab-out");
	}
}
function turnon_this_tab(e, id) {
	var parentUL = e.parentNode;
	for(var i=0; i< parentUL.childNodes.length; i++) {
		if(parentUL.childNodes[i].nodeName=="LI" && (parentUL.childNodes[i].style.backgroundImage=="" || parentUL.childNodes[i].style.backgroundImage=="url(../images/channel-header-fixedin-bg.png)" || parentUL.childNodes[i].style.backgroundImage=="" || parentUL.childNodes[i].getAttribute("class")=="tab-fixed-in") )
		{
			parentUL.childNodes[i].setAttribute("class", "tab-out");
			parentUL.childNodes[i].style.backgroundImage = "url(../images/channel-header-out-bg.png)";
		}
	}
	e.setAttribute("class", "tab-fixed-in");
	e.style.backgroundImage = "url(../images/channel-header-fixedin-bg.png)";

	/* call AJAX here to reflash the content with id */
}
function autoCompleteBoxInit(e, types, otherclass) {
	if(checkInputValue(e.value)) {
		xajax_autoCompleteBoxInit(e.value, e.id, types, otherclass);
	}
}
function autoCompleteResponse(result, id, otherclass) {
	var target = (document.getElementById(id)!=undefined) ? document.getElementById(id) : false;
	var result = result.split(",");
	var _html = "<ul>";
	for(var i=0;i<result.length;i++) {
		_html += "<li><div onmouseover=\"this.style.backgroundColor='#bbbbcc';\" onmouseout=\"this.style.backgroundColor='#ffffff';\" onclick=\"autoCompleteFieldResponse('"+result[i]+"','"+id+"');\">"+result[i]+"</div></li>";
	}
	_html += "</ul>";
	if(target) {
		globalajaxboxindex = PM.autoCompleteBox(_html,target, otherclass);
	}
}
function autoCompleteFieldResponse(result, id) {
	var target = (document.getElementById(id)!=undefined) ? document.getElementById(id) : false;
	if(target) target.value = result;
	return false;
}
function loginForm(e) {
	if (checkFormValue('username')==false) { alert('請輸入帳號！'); return false;}
	if (checkFormValue('password')==false) { alert('請輸入密碼！'); return false;}
	e.submit();
}
function closeloginbox() {
	PM._hideMessage(globalajaxboxindex);
}
function userloginbox() {
	var _html = "<div class=\"userloginbox\"><div class=\"ajax-popup-close-button\"><img src=\""+globalpath+"images/ajax_popup_close_button.png\" border=\"0\" width=\"14\" height=\"14\" alt=\"close\" title=\"close\" onclick=\"closeloginbox();\"/></div><h3>會員登入</h3><form method=\"post\" action=\""+globalpath+"login.php\"><input type=\"hidden\" name=\"refurl\" id=\"refurl\" value=\""+referer+"\" /><dfn>帳號：<input type=\"text\" id=\"username\" name=\"username\" value=\"\" size=\"24\" maxlength=\"64\" /></dfn><br /><dfn>密碼：<input type=\"password\" id=\"password\" name=\"password\" value=\"\" size=\"16\" maxlength=\"16\" onkeydown=\"if (event.keyCode == 13) loginForm(this.form);\" />&nbsp;<input type=\"button\" value=\"確認\" onclick=\"loginForm(this.form);\" />&nbsp;<input type=\"reset\" value=\"清除\" /></dfn><br /></form><dfn>忘記密碼？&nbsp;尚未成為會員？</dfn></div>";
	globalajaxboxindex = PM.showMessage(_html, "center", "middle", 0);
}

function checksearchform(e) {
	try {
		var form = document.getElementById(e);
		var q = document.getElementById('searchkeyword');
		if(!checkInputValue(q.value)) {
			PM.showErrorMessage('請輸入搜尋關鍵字。','center','middle');
		} else {
			top.location.href = globalpath + 'search/'+encodeURIComponent(q.value);
		}
	} catch(e) {}
}

function subscribeEDM(e) {
	try {
		var act = (e==1) ? true : false;
		var email = document.getElementById('subscribe-email').value;
		if (!email_check(email)) {
			PM.showErrorMessage('E-mail 格式錯誤。','center','middle');
			return false;
		}
		if(act) {
			xajax_subscribeSuntravelEDM(email, 1);
		} else {
			xajax_subscribeSuntravelEDM(email, 0);
		}
	} catch(e) {}
}

function switchHeaderNewsTab(id, e) {
	try {
		var parentUL = e.parentNode;
		for(var i=0; i< parentUL.childNodes.length; i++) {
			if(parentUL.childNodes[i].nodeName=="LI" && parentUL.childNodes[i].className=='news-header-tabs-on') {
				parentUL.childNodes[i].className = 'news-header-tabs-off';
			}
		}
		e.className = 'news-header-tabs-on';
		xajax_switchHeaderNewsTab(id);
	} catch(e) {}
}

function todosearch(e) {
	try {
		var form = e;
		var reg = /([^0-9\/]+)/gi;
		var k = encodeURIComponent(str_replace('/','_|',form.keyword.value));
		if(!checkInputValue(k)) {
			PM.showErrorMessage('請填寫關鍵字。','center','middle');
			return false;
		}
		var r = encodeURIComponent(str_replace('/','_|',form.reporter.value));
		var t = encodeURIComponent(str_replace('/','_|',form.tag.value));
		var p = encodeURIComponent(str_replace('/','_|',form.photo.value));
		
		var dateline = new Array();
		for(var i=0;i<4;i++) {
			if(form.dateline[i].checked==true) var dl = form.dateline[i].value;
		}
		dateline.push(dl);
		switch(dl) {
			case 'year':
				var yop = form.news_search_years_op.value;
				var yed = form.news_search_years_ed.value;
				if(reg.test(yop) || !checkInputValue(yop) || reg.test(yed) || !checkInputValue(yed)) {
					PM.showErrorMessage('請填寫正確日期。','center','middle');
					return false;
				}
				dateline.push(yop);
				dateline.push(yed);
			break;
			case 'month':
				var myop = form.news_search_month_years_op.value;
				var mop = form.news_search_month_op.value;
				var med = form.news_search_month_ed.value;
				if(reg.test(myop) || !checkInputValue(myop) || reg.test(mop) || !checkInputValue(mop) || reg.test(med) || !checkInputValue(med)) {
					PM.showErrorMessage('請填寫正確日期。','center','middle');
					return false;
				}
				dateline.push(myop);
				dateline.push(mop);
				dateline.push(med);
			break;
			case 'fulldate':
				var dlop = form.search_news_by_dateline_op.value;
				var dled = form.search_news_by_dateline_ed.value;
				if(reg.test(dlop) || !checkInputValue(dlop) || reg.test(dled) || !checkInputValue(dled)) {
					PM.showErrorMessage('請填寫正確日期。','center','middle');
					return false;
				}
				dateline.push(dlop);
				dateline.push(dled);
			break;
			case 'all':
			default:
		}
		var channel = new Array();
		for(var i=0; i<form.search_news_channels.options.length; i++) {
			if (form.search_news_channels.options[i].selected) channel.push(form.search_news_channels.options[i].value);
		}
		var category = new Array();
		for(var i=0; i<form.search_news_categories.options.length; i++) {
			if (form.search_news_categories.options[i].selected) category.push(form.search_news_categories.options[i].value);
		}
		
		var searchQuery = '/'+k+'?r='+r+'&t='+t+'&p='+p+'&ch='+channel.toString()+'&ct='+category.toString()+'&dl='+dateline.toString();
		top.location.href = globalpath+'search'+searchQuery;
		
	} catch(e) { alert(e); }
}

/* PHP to javascript */
function is_array( mixed_var ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Legaev Andrey
    // +   bugfixed by: Cord
 
    return ( mixed_var instanceof Array );
}
function str_replace(search, replace, subject) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // -    depends on: is_array
    
    var f = search, r = replace, s = subject;
    var ra = is_array(r), sa = is_array(s), f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;
 
    while(j = 0, i--){
        while(s[i] = s[i].split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f);
    }
     
    return sa ? s : s[0];
}

/* common prototype */
function makeMaskerInvisibile() {
	/* fixed IE6 div tag cannot cover the select tag */
	if(isMinIE6) {
		var allSelect = document.getElementsByTagName("SELECT");
		for (var i=0;i<allSelect.length; i++) {
			allSelect[i].style.display = (allSelect[i].style.display=="none") ? "inline" : "none";
		}
	}
}

//////////////////////////////////// xml2array() ////////////////////////////////////////
//See http://www.openjs.com/scripts/xml_parser/
var not_whitespace = new RegExp(/[^\s]/);//This can be given inside the funciton - I made it a global variable to make the scipt a little bit faster.
var parent_count;
//Process the xml data
function xml2array(xmlDoc,parent_count) {
	var arr;
	var parent = "";
	parent_count = parent_count || new Object;

	var attribute_inside = 0; /*:CONFIG: Value - 1 or 0
	*	If 1, Value and Attribute will be shown inside the tag - like this...
	*	For the XML string...
	*	<guid isPermaLink="true">http://www.bin-co.com/</guid>
	*	The resulting array will be...
	*	array['guid']['value'] = "http://www.bin-co.com/";
	*	array['guid']['attribute_isPermaLink'] = "true";
	*	
	*	If 0, the value will be inside the tag but the attribute will be outside - like this...	
	*	For the same XML String the resulting array will be...
	*	array['guid'] = "http://www.bin-co.com/";
	*	array['attribute_guid_isPermaLink'] = "true";
	*/

	if(xmlDoc.nodeName && xmlDoc.nodeName.charAt(0) != "#") {
		if(xmlDoc.childNodes.length > 1) { //If its a parent
			arr = new Object;
			parent = xmlDoc.nodeName;
			
		}
	}
	var value = xmlDoc.nodeValue;
	if(xmlDoc.parentNode && xmlDoc.parentNode.nodeName && value) {
		if(not_whitespace.test(value)) {//If its a child
			arr = new Object;
			arr[xmlDoc.parentNode.nodeName] = value;
		}
	}

	if(xmlDoc.childNodes.length) {
		if(xmlDoc.childNodes.length == 1) { //Just one item in this tag.
			arr = xml2array(xmlDoc.childNodes[0],parent_count); //:RECURSION:
		} else { //If there is more than one childNodes, go thru them one by one and get their results.
			var index = 0;

			for(var i=0; i<xmlDoc.childNodes.length; i++) {//Go thru all the child nodes.
				var temp = xml2array(xmlDoc.childNodes[i],parent_count); //:RECURSION:
				if(temp) {
					var assoc = false;
					var arr_count = 0;
					for(key in temp) {
						if(isNaN(key)) assoc = true;
						arr_count++;
						if(arr_count>2) break;//We just need to know wether it is a single value array or not
					}

					if(assoc && arr_count == 1) {
						if(arr[key]) { 	//If another element exists with the same tag name before,
										//		put it in a numeric array.
							//Find out how many time this parent made its appearance
							if(!parent_count || !parent_count[key]) {
								parent_count[key] = 0;

								var temp_arr = arr[key];
								arr[key] = new Object;
								arr[key][0] = temp_arr;
							}
							parent_count[key]++;
							arr[key][parent_count[key]] = temp[key]; //Members of of a numeric array
						} else {
							parent_count[key] = 0;
							arr[key] = temp[key];
							if(xmlDoc.childNodes[i].attributes.length) {
								for(var j=0; j<xmlDoc.childNodes[i].attributes.length; j++) {
									var nname = xmlDoc.childNodes[i].attributes[j].nodeName;
									if(nname) {
										/* Value and Attribute inside the tag */
										if(attribute_inside) {
											var temp_arr = arr[key];
											arr[key] = new Object;
											arr[key]['value'] = temp_arr;
											arr[key]['attribute_'+nname] = xmlDoc.childNodes[i].attributes[j].nodeValue;
										} else {
										/* Value in the tag and Attribute otside the tag(in parent) */
											arr['attribute_' + key + '_' + nname] = xmlDoc.childNodes[i].attributes[j].nodeValue;
										}
									}
								} //End of 'for(var j=0; j<xmlDoc. ...'
							} //End of 'if(xmlDoc.childNodes[i] ...'
						}
					} else {
						arr[index] = temp;
						index++;
					}
				} //End of 'if(temp) {'
			} //End of 'for(var i=0; i<xmlDoc. ...'
		}
	}

	if(parent && arr) {
		var temp = arr;
		arr = new Object;
		
		arr[parent] = temp;
	}
	return arr;
}
//]]!>