// Binds a callback function to an element's event handler dynamically, passing the callback function a parameter.
function bind_function(func, this_object) 
{
	var args = new Array(arguments[1]);
	return function() 
	{ 
		return func.apply(this_object, args);
	};
}

$(document).ready(onload_event_handler);
function onload_event_handler()
{
	// Bind a callback function to the onfocus event of all the elements in the form.
	var form = document.getElementById('wizard-form');
	for(i = 0; i < document.forms.length; ++i)
	{
		for(j = 0; j < document.forms[i].elements.length; ++j)
		{
			// FF, Safari, etc..
			if(document.forms[i].elements[j].addEventListener)
			{
				document.forms[i].elements[j].addEventListener('focus', bind_function(update_instructions, document.forms[i].elements[j]), false);
			}
			// IE
			else
			{
				document.forms[i].elements[j].attachEvent('onfocus', bind_function(update_instructions, document.forms[i].elements[j]));
			}
		}
	}

	// Check if there's any fields the user didn't fill to highlight.
	highlight_missing_fields(missing_fields);

	// On the first wizard page, some static instructions should be displayed in the sidebar.
	if(wizard_page == 1)
	{
		$("#sidebar").html("\
		<h2>Instructions</h2>\
		<div>\
			Answer the questions by choosing from the dropdown menus. <br /><br />\
			When you click on a question's menu item, an explanation of that question or data request will display here.\
		</div>"
		);		
	}
	
	// If we're on the second page of the wizard, update the instructions pane with the state / project type / contractor role instrictions.
	else if(wizard_page == 2)
	{
		$("#sidebar").html("\
		<h2>Instructions</h2>\
		<div> " + second_page_instructions + "</div>"
		);
	}
	
	else if(wizard_page == 100)
	{
		$("#sidebar").html("\
		<h2>Instructions</h2>\
		<div>\
			Create a free account with Express Lien to use our industry-leading Lien Pilot web application.<br /><br />\
			With an Express Lien account, you can manage project data, preliminary notices and lien documents,\
			and even keep track of lien and notice deadlines.\
			Then, filing a construction lien or notice takes just a click of the button.\
		</div>"
		);		
	}
	
	else if(wizard_page == 3)
	{
		$("#sidebar").html("\
		<h2>Instructions</h2>\
		<div>Add here your personal data</div>"
		);
		// for "About You" wizard page
		$("#company_name").trigger("focus");
	}
	
	else if(wizard_page == 4)
	{
		$("#sidebar").html("\
		<h2>Instructions</h2>\
		<div>Add here Project data or select an existent one</div>"
		);
		// for "About You" wizard page
		$("#company_name").trigger("focus");
	}
	
	else if(wizard_page == 5)
	{
		$("#sidebar").html("\
		<h2>Instructions</h2>\
		<div>Add here personal data of Project contacts</div>"
		);
	}
	
	else if(wizard_page == 6)
	{
		$("#sidebar").html("\
		<h2>Instructions</h2>\
		<div>Add here order information</div>"
		);
	}
}

// The function responsible for updating the instructions.
function update_instructions(field) 
{
	if(instructions[field.name])
	{
		$("#sidebar").html("\
		<h2>Instructions</h2>\
		<div> " + instructions[field.name] + "</div>"
			);
	}
}

// Compare the form field id's to the names of missing fields stored in the session to highlight the paragraphs enclosing the missing fields.
function highlight_missing_fields(fields)
{
	for(var i = 0; i < fields.length; ++i)
	{
		var field = document.getElementById(fields[i]);		
		var parent = field.parentNode;
		$(parent).addClass("error-highlight");
	}
}

function select_price(obj, spanId)
{
	var priceId = $(obj).val();
	var html = productPrices['id-'+priceId] + "&nbsp;&nbsp;&nbsp;" + "<span style=\"cursor:pointer;\" id=\"help-price\"><img src=\""+baseUrl+"/images/help.png\" /></span>"
	$("#" + spanId).hide().html(html).fadeIn();
}
