//******************************
function require_field( thisfield, label ) {
	if( !thisfield.value ) {
		if( !focus ) focus = thisfield;
		msg += "Please enter " + label + "\n";
	}
}

//******************************
function validate_form( thisform ) {
	var validated = validate_items( thisform );
	if (!validated) { return false; }

	// now validate_form Customer Information
	focus = null;
	msg = "";

	require_field( thisform['Name'], 'your Name' );
	require_field( thisform['Company'], 'Company name' );
	require_field( thisform['Address'], 'Street Address' );
	require_field( thisform['City'], 'City' );
	require_field( thisform['State'], 'State' );
	require_field( thisform['PostalCode'], 'Postal/Zip Code' );
	require_field( thisform['Phone'], 'Phone Number' );
	require_field( thisform['Email'], 'Email Address' );

	if( focus != null ) {
		focus.focus();
		alert( "Order Information is incomplete:\n\n" + msg );
		return false;
	}
	else {
		//thisform.submit();
		return true;
	}
}


//******************************
function validate_items( thisform ) {
	var quantity = 0;

	for( var name in Products ) {
		var measurements = Products[name];

		var qel = get_element( thisform, name, 'Quantity' );
		if( qel.value == '?' ) {
			check_quant( thisform, name );
			return false;
		}
		var qty = parseInt( qel.value );
		if( qel.value == '' || qty == 0 )
		{
			qel.value = '';	// simplify filtering
			continue;		// Skip the ones without Quantity
		}
		if( isNaN( qty ) || qty < 0 )
		{
			qel.focus();
			qel.select();
			alert( name + "\n\nPlease correct the Quantity\n\n" + qel.value + " - is not a valid Qualtity!" )
			return false;
		}

		// Item chosen, validate_form that all of the options are properly selected/entered

		quantity += qty;
		focus = null;
		msg = "";

		var finish = get_element( thisform, name, 'Finish');
		if( REQUIRE_ALL && !finish.selectedIndex ) {
			if( !focus ) focus = get_element( thisform, name, 'Finish' );
			msg += "Please select the Finish\n";
		}

		if( REQUIRE_ALL && !get_radiovalue( thisform, name, 'RollCut' ) )
		{
			if( !focus ) focus = get_element( thisform, name, 'RollCut' );
			msg += "Please specify Roll or Cut Threading\n";
		}

		if( REQUIRE_ALL && !get_radiovalue( thisform, name, 'UNCUNF' ) )
		{
			if( !focus ) focus = get_element( thisform, name, 'UNCUNF' );
			msg += "Please specify UNC, UNF, or Custom Threading\n";
		}
		else {
			var other = get_option_OTHER( thisform, name, 'UNCUNF' );
			if( other.checked )
			{
				var val = get_element( thisform, name, 'UNCUNF_Other' ).value;
				if( val == "Other" )
				{
					if( !focus ) focus = get_element( thisform, name, 'UNCUNF_Other' );
					msg += "Please replace the word 'Other' with your Custom Threading option.\n";
				}
				else
					other.value = val;
			}
		}

		if( REQUIRE_ALL && !get_radiovalue( thisform, name, 'LeftRight' ) )
		{
			if( !focus ) focus = get_element( thisform, name, 'Finish' );
			msg += "Please specify Left hand or Right hand Threading\n";
		}

		if( REQUIRE_ALL && !get_radiovalue( thisform, name, 'Material' ) )
		{
			if( !focus ) focus = get_element( thisform, name, 'Material' );
			msg += "Please specify Material\n";
		}
		else {
			var other = get_option_OTHER( thisform, name, 'Material' );
			if( other.checked )
			{
				var val = get_element( thisform, name, 'Material_Other' ).value;
				if( val == "Other" )
				{
					if( !focus ) focus = get_element( thisform, name, 'Material_Other' );
					msg += "Please replace the word 'Other'\nwith your Custom Material option.\n";
				}
				else
					other.value = val;
			}
		}

		if( REQUIRE_ALL && !get_radiovalue( thisform, name, 'Material_Rolled' ) )
		{
			if( !focus ) focus = get_element( thisform, name, 'Material_Rolled' );
			msg += "Please specify Cold Rolled or Hot Rolled Material\n";
		}

		if ( REQUIRE_ALL ) {
			for( var m in measurements ) {
				var e = get_element( thisform, name, "Measurements][" + m );
				if( !e.value )
				{
					if( !focus ) focus = e;
					msg += "Please enter value for Measurement " + measurements[m] + "\n";
				}
			}
		}


		if( focus != null ) {
			//focus.focus();
			alert( name + "\n\n" + msg );
			return false;
		}
	}

	if( quantity < 1 ) {
		alert( "Please specify Quantity for one of the items before Submitting" );
		return false;
	}

	return true;
}


//******************************
function get_element( thisform, name, option ) {
	return thisform["Items[" + name + "][" + option + "]"];
}


//******************************
function get_option_OTHER( thisform, name, option ) {
	var options = thisform["Items[" + name + "][" + option + "]"];
	return options[options.length - 1];
}


//******************************
function check_quant( thisform, name ) {
	var e = get_element( thisform, name, 'Quantity' );
	var qty = parseInt( e.value );
	if( isNaN( qty ) || qty < 1 )
	{
		e.value = '?';
		e.focus();
		e.select();
		alert( name + "\n\nPlease specify Quantity" );
	}
}


//******************************
function set_option_OTHER( thisform, name, option ) {
	var option = get_option_OTHER( thisform, name, option );
	option.checked = true;
	check_quant( thisform, name );
}


//******************************
function get_radiovalue( thisform, name, option ) {
	var radio = get_element( thisform, name, option );
	for( var i = 0; i < radio.length; i++ ) {
		if( radio[i].checked )
			return radio[i].value;
	}
	return null;
}


///////////////////////////////////////////////////////////////////////////////////////////////////

var Products = {
	'Eye-Bolt Closed':	   {a:'A', b:'B', c:'C', t:'T'},
	'Eye-Bolt Open':	      {a:'A', b:'B', c:'C', e:'E', t:'T'},
	'Hook Bolt':	         {a:'A', b:'B', c:'C', t:'T', r:'R', l:'L°'},
	'L Bolt':               {a:'A', b:'B', c:'C', t:'T', r:'R'},
	'Offset Square U-Bolt':	{a:'A', b:'B', c:'C', r:'R', t:'T'},
	'Roof Top V-Bolt':      {a:'A', b:'B', c:'C', d:'D', t:'T', r:'R', l:'L°'},
	'Round J-Bolt':		   {a:'A', b:'B', c:'C', d:'D', t:'T'},
	'Round U-Bolt':		   {a:'A', b:'B', c:'C', t:'T'},
	'Slant U-Bolt':         {a:'A', b:'B', c:'C', d:'D', t:'T', r:'R', l:'L°'},
	'Spade Bolt':		      {a:'A', b:'B', c:'C', d:'D', e:'E', f:'F', g:'G', t:'T'},
	'Square J-Bolt':	      {a:'A', b:'B', c:'C', d:'D', t:'T', r:'R'},
	'Square U-Bolt':	      {a:'A', b:'B', c:'C', r:'R', t:'T'}
};

var focus = null;
var msg = "";
var REQUIRE_ALL = false;