window.focus();

function lTrim(str) {
    var whitespace = new String(" \t\n\r");
    var s = new String(str);
    if (whitespace.indexOf(s.charAt(0)) != -1) {
        var j=0, i = s.length;
        while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
        j++;
        s = s.substring(j, i);
    }
    return s;
}
function rTrim(str) {
    var whitespace = new String(" \t\n\r");
    var s = new String(str);
    if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
        var i = s.length - 1;
        while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
        i--;
        s = s.substring(0, i+1);
    }
    return s;
}
function trim(str) {
    return rTrim(lTrim(str));
}
function validEmail(em) {
    //var objRegExp  = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
    //var objRegExp = /^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
    var objRegExp = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    if(objRegExp.test(trim(em.value))) { return true; }
    else { return false; }
    if(!(em.value.indexOf("dodgeit.com",0)==-1) || !(em.value.indexOf("mailinator.com",0)==-1)){ isEmail = false; }
    return isEmail;
}
function validEmail2(str) {
  var re1 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/
  var re2 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/
  if (str.search(re1) != 0) { return false; }
  else if (str.search(re2) > -1) { return false; }
  else { return true; }
}
function validPhone(str) {
	var validChars = '0123456789';
	var str2 = '';
	for (var i = 0; i < str.length; i++) {
		if (validChars.indexOf(str.charAt(i)) != -1) {
			str2 = str2 + str.charAt(i);
		}
	}
	var ph;
	if(str2.length == 10) { // 10 numbers
		ph = true;
		str2 = '1' + str2; // add the 1 infront
	}
	else if (str2.length == 11	&& str2.charAt(0) == 1) { // 11 numbers starting with a 1
		ph = true;
	} else { // bad phone
		ph = false;
	}
	if (ph == true) { // make (xxx) xxx-xxxx
		var str3 = '(' + str2.substr(1,3) + ') ' + str2.substr(4,3) + '-' + str2.substr(7,4);
		document.contactForm.phone.value = str3;
		return true;
	} else { return false; }
}
function testForm2() {
    var nameEr = 'Please enter your name.';
    var emEr = 'Please enter a valid email.';
    var comEr = 'Please enter your questions/comments.';
    var salEr = 'So that we can get back to you.';
    var testFlag = 0; /* set the flag if something has not be completed */
    var testEr = ''; /* variable combination of registration error messages */
	
    if (trim(document.testimonialForm.Name.value) == '') {
        testFlag++;
        testEr = testEr + testFlag + '. ' + nameEr + '\n';
    }
    if (!validEmail(document.testimonialForm.Email)) {
        testFlag++;
        testEr = testEr + testFlag + '. ' + emEr + '\n';
    }
    if (trim(document.testimonialForm.Comments.value) == '') {
        testFlag++;
        testEr = testEr + testFlag + '. ' + comEr + '\n';
    }
    if (testFlag > 0) {
        testEr = testEr + '\n' + salEr;
        alert(testEr);
        return false;
    } else {
        document.testimonialForm.submit();
    }
}
function testForm() {
    var firstEr = 'Please enter your first name.';
    var lastEr = 'Please enter your last name.';
    var emEr = 'Please enter a valid email.';
    var compEr = 'Please enter your company name.';
    var phEr = 'Please enter a valid phone.';
    var callEr = 'Please select the best time to call you.';
    var comEr = 'Please enter your questions/comments.';
    var salEr = 'So that we can get back to you.';
    var conFlag = 0; /* set the flag if something has not be completed */
    var conEr = ''; /* variable combination of registration error messages */
    if (trim(document.contactForm.first.value) == '') {
        conFlag++;
        conEr = conEr + conFlag + '. ' + firstEr + '\n';
    }
    if (trim(document.contactForm.last.value) == '') {
        conFlag++;
        conEr = conEr + conFlag + '. ' + lastEr + '\n';
    }
    if (!validEmail(document.contactForm.email)) {
        conFlag++;
        conEr = conEr + conFlag + '. ' + emEr + '\n';
    }
    if (trim(document.contactForm.company.value) == '') {
        conFlag++;
        conEr = conEr + conFlag + '. ' + compEr + '\n';
    }
    if (!validPhone(trim(document.contactForm.phone.value))) {
        conFlag++;
        conEr = conEr + conFlag + '. ' + phEr + '\n';
    }
    if (trim(document.contactForm.calltime.value) == '--') {
        conFlag++;
        conEr = conEr + conFlag + '. ' + callEr + '\n';
    }
    if (trim(document.contactForm.comments.value) == '') {
        conFlag++;
        conEr = conEr + conFlag + '. ' + comEr + '\n';
    }
    if (conFlag > 0) {
        conEr = conEr + '\n' + salEr;
        alert(conEr);
        return false;
    } else {
        document.contactForm.submit();
    }
}