// Preload images
var empty = new Image(); empty.src = "/graphics/forms/required_field_star.gif";

var haveerrors = 0;
function showImage(imagename, imageurl, errors) {
document[imagename].src = imageurl;
if (!haveerrors && errors) haveerrors = errors;
}

var submitted = false;
function SubmitTheForm() {
  if(submitted == true) { return; }
  document.getElementById('submit').disabled = true;
  document.getElementById('submitButton').innerHTML = '<span class="body_light">Please wait...</span>';
  submitted = true;
}

function trim(str) {
  return str.replace(/^\s+|\s+$/g,'')
}

function validateForm(f) {
haveerrors = 0;

(trim(f.first_name.value).length < 1) // validate first name length
? showImage("firstnameerror", "/graphics/forms/required_field_star.gif", true)   // no semi-colon after this line!
: showImage("firstnameerror", "/graphics/site/spacer.gif", false); // true = errors, false = no errors

(trim(f.last_name.value).length < 1) // validate last name length
? showImage("lastnameerror", "/graphics/forms/required_field_star.gif", true)
: showImage("lastnameerror", "/graphics/site/spacer.gif", false);

(trim(f.title.value).length < 1) // validate job title length
? showImage("titleerror", "/graphics/forms/required_field_star.gif", true)
: showImage("titleerror", "/graphics/site/spacer.gif", false);

(trim(f.company.value).length < 1) // validate company name length
? showImage("companyerror", "/graphics/forms/required_field_star.gif", true)
: showImage("companyerror", "/graphics/site/spacer.gif", false);

(trim(f.phone.value).length < 1 || checkPhone(f.phone.value)==false) // validate phone number
? showImage("phoneerror", "/graphics/forms/required_field_star.gif", true)
: showImage("phoneerror", "/graphics/site/spacer.gif", false);

((trim(f.state.value).length < 1 || f.state.value == 'N/A') && (trim(f.province.value).length < 1 || f.province.value == 'N/A')) // validate state length
? showImage("stateerror", "/graphics/forms/required_field_star.gif", true)
: showImage("stateerror", "/graphics/site/spacer.gif", false);

(trim(f.country.value).length < 1) // validate country length
? showImage("countryerror", "/graphics/forms/required_field_star.gif", true)
: showImage("countryerror", "/graphics/site/spacer.gif", false);

(f.email.value.search("@") == -1 || f.email.value.search("[.*]") == -1 || f.email.value == "(username@hostname.com)")
? showImage("emailerror", "/graphics/forms/required_field_star.gif", true)
: showImage("emailerror", "/graphics/site/spacer.gif", false);


if(!haveerrors) { 
toEntity("first_name", "last_name", "title", "company", "phone", "lead_desc");
SubmitTheForm(); }

return (!haveerrors);
}

function swapField() {
  if (document.forms.akamai_form.country.value=='USA') {
    document.getElementById('statefield').style.display='inline';
    document.getElementById('provincefield').style.display='none';
    document.forms.akamai_form.province.value='N/A';
  }
  else {
    document.getElementById('provincefield').style.display='inline';
    document.getElementById('statefield').style.display='none';
    if (document.forms.akamai_form.province.value=='N/A') {
      document.forms.akamai_form.province.value='';
    }
    document.forms.akamai_form.state.value='N/A';
  }
}

// Ensure Phone Number is all numeric characters

var phoneNumberDelimiters = ".+()- ";
var minDigitsInPhoneNumber = 6;

function isInteger(s) {
  var i;
  for (i = 0; i < s.length; i++) {
    // Check that current character is number.
    var c = s.charAt(i);
    if (((c < "0") || (c > "9"))) return false;
  }
  // All characters are numbers.
  return true;
}

function stripCharsInBag(s, bag) {
  var i;
  var returnString = "";
  // Search through string's characters one by one.
  // If character is not in bag, append to returnString.
  for (i = 0; i < s.length; i++) {
    // Check that current character isn't whitespace.
    var c = s.charAt(i);
    if (bag.indexOf(c) == -1) returnString += c;
  }
  return returnString;
}

function checkPhone(strPhone){
  s=stripCharsInBag(strPhone,phoneNumberDelimiters);
  return (isInteger(s) && s.length >= minDigitsInPhoneNumber);
}

function makeEmpty(id,text){
  if(document.getElementById(id).value==text) {document.getElementById(id).value='';}  
}
function populate(id,text){
  if(document.getElementById(id).value=='') {document.getElementById(id).value=text;}  
}

function setMaxLength() {
	var x = document.getElementsByTagName('textarea');
	var counter = document.createElement('div');
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxlength')) {
			var counterClone = counter.cloneNode(true);
			counterClone.relatedElement = x[i];
			counterClone.innerHTML = '(maximum '+x[i].getAttribute('maxlength')+' characters)';
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);

			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}

function checkMaxLength() {
	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength)
        this.value = this.value.substring(0, maxLength);
}

window.onload = setMaxLength;

