var hasCropChanged = false;

function show_srg_div(div_id, anchorName) {
    // hide all the divso
    document.getElementById('advanced').style.display = 'none';
    document.getElementById('advanced_dropdown').style.display = 'none';
    // show the requested div
    document.getElementById(div_id).style.display = 'block';
    if (anchorName != null) {
        location.href = '#' + anchorName;
    }
}

function searching() {
    document.getElementById('searching').style.display = 'block';
}

function updatedCrop(lastCrop, currentCrop) {
      if (validCrop()) {
          if (lastCrop != currentCrop) {
            hasCropChanged = true;
          }
          else if ((lastCrop == currentCrop) && hasCropChanged)
          {
              hasCropChanged = false;
          }
      }
}

function clearTraits(traits) {
    if (hasCropChanged)
    {
        traitAllChecked(traits);
    }
}

function numericOnlyKeypress(e) {
    var unicode = e.charCode ? e.charCode : e.keyCode;

    if (unicode > 26) { // don't mess with control chars
        if (unicode < 48 || unicode > 57) { //if not a number
            return false //disable key press
        }
    }
}

function validateStandardSearch() {
    //zip no longer required
    //zipValid = validZip();
    var errorMessage;
    var zipValid = true;
    var cropValid = validCrop();

    if (!cropValid && !zipValid) {
        errorMessage = "Please select a crop and enter your zip code to find seed products in your area.";
    } else if (!cropValid) {
        errorMessage = "Please select a crop to find seed products in your area.";
    } else if (!zipValid) {
        errorMessage = "Please enter your zip code to find seed products in your area.";
    } else {
        errorMessage = "";
    }
    document.getElementById('error').innerText = errorMessage;

    if (cropValid && zipValid) {
        searching();
        return true;
    } else {
        return false;
    }
}

function validCrop() {
    cropNum = document.getElementById('cropField').selectedIndex
    if (cropNum == 0) {
        return false;
    } else {
        return true;
    }
}

function validZip() {
    var zip = document.getElementById('zipcode').value
    if (zip == null || zip == "") {
        return false;
    } else {
        return true;
    }
}

function clearRMFilterOnSubmit(form) {
    form.action = "seeds?clearrm=true"
}

function findASeed() {
    searching();
    document.getElementById('productId_footer').value = document.getElementById('findASeedProductId').value;
    document.getElementById('FindASeed_footer').submit();
}

function findASeedKeypress(e) {
    var keynum = 0
    var keychar
    var numcheck
    if (window.event) {  // IE
        keynum = e.keyCode
    } else if (e.which) {  // Netscape/Firefox/Opera
        keynum = e.which
    }

    if (keynum == 13) {
        findASeed();
        return false;
    } else {
        return true;
    }
}

function traitChecked(traits) {
    //turn off 'all'
    traits[0].checked = false;
}

function traitAllChecked(traits) {
    //turn off all except 'all'
    for (i = 1; i < traits.length; i++) {
        traits[i].checked = false;
    }
}