Event.observe(window, 'load', function() {
	var dfn_containers = $A([document.getElementsByClassName('rankingTable'),document.getElementsByClassName('control')]).flatten()
	set_definitions(dfn_containers);
	var table = $$('tr.rankingItemRow');
	for (var i = 0; i < table.length; i++) {
		if ( i % 2 == 0 ) { table[i].addClassName('stripe'); }
	}
  /*weights = {
	  region: new ratingControl('region',2),
	  service_offered: new ratingControl('service_offered',2),
  }*/
  c = $A(document.getElementsByClassName('control_collapse'));
  c.each( function(c) {
  		Event.observe(c, 'click', control_collapse.bindAsEventListener(self), [useCapture = false]);
  }); 
  
});

function handle_form(form_container, status) {
	if (status != 'disable' && status !='enable') { return false; }
	$A(form_container.getElementsByTagName('input')).invoke(status);
	if (status == 'disable') {
		$A(form_container.getElementsByTagName('label')).each( function(label){
				var input = label.getElementsByTagName('input')[0];
				if (input.type == 'text') {
					input.value = '$';
				}
				else { input.checked = false; }
				Event.observe(label, 'click', notify);
		} );
	}
	else {
		$A(form_container.getElementsByTagName('label')).each( function(label){
				Event.stopObserving(label, 'click', notify);
		} );	
	}
}

function notify(evt) {
	evt = (arguments[0].target)?arguments[0].target:arguments[0].srcElement;
	rating_control = evt.parentNode.getElementsByClassName('rating_control')[0];
	weight_warning = new Warning($('warning'), 'Please use these stars to select the importance of this category first.');
	weight_warning.raise_intermittent(rating_control, 4);
}

function serialize_controls() {

	function validate(control, validator) {
		if (!values[control] || typeof validator != 'function') { return; }
		values[control] = validator(values[control])
	}
	function price_check(price) {
		if (price.value == "$" || price.value.length == 0) { return null; }
		price.value = price.value.replace(/,/, ''); //commas are legal, but we'll remove them
		price.value = price.value.replace(/\$/, ''); //asme for '$'.
		if ( price.value.match(/[^0-9]+/) ) {
			validation_failure_flag = "Home price must be a number. You entered " + price.value + ".";
			validation_failure_point = price
			return null;
		}
	}
	function escape_special_chars_and_encode(string) {
		var matches = [[/\^/g, "^c"],[/\//g, "^s"], [/\+/g, "^p"]];
		$A(matches).each( function(match) {
			string = string.toString().replace(match[0], match[1]);
		});
		return encodeURI(string)
	}
	
	
	validation_failure_flag = false;
	validation_failure_point = null;
	var values = Form.serializeElements(document.data_parms.getInputs(), [getHash = true]);
	//Validation
	if (typeof values.home_price != 'undefined') {
		validate(values.home_price, price_check)
	}
	
	function convert_to_url_query(key, value) {
		//key: [value1, value2] -> key+values1+value2
		if (typeof value == 'object') {
			return escape_special_chars_and_encode(key) + $A(value).inject("", function(query, value) { return query += "+" + escape_special_chars_and_encode(value) });
		}
		//key: value -> key+value
		else {
			return escape_special_chars_and_encode(key) + "+" + escape_special_chars_and_encode(value);
		}
	}
	var url_query = []
	Object.keys(values).each( function(key) {
		url_query.push(convert_to_url_query(key, values[key]));
	});
	var sorted_weights = $H(weights).keys().sort(); //for caching purposes, URI param ordering should be consistent
	/*sorted_weights.each( function(weight) {
		        if (weights[weight].value != 0) {
				url_query.push(escape_special_chars_and_encode(weight) + "+" + escape_special_chars_and_encode(weights[weight].value));
			}
		});*/
	url_query = url_query.join("/");

	if (validation_failure_flag) {
		
		field_validation_warning =  new Warning($('warning'), validation_failure_flag);
		field_validation_warning.raise(validation_failure_point, 6, [120, 0]);
	}

	else { 
		var subdomain = (window.location.toString().match(/^http:\/\/www/))?"www":"devel";
		window.location = 'http://' + subdomain + '.usnews.com/directories/retirement/index_html/' + url_query; 
	}
}
var active_control;
control_collapse = function(self, evt) {
	var evt = arguments[0];
	var control = {
		'open': function(el) {
			el.title = "Close this control";
			//el.getElementsByTagName('span')[0].innerHTML = "(close)";
			el.parentNode.parentNode.addClassName('open');
		},
		'close': function(el) {
			el.title = "Open this control";
			//el.getElementsByTagName('span')[0].innerHTML = "(open)";
			el.parentNode.parentNode.removeClassName('open');		
		}
	}
	var self = (arguments[0].target)?arguments[0].target:arguments[0].srcElement;
	self = findParent(self, function(node) { return node.hasClassName('control_collapse') });
	var action = (self.parentNode.parentNode.hasClassName('open'))?'close':'open';
	//if (active_control) { control.close(active_control); }
	control[action](self);
	active_control = self;
	Event.stop(evt);

}

function reportToOmniture(sPropNumber, value) {
	s['prop' + sPropNumber] = value
}