// Next func tries to duplicate Listrak valid

// email check. Allows most legal forms but not all. for Join as Organization

function checkEmail(theForm){



  var ret = checkAddress();

  if (ret == false)
    return false;





  var errors = "Please complete the following required fields:";

  var numerrors = 0;



  var comp = document.getElementById("company_name").value;



  if (comp == ""){

    errors += "\n\t- Company Name";

    numerrors++;

  }



  var fname = document.getElementById("first-name").value;



  if (fname == ""){

    errors += "\n\t- First Name";

    numerrors++;

  }



  var lname = document.getElementById("last-name").value;



  if (lname == ""){

    errors += "\n\t- Last Name";

    numerrors++;

  }



  var title = document.getElementById("title").value;



  if (title == ""){

    errors += "\n\t- Title";

    numerrors++;

  }



  var addr1 = document.getElementById("address1").value;



  if (addr1 == ""){

    errors += "\n\t- Address1";

    numerrors++;

  }



  var city = document.getElementById("city").value;



  if (city == ""){

    errors += "\n\t- City";

    numerrors++;

  }



  var zip = document.getElementById("zip").value;



  if (zip == ""){

    errors += "\n\t- Zip Code";

    numerrors++;

  }



//   var phone = document.getElementById("phone").value;



//   if (phone == ""){

//     errors += "\n\t- Telephone Number";

//     numerrors++;

//   }







  if (numerrors != 0){

    alert(errors);

    return false;

  }







  return true;

}







/////////////////////////////

// Form check for Join as an Individual

/////////////////////////////



function checkEmailInd(theForm){



   var ret = checkAddress();

   if (ret == false)
     return false;







  var errors = "Please complete the following required fields:";

  var numerrors = 0;



    var fname = document.getElementById("first-name").value;



  if (fname == ""){

    errors += "\n\t- First Name";

    numerrors++;

  }



  var lname = document.getElementById("last-name").value;



  if (lname == ""){

    errors += "\n\t- Last Name";

    numerrors++;

  }





  var zip = document.getElementById("zip").value;



  if (zip == ""){

    errors += "\n\t- Zip Code";

    numerrors++;

  }



  if (numerrors != 0){

    alert(errors);

    return false;

  }







  return true;

}



/////////////////////////////

// Form check for Join as an Individual Style 1

/////////////////////////////



function checkEmailIndNat(theForm){




  var ret = checkAddress();

  if (ret == false)
    return false;



  var errors = "Please complete the following required fields:";

  var numerrors = 0;



    var fname = document.getElementById("first-name").value;



  if (fname == ""){

    errors += "\n\t- First Name";

    numerrors++;

  }



  var lname = document.getElementById("last-name").value;



  if (lname == ""){

    errors += "\n\t- Last Name";

    numerrors++;

  }





  var zip = document.getElementById("zip").value;

  if (zip == ""){
    errors += "\n\t- Zip Code";
    numerrors++;
  }


//   var phone = document.getElementById("phone").value;

//   if (phone == ""){
//     errors += "\n\t- Telephone Number";
//     numerrors++;
//   }



  var state = document.getElementById("state").value;

  if (state == ""){

    errors += "\n\t- State";

    numerrors++;

  }



  if (numerrors != 0){

    alert(errors);

    return false;

  }







  return true;

}






function checkAddress(){
  var estr = document.getElementById("email").value;

  var i = 0;

  var j = 0;

  var dots = new Array();



  if (estr == ""){

     alert("Email address required.");

     return false;

  }



	// Quick check to make sure we have at least 1 @ and at least 1 .

	// Also make sure the positions are plausable and there is only one @.

  var at = estr.indexOf('@');

  var at2 = estr.lastIndexOf('@');

  var dot = estr.lastIndexOf('.');

  var len = estr.length;

  var diff = len - dot;

  









  if (at < 1 || diff < 3 || dot < at || at != at2){

    alert("Please check your email address.\nIt appears to be invalid.");

    return false;

  }





	// check for consecutive dots

  while (i < len){

    if (estr[i] == '.'){

	dots[j] = i;

	j++;

    }

    i++;

  }  



  for (i = 0; i < j; i++){

    if (i != 0){

      if (dots[i] - dots[i-1] < 2){

         alert("Invalid email address.\nAt least one occurance of ..");

	 return false;

      }

    }

  }







  var parts = estr.split("@");



	// see if local section starts or ends with .

  if (parts[0].indexOf(".") != -1){

    var dot_local = parts[0].indexOf('.');

    var dot2_local = parts[0].lastIndexOf('.');

    var lenLocal = parts[0].length;





    if (dot_local == 0 || (lenLocal - dot2_local < 2)){

	alert("Invalid email address.\nLocal section can not start or end with .");

	return false;

    }



  }





	// check for legal chars in local section

  var localRegExp = /[^a-z\d!#$%&'*+-/=?^_`{|}~]/i;

  var ret = localRegExp.test(parts[0]);



    if (ret){

       alert("There is an illegal character in the local section of your email address.");

       return false;

    }





  // domain section

  

  // check for legal length

  if (parts[1].length > 253){

     alert("Please fix the domain part. Length is too long.");

     return false;

  }



    // domain part can't start with . It would come immediatly after @

  if (parts[1][0] == "."){

     alert("There is a . right after the @\nThat is not legal in an email address.");

     return false;

  }



  var domParts = parts[1].split(".");



  var domainRegExp = /[^a-z\d-]/i;



    // final checks for legality in domain part

  for (i = 0; i < domParts.length; i++){

         // check max length of parts

     if (domParts[i].length > 63){

        alert("Please check the domain part.\nMax length exceeded.");

        return false;

     }



     if (domParts[i].length < 2){

        alert("Please check the domain part.\nMinimum length error.");

        return false;

     }



       // check for legal characters

    var ret = domainRegExp.test(domParts[i]);

     if (ret){

        alert("There is an illegal character in the domain section of your email address.");

        return false;

     }



     // domain part can't start with -

    if (domParts[i][0] == "-"){

       alert("There is a - right after a . or @ in the domain part.\nPlease correct this.");

       return false;

    }





  }

  return true;
}

