function ShowFields(id,int1,idHide,int2){
for (i = 0; i < int1; i++){
	var idnum = document.getElementById(id + i);
	idnum.style.display="";
}
for (i = 0; i < int2; i++){
	var idnum = document.getElementById(idHide + i);
	idnum.style.display="none";
}



if (id == 'pneumatic'){
		for (i = 0; i < 3; i++){
			var idnum = document.getElementById('small' + i);
			idnum.style.display="none";
		}
		for (i = 0; i < 2; i++){
			var idnum = document.getElementById('large' + i);
			idnum.style.display="none";
		}
	}

if (id == 'electric' && document.forms[1].elecType(0).checked == true){
		for (i = 0; i < 3; i++){
			var idnum = document.getElementById('small' + i);
			idnum.style.display="";
		}
	}
if (id == 'electric' && document.forms[1].elecType(1).checked == true){
		for (i = 0; i < 2; i++){
			var idnum = document.getElementById('large' + i);
			idnum.style.display="";
		}
	}
}

function Show(selId,showId){
	if (document.getElementById(selId).selectedIndex == 1)
	{
		var idnum = document.getElementById(showId);
		idnum.style.display="";
	} else {
		var idnum = document.getElementById(showId);
		idnum.style.display="none";
	}
}

function checkPositioner(form,clickedId){
	if (clickedId == 'Potentiometer' && form.Potentiometer.checked == true|| clickedId == 'Brake' && form.Brake.checked == true){
		form.Positioner.checked = false;
	} 
	if (clickedId == 'Positioner' && form.Positioner.checked == true){
		form.Potentiometer.checked = false;
		form.Brake.checked = false;
	} 
}

function checkModulation(form,clickedId){
	if (clickedId == 'ControlType' && form.ControlType[0].checked == true){
		form.ModulationControl.checked = false;
	}
	if (clickedId == 'ModulationControl' && form.ModulationControl.checked == true){
		form.ControlType[0].checked = false;
		form.ControlType[1].checked = true;
	}
}		
			
function QuoteFormChecker(form) {
    var errors='';
    var fieldFocus='';
	var formPass = false;
    //check the simple basic stuff first.
    //there is probobly a more glamorous way to do all this,  but I kept it simple since this is for single use. 
    if (form.Name.value == '') {
        errors += 'Name is required field.\n';
		fieldFocus = form.Name;
    }
    if (form.City.value == '') {
        errors += 'City is required field.\n';
		fieldFocus = form.City;
    }
    if (form.State.value == '') {
        errors += 'State is required field.\n';
		fieldFocus = form.State;
    }
	
    //make sure one of these is checked 
    if (form.email.value == '' && form.Phone.value == '' && form.Fax.value == '') {
        errors += 'You must enter either an e-mail address, phone number or fax number.\n';
		if (fieldFocus == '') {
			fieldFocus ='form.email';
    	}
    }
    
    //if email address is entered ensure it is valid
    if (form.email.value != '') {
		
        //var rx=new RegExp(/[a-z]+([\._\-]?[a-z0-9]+)*@([a-z0-9])+(-[a-z0-9])*(\.([a-z0-9]){2,4}){1}(\.([a-z0-9]){2})?/i);
        //if (!rx.test(form.email.value)) {
        //    errors += 'Your e-mail address is not in the proper format.\n';
		//	  if (fieldFocus == '') {
		//		fieldFocus ='form.email';
		//	}
			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.email.value) && r2.test(form.email.value);
  			if (!isOK) {
  				errors += 'Your e-mail address is not in the proper format.\n';
  			}
    }
	
	if (form.Media.value == '') {
        errors += 'Media is a required field.\n';
		if (fieldFocus == '') {
			fieldFocus = form.Media;
    	}
    }
    if (form.Pressure.value == '') {
        errors += 'Pressure is a required field.\n';
		if (fieldFocus == '') {
			fieldFocus ='form.Pressure';
    	}
    }
    if (form.Temperature.value == '') {
        errors += 'Temperature is a required field.\n';
		if (fieldFocus == '') {
			fieldFocus ='form.Temperature';
    	}
    }
    

    if (!form.ManualValveOnly[0].checked &&!form.ManualValveOnly[1].checked) {
        errors += 'You must specify if this is a manual valve.\n';
		if (fieldFocus == '') {
			fieldFocus ='form.ManualValveOnly[0]';
    	}
    }    
	
    //make sure one of these radio buttons is selected. for each group 
    if (!form.Brand[0].checked && !form.Brand[1].checked) {
        errors += 'You must select a brand.\n';
		if (fieldFocus == '') {
			fieldFocus ='form.Brand[0]';
    	}
    }
	

    //now it starts to get confusing
    //Check required fields if brand is FNW Valve
    if (form.Brand[0].checked) {
        if (form.Quantity.value == '') {
            errors += 'Quantity is a required field.\n';
			if (fieldFocus == '') {
				fieldFocus ='form.Quantity';
			}
        }
        if (form.Size.value == '') {
            errors += 'Size is a required field.\n';
			if (fieldFocus == '') {
				fieldFocus ='form.Size';
			}
        }
    }
	
	
	if (form.ManualValveOnly[1].checked &&!form.Acuation[0].checked && !form.Acuation[1].checked) {
        errors += 'You must select an actuation type.\n';
		if (fieldFocus == '') {
			fieldFocus ='form.Acuation[0]';
    	}
    }
 
 


    //Check required fields if brand is other than FNW Valve
    if (form.Brand[1].checked) {
        if (form.Quantity.value == '') {
            errors += 'Quantity is a required field.\n';
			if (fieldFocus == '') {
				fieldFocus ='form.Quantity';
			}
        }
        if (form.Manufacturer.value == '') {
            errors += 'Manufacturer is a required field.\n';
			if (fieldFocus == '') {
				fieldFocus ='form.Manufacturer';
			}
        }
        if (form.ModelNo.value == '') {
            errors += 'Model Number is a required field.\n';
			if (fieldFocus == '') {
				fieldFocus ='form.ModelNo';
			}
        }
        if (form.Size.value == '') {
            errors += 'Size is a required field.\n';
			if (fieldFocus == '') {
				fieldFocus ='form.Size';
			}
        }
    }


    //Check required fields if Actuation type is Pnumatic
    if (form.Acuation[0].checked) {
        if (!form.actionType[0].checked && !form.actionType[1].checked) {
            errors += 'You must select an actuation action type.\n';
			if (fieldFocus == '') {
				fieldFocus ='form.actionType[0]';
			}
        }
        if (form.MinSupplyPressure.value == '') {
            errors += 'Minimum supply pressure is a required field.\n';
			if (fieldFocus == '') {
				fieldFocus ='form.MinSupplyPressure';
			}
        }
    }


    //Finally, check required fields if Actuation type is Electric (this one has the most nested if's)
    if (form.Acuation[1].checked) {
        if (!form.actionType[0].checked && !form.actionType[1].checked) {
            errors += 'You must select an actuation action type.\n';
			if (fieldFocus == '') {
				fieldFocus ='form.actionType[0]';
			}
        }
        if (form.SupplyVoltage.value == '') {
            errors += 'Supply voltage is a required field.\n';
			if (fieldFocus == '') {
				fieldFocus ='form.SupplyVoltage';
			}
        }
        
        //check for electrical type
        if (!form.elecType[0].checked && !form.elecType[1].checked) {
            errors += 'You must select an electrical type.\n';
			if (fieldFocus == '') {
				fieldFocus ='form.elecType[0]';
			}
        }
    }
    //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);
        // set focus
		//fieldFocus.focus();
		return false;
	} 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");
  			// Remove uneccessary white space around operators, braces and parentheces.
  			//fieldValue = fieldValue.replace(/\s([\x21\x25\x26\x28\x29\x2a\x2b\x2c\x2d\x2f\x3a\x3b\x3c\x3d\x3e\x3f\x5b\x5d\x5c\x7b\x7c\x7d\x7e])/g, "$1");
  			//fieldValue = fieldValue.replace(/([\x21\x25\x26\x28\x29\x2a\x2b\x2c\x2d\x2f\x3a\x3b\x3c\x3d\x3e\x3f\x5b\x5d\x5c\x7b\x7c\x7d\x7e])\s/g, "$1");
			form.elements[i].value = fieldValue;
		}
		return true;
    }
}
