/*validation for contact.html by LMA 11/09 */

/*  The JavaScript Source!! http://javascript.internet.com
can click on text associated with button to select that button  */
function changeBox(cbox) {
box = eval(cbox);
box.checked = !box.checked;
}

//removes  white spaces from fields onblur
function trim(str)
{
  return compressWhiteSpace(str);
}
function compressWhiteSpace(s) {
  // Condense white space.
  s = s.replace(/\s+/g, "");
  s = s.replace(/^\s(.*)/, "$1");
  s = s.replace(/(.*)\s$/, "$1");
  return s;
}


/* format phone on blur */
//phone and fax number formatting
function doThis(nums){

var re= /\D/;
// test for this format: xxx-xxxx
//var re2 = /^\d{3}-\d{4}/; 
// test for this format: (xxx)xxx-xxxx
var re2 = /^\({1}\d{3}\)\s\d{3}-\d{4}/; 
// test for this format: xxx-xxx-xxxx
//var re2 = /^\d{3}-\d{3}-\d{4}/;

for (i=0; i<nums.length;i++){
var num=eval(nums+'.value');

var newNum;
 if (num != "" && re2.test(num)!=true){
   if (num != ""){
     while (re.test(num)){
     num = num.replace(re,"");
	}
   }

  if (num.length != 10){
    alert('Please enter a 10 digit number');
    eval(nums).select();
    break;
    }
   else {
	   // for format  xxx-xxxx
    // newNum = num.substring(0,3) + '-' + num.substring(3,7);
     // for format (xxx) xxx-xxxx
     newNum = '(' + num.substring(0,3) + ')' + ' ' + num.substring(3,6) + '-' + num.substring(6,10);
     // for format xxx-xxx-xxxx
     // newNum = num.substring(0,3) + '-' + num.substring(3,6) + '-' + num.substring(6,10);
     eval(nums).value=newNum;
     }
   }
  }
}



/* validate form */
function QuoteFormChecker(form) {
    var errors='';
    var fieldFocus='';
	var formPass = false;
    //name required
    if (form.name.value == '') {
        errors += 'Your Name is required.\n';
    }
	
	//if zip code is entered ensure it is valid
	if (form.zip.value != '') {
		var rx=new RegExp("\\d{5}");
		if (!rx.test(form.zip.value)) {
			errors += 'Your Zip must be 5 digits.\n';
		}
	}
	if (form.emailAddress.value == '') {
        errors += 'Email address is required.\n';
    }
    
		//if email address is entered ensure it is valid
		if (form.emailAddress.value != '') {
				var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
				var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
				var isOK = !r1.test(form.emailAddress.value) && r2.test(form.emailAddress.value);
				if (!isOK) {
					errors += 'Your e-mail address is not in the proper format.\n';
				}
		}
	
//if Phone is selected as preferred Contact method, then Phone field is required
    if (form.contact_by[1].checked==true && form.phone.value == '') {
        errors += 'Phone Number is required (since you want to be contacted by phone)\n';
    }
	//if phone is entered ensure it is valid
		if (form.phone.value != '') {
			var rx=new RegExp("^\\(\\d{3}\\)\\s\\d{3}-\\d{4}");
			if (!rx.test(form.phone.value)) {
				errors += 'Phone number must be formatted (###) ###-####.\n';
			}
		}
			
	//if fax is entered ensure it is valid
			if (form.fax.value != '') {
				var fx=new RegExp("^\\(\\d{3}\\)\\s\\d{3}-\\d{4}");
				if (!fx.test(form.fax.value)) {
					errors += 'Fax number must be formatted (###) ###-####.\n';
				}
			}


    //return failures if it failed validation
    if (errors != ''){
        alert('The required information is incomplete or contains errors:\t\t\t\t\t\n\n'+errors);
		return false;
		
		//strip out extra white space
	} else {
		 
		for (var i=0;i<form.length;i++) {
			fieldValue = form.elements[i].value;
  			fieldValue = fieldValue.replace(/\s+/g, " ");
  			fieldValue = fieldValue.replace(/^\s(.*)/, "$1");
  			fieldValue = fieldValue.replace(/(.*)\s$/, "$1");
			form.elements[i].value = fieldValue;
		}
		return true;
    }
}