﻿



function doSubmit() {
   var q;
   var type;
   var f = document.search_form;
   if(f.biz.type == "text")
   {
      if(f.biz.value == "")
      {
         alert("商家為必填欄位，不得為空");
         f.biz.focus();
         return false;
      }
      type = "searchbiz";
      q = "biz="+encodeURI(f.biz.value)+"&country="+encodeURI(f.country.value)+"&addr=&lat=&lon=&cid=&dispAddr=&dispCat=&p=";

   }

   if(f.addr.type == "text")
   {
      if(f.addr.value == "")
      {
         alert("地址欄位不得為空");
         f.addr.focus();
         return false;
      }
      type = "searchaddr";
      q = "addr="+encodeURI(f.addr.value)+"&biz=&country=&lat=&lon=&cid=&dispAddr=&dispCat=&p=";
   }

   trackSearch("http://www.livepages.com.tw/cgi-bin/search.cgi", type, q, "1");

   f.submit();
}

function doCatSearch(biz, country, addr, lat, lon, cid, dispAddr, dispCat, p, catPath, catNamePath) {

   var myform = document.search_form;

   input_catPath = document.createElement("input");
   input_catPath.name = "catPath";
   input_catPath.type = "hidden";
   input_catPath.value = catPath

      input_catNamePath = document.createElement("input");
   input_catNamePath.name = "catNamePath";
   input_catNamePath.type = "hidden";
   input_catNamePath.value = catNamePath

      myform.appendChild(input_catPath);
   myform.appendChild(input_catNamePath);


   doSearch(biz, country, addr, lat, lon, cid, dispAddr, dispCat, p);
}

function doSearch(biz, country, addr, lat, lon, cid, dispAddr, dispCat, p) {

   var myform = document.search_form;

   myform.biz.value = biz;
   myform.country.value = country;
   myform.addr.value = addr;
   myform.lat.value = lat;
   myform.lon.value = lon;
   myform.cid.value = cid;
   myform.dispAddr.value = dispAddr;
   myform.dispCat.value = dispCat;
   myform.p.value = p;

   myform.submit();
}

function doBrowse(act, catPath, catNamePath, cid, dispCat) {

   //var myform = document.search_form;
   var myform = document.browse_form;

   myform.act.value = act;
   myform.catPath.value = catPath;
   myform.catNamePath.value = catNamePath;
   myform.cid.value = cid;
   myform.dispCat.value = dispCat;

   myform.submit();
}

function doBrowse_UpdatePanel(act, catPath, catNamePath, cid, dispCat) {

   var url = document.browse_form.action + 
      "?act=" + encodeURIComponent(act) + 
      "&catPath=" + encodeURIComponent(catPath) + 
      "&catNamePath=" + encodeURIComponent(catNamePath) + 
      "&cid=" + encodeURIComponent(cid) + 
      "&dispCat=" + encodeURIComponent(dispCat);
   updatePanel(url);
   /*
      var conn = XMLHTTPInit();
      var result = XMLHTTPSyncResult(conn, url);

      obj = document.getElementById('panel');
      obj.innerHTML = result;
    */
}

function updatePanel(url) {

   var conn = XMLHTTPInit();
   var result = XMLHTTPSyncResult(conn, url);

   obj = document.getElementById('panel');
   obj.innerHTML = result;
}

function doLiveSearch() {

   var myform = document.search_form;

   liveinput = document.createElement("input");
   liveinput.name = "q";
   liveinput.type = "hidden";
   liveinput.value = myform.addr.value;

   liveform = document.createElement("form");
   liveform.action = "http://search.live.com/results.aspx";
   liveform.method = "get";
   liveform.target = "_blank";
   liveform.appendChild(liveinput);
   document.body.appendChild(liveform);

   liveform.submit();
}

   function checkSubmit(event) {
      if(event.keyCode == 13)
      {
         return doSubmit();
      }
   }

function trackClick(event, searchMode, queryString, e) {

   url='http://g.msn.com/_TWLPG0100/01/?' + escape(event.href) + '&&' 
      + 'pi=9570&di=13021' + '&cm=' + searchMode + '&ce=' + e + '&hl=' + queryString;
   //  alert(url);  

   i = new Image();
   i.src = url;
}



function trackSearch(url, searchMode, queryString, e) {

   track_url='http://g.msn.com/_TWLPG0100/01?' + url + '&&' 
      + 'pi=9570&di=13021' + '&cm=' + searchMode + '&ce=' + e + '&hl=' + queryString;
   //  alert(track_url);  

   i = new Image();
   i.src = track_url;
}

//=== Begin - 開啟 activity 相關函式 ===

var g_activityAppId = '10281681';                 
var g_cookieSizeLimit = 3800;    //4096 - (給act, lat, lon, zoom的大小)

var g_markerInfo = new Array();

var g_isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;

function launchActivityWholeMap()
{
   if(!g_isIE)
   {
      alert("請使用Internet Explorer瀏覽器開啟此頁面，再點選該連結");  
      return;
   }

   if(checkCookiePriv() == false)
      return;

   var curPos = mapviewer.getCurrentPosition();
   var lat = curPos.lat;
   var lon = curPos.lon;
   var zoom = mapviewer.getZoomFactor();

   var markers = '';
   for(i = 0; i < g_markerInfo.length; ++i)
   {
      var marker = g_markerInfo[i];
      var markerProps = '';          //marker properties 
      var isFirstProp = true;
      for(prop in marker)
      {
         if(isFirstProp)
         {
            markerProps += prop + ':' + marker[prop];
            isFirstProp = false;
         }
         else
         {
            markerProps += ',' +prop + ':' + marker[prop];
         }
      }

      if(markers.length + markerProps.length >= g_cookieSizeLimit)
      {
         alert("商家資訊超過cookie大小的上限，故僅能擷取部分商家資訊於互動瀏覽");
         break;
      }
      else
      {
         if(i != 0)
            markers += '||';

         markers += markerProps;
      }
   }

   cleanActivityLongCookie();

   //set cookie
   document.cookie = 'act=draw_whole_map;expires=' + cookieTimeOut() + ';path=/';
   document.cookie = 'lat=' + lat + ';expires=' + cookieTimeOut() + ';path=/';
   document.cookie = 'lon=' + lon + ';expires=' + cookieTimeOut() + ';path=/';
   document.cookie = 'zoom=' + zoom + ';expires=' + cookieTimeOut() + ';path=/';
   document.cookie = 'markers=' + markers + ';expires=' + cookieTimeOut() + ';path=/';

   var obj = createMessenger();
   if(obj == null)
   {
      //TODO change message. msn version or IE version.
      alert("請使用Internet Explorer瀏覽器開啟此頁面，再點選該連結");
      return;
   }
   obj.LaunchApp(g_activityAppId, '');
}

function launchActivityOneBiz(lat, lon, name, addr, tel)
{
   if(!g_isIE)
   {
      alert("請使用Internet Explorer瀏覽器開啟此頁面，再點選該連結");  
      return;
   }

   if(checkCookiePriv() == false)
      return;

   cleanActivityLongCookie();

   //set cookie
   document.cookie = 'act=draw_one_biz;expires=' + cookieTimeOut() + ';path=/';
   document.cookie = 'lat=' + lat + ';expires=' + cookieTimeOut() + ';path=/';
   document.cookie = 'lon=' + lon + ';expires=' + cookieTimeOut() + ';path=/';
   document.cookie = 'name=' + name + ';expires=' + cookieTimeOut() + ';path=/';
   document.cookie = 'addr=' + addr + ';expires=' + cookieTimeOut() + ';path=/';
   document.cookie = 'tel=' + tel + ';expires=' + cookieTimeOut() + ';path=/';

   var obj = createMessenger();
   if(obj == null)
   {
      //TODO change message. msn version or IE version.
      alert("請使用Internet Explorer瀏覽器開啟此頁面，再點選該連結");
      return;
   }
   obj.LaunchApp(g_activityAppId, '');
}

function cleanActivityLongCookie()
{
   var past;
   past = new Date();
   past.setFullYear('1970');

   document.cookie = 'markers=0' + ';expires=' + past.toGMTString() + ';path=/';
   document.cookie = 'name=0' + ';expires=' + past.toGMTString() + ';path=/';
   document.cookie = 'addr=0' + ';expires=' + past.toGMTString() + ';path=/';
}


function checkCookiePriv()
{
   if(!navigator.cookieEnabled)
   {
      //TODO msg更完整
      var msg = "您的瀏覽器未開啟Cookie，請確認開啟後再點選此連結。" 
         alert(msg);
      return false;
   }
   else
   {
      return true;
   }

}

function createMessenger()
{
   var obj = null;
   try {
      obj =  new ActiveXObject( 'MSNMessenger.P4QuickLaunch');
   }
   catch(e) {
      obj = null;
   }

   return obj;
}


function cookieTimeOut()
{
   var now;
   now = new Date();
   now.setTime( now.getTime() + 1000 * 60 * 30);

   return now.toGMTString();
}

//=== End - 開啟 activity 相關函式 ===


//---------------------- Wallace Hsu Share Map -----------------------//START

function cmGetX (obj)
{
   var x = 0;

   do
   {
      //alert(obj.offsetLeft);
      x += obj.offsetLeft;
      obj = obj.offsetParent;
   }
   while (obj);
   return x;
}//end of function cmGetX (obj)

function cmGetY (obj)
{
   var y = 0;
   do
   {
      y += obj.offsetTop;
      obj = obj.offsetParent;
   }
   while (obj);
   return y;
}//end of function cmGetY (obj)

var debug_mode = false;
//---------------- debug ------------------------------//START
if(debug_mode)
{
   debug_aa = window.open("about:blank",'debug');
   debug_aa.focus();
}//end of if(debug_mode)
//---------------- debug ------------------------------//END

function showShareMenu()
{
   var menuObj = document.getElementById("share_menu");
   menuObj.style.visibility = "visible";

   var iconObj = document.getElementById("share_icon");

   //alert(cmGetX(iconObj));
   menuObj.style.left = cmGetX(iconObj);
   menuObj.style.top = cmGetY(iconObj) + iconObj.height + 5;	
}//end of function showShareMenu()

//function mouseCheck(evt)
function mouseCheck(e)
{
   var mouse_x;
   var mouse_y;

   if (!e) 
      var e = window.event;

   if (e.pageX || e.pageY)
   {
      mouse_x = e.pageX;
      mouse_y = e.pageY;
   }
   else if (e.clientX || e.clientY)
   {
      mouse_x = e.clientX + document.body.scrollLeft;
      mouse_y = e.clientY + document.body.scrollTop;
   }

   /*
      if(document.all)
      {
      mouse_x = event.x;
      mouse_y = event.y;

      }//end of if(document.all)
      else
      {
      mouse_x = evt.pageX;
      mouse_y = evt.pageY;
      }//end of if(document.all) else

    */

   if(document.getElementById("share_icon"))
   {
      check_x_left = cmGetX(document.getElementById("share_icon"))
   }//end of if(document.getElementById("share_icon"))
   else
   {
      return;
   }//end of //end of if(document.getElementById("share_icon"))
   if(document.getElementById("print_icon"))
   {
      check_x_right = cmGetX(document.getElementById("print_icon"))
   }//end of if(document.getElementById("print_icon"))
   else
   {
      return;
   }//end of //end of if(document.getElementById("print_icon"))

   check_y_top = cmGetY(document.getElementById("share_icon"));
   check_y_bottom = cmGetY(document.getElementById("share_icon")) + 80;
   if(mouse_x > check_x_right)
   {
      document.getElementById("share_menu").style.visibility = "hidden";
   }
   if(mouse_x < check_x_left)
   {
      document.getElementById("share_menu").style.visibility = "hidden";
   }
   if(mouse_y > check_y_bottom)
   {
      document.getElementById("share_menu").style.visibility = "hidden";
   }
   if(mouse_y < check_y_top)
   {
      document.getElementById("share_menu").style.visibility = "hidden";
   }

   /* 
      var msg = 'mouse_x: ' + mouse_x +
      ' mouse_y: ' + mouse_y + 
      ' check_x_right: ' + check_x_right +
      ' check_x_left: ' + check_x_left +
      ' check_y_top: ' + check_y_top +
      ' check_y_bottom: ' + check_y_bottom;
      window.status = msg;
    */

}//end of function mouseCheck(evt)
document.onmousemove=mouseCheck;
//---------------------- Wallace Hsu Share Map -----------------------//END




/*============================================================================
  XMLHTTP JS Library, 2006, by Howard Ruan-Lu, Openfind Inc.,

  this library an easy way to handle xmlhttp connections.

  $Author: oris $
  $Date: 2006/05/05 10:01:08 $
  $Id: xmlhttp.js,v 1.11 2006/05/05 10:01:08 oris Exp $
  ============================================================================*/

/* initial xmlhttp connection object (cross-platform) */

function XMLHTTPInit()
{
   var xmlhttp = false;

   try
   {
      /* new IE */
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   }
   catch(e)
   {
      try
      {
         /* old IE */
         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(E)
      {
         /* reset for non-IE */
         xmlhttp = false;
      }
   }
   if (!xmlhttp && typeof XMLHttpRequest!='undefined')
   {
      /* non-IE */
      xmlhttp = new XMLHttpRequest();
   }

   return xmlhttp;
}


/* open a synchronized connection and get data. the 'postData' parameter
   is optional, if call want to use 'GET' method to open a http request.
   in another word, the function will use "POST" method to open a http
   request, if the 'postData' parameter is not null value.
 */

function XMLHTTPSyncResult(xmlhttp, url, postData)
{
   if(null != postData)
   {
      xmlhttp.open("POST", url, false);
      xmlhttp.setRequestHeader("Content-Type", 
            "application/x-www-form-urlencoded; charset=UTF-8");
      xmlhttp.send(postData);
   }
   else
   {
      xmlhttp.open("GET", url, false);
      xmlhttp.send(null);
   }

   if(null == xmlhttp.status)
      return null;
   else if(200 != xmlhttp.status)
      return "";
   else
      return xmlhttp.responseText;
}

/* open a asynchronized connection and get data. A call-back function must
   be assigned to use this function. when a connection is return
   (by evet trigger), the call-back function will be executed.    
   the real function interface must be "func(result, param)"
   if the xmlhttp respond 200 OK, the 'result' will be the result data,
   else the 'result' will be null. the 'param' is the user assign data,
   it can be any variable type, and will always be assigned to the 
   call-back function, no matter xmlhttp respond 200 OK or not.  
 */
function XMLHTTPAsyncResult(xmlhttp, url, func, param, postData)
{
   /* bind event on ready status change (use a inner function object) */   
   xmlhttp.onreadystatechange = 
      function( )
      {
         if(xmlhttp.readyState != 4)
            return false;

         /* if the respond status is 200 OK, then call the call-back, 
            pass the result data, and the user assigned parameter 
            else, pass null instead of the resultdata */
         /* diatango, 070725, use try-cache to fix NS_ERROR_NOT_AVAILABLE for FF */
         try
         {
            if(null == xmlhttp.status)
               func(null, param);
            else if(xmlhttp.status != 200)
               func("", param);
            else
               func(xmlhttp.responseText, param);
         }
         catch(err)
         {            
            return false;
         }

         return true;
      }

   if(null != postData)
   {
      xmlhttp.open("POST", url, true);
      xmlhttp.setRequestHeader("Content-Type", 
            "application/x-www-form-urlencoded; charset=UTF-8");
      xmlhttp.send(postData);
   }
   else
   {
      xmlhttp.open("GET", url, true);
      xmlhttp.send(null);
   }

   return true;
}

/* escape '%', '&', '+', '=', and '?' in the value string */
function XMLHTTPEscPostValue(value)
{
   var re = new RegExp();
   var arr = null;
   var escStr = "";

   re.compile("([^%&\+=\?]*)([%&\+=\?])([^%&\+=\?]*)", "g");  

   while((arr = re.exec(value)) != null)
   {
      for(var i = 1; i < arr.length; i++)
      {
         switch(arr[i])
         {
            case "":
               break;
            case "%":
               escStr += "%25";
            break;
            case "&":
               escStr += "%26";
            break;
            case "+":
               escStr += "%2b";
            break;
            case "=":
               escStr += "%3d";
            break;
            case "?":
               escStr += "%3f";
            break;
            default:
            escStr += arr[i];
            break;
         }
      }
   }
   if(escStr == "")
      escStr = value;
   return escStr;
}
