var strErrorSelectInvestorRequest = "";

function isEmptyInvestorRequest(str)
{
  for(var intLoop=0; intLoop<str.length; intLoop++)
    if(str.charAt(intLoop)!=" ")
      return false;
  return true;
}

function setFocusInvestorRequest (value) {
  if (strErrorSelectInvestorRequest == "") {
    strErrorSelectInvestorRequest = value;
  }
}

function validateFormInvestorRequest ()
{
  var f= document.investor_request;
  var strError= "";
  var strErrorSelect = "";
  
  if (isEmptyInvestorRequest(f.name.value)) {
    strError +="\n-- Name";
    setFocusInvestorRequest("name");
  } else if (f.name.value.length > 50) {
    strError +="\n-- Name is too long";
    setFocusInvestorRequest("name");
  }
  if (isEmptyInvestorRequest(f.address1.value)) {
    strError +="\n-- Address";
    setFocusInvestorRequest("address1");
  } else if (f.address1.value.length > 300) {
    strError +="\n-- Address is too long";
    setFocusInvestorRequest("address1");
  }
  if (isEmptyInvestorRequest(f.city.value)) {
    strError +="\n-- City";
    setFocusInvestorRequest("city");
  } else if (f.city.value.length > 30) {
    strError +="\n-- City Name is too long";
    setFocusInvestorRequest("city");
  }
  if (isEmptyInvestorRequest(f.state.value)) {
    strError +="\n-- State";
    setFocusInvestorRequest("state");
  }
  if (isEmptyInvestorRequest(f.zip.value)) {
    strError +="\n-- Zip";
    setFocusInvestorRequest("zip");
  } else if (!validZipInvestorRequest(f.zip.value)) {
    strError +="\n-- Invalid Zip Code, example 48070-1234 or 48070";
    setFocusInvestorRequest("zip");
  }
  if (isEmptyInvestorRequest(f.email.value)) {
    strError +="\n-- Email";
    setFocusInvestorRequest("email");
  } else if (!validEmailInvestorRequest(f.email.value)) {
    strError +="\n-- Invalid Email Address";
    setFocusInvestorRequest("email");
  }
  if (isEmptyInvestorRequest(f.company.value)) {
    strError +="\n-- Company or Organization";
    setFocusInvestorRequest("company");
  } else if (f.company.value.length > 30) {
    strError +="\n-- Company or Organization Name is too long";
    setFocusInvestorRequest("company");
  }
  if (f.question.value.length > 65535) {
    strError +="\n-- Comments and Questions is too long";
    setFocusInvestorRequest("question");
  }
  
  strErrorSelect = strErrorSelectInvestorRequest;

//display error message
  if(strError !="") {
  alert("The following required data is missing or not valid:\n" + strError);
  if (strErrorSelect != "") {
      if (document.investor_request[strErrorSelect]) {
          document.investor_request[strErrorSelect].focus();
      }
  }
  strErrorSelectInvestorRequest = "";
  return false;
  } else {
  return true;
  }
}

function validEmailInvestorRequest(emailS) {
  if (emailS.length < 70 && emailS.indexOf("@") > 0 && emailS.indexOf(".") != -1 && emailS.lastIndexOf(".") < (emailS.length-2) && emailS.lastIndexOf(".")>0 && (emailS.lastIndexOf(".") - emailS.lastIndexOf("@") > 1) ) {
    return true;
  } else {
    return false;
  }  
}

    function validZipInvestorRequest(input_zip) {
      var stored_zip = input_zip;
      for (var j=0; j<stored_zip.length; j++) {
        var char_curr = stored_zip.charAt(j);
        var char_code = char_curr.charCodeAt(0);
        if (((char_code > 47 && char_code < 58) || (char_curr == "-")) && (stored_zip.length < 11 && stored_zip.length > 4)) {
          //use default true
        }
        else {
          return false;
        }
      }
      return true;
    }

function nextFieldInvestorRequest(as_next)
{
  document.investor_request[as_next].focus();
}

function formSubmitInvestorRequest() {
  if (validateFormInvestorRequest()) {
    document.investor_request.submit();
  }
}

function formResetInvestorRequest() {
  for (i=0; i<document.investor_request.elements.length; i++) {
    if ((document.investor_request.elements[i].type.toLowerCase() != "submit") && (document.investor_request.elements[i].type.toLowerCase() != "button") && (document.investor_request.elements[i].type.toLowerCase() != "reset") && (document.investor_request.elements[i].type.toLowerCase() != "hidden") && (document.investor_request.elements[i].id != "C0D3_js2")) {
      document.investor_request.elements[i].value = "";
      document.getElementById("C0D3").value = "";
      document.getElementById("C0D3_feedback").innerHTML = ""; 
    }
  }
}

function newC0D3InvestorRequest() {
  document.getElementById("C0D3_new").value = "NEW CODE";
  document.getElementById("C0D3_feedback").innerHTML = "";
  document.investor_request.submit();
}


