/*validation for FEIonline_form.shtml by LMA 8/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;
}

/*  if text entered in text field associated button is selected auto-magically
MUST put in the correct number - remember to start count from 0 not 1 */
function selectMe() {
	if (document.regForm.hearAboutOther.value != '') {
	document.regForm.hearAbout[6].checked=true;
	}
	if (document.regForm.businessTypeOther.value != '') {
	document.regForm.businessType[12].checked=true;
	}
}

/* The JavaScript Source!! http://javascript.internet.com Created by: Cyanide_7 |  
auto tab phone number*/
var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {
  var keyCode = (isNN) ? e.which : e.keyCode; 
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  if(input.value.length >= len && !containsElement(filter,keyCode)) {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
  }

  function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
    if(arr[index] == ele)
    found = true;
    else
    index++;
    return found;
  }

  function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
    if (input.form[i] == input)index = i;
    else i++;
    return index;
  }
  return true;
}

/* 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 = /^\(\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;
    //check the simple basic stuff first.
    if (form.name.value == '') {
        errors += 'Your First Name is required.\n';
		if (fieldFocus == '') {
			fieldFocus = 'form.name';
		}
    }
	 if (form.lastName.value == '') {
        errors += 'Your Last Name is required.\n';
		
    }
	if (form.companyName.value == '') {
        errors += 'Company Name is required.\n';
		
    }
	if (form.companyAddress.value == '') {
        errors += 'Company Address is required.\n';
		
    }
    if (form.city.value == '') {
        errors += 'City is required.\n';
		
    }
	//make sure state is not left on "Select One.."
	 if (form.state.value == 'Select') {
            errors += 'Your State is required.\n';
			
        }
    if (form.zipCode.value == '') {
        errors += 'Zip is required (5 digit).\n';
		
    }
		//if zip code is entered ensure it is valid
		if (form.zipCode.value != '') {
			var rx=new RegExp("\\d{5}");
			if (!rx.test(form.zipCode.value)) {
				errors += 'Your Zip Code 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 (form.phoneNumber.value == '') {
        errors += 'Phone Number is required.\n';
		
    }
		//if phone is entered ensure it is valid
			if (form.phoneNumber.value != '') {
				var zx=new RegExp("^\\(\\d{3}\\)\\s\\d{3}\\-\\d{4}$");
				if (!zx.test(form.phoneNumber.value)) {
					errors += 'Your phone number must be formatted (###) ###-####.\n';
					 
				} 
			}
   //make sure one of these check boxes is selected
    if (document.regForm.businessType) {
		var checkList = 0;
		  for (i = 0; i < document.regForm.businessType.length; i++) {
			 if (document.regForm.businessType[i].checked) {
				if ((document.regForm.businessType[12].checked==true) && (document.regForm.businessTypeOther.value == "")) {
					errors += 'Please fill in a value for Other -- Type of Business.\n';
					checkList = 12;
					break;
				}
				
			  checkList++;
			 }
		  }
		 if (checkList == 0) {
			 errors += 'At least 1 Business Type is required.\n';
		 }		  
   }
if (form.businessType[12].checked==false) {
					form.businessTypeOther.value = "";
				}

    //make sure one of these radio buttons is selected
	if (document.regForm.hearAbout) {
		var hearAboutList = 0;
		for (i = 0; i < document.regForm.hearAbout.length; i++) {
			if (document.regForm.hearAbout[i].checked) {
				if (document.regForm.hearAbout[6].checked==false) {
					document.regForm.hearAboutOther.value="";
				}
				if ((document.regForm.hearAbout[6].checked==true) && (document.regForm.hearAboutOther.value == "")) {
					errors += 'Please fill in a value for Other -- How your heard about Ferguson Online.\n';
					hearAboutList = 6;
					break;
				}
				hearAboutList++;
			}
		 }
		 
		 if (hearAboutList == 0) {
			 errors += 'How your heard about Ferguson Online is required.\n';
		 }		  
	}
	
 //make sure one of these radio buttons is selected
    if (!form.feiCust[0].checked && !form.feiCust[1].checked) {
        errors += 'Do you have a Ferguson account?\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 {
		if (form.feiCust[0].checked) {
			form.success_html.value="http://www.ferguson.com/FEIonline_form_Register_success.shtml";
		} else {
			form.success_html.value="http://www.ferguson.com/FEIonline_form_success.shtml";
			}
		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;
    }
}