﻿

// ------------------------------------------------------------------------------------

// Requires that the SNLibrary function: initializeSNLibrary function is called prior to
// initializeForm
// Requires that profileFormText.js be included before this file.

var oldMSIE = false;

function initializeForm () {

	if ($.browser.msie && $.browser.version.substr(0, 1) <= '7')
		oldMSIE = true;

	loadMachineData(); 						//	in formMachines.js
	//	$("#country").load("/database/countries.html");
	$("#country").html(countryOptions());

	// Hide all the machineType elements and then show the first one.
	
	$(".machineType").hide ();
	$("#machineType1").show ();
	
	// Hide the dynamic header labels
	$(".dynamicLabel").hide ();

	// 	Remove machineBrand, machinePower and machineYears sibling Element nodes.
	//	Each name is followed by a number.

	$(".machineBrand").remove();
	$(".machineController").remove();
	$(".machineFeature").remove();

}

// ------------------------------------------------------------------------------------

function validate(form) {

	var errorText;
	var error = false;

	//	Clear all errors from from the previous submission
	
	$('.errorText').remove();
	
	transferFeatures ()

//	features = '';
//	if (form.bevel1.checked)
//		features = features + form.bevel1.value;
//	if (form.drill1.checked)
//		features = features + "; " + form.drill1.value;
//	$('#hiddenFeature1').attr('value', features);
	
  	if (form.firstName.value == "") {
		$('<p class="errorText">' + noFirstName + '<p>').insertAfter('#firstName');
		if (!error)
    		form.firstName.focus();
    	error = true;
  	}
 
   	if (form.lastName.value == "") {
		$('<p class="errorText">' + noLastName + '<p>').insertAfter('#lastName');
		if (!error)
    		form.lastName.focus();
    	error = true;
  	}
 
 	if (form.organization.value == "") {
 		$('<p class="errorText">' + noOrganization + '<p>').insertAfter('#organization');
   		if (!error)
    		form.organization.focus();
    	error = true;
  	}
 
	if (form.country.options[form.country.selectedIndex].text == "Select a country") {
		$('<p class="errorText">' + noCountry + '<p>').insertAfter('#country');
   		if (!error)
    		form.country.focus();
    	error = true;
  	}

  	if (form.email.value == "" && form.workPhone.value == "") {
		$('<p class="errorText">' + noEmail + '<p>').insertAfter('#email');
		if (!error)
    		form.email.focus();
    	error = true;
  	}
 	
	if (form.reference.options[form.reference.selectedIndex].text == "Select an item") {
 		$('<p class="errorText">' + noReference + '<p>').insertAfter('#reference');
		if (!error)
    		form.reference.focus();
    	error = true;
  	}

	if (form.human.options[form.human.selectedIndex].text != humanTestValue) {
 		$('<p class="errorText">' + noCaptcha + '<p>').insertAfter('#human');
		if (!error)
    		form.reference.focus();
    	error = true;
  	}

	if (error)
		return (false);
	else
		return (true);
}


// ----------------------------------------------------------------------------------------
//	Transfer the feature checkbox values to the hidden fields so they will transfer
//	back to the server

function transferFeatures () {

	var features;
	var firstFeature = true;

	features = '';
	$('#machineFeature1').children('input').each (function (i) {
		if (this.checked) {
			if (firstFeature) {
				features = this.value;
				firstFeature = false;
			} else {
				features = features + ", " + this.value;
			}
		}
	});
	$('#hiddenFeature1').attr('value', features);
	
	features = '';
	firstFeature = true;
	$('#machineFeature2').children('input').each (function (i) {
		if (this.checked) {
			if (firstFeature) {
				features = this.value;
				firstFeature = false;
			} else {
				features = features + ", " + this.value;
			}
		}
	});
	$('#hiddenFeature2').attr('value', features);
	
	features = '';
	firstFeature = true;
	$('#machineFeature3').children('input').each (function (i) {
		if (this.checked) {
			if (firstFeature) {
				features = this.value;
				firstFeature = false;
			} else {
				features = features + ", " + this.value;
			}
		}
	});
	$('#hiddenFeature3').attr('value', features);
	
	features = '';
	firstFeature = true;
	$('#machineFeature4').children('input').each (function (i) {
		if (this.checked) {
			if (firstFeature) {
				features = this.value;
				firstFeature = false;
			} else {
				features = features + ", " + this.value;
			}
		}
	});
	$('#hiddenFeature4').attr('value', features);
	
	features = '';
	firstFeature = true;
	$('#machineFeature5').children('input').each (function (i) {
		if (this.checked) {
			if (firstFeature) {
				features = this.value;
				firstFeature = false;
			} else {
				features = features + ", " + this.value;
			}
		}
	});
	$('#hiddenFeature5').attr('value', features);
	
	features = '';
	firstFeature = true;
	$('#machineFeature6').children('input').each (function (i) {
		if (this.checked) {
			if (firstFeature) {
				features = this.value;
				firstFeature = false;
			} else {
				features = features + ", " + this.value;
			}
		}
	});
	$('#hiddenFeature6').attr('value', features);
	
}