/*
requires jquery
*/

var AWCFormUtil = jQuery.extend({},{

	populateAjaxOptions: function(source_value, destination_input_id, ajax_method) {
		// save the selected value
		var saved_value = jQuery('#'+destination_input_id).val();

		this.prepareDestinationOptionsForUpdate(destination_input_id);

		var get_data = {};
		get_data['method'] = 'AjaxOptionsBuilder::populateAjaxOptions';
		get_data['value'] = source_value;
		get_data['options_method'] = ajax_method;
		get_data['r'] = Math.round(Math.random() * 100000);
		jQuery.getJSON('/_awc_ajax', get_data, function(response_data) { AWCFormUtil._populateAjaxOptionsResponse(destination_input_id, response_data, saved_value) });
		
	},

	prepareDestinationOptionsForUpdate: function(destination_input_id) {
		var dest_input_el;
		dest_input_el = jQuery('#'+destination_input_id);

		var options_text = '<option value="">---</option>';
		dest_input_el.hide(); 
		dest_input_el.empty().append(options_text);
		dest_input_el.show();
	},
	
	_populateAjaxOptionsResponse: function(destination_input_id, response_data, saved_value) {
		var dest_input_el = jQuery('#'+destination_input_id);
		
		// calculate the new select options body
		var options_text = '';
		jQuery.each(response_data, function(key, value) {
			options_text += '<option value="'+value+'">'+key+'</option>';
		});

		// rebuild the options text
		// hide and show the select due to a ms bug
		dest_input_el.hide(); 

		dest_input_el.empty().append(options_text);

		dest_input_el.show();

		// do this 10 ms later because IE 6 is stupid
		window.setTimeout(function() {
			// select the default value
			if (saved_value.length) {
				dest_input_el.val(saved_value);
			} else {
				dest_input_el.val('');
			}
		}, 10);

	},



	checkAddYears: function(element) {
		var value=element.value;

		if (value == 'add') {
			// add more and less years
			var options = element.options;

			var lo = 0;
			var hi = 0;
			var val = 0;

			for(var i=0; i<options.length; ++i) {
				val = parseInt(options[i].value);
				if (val > 0) {
					if (val > hi) hi = val;
					if (lo == 0 || val < lo) lo = val;
				}
			}

			// add more years
			lo -= 5;
			hi += 5;
			if (hi > 2036) hi = 2036;
			if (lo < 1970) lo = 1970;

	//		alert('i='+i+'');
			element.options.length = 0;

			// build again
			var option;

			option = new Option('--','');
			element.options[element.options.length] = option;

			for(var i=lo; i<=hi; ++i) {
				option = new Option(i,i);
				element.options[element.options.length] = option;
			}

			option = new Option('------','-');
			element.options[element.options.length] = option;

			option = new Option('More Years','add');
			element.options[element.options.length] = option;

			option = new Option('Other','other');
			element.options[element.options.length] = option;

			element.value = '';
		}

		if (value == 'other') {
			var new_option_text = prompt('Enter Year', "");
			if (new_option_text != null && new_option_text.length > 0) {
				element.options[element.options.length] = new Option('------','-');
				element.options[element.options.length] = new Option(new_option_text, new_option_text, true, true);
			}
		}


		return false;
	},

	pulldownChange: function(href, in_new_window) {
		if (href.length < 1) return;
		if (in_new_window) {
			var a_win = window.open(href, '_blank');
			a_win.focus();
		} else {
			window.location = href;
		}
	},

	limitText: function(input_el, counter_id, limit) {
		var input_text = input_el.value;
		input_text = input_text.replace(/\\r\\n/g,"\\n");

		if (input_text.length > limit) {
			input_el.value = input_text.substring(0, limit);
			document.getElementById(counter_id).innerHTML = '0';
		} else {
			document.getElementById(counter_id).innerHTML = limit - input_text.length;
		}
	},

	trg_selectOther: function(el, prompt_text, other_label) {
		var form = el.form;
		var selected = el.options[el.selectedIndex].value

		if (selected == 'Other') {
			newOption = "";
			while (newOption == ""){
				newOption=prompt (prompt_text, "");
			}
			if (newOption != null) {
				el.options[(el.options.length-1)]=new Option(newOption,newOption,true,true);
				el.options[el.options.length]=new Option(other_label,'Other');
			}
		}
	},

	trg_populateSelect: function(uid, el) {
		var selected = el.options[el.selectedIndex].value
		inForm = el.form;
		var target_el_name = el.name.substr(0,el.name.length-3)+'[1]';
		var target_el = inForm[target_el_name];
		var offset = el.selectedIndex + 1;
		if (el.options[0].value.length < 1) {
			--offset;
		}

		var selectedArray = eval("trg_array_"+uid+"_"+offset);
		// while (selectedArray.length < target_el.options.length) {
		//	target_el.options[(target_el.options.length - 1)] = null;
		// }
		target_el.options.length = 0;
		for (var i=0; i < selectedArray.length; i++) {
			eval("target_el.options[i]=" + "new Option" + selectedArray[i]);
		}
		if (el.options[0].value == '') {
			el.options[0]= null;
			if ( navigator.appName == 'Netscape') {
				if (parseInt(navigator.appVersion) < 5) {
					window.history.go(0);
				} else {		
	/*
					if (navigator.platform == 'Win32' || navigator.platform == 'Win16') {
						window.history.go(0);
					}
	*/
				}
			}
		}
	},


	trg_updateTargetSelect: function(uid, el, prompt_text, other_label) {
			var form = el.form;
			var selected = el.options[el.selectedIndex].value

		if (selected == 'Other') {
			newOption = "";
			while (newOption == ""){
				newOption=prompt (prompt_text, "");
			}
			if (newOption != null) {
				el.options[(el.options.length-1)]=new Option(newOption,newOption,true,true);
				el.options[el.options.length]=new Option(other_label,'Other');
			}
		}
	},
	
	_end: {}
});
