var emptySelect = "empty_select";

function showLanguages(){
  var obj = document.getElementById("otherLanguages");
  obj.style.display = "";
}

function hideLanguages(){
  var obj = document.getElementById("otherLanguages");
  obj.style.display = "none";
}

/* Change the language */
function doChangePageLanguage(oldLang, newLang){
  var formObj = document.getElementById('changeLanguageForm');
  var langObj = document.getElementById('doChangeLanguageParam');
  if(langObj != null){
    if(newLang!=null && newLang!="" && newLang.toLowerCase()!=oldLang.toLowerCase()){
      if(newLang.toLowerCase()=='es'){
        window.location="http://www.aegps.es/";
        return;
      }
      if(newLang.toLowerCase()=='lcn'){
        window.location="http://www.aegps.com.cn/";
        return;
      }
      else {
        langObj.value = newLang;
        formObj.action = document.location.pathname.replace("/"+oldLang.toLowerCase()+"/", "/" + newLang.toLowerCase()+ "/");
        formObj.submit();
      }
    }

  }
}

/* Parse the entered query String */
function parseSearchQuery(theForm, message) {
  var queryValue = theForm.elements["query2"].value;
  var testValue = queryValue.replace(/ /g, "");
  if (testValue.length < 3) {
    alert(message);
    return (false);
  }
  theForm.elements["query"].value = queryValue;
  return (true);
}

/* Open the print version popup window */
function openPrintVersion(params) {
  if (document.location.search != "") {
    params = document.location.search + "&" + params;   
  } else {
    params = "?" + params;
  }
  window.open(document.location.pathname + params, "print", "width=850,height=700,dependent=yes,status=no,toolbar=no,location=no,scrollbars=yes");
}

/* CHange the language */
function doChangeLanguage(theSelect, theForm, theLang){
  if(theSelect.value && theSelect.value!="" && theSelect.value.toLowerCase()!=theLang.toLowerCase()){
    theForm.action = document.location.pathname.replace("/" + theLang + "/", "/" + theSelect.value.toLowerCase()+ "/");
    theForm.submit();
  }
}

function doSelectedAction(count, typeExternalLink, typeInternalLink, typeDownload){
  var id = "sidebar_select_" + count;
  var action = document.getElementById(id).value; 
  var selectedIndex = document.getElementById(id).options.selectedIndex;
  var selectedId = document.getElementById(id).options[selectedIndex].id;
  if(selectedId == typeExternalLink){
    eval(action);
  } else if(selectedId == typeInternalLink){
    window.location.href = action;
  } else if(selectedId == typeDownload){
    window.open(action,"","width=850,height=700,dependent=no,status=yes,toolbar=yes,location=yes,menubar=yes,scrollbars=yes");
  }
}

function showImage(url){
  window.open(url,"","menubar=0,resizable=1,width=520,height=520");
};

function showImage(url,w,h){
  var wi;
  var he;
  if (w<300){wi=330;}
  if (w>800){wi=830;}
  else if(w>300 && w<800){wi=w+30;}
  if (h<300){he=330;}
  if (h>600){he=630;}
  else if (h>300 && h<600){he=h+30;}
  window.open(url,'','menubar=0,resizable=1,width='+wi+',height='+he+',location=0,toolbar=0');
};

function doSearch(index, errNoRightLength, errNoDigitsOnly){
  var border = "<div class=\"border_bottom_4px\"><!-- --></div>";
  var zipCode = document.getElementById("zip_code").value;
  if(zipCode.length != 5 && zipCode.length != 4){
    document.getElementById("content_download").innerHTML = border+"<br /><div style=\"margin-left: 10px;\"><b>&nbsp;&nbsp;&nbsp;"+errNoRightLength+"</b></div>";
    return false;
  } else {
    var zipCodeString = zipCode.toString();
    var isValid = 1;
    for(var i=0; i<zipCode.length; i++){
      var c = zipCodeString.charAt(i);
      if(c < '0' || c > '9'){
        isValid = 0;
      }
    }
    if(isValid == 1){
      document.getElementById("result_page").value = index;
      return true;
    } else {
      document.getElementById("content_download").innerHTML = border+"<br /><div style=\"margin-left: 10px;\"><b>&nbsp;&nbsp;&nbsp;"+errNoDigitsOnly+"</b></div>";
      return false;
    }
  }
};

function doDealerSearch(index, errNoRightLength, errNoDigitsOnly){
  var zipCode = document.getElementById("zip_code").value;
  if(zipCode.length != 5){
    alert(errNoRightLength);
    return false;
  } else {
    var zipCodeString = zipCode.toString();
    var isValid = 1;
    for(var i=0; i<zipCode.length; i++){
      var c = zipCodeString.charAt(i);
      if(c < '0' || c > '9'){
        isValid = 0;
      }
    }
    if(isValid == 1){
      document.getElementById("result_page").value = index;
      return true;
    } else {
      alert(errNoDigitsOnly);
      return false;
    }
  }
};

function doGeoSearch(index, errNoRightLength, errNoDigitsOnly){
  var selectedIndex = document.getElementById("country_select").selectedIndex;
  var countrySelect = document.getElementById("country_select")[selectedIndex];
  var country = countrySelect.value;
   
  if(country == "Germany"){
    var zipCode = document.getElementById("zipcode_select").value;
    if(zipCode.length != 5){
      alert(errNoRightLength);
      return false;
    } else {
      var zipCodeString = zipCode.toString();
      var isValid = 1;
      for(var i=0; i<zipCode.length; i++){
        var c = zipCodeString.charAt(i);
        if(c < '0' || c > '9'){
          isValid = 0;
        }
      }
      if(isValid == 1){
        document.getElementById("result_page").value = index;
        return true;
      } else {
        alert(errNoDigitsOnly);
        return false;
      }
    }
  } else {
    document.getElementById("result_page").value = index;
    document.getElementById("zipcode_select").value="";
    return true;
  }
};

function doSearchYear(resultPage,eventYear){
  document.getElementById("result_page").value=resultPage;
  if(eventYear) document.getElementById("event_year").value=eventYear;
  document.getElementById("mainForm").submit();
};

function toggleFAQ(quest){
  var link = quest;
  if(link.className=="link_row"){
    link.className="link_row_open";
  } else {
    link.className="link_row";
  }
  var answ  = quest.parentNode.nextSibling;
  if(answ.className=="display_shown"){
    answ.className="";
  }else{
    answ.className="display_shown";
  }
  quest.blur();
}

function makeSubmit(){
  document.getElementById("mainForm").submit();
}

function onProductCategorySelectChange(chooseOptionText, protectUSVFolderPath, accessoriesFolderPath, compuwatchFolderPath, OPTION_ALL){
  var select_obj = document.getElementById("product_category");
  removeOptions(chooseOptionText);
  var selected_option = select_obj.options[select_obj.selectedIndex].value;
  if(selected_option == protectUSVFolderPath) isProtectUSV();
  else if(selected_option == accessoriesFolderPath) isAccessories();
  else if(selected_option == compuwatchFolderPath) isCompuwatch();
  else if(selected_option == OPTION_ALL) isAllProducts();
};

function onProductCategorySelectChange(chooseOptionText, protectUSVFolderPath, rectifiersChargersFolderPath, 
    modIndPowACDCFolderPath, modIndPowDCDCFolderPath, solarInvertersFolderPath, OPTION_ALL){
  var select_obj = document.getElementById("product_category");
  removeOptions(chooseOptionText);
  var selected_option = select_obj.options[select_obj.selectedIndex].value;
  if(selected_option == protectUSVFolderPath) isProtectUSV();
  else if(selected_option == rectifiersChargersFolderPath) isRectifiersChargers();
  else if(selected_option == modIndPowACDCFolderPath) isModIndPowACDC();
  else if(selected_option == modIndPowDCDCFolderPath) isModIndPowDCDC();
  else if(selected_option == solarInvertersFolderPath) isSolarInverters();
  else if(selected_option == OPTION_ALL) isAllProducts();
};

function removeOptions(chooseOptionText){
  var selectObject = document.getElementById("product_filter");
  var i;
  for(i=selectObject.options.length-1;i>=0;i--) selectObject.remove(i);
  var option = document.createElement("OPTION");
  option.text = chooseOptionText;
  option.value = emptySelect;
  selectObject.options.add(option);
};

function doSearch(resultPage, errorNoSelect){
  var select1 = document.getElementById("product_category");
  var select2 = document.getElementById("product_filter");
  document.getElementById("result_page").value=resultPage;
  var option1 = select1.options[select1.selectedIndex].value;
  var option2 = select2.options[select2.selectedIndex].value;
  if(option1 == emptySelect || option2 == emptySelect){
    alert(errorNoSelect);
  } else {
    document.getElementById("mainForm").submit();
  }
};

function doSearchByLetter(firstChar, lastChar, resultPage) {
  document.getElementById("first_letter").value=firstChar;
  document.getElementById("last_letter").value=lastChar;
  document.getElementById("result_page").value=resultPage;
  document.getElementById("mainForm").submit();
};

function goToPageResult(index, firstLetterParam, lastLetterParam) {
  document.getElementById("first_letter").value = firstLetterParam;
  document.getElementById("last_letter").value = lastLetterParam;
  document.getElementById("result_page").value = index;
  document.getElementById("mainForm").submit();
};

function goToPageResult(index, zipCodeParam){
  document.getElementById("result_page").value = index;
  document.getElementById("zip_code").value = zipCodeParam;
  document.getElementById("mainForm").submit();
};

function goToPageResult(index){
  document.getElementById("result_page").value=index;   
  document.getElementById("mainForm").submit();
};

function goToPageGeoResult(index, zipCodeParam, countryParam){
  document.getElementById("result_page").value=index;
  document.getElementById("zipcode_select").value=zipCodeParam;
  document.getElementById("country_select").value=countryParam;
  document.getElementById("mainForm").submit();
};

function doSubmit(errNoRightLength, errNoDigitsOnly){
  if(doSearch(1, errNoRightLength, errNoDigitsOnly)){
    document.getElementById('mainForm').submit();
  }
}

function doGeoSubmit(errNoRightLength, errNoDigitsOnly){
  if(doGeoSearch(1, errNoRightLength, errNoDigitsOnly)){
    document.getElementById('mainForm').submit();
  }
}

function showZipcodeTr(){
  var selectedIndex = document.getElementById("country_select").selectedIndex;
  var country = document.getElementById("country_select")[selectedIndex].value;
  if(country == "Germany"){
    document.getElementById("zipcodeTr").style.visibility = "visible";
  } else {
    document.getElementById("zipcode_select").value="";
    document.getElementById("zipcodeTr").style.visibility = "hidden";
  }
}

function doSearchCNF(index){
  var code = document.getElementById("cnf_code").value;
  if(isValidCNF(code)){
    document.getElementById("result_page").value = index;
    return true;
  } else {
    return false;
  }
}

function isValidCNF(code){
  if(code.legth != 8){
    alert("length != 8");
    return false;
  }
  var isValid = 1;
  for(var i=0; i<code.length; i++){
    var c = code.charAt(i);
    if(c < '0' || c > '9'){
      isValid = 0;
    }
  }
  if(isValid == 0){
    alert("input must contain only digits");
    return false;
  } else {
    alert("success");
    return false;
  }
}

function doSubmitCNF(){
  if(doSearchCNF(1)){
    document.getElementById('mainForm').submit();
  }
}

function doSearchCNF2(){
  var code = document.getElementById("cnf_code").value;
  if(isValidCNF(code)){
    return true;
  } else {
    return false;
  }
}

function isValidCNF2(code, keyError){
  if(code.length != 8){
    alert(keyError);
    return false;
  }
  return true;
}

function doSubmitCNF2(keyError){
  var code = document.getElementById("cnf_code").value;
  if(isValidCNF2(code, keyError) == true){
    document.getElementById("mainForm").submit();
  }
}

function doFormSubmit(){
  var form = document.getElementById("emailform");
  if(form){
    form.submit();
  }
}

function goUrl(selectUrl) {
url=window.document.selectform.products.options[window.document.selectform.products.selectedIndex].value ;
window.location = url;
}