// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();
var checkAjaxStep = [""];
var http_request = false;
var cache = new Array();
var xml_file = "";
// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
      { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

function makeRequest(func) {
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
    	try{
	        http_request = new XMLHttpRequest();
    	    if (http_request.overrideMimeType) {
        	    http_request.overrideMimeType('text/xml');
        	}
    	} catch (e) {
    		alert("error");
    	}
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e){
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){}
        }
    }
    if (!http_request) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
	var cacheEntry = cache.shift();
    http_request.open("POST", "ajax/" + xml_file + ".php", true);
    http_request.onreadystatechange = func;
    http_request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    http_request.send(cacheEntry);
}
function alertContents()
{
	var xmldoc, root_node;
	if (http_request.readyState == 4)
	{
		if (http_request.status == 200)
		{
			xmldoc = http_request.responseXML;
			root_node = xmldoc.getElementsByTagName('ReturnCode').item(0);
			var n = parseInt(root_node.firstChild.data,10);
			if(n < 500)
			{
				// do some thing...
			}
		}
		else
		{
//			alert("There was a problem with the request.");
			document.location.reload();
		}
	}
}
function ajaxGetAreaByCity()
{
	var xmldoc, root_node;
	if (http_request.readyState == 4)
	{
		if (http_request.status == 200)
		{
			xmldoc = http_request.responseXML;
			root_node = xmldoc.getElementsByTagName('ReturnCode').item(0);
			var _data = root_node.firstChild.data;
			var x = document.searchFrm;
			x.area.options.length = 1;
			x.area.options[i] = new Option("", "不分區域");
			if(_data != "NULL")
			{
				if(_data != "")
				{
					var areaList = new Array;
					areaList = _data.split(",");
					for(var i=1; i<areaList.length; i++)
					{
						x.area.options[i] = new Option(areaList[i], areaList[i]);
					}
				}
			}
		}
		else
		{
//			alert("There was a problem with the request.");
			document.location.reload();
		}
	}
}
function get_area_by_city(city)
{
	cache.push("city="+city);
	xml_file = "getAreaByCity";
	makeRequest(ajaxGetAreaByCity);
}
function get_request()
{
	makeRequest(alertContents);
}
function checkRepeatData(value, rsid, field, tab, dataid)
{
  cache.push("value="+value+"&rsid="+rsid+"&field="+field+"&tab="+tab+"&dataid="+dataid);
  // only continue if xmlHttp isn't void
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    // try to connect to the server
   try
    {	  
	  var cacheEntry = cache.shift();
      // initiate reading the async.txt file from the server
      xmlHttp.open("POST", "ajax/checkRepeatData.php", true);
	  xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      xmlHttp.onreadystatechange = handleRequestStateChange;
      xmlHttp.send(cacheEntry);
    }
    // display the error in case of failure
    catch (e)
    {
//      alert("Can't connect to server:\n" + e.toString());
			document.location.reload();
    }
  }
}
function getComListHandler(str, frmName, fieldName)
{
  cache.push("str="+str+"&frmName="+frmName+"&fieldName="+fieldName);
  // only continue if xmlHttp isn't void
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    // try to connect to the server
   try
    {	  
	  var cacheEntry = cache.shift();
      // initiate reading the async.txt file from the server
      xmlHttp.open("POST", "ajax/getComListHandler.php", true);
	  xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      xmlHttp.onreadystatechange = handleRequestStateChange;
      xmlHttp.send(cacheEntry);
    }
    // display the error in case of failure
    catch (e)
 
    {
//      alert("Can't connect to server:\n" + e.toString());
			document.location.reload();
    }
  }
}
function getNewEventByDate(datestr)
{
  cache.push("datestr="+datestr);
  // only continue if xmlHttp isn't void
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    // try to connect to the server
   try
    {	  
	  var cacheEntry = cache.shift();
      // initiate reading the async.txt file from the server
      xmlHttp.open("POST", "ajax/getNewEventByDate.php", true);
	  xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      xmlHttp.onreadystatechange = handleRequestStateChange;
      xmlHttp.send(cacheEntry);
    }
    // display the error in case of failure
    catch (e)
    {
//      alert("Can't connect to server:\n" + e.toString());
			document.location.reload();
    }
  }
}
function searchEventKeyword(keyword)
{
  cache.push("keyword="+keyword);
  // only continue if xmlHttp isn't void
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    // try to connect to the server
   try
    {	  
	  var cacheEntry = cache.shift();
      // initiate reading the async.txt file from the server
      xmlHttp.open("POST", "ajax/searchEventKeyword.php", true);
	  xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      xmlHttp.onreadystatechange = handleRequestStateChange;
      xmlHttp.send(cacheEntry);
    }
    // display the error in case of failure
    catch (e)
    {
//      alert("Can't connect to server:\n" + e.toString());
			document.location.reload();
    }
  }
}
function coop_click_handler(id)
{
  cache.push("id="+id);
  // only continue if xmlHttp isn't void
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    // try to connect to the server
   try
    {	  
	  var cacheEntry = cache.shift();
      // initiate reading the async.txt file from the server
      xmlHttp.open("POST", "ajax/coop_click_handler.php", true);
	  xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      xmlHttp.onreadystatechange = handleRequestStateChange;
      xmlHttp.send(cacheEntry);
    }
    // display the error in case of failure
    catch (e)
    {
//      alert("Can't connect to server:\n" + e.toString());
		document.location.reload();
    }
  }
}
// function that handles the HTTP response
function handleRequestStateChange()
{
  if (xmlHttp.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
        response = xmlHttp.responseText;
        eval(response);
        if(checkAjaxStep.length > 1)
        {
        	var n = checkAjaxStep.length-1;
        	eval(checkAjaxStep[n]);
        	checkAjaxStep.length = n;
        }
      }
      catch(e)
      {
//        alert("Error reading the response: " + e.toString());
			document.location.reload();
      }
    } 
    else
    {
      // display status message
//      alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
			document.location.reload();
    }
  }
}
function word_encode(str)
{
	var rsStr = "";
	for(var i=0; i<str.length; i++)
	{
		if(str.charCodeAt(i) < 128)
		{
			rsStr += str.substring(i, i+1);
		}
		else
		{
			rsStr += "---" + str.charCodeAt(i) + ";";
		}
	}
	rsStr = rsStr.replaceAll("\n", "(xNL)");
	return rsStr;
}
String.prototype.replaceAll = strReplace;
function strReplace(findText, replaceText)
{
   var str = new String(this);
   while (str.indexOf(findText)!=-1) {
      str = str.replace(findText, replaceText);
   }
   return str;
}

document.writeln("<div id=\"dataFinderDiv\" style=\"z-index:1; position:absolute; border:1px solid #b2b2b2; background-color: #e2e2e2; left:200px; top:200px; width:200px; height:200px; overflow:auto; display:none;\"></div>");
