﻿var baseTime = 7*60*60*24;
function ChangeArea(val, obj){
document.WeatherImg.src = 'http://udn.com/WEATHER/IMAGES/'+val+'icon.gif';
document.WeatherLow.src = 'http://udn.com/WEATHER/IMAGES/'+val+'lowtemp.gif';
document.WeatherHigh.src = 'http://udn.com/WEATHER/IMAGES/'+val+'hightemp.gif';
document.getElementById('div_weatherlist').style.display = "none";
document.getElementById('spn_weathercity').innerHTML = obj;
document.getElementById('spn_weatherarrow').innerHTML = "▼";
DelCookie("OMG_Weather");
SetCookie("OMG_Weather", val + "_" + obj, baseTime, "/", ".98173.com.tw");
}
function FirstLoadArea(){
    var area = GetCookie("OMG_Weather");
    if(area == null){
        ChangeArea("taipei", "台北");
    }else{
        arr_area = new Array();
        arr_area = area.split("_");
        ChangeArea(arr_area[0], arr_area[1]);
    }
}
function realPosX (oTarget) {
	try {
		var realX = oTarget.offsetLeft;
		if (oTarget.offsetParent != null && oTarget.offsetParent.tagName.toUpperCase() != "BODY") {
			realX += realPosX(oTarget.offsetParent); 
		}
		return realX;
	} catch (e) {
		alert("realPosX: "+e);
	} 
}
function realPosY (oTarget) {
	try {
		var realY = oTarget.offsetTop;
		if (oTarget.offsetParent != null && oTarget.offsetParent.tagName.toUpperCase() != "BODY") {
			realY += realPosY(oTarget.offsetParent);
		}
		return realY;
	} catch (e) { 
		alert("realPosY: "+e);
	}
}
function callWeatherList(obj){
    document.getElementById('div_weatherlist').innerHTML = BindWeatherList();
    objList = document.getElementById('div_weatherlist');
    if(objList.style.display == ""){
        objList.style.display = "none";
        document.getElementById('spn_weatherarrow').innerHTML = "▼";
    }else{
        document.getElementById('spn_weatherarrow').innerHTML = "▲";
        objList.style.display = "";
        y = realPosY(obj) + 15;
        x = realPosX(obj);
        objList.style.top = y.toString() + "px";
        objList.style.left = x.toString() + "px";
    } 
}
function BindWeatherList(){
    var objCounty = [{en:"taipei",tw:"台北"},{en:"kaoshiung",tw:"高雄"},{en:"jilung",tw:"基隆"},{en:"taoyuan",tw:"桃園"},{en:"xinju",tw:"新竹"},{en:"miaoli",tw:"苗栗"},{en:"taichung",tw:"台中"},{en:"zhanghua",tw:"彰化"},{en:"nantou",tw:"南投"},{en:"yunlin",tw:"雲林"},{en:"jiayi",tw:"嘉義"},{en:"tainan",tw:"台南"},{en:"pingtong",tw:"屏東"},{en:"hengchun",tw:"恆春"},{en:"yilan",tw:"宜蘭"},{en:"hualian",tw:"花蓮"},{en:"taitung",tw:"台東"},{en:"penghu",tw:"澎湖"},{en:"jinmeng",tw:"金門"},{en:"matsu",tw:"馬祖"}];
    objHtml = "<table width=\"200\" height=\"130\" align=\"center\" border=\"1\" id=\"tbweather\" cellspacing=\"2\" cellpadding=\"1\" style=\"color: #333;background-color:White;border-color:#C0C5E2\"><tbody>";
    for(var i=0; i<objCounty.length; i++){
        if((i)%5 == 0){
            objHtml += "<tr>";
        }
        objHtml += "<td bordercolor=\"#FFFFFF\"><span onclick=\"ChangeArea('" + objCounty[i].en + "','" + objCounty[i].tw + "');\" style=\"cursor:pointer;text-decoration:none;color: #333;\">" + objCounty[i].tw + "</span></td>";
        if((i+1)%5 == 0){
            objHtml += "</tr>";
        }
    }
    return objHtml + "</tbody></table>";
}//-----------------------cookieset-------------------------
function GetCookieVal(offset){//取得Cookie解碼後的值
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
    endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}
function SetCookie(name, value){//設定Cookie值
    var expdate = new Date();
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));
    document.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))
    +((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
    +((secure == true) ? "; secure" : "");
}
function DelCookie(name){//删除Cookie
    var exp = new Date();
    exp.setTime (exp.getTime() - 1);
    var cval = GetCookie (name);
    document.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();
}
function GetCookie(name){//取得Cookie的原始值
    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;
}//----------------------------String--------------------------
function trim(strText){
    return strText.replace(/^\s+|\s+$/g, '');
}
function clrHtmlTag(strHTML){
    return strHTML.replace(/<[^<>]+>/g, '')
}
function repSpecialWord(strText){
	strText = trim(strText);
	strText = strText.replace(/'/g, '');
	strText = strText.replace(/"/g, '');
	strText = strText.replace(/</g, '＜');
	strText = strText.replace(/>/g, '＞');
	return strText;
}
function TrimAndClear(strText){
    return repSpecialWord(clrHtmlTag(trim(strText)));
}
function getRadom(intMax){
	var intR = Math.floor(Math.random() * (intMax + 1));
	if (intR == 0)
		intR = 1;
	return intR;
}
// Initializes a new instance of the StringBuilder class(var sb = new StringBuilder();)
// and appends the given value if supplied
function StringBuilder(value){
    this.strings = new Array("");
    this.append(value);
}
// Appends the given value to the end of this instance.(sb.append(.....))
StringBuilder.prototype.append = function (value){
    if (value){
        this.strings.push(value);
    }
}
// Clears the string buffer(sb.clear())
StringBuilder.prototype.clear = function (){
    this.strings.length = 1;
}
// Converts this instance to a String.(sb.toString())
StringBuilder.prototype.toString = function (){
    return this.strings.join("");
}
function ReplaceAll(sString, strSource, strObject){
	while(sString.indexOf(strSource) != -1){
		sString = sString.replace(strSource, strObject);
	}
	return sString;
}//-------------------------Search
function searchCheck(){
    var sk = escape(TrimAndClear(document.getElementById("txt_keyword").value));
    var objarea = document.getElementById("sel_sarea");
    var objstp = document.getElementById("sel_stype");
    if(objarea.selectedIndex == 0 && objstp.selectedIndex == 2){
        if(document.getElementById('txt_ssdate').value == ""){
            alert("請選擇起始日期");
            onfireClick("txt_ssdate");
            return false;
        }else if(document.getElementById('txt_sedate').value == ""){
            alert("請選擇結束日期");
            onfireClick("txt_sedate");
            return false;
        }else{
            sk = document.getElementById('txt_ssdate').value + "~" + document.getElementById('txt_sedate').value;
        }
    }else if(sk == ""){
        alert("請輸入搜尋關鍵字");
        document.getElementById('txt_keyword').focus();
        return false;
    }
    
    if (objarea.selectedIndex == 0){
        location.href = "/News/SearchResult.aspx?area=" + objarea.value + "&stp=" + objstp.value + "&sk=" + sk;
    }else{
        location.href = "/Forum/SearchResult.aspx?area=" + objarea.value + "&stp=" + objstp.value + "&sk=" + sk;
    }
}
function onfireClick(objName){
    if(document.all && typeof(document.all) == "object"){
        document.getElementById(objName).fireEvent("onclick");
    }else{  
        var evt = document.createEvent('MouseEvent');   
        evt.initEvent('click',false,false);
        document.getElementById(objName).dispatchEvent(evt);
    }
}
function searchAreaChange(){
    if(document.getElementById("sel_sarea").value == "1"){
        document.getElementById("sel_stype").remove(2);
    }else{
        document.getElementById("sel_stype").options[2] = new Option("時間","2");
    }
    document.getElementById("sel_stype").options[0].selectedIndex = 0;
    searchTypeChange();
}
function searchTypeChange(){
    if(document.getElementById("sel_sarea").value == "0" && document.getElementById("sel_stype").value == "2"){
        document.getElementById("txt_keyword").style.display = "none";
        document.getElementById("spn_datectrl").style.display = "";
    }else{
        document.getElementById("txt_keyword").style.display = "";
        document.getElementById("spn_datectrl").style.display = "none";
    }
}
function onSearchList(){
    if(area == '0' && stp == '2' && reqsk.indexOf('~') != -1){
        var arrsk = new Array();
        arrsk = reqsk.split('~');
        document.getElementById('txt_ssdate').value = arrsk[0];
        document.getElementById('txt_sedate').value = arrsk[1];
    }else{
        document.getElementById('txt_keyword').value = reqsk;
    }
    if(reqsk != ""){
        document.getElementById("sel_sarea").selectedIndex = eval(area);
        searchAreaChange();
        document.getElementById("sel_stype").selectedIndex = eval(stp);
        searchTypeChange();
    }
}
function onSearchBtnOver(obj){
    if(obj.src.indexOf("_on") != -1){
        obj.src = obj.src.replace("_on", "");
    }else{
        obj.src = obj.src.replace(".gif", "_on.gif");
    }
}