function defaultParameter(inCallerArgumentsArray, inPosition, inDefault) {
	return (inCallerArgumentsArray.length >= inPosition) ? inCallerArgumentsArray[inPosition - 1] : inDefault;
}

// Note default parameters
function dumpObject(obj) {
//	var debugOutput = document.getElementById('debugOutput');

	var asHTML = defaultParameter(arguments, 2, true);
	var maxDumpDepth = defaultParameter(arguments, 3, 10);
	var depth = defaultParameter(arguments, 4, 0);
	var indentString = defaultParameter(arguments, 5, '');
	var dumpTitle = defaultParameter(arguments, 6, 'The Object');
	
	var indentChar = (asHTML ? '+' : '\t');
	var newline = (asHTML ? '<br/>' : '\n');
	var lt = (asHTML ? '&lt;' : '<');
	var gt = (asHTML ? '&gt;' : '>');

	if(depth >= maxDumpDepth) {
		return indentString + dumpTitle + ': ' + lt + 'Maximum Depth Reached' + gt + newline;
	}
	
	if(typeof obj == "object") {
		var child = null;
		var output = indentString + dumpTitle + ':' + newline;
		
//		debugOutput.innerHTML += output;
		
		indentString += indentChar;
		
		var hasItems = false;
		for(var item in obj)
		{
			hasItems = true;
			try {
				child = obj[item];
			} catch (e) {
				child = lt + 'Unable to Evaluate' + gt;
			}
			
			var re = new RegExp("(parent)|(sibling)|(owner)", "i");
			var isParentOrSibling = re.test(item);

			if ((typeof child == "object") && !isParentOrSibling) {
//				debugOutput.innerHTML += '; child is object--dumping<br/>';
				output += dumpObject(child, asHTML, maxDumpDepth, (depth + 1), indentString, item);
			} else {
//				debugOutput.innerHTML += '; child is not an object; child is ' + item + ' value is ' + child;
				if(isParentOrSibling)
					output += indentString + item + ": " + lt + 'Parent/Sibling not shown' + gt + newline;
				else
					output += indentString + item + ": " + child + newline;
			}
		}
		
//		if(!hasItems)
//			output = indentString + dumpTitle + ': (empty object)' + newline;
		
//		debugOutput.innerHTML += 'returning ' + output;
		return output;
	} else {
		return obj;
	}
}

$(document).ready(
	function() {
		$('#form1').submit(
			// When the user attempts to submit the form, read the zip code he specified and
			// ask the server for routing information for that zip. Based on the routing information,
			// decide which action to assign to the form. Doing it this way because Marty's sites
			// are set up for a form-post to topproducer, which apparently does some redirects, which
			// are probably set up on Marty's account with topproducer, so it seems easier to fit into
			// that existing framework than to use curl to post and then try to figure out what to do
			// after that.
			function() {
				if(!ValidateData()) {
					return false;
				}
				
				$('#Submit').attr('disabled', 'disabled');

				var http = 'http://';
				var theServerWeAreOn = window.location.href.substr(window.location.href.indexOf(http) + 7);
				var slashPosition = theServerWeAreOn.indexOf('/');
				theServerWeAreOn = theServerWeAreOn.substr(0, slashPosition);

				var ajaxURL = 'http://' + theServerWeAreOn + '/admin/ajax.php';
				
				// This first post is left over from when I was doing stuff differently. I could
				// probably take it out, but I don't want to deal with the associated risk of
				// breaking something subtle. Just know that it's not doing anything useful.
				$.post(ajaxURL,
					{
						'function' : 'getRouteForZip',
						'leadType' : 'lease-option buyer',
						'zip' : $('#zip').val()
					},

					function(xml) {
						var statusNode = $('FunctionStatus', xml).get(0);
						var success = czToBool(statusNode.getAttribute('success'));
						var message = statusNode.getAttribute('message');
						if(success === true) {
							statusNode = $('RouteForZip', xml).get(0);
							success = czToBool(statusNode.getAttribute('success'));
							message = statusNode.getAttribute('message');

							if(success === true) {
								$.post(ajaxURL,
									{
										'function' : 'processVIPBuyer',
										'leadType' : 'lease-option buyer',
										'zip' : $('#zip').val(),
										'state' : $('#state').val(),
										'buyerid' : $('#buyerid').val(),
										'max_monthly_payment' : $('#max_monthly_payment').val(),
										'fname' : $('#fname').val(),
										'lname' : $('#lname').val(),
										'state_name' : $('#state_name').val(),
										'city' : $('#city').val(),
										'phone' : $('#phone').val(),
										'email' : $('#email').val(),
										'hear' : $('#hear').val(),
										'date1' : $('#date1').val(),
										'down_payment' : $('#down_payment').val(),
										'when_wanna_buy' : $('#when_wanna_buy').val(),
										'credit_rating' : $('#credit_rating').val(),
										'radius' : $('#radius').val()
									},
								
									function(xml) {
										var statusNode = $('FunctionStatus', xml).get(0);
										var success = czToBool(statusNode.getAttribute('success'));
										var message = statusNode.getAttribute('message');
										if(success === true) {
											var kROUTE_XMLPOST = '1';
											var kROUTE_TOPPRODUCER = '2';
											var kROUTE_EMAIL = '4';

											statusNode = $('ProcessingStatus', xml).get(0);
											success = czToBool(statusNode.getAttribute('success'));
											message = statusNode.getAttribute('message');

											if(success === true) {
												var routeAction = false;
												var assignedRoute = statusNode.getAttribute('route');
												if((assignedRoute & kROUTE_TOPPRODUCER) !== 0) {
													// If top producer is one of the route options, we have to do
													// it via form submit. At this point, the other route options
													// have been addressed.
													routeAction = 'https://www.topproduceronline.com/LeadToolkit.asp';

													$('#form1').unbind('submit');	// Deactivate this function and submit the form
													$('#form1').attr('action', routeAction);
													$('#form1').submit();
												} else {
													// Top producer isn't among the route options; we've already
													// taken care of the other options, so just go to the success page.
													window.location = "success.html?scode=11";
												}
											}
										}
										
										if(success === false) {
											$('#Submit').removeAttr('disabled');
											alert('Internal error: ' + message);
										}
									}
								);
							} else {
								$('#Submit').removeAttr('disabled');
								alert('Could not process your information: ' + message);
							}
						}
					}
				);

				return false;
			}
		);
	}
);

function czToBool(charValue) {
	return ((charValue == '0') ? false : true);
}
