﻿NS4 = (document.layers) ? true : false;

function checkEnter(event, version) {
  var code = 0;
  if (NS4)
      code = event.which;
  else
      code = event.keyCode;
      
  if (code == 13)
    return submitSearch(version);
}

function submitSearch(version) {
  var search_query = document.Form1.search_query_temp.value;
  
  if (document.Form1.search_query_temp.value == "") {
    if (document.Form1.search_language.value == "en") {
      alert("Please input the keywords for searching");
    }
    if (document.Form1.search_language.value == "big5") {
      alert("關鍵字必須填寫");
    }
    if (document.Form1.search_language.value == "gb") {
      alert("关键字必须填写");
    }
     
    document.Form1.action = "";
    return;
  }
  else
  if ((search_query.length < 2) &&
      (document.Form1.search_language.value == "big5")) {
    alert("請輸入最少兩個字");
     
    document.Form1.action = "";
    return;
  }
  else
  if ((search_query.length < 2) &&
      (document.Form1.search_language.value == "gb")) {
    alert("请输入最少两个字");
     
    document.Form1.action = "";
    return;
  }
  else {
    if (version == "text") {
      webPath = "text";
    }
    else {
      webPath = "web";
    }      
  
    addOperator(document.Form1);
    //document.Form1.action = "/search_cyberport/" + webPath + "/" + document.Form1.search_language.value + "/search_result.jsp";
    document.Form1.action = "/search_cyberport/" + webPath + "/" + document.Form1.search_language.value + "/result.jsp";
    document.Form1.submit();
  }
}

function replace(origStr, findStr, replaceStr) {
// Replaces findStr with replaceStr in origStr
    var origStrLength = origStr.length;
    var findStrLength = findStr.length;
    
    if ((origStrLength == 0) || (findStrLength == 0)) return origStr;
    if (findStr == replaceStr) return origStr;
    
    var i = origStr.indexOf(findStr);
    if ((!i) && (findStr != origStr.substring(0, findStrLength))) return origStr;
    if (i == -1) return origStr;
    
    var newStr = origStr.substring(0,i) + replaceStr;
    
    if (i+findStrLength < origStrLength)
      newStr += replace(origStr.substring(i+findStrLength,origStrLength), findStr, replaceStr);

    return newStr;
}

function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
}

function Trim(s) {
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
  {
    s = s.substring(1,s.length);
  }

  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
  {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function getRadioValue(theRadioField) {
  var count=0;
  for (var i =0;i<theRadioField.length;i++)
  {
    if (theRadioField[i].checked=="1")
    {
      return theRadioField[i].value;
    }
  }
  return false;
}

function addOperator(theForm) {
  var search_field = Trim(theForm.search_query_temp.value);
  var operator_field = "";
  
  if (getRadioValue(theForm.operator) == 'AND') {
    operator_field = " AND ";
  }
  else 
  if (getRadioValue(theForm.operator) == 'OR') {
    operator_field = " OR ";
  }
  else
  if (theForm.operator.value == 'AND') {
    operator_field = " AND ";
  }
  else 
  if (theForm.operator.value == 'OR') {
    operator_field = " OR ";
  }
  else {
    alert("Unknown operator");
  }
  
  if (search_field == "") {
    alert("Please input the keywords for searching");
    return false;
  }
  
  search_field = replaceSubstring(search_field, "  ", " ");
  search_field = replace(search_field, " ", operator_field);
  theForm.search_query.value = search_field;

  //("[\\s]+")
  //var theString = search_field.replace(/^\s*/, '').replace(/\s*$/, '');
  //alert(theString.replace(/([\\s]+)/," "));
  //return false;
  return true;
}
