<!-- Javascript Document -->

// form validation function //
function validate(form) {
  var form;
  form = document.forms["mainForm"];

  // set form variables
  var name = form.first_name.value;
  var last_name = form.last_name.value;
  var email = form.email.value;
  var phone = form.phone.value;
  var company = form.company.value;
  var pw1 = form.password.value;
  var pw1_length = form.password.value.length;
  var pw2 = form.password_confirm.value;
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var pw1RegEx = /\W+/; var pw1RegEx2 = /\d+/;
  var agrees_to_terms = form.agrees_to_terms;
  
  // validate form
  if(name == "") {
    inlineMsg('first_name','Please enter your name.',2);
    return false;
  }  
  if(last_name == "") {
    inlineMsg('last_name','<strong>Error</strong><br />You must enter your last name.',2);
    return false;
  }
  if(email == "") {
    inlineMsg('email','<strong>Error</strong><br />You must enter your email.',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email.',2);
    return false;
  }
  if(phone == "") {
    inlineMsg('phone','<strong>Error</strong><br />You must enter a phone number.',2);
    return false;
  }
  if(company == "") {
    inlineMsg('company','You must enter your company name.');
    return false;
  } 
  if(pw1 == "") {
    inlineMsg('password','You must enter a password.');
    return false;
  }  
  if(pw2 == "") {
    inlineMsg('password_confirm','You must enter a password.');
    return false;
  }  
  if(pw1 != pw2) {
    inlineMsg('password','Passwords do not match');
    return false;
  }
  if ( !(pw1_length >= 7) || !(pw1_length <= 25)  ) {	
     inlineMsg('password','Password must be 7-25 characters');
	 return false;
  }
  if( !pw1.match(pw1RegEx) && !pw1.match(pw1RegEx2) ) {
    inlineMsg('password','<strong>Error</strong><br />Invalid password.',2);
    return false;
  } 
   if(!agrees_to_terms.checked) {
    inlineMsg('agrees_to_terms','<strong>Error</strong><br />Please agree to the terms.');
    return false;
  } 
   //if all valid, submit form to salesforce then to mysite	
   submitMySite();
   return true;
}

function submitMySite (){
	
	// change value of checkbox since Salesforce uses the opposite values
	if(document.forms['mainForm'].emailOptOut.checked == false) {
		// send SalesForce a value that is 1 = did opt out of emai		
		document.forms['mainForm'].emailOptOut.value = '1';
		document.forms['mainForm'].emailOptOut.checked = true;	
  	}
	// submit to salesforce
	document.forms["mainForm"].action = "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8";document.forms["mainForm"].method = "post";document.forms["mainForm"].submit();
    		
	var mimeoSignUpForm = document.forms['mimeoSignUpForm'];
	var mainForm = document.forms['mainForm'];
	
	// assign current form values (mainForm)
	// to the hidden form (mimeoSignUpForm) that will be submitted to mysite
	mimeoSignUpForm.FirstName.value = mainForm.first_name.value;
	mimeoSignUpForm.LastName.value = mainForm.last_name.value;
	mimeoSignUpForm.Email.value = mainForm.email.value;
	mimeoSignUpForm.MarketingContactNumber.value = mainForm.phone.value;
	mimeoSignUpForm.MarketingCompanyName.value = mainForm.company.value;
	mimeoSignUpForm.MimeoPassword.value = mainForm.password.value;
	mimeoSignUpForm.MimeoConfirmPassword.value = mainForm.password_confirm.value;
	if (mainForm.emailOptOut.value == '0') {
		mimeoSignUpForm.WantsUpdates.checked = true; 
	}
	if (mainForm.emailOptOut.value == '1') {
		mimeoSignUpForm.WantsUpdates.checked = false;
	}
	mimeoSignUpForm.AgreesToTerms.checked = true;
	
	//submit to mysite	
	document.forms['mimeoSignUpForm'].submit();
	return false;	
}



// START OF MESSAGE VALIDATION SCRIPT //
var MSGTIMER = 5;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 2000));
}
// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}
// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 200);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 199) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}
// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}
// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = "../images/msg_arrow.gif"; 
}
