//--------------------------------------------------------------------------------------------
// Author:       David Boddy
// Date:         March 2010
// Description:  Form Validation (Client Side) Javascript
//--------------------------------------------------------------------------------------------

var vstyle = '';  
var RE_Email = /^(\w+[\-\.])*\w+@(\w+\.)+[A-Za-z]+$/;

// Filter out invalid characters that may have been pasted into field
function filterChars(strValue) {
    var intLoop;
    var strFiltered = '';
	var strValidChars = 'abcdefghijklmnopqrstuvwxyz 0123456789-+,.?@#$%&*_/:' + 
	    String.fromCharCode(13) + String.fromCharCode(39) + String.fromCharCode(34);
		// 13 = return, 39 and 34 = single and double quotes
    for (intLoop = 0; intLoop < strValue.length; intLoop++) {
		var strChar = strValue.charAt(intLoop);
        if (strValidChars.indexOf(strChar.toLowerCase()) != -1) strFiltered += strChar;
    }

    // Multiple single quotes filtered
    while (strFiltered.indexOf("''") > 0) {
        strFiltered = strFiltered.replace("''","'");
    }
	// alert('Was ' + strValue + '\nNow ' + strFiltered );  
    return strFiltered;
}

function checkname(fld){
 fld.value = filterChars(fld.value);
 if (fld.value.length < 1) { vstyle = 'inline'; }
 else { vstyle = 'none'; }
document.getElementById('msgname').style.display = vstyle; 
}

function checkemail(fld){
 fld.value = filterChars(fld.value);
 if (!RE_Email.test(fld.value)) { vstyle = 'inline'; }
 else { vstyle = 'none'; }
document.getElementById('msgemail').style.display = vstyle;  
}

function confirmemail(fld){
 fld.value = filterChars(fld.value);
 email = SendForm.Email.value;
 if (fld.value!=email) { vstyle = 'inline'; }
 else { vstyle = 'none'; }
 
document.getElementById('msgconemail').style.display = vstyle;  
}

function checkmsg(fld){
 fld.value = filterChars(fld.value);
 if (fld.value.length < 1) { vstyle = 'inline'; }
 else { vstyle = 'none'; }
document.getElementById('msgmessage').style.display = vstyle; 
}

function validate(form){
		
 var errors = [];
 if (form.Name.value.length<1) {
  errors[errors.length] = "your name is required";
  document.getElementById('msgname').style.display = 'inline';   
 }
 if (!RE_Email.test(form.Email.value)) {
  errors[errors.length] = "a valid email address is required";
  document.getElementById('msgemail').style.display = 'inline';   
  }
  if (form.ConfirmEmail.value!=form.Email.value) {
  errors[errors.length] = "email addresses are different";
  document.getElementById('msgemail').style.display = 'inline';   
  }  
 if (form.Message.value.length<1) {
  errors[errors.length] = "a message is required";
  document.getElementById('msgmessage').style.display = 'inline';   
  }
 if (errors.length > 0) {
  ShowErrors(errors);
  document.getElementById('Name').focus();
  // document.getElementById('Name').select();  
  return false;
 }

 	SubScribe();
	
	PauseComp(1000);	
	
	return true;
    //return false;

}

function PauseComp(millis) 
{
 	var date = new Date();
 	var curDate = null;
 
	do { curDate = new Date(); } 
	while(curDate-date < millis);
 } 

function SubScribe(){	
	//alert(document.getElementById('Name').value);
	//alert(document.getElementById('Email').value);	

	document.getElementById('mc-embedded-subscribe-form').FNAME.value = document.getElementById('Name').value;
	document.getElementById('mc-embedded-subscribe-form').EMAIL.value = document.getElementById('Email').value;
	document.getElementById('subscribe').click();	
}

function ShowErrors(errors){
 var msg = "The contact form can not be sent with the following errors:\n";
 for (var i = 0; i<errors.length; i++) {
  var numError = i + 1;
  msg += "\n" + numError + ".    " + errors[i];
 }
 alert(msg);
}


