/* begin: fundamental functions */
function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?"));
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if ( 
          aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return strReturn;
}
/* end: fundamental functions */

/* begin: swap image functions */
function swapImage(thisImg, srcURL){
  thisImg.src = srcURL;
}
/* end: swap image functions */

/* begin: init functions*/
function initFaq(){
  var ui_lang = getURLParam('ui_lang');
  if (ui_lang!='zh-hk' && ui_lang!='zh-cnhk'){
      ui_lang='zh-hk';
  }
  if (ui_lang == 'zh-cnhk'){
    var v = ['topNav', 'topNavLang', 'breadcrumbLink'];
    var tc = new Array();
    var sc = new Array();
    for (var i = 0; i < v.length; i++){
      tc[i] = document.getElementById(v[i]+'TcBlock');
      sc[i] = document.getElementById(v[i]+'ScBlock');
      tc[i].setAttribute('class', 'hide');
      tc[i].setAttribute('className', 'hide');
      sc[i].setAttribute('class', 'show');
      sc[i].setAttribute('className', 'show');
    }
  }
}

function initUILang(){
  var outForm = document.adv_search;
  with(outForm){
    ui_lang.value = getURLParam('ui_lang');
    if (ui_lang.value!='zh-hk' && ui_lang.value!='zh-cnhk' && ui_lang.value!='en'){
      ui_lang.value='zh-hk';
    }
    /*
      when url contains http://sc.info.gov.hk/TuniS/
      ui_lang should be regarded as 'zh-cn'
    */
    if (window.location.href.match('/schs/')){
      ui_lang.value='zh-cnhk';
    }
    if (ui_lang.value == 'zh-cnhk'){
      var v = ['topNav', 'topNavLang', 'searchBtnTop', 'searchBtnBtm', 'faqLink'];
      var tc = new Array();
      var sc = new Array();
      for (var i = 0; i < v.length; i++){
        tc[i] = document.getElementById(v[i]+'TcBlock');
        sc[i] = document.getElementById(v[i]+'ScBlock');
        tc[i].setAttribute('class', 'hide');
        tc[i].setAttribute('className', 'hide');
        sc[i].setAttribute('class', 'show');
        sc[i].setAttribute('className', 'show');
      }
    }
  }
}
function initQuery(thisForm){
  with(thisForm){
    query.value = decodeURIComponent(getURLParam('init_query'));
    exact_q.value = decodeURIComponent(getURLParam('init_exact_q'));
    any_q.value = decodeURIComponent(getURLParam('init_any_q'));
    none_q.value = decodeURIComponent(getURLParam('init_none_q'));
  }
}
function initWebsite(thisForm){
  with (thisForm){
    var x = decodeURIComponent(getURLParam('gp1'));
    if (x){
      var v = new Array();
      v = x.split(',');
      wb_all.checked = 1;
      var rsd_cnt = 0;
      for (var i = 0; i < v.length; i++){
        var flag = false;
        for (var j = 1; j < wb_within_v.length; j++){              
          if (v[i] == wb_within_v[j].value){
            wb_within_v[j].checked = 1;
            wb_within.checked = 1;
            flag = true;
            break;
          }
        }
        if (flag){
          continue;
        }
        for (var j = 0; j < wb_within_rsd_v.length; j++){
          if (v[i] == wb_within_rsd_v[j].value){
            wb_within_rsd_v[j].checked = 1;
            wb_within.checked = 1;
            flag = true;
            rsd_cnt++;
            break;
          }
        }
        if (flag){
          continue;
        }
        for (var j = 0; j < wb_dept_list.options.length; j++){
          if (v[i] == wb_dept_list.options[j].value){
            wb_dept_list.options[j].selected = 1;
            wb_spec.checked = 1;
            break;
          }
        }
      }
      if (rsd_cnt == wb_within_rsd_v.length){
        wb_within_v[0].checked = 1;
      }
    }
  }
}
function initLang(thisForm){
  with(thisForm){
    var x = getURLParam('r_lang');
    if (x=="en" || x=="ch"){
      lga_any.checked = 0;
      lga_other.checked = 1;
      if (x == "en"){
        lga_en.checked = 1;
      }
      if (x == "ch"){
        lga_ch.checked = 1;
      }
    }
  }
}
//function initDate(thisForm){
//  with(thisForm){
//    var x = decodeURIComponent(getURLParam('last_mod'));
//    if (x){
//      if (x.charAt(0) == '#'){
//        if (x.charAt(1) == '-'){
//          date_any.checked = 1;
//        }else{
//          date_within.checked = 1;
//          for (var i = 0; i < date_last.options.length; i++){
//            if (x == date_last.options[i].value){
//              date_last.options[i].selected = 1;
//              break;
//            }
//          }
//        }
//      }else{
//        date_fromto.checked = 1;
//        var y = '';
//        /* start date year */
//        y = x.substr(0, 4);
//        for (var i = 1; i < s_date_year.options.length; i++){
//          if (y == s_date_year.options[i].value){
//            s_date_year.options[i].selected = 1;
//            break;
//          }
//        }
//        /* start date month */
//        y = x.substr(4, 2);
//        for (var i = 1; i < s_date_month.options.length; i++){
//          if (y == s_date_month.options[i].value){
//            s_date_month.options[i].selected = 1;
//            break;
//          }
//        }
//        /* start date day */
//        y =  x.substr(6, 2);
//        for (var i = 1; i < s_date_day.options.length; i++){
//          if (y == s_date_day.options[i].value){
//            s_date_day.options[i].selected = 1;
//            break;
//          }
//        }
//        /* end date year */
//        y = x.substr(9, 4);
//        for (var i = 1; i < e_date_year.options.length; i++){
//          if (y == e_date_year.options[i].value){
//            e_date_year.options[i].selected = 1;
//            break;
//          }
//        }
//        /* end date month */
//        y = x.substr(13, 2);
//        for (var i = 1; i < e_date_month.options.length; i++){
//          if (y == e_date_month.options[i].value){
//            e_date_month.options[i].selected = 1;
//            break;
//          }
//        }
//        /* start date day */
//        y =  x.substr(15, 2);
//        for (var i = 1; i < e_date_day.options.length; i++){
//          if (y == e_date_day.options[i].value){
//            e_date_day.options[i].selected = 1;
//            break;
//          }
//        }
//      }
//    }
//  }
//}
function initFileFormat(thisForm){
  with(thisForm){
    var x = getURLParam('doc_type');
    if (x){
      for (var i = 0; i < file_type.options.length; i++){                  			
        if (x == file_type.options[i].value){
          file_type.options[i].selected = 1;
          break;
        }
      }
    }
  }
}
function initDisplay(thisForm){
  with(thisForm){
    x = getURLParam('p_size');
    if (x){
      for (var i = 0; i < page_size.options.length; i++){
        if (x == page_size.options[i].value){
          page_size.options[i].selected = 1;
          break;
        }
      }
    }
  }
}
function initForm(){
  var frontForm = document.adv_search;
  initQuery(frontForm);
  var backForm = document.adv_search_backend;
  initFileFormat(backForm);
  initWebsite(backForm);
  initLang(backForm);
//  initDate(backForm);
  initDisplay(backForm);
}
function init(){
  initUILang();
  if (getURLParam('is_init') == 'true'){
    initForm();
  }
}
/* end: init */

/* begin: form control */
function selectWbAll(){
  var thisform = document.adv_search_backend;
  with (thisform){
    for (var i = 0; i < wb_section_v.length; i++){
      if (wb_section_v[i].checked){
        wb_section_v[i].checked=0;
      }
    }
  }
}
function selectWbThis(){
  var thisform = document.adv_search_backend;
  with (thisform){
    for (var i = 0; i < wb_section_v.length; i++){
      if (wb_section_v[i].checked){
        wb_section_v[i].checked=0;
      }
    }
  }
}
function selectSomeWbSection(){
  var thisform = document.adv_search_backend;
  with (thisform){
    var all_unchecked = 1;
    for (var i = 0; i < wb_section_v.length; i++){
      if (wb_section_v[i].checked){
        all_unchecked = 0;
      }
    }
    wb_all.checked =0; 
    if (all_unchecked){
      wb_this.checked = 1;
      wb_section.checked = 0;
    }else{
      wb_this.checked = 0;
      wb_section.checked = 1;
    }
  }
}
function selectSomeWbWithin(){
  var thisform = document.adv_search_backend;
  with (thisform){
    wb_all.checked = 0;
    wb_spec.checked =0;
    wb_within.checked = 1;
  }
}
function selectWbWithinRsd(){
  selectSomeWbWithin();
  var thisform = document.adv_search_backend;
  with (thisform){
    for (var i = 0; i < wb_within_rsd_v.length; i++){
      wb_within_rsd_v[i].checked = wb_within_rsd.checked;
    }
  }
}
function selectWbWithinBsn(){
  selectSomeWbWithin();
}
function selectWbWithinNrsd(){
  selectSomeWbWithin();
}
function selectSomeWbWithinRsd(){
  selectSomeWbWithin();
  var thisform = document.adv_search_backend;
  with (thisform){
    for (var i = 0; i < wb_within_rsd_v.length; i++){
      if (!wb_within_rsd_v[i].checked){
        wb_within_rsd.checked = 0;
        return ;
      }
    }
    wb_within_rsd.checked = 1;
  }
}
function selectWbSpec(){
  var thisform = document.adv_search_backend;
  with (thisform){
    wb_all.checked = 0;
    wb_within.checked = 0;
    wb_spec.checked = 1;
  }
}
function selectLgaAny(){
  var thisform = document.adv_search_backend;
  with (thisform){
    lga_en.checked = 0;
    lga_ch.checked = 0;
    lga_other.checked = 0;
  }
}
function selectLgaSome(){
  var thisform = document.adv_search_backend;
  with (thisform){
    lga_other.checked = (lga_en.checked || lga_ch.checked);
    lga_any.checked = !lga_other.checked;
  }
}
//function selectDateWithin(){
//  var thisform = document.adv_search_backend;
//  with (thisform){
//    date_any.checked = 0;
//    date_fromto.checked = 0;
//    date_within.checked = 1;
//  }
//}
//function selectDateFromto(){
//  var thisform = document.adv_search_backend;
//  with (thisform){
//    date_any.checked = 0;
//    date_within.checked = 0;
//    date_fromto.checked = 1;
//  }
//}
/* end: form control */

/* begin: validate form */
function isQueryValid(thisForm){
  with(thisForm){
    if (!(query.value || exact_q.value || any_q.value)){
      return 0;
    }
  }
  return 1;
}
function isLeapYear(year){
  if (year%4){ return 0; }
  if (!(year%100) && year%400){ return 0; }
  return 1;
}
//function isDateValid(thisForm){
//  with(thisForm){
//    if (date_fromto.checked){
//      days = [[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]];
//      var month, year, isLeap, maxDays;
//
//      month = s_date_month.options[s_date_month.selectedIndex].value;
//      year = s_date_year.options[s_date_year.selectedIndex].value;
//      isLeap = isLeapYear(parseInt(year));
//      maxDays = days[isLeap][parseInt(month)-1];
//      while (parseInt(s_date_day.options[s_date_day.selectedIndex].value) > maxDays){
//        s_date_day.options[s_date_day.selectedIndex-1].selected = 1;
//      }
//      var s_date = year+month+s_date_day.options[s_date_day.selectedIndex].value;
//      
//      month = e_date_month.options[e_date_month.selectedIndex].value;
//      year = e_date_year.options[e_date_year.selectedIndex].value;
//      isLeap = isLeapYear(parseInt(year));
//      maxDays = days[isLeap][parseInt(month)-1];
//      if (parseInt(e_date_day.options[e_date_day.selectedIndex].value) > maxDays){
//        e_date_day.options[maxDays].selected = 1;
//      }
//      var e_date = year+month+e_date_day.options[e_date_day.selectedIndex].value;
//      
//      if (s_date > e_date){
//        return 0;
//      }
//    }
//  }
//  return 1;
//}
function validateForm(){  
  var flag = 1;
  var frontForm = document.adv_search;  
  var lang = 0;
  if (frontForm.ui_lang.value == 'en') lang = 0;
  if (frontForm.ui_lang.value == 'zh-hk') lang = 1;
  if (frontForm.ui_lang.value == 'zh-cnhk') lang = 2;

  /* avoid null query */
  if (!isQueryValid(frontForm)){ alert(alertMsg[0][lang]); flag = 0;}
  var endForm = document.adv_search_backend;
  /* avoid start date > end date */
//  if (!isDateValid(endForm)){ alert(alertMsg[1][lang]); flag = 0;}

  return flag;
}
/* end:validate form */

/* begin: submit form */
function trimForm(){

  var outForm = document.adv_search;

  with (outForm){
    /* query related data */
    init_query.value = encodeURIComponent(query.value);
    init_exact_q.value = encodeURIComponent(exact_q.value);
    init_any_q.value = encodeURIComponent(any_q.value);
    init_none_q.value = encodeURIComponent(none_q.value);

    var inForm = document.adv_search_backend;
    with (inForm){
      
      /* doc type related data */
      doc_type.value = file_type.value;
      /* display related data */
      p_size.value = page_size.value;

//      /* trim date related data */
//      if (date_any.checked){
//        /* any time */
//        last_mod.value=date_any.value;
//      }else{
//        if (date_within.checked){
//          /* within the past */
//          last_mod.value = date_last.options[date_last.selectedIndex].value;
//        }else{
//          /* from to */
//          if (date_fromto.checked){
//              last_mod.value = 
//              s_date_year.options[s_date_year.selectedIndex].value +
//              s_date_month.options[s_date_month.selectedIndex].value +
//              s_date_day.options[s_date_day.selectedIndex].value +
//              ',' +
//              e_date_year.options[e_date_year.selectedIndex].value +
//              e_date_month.options[e_date_month.selectedIndex].value +
//              e_date_day.options[e_date_day.selectedIndex].value;
//          }
//        }
//      }
//
      /* trim language related data */
      if (lga_any.checked){
        /* any language */
        r_lang.value = lga_any.value;
      }else{
        if (lga_other.checked){
          if (lga_en.checked && lga_ch.checked){
            r_lang.value = lga_any.value;
          }else{
            if (lga_en.checked){
              r_lang.value = lga_en.value;
            }
            if (lga_ch.checked){
              r_lang.value = lga_ch.value;
            }
          }
        }
      }
      
      /* trim website related data */
      var flag = true;
      if (wb_all.checked){
        /* all websites */
        gp1.value = "0";
        web.value = "all";
      }else{
        gp1.value = "";
        web.value = "this";
        if (wb_this.checked){
          /* this website */
          gp1.value = gp0.value;
        }else{
          if (wb_section.checked){
            /* within some sections */
            for (var i = 0; i < wb_section_v.length; i++){
              if (wb_section_v[i].checked){
                /* select all */
                if (flag){
                  flag = !flag;
                }else{
                  gp1.value += ",";
                }
                gp1.value += wb_section_v[i].value;
              }
            }
          }
        }
      }       
    }
  }
  return true;
}
/* end: submit form */
