var newWin = null;

function openWin(url, w, h) {

	var props = "width=" + w + ",height=" + h + ",toolbar=no,menubar=no,personalbar=no,copyhistory=no,scrollbars=yes";

	var handle = "oNewWin";

	if(newWin && !newWin.closed) {

		newWin.focus();

	}

	newWin = window.open(url, handle, props);

}





function goToSection(section, section_prefix, num_of_sections, header_prefix) {

	for (i=1; i<= num_of_sections; i++) {

		section_target_id = section_prefix + i;

		section_target = document.getElementById(section_target_id);



		header_target_id = header_prefix + i;

		header_target = document.getElementById(header_target_id);

		

		if (section == i) {

			section_target.style.display = 'block';

			header_target.className = 'active';

			

			// jquery bit to set focus to first text box in active area

			$("#" + section_target_id + " input[type='text']:first").focus();

		} else {

			section_target.style.display = 'none';

			header_target.className = '';

		}

	}

	

	

}



function jumpTo (select_name, prefix) {

	var goto_select = document.getElementById(select_name);

	var goto_id = goto_select.options[goto_select.selectedIndex].value;

	

	if (!goto_id) {

		return false;

	} else {

		window.location.href = prefix + goto_id;

	} 



} 



// same as above but modified for review page, which adds a checkbox 

function jumpToReview (select_name, prefix, check_name, sort_by) {

	var checkBox = document.getElementById(check_name);

	var isChecked = "";

	if (checkBox.checked) {

		isChecked = "true";

	}

	

	var url = prefix;

	

	if (select_name) {

		var goto_select = document.getElementById(select_name);

		var goto_id = goto_select.options[goto_select.selectedIndex].value;

	

		if (!goto_id) {

			return false;

		} else {

			url = url + goto_id + "&" + check_name + "=" + isChecked;

		} 

	} else {

		url = url + "&" + check_name + "=" + isChecked;

	}

	

	if (sort_by) {

		url =  url + "&sortby="+sort_by+"&";

	}

	else {

		url = url + "&sortby=submission&";

	}

	

	window.location.href=url;



}

 

function getSelectedSort(f) {

	for (i = 0; i < f.sortby.length; i++) {

		if (f.sortby[i].checked) {

			return f.sortby[i].value;

		}

	}



	return 'submission';

}

/*

sb_error_check returns true or false depending on whether the tests specified were

successful.

	formname: the id of the form containing what needs to be tested

	testname: which set of tests to check

if a third argument is passed in, it's assigned to checkarray (assumed to be values that

will be used for testing).

if the tests weren't successful, it adds text to, and makes visible, the box named

"sb_error_check_" + testname.

if further effects for a test are necessary (styles added to associated divs, etc)

they are done in the case block for that test.



*/

function sb_error_check(formname, testname) {

	var passed = false;

	var error_msg = "";

	var theForm = $("#"+formname);

	var theErrorBlock = $("#sb_error_check_"+testname);

	

	if (arguments.length > 2) {

		var checkarray = arguments[2];

	}

	

	var debug = false; //for testing this code

	var test_this = false;

	 

	switch (testname) {

		

		case 'check_par_movement' : 

			

			var radio_val = $("input[name='input[subject_to_change]']:checked").val();

			var text_val = $("input[name='input[maturity_subject_to_change]']").val();



			if (radio_val == 1 && text_val == '') {

				error_msg = "You selected &quot;Yes&quot;, so please enter text for the Par Movement for this bid."

			} else {

				passed = true;

			}

		

			break;

			

		case 'bid_min':

		

			var bid_val = $("input[name='input[bid_amount]']").val();

			// remove anything not a number or decimal

			bid_val = bid_val.replace(/[^\d\.]/g, "");

			if (bid_val < parseFloat(checkarray['min'])) {

				error_msg = "Your bid amount must be greater than or equal to the bid minimum.";

			} else {

				passed = true;

			}

			break;

			

		case 'maturities_input':

		

			//test_this = true;

			

			// all must be completed

			var maturity_inputs = $(".maturity-input input");

			var all_entered = true;

			for(var i=0;i<maturity_inputs.length;i++) {

				var theInput = $(maturity_inputs[i]);

				var yearLabel = theInput.prev(".maturity_year");

				if (theInput.val()) {

					yearLabel.removeClass("error");

				} else {

					yearLabel.addClass("error");

					all_entered = false;

				}

			}

			

			if (!all_entered) {

				error_msg = "Values must be entered for all maturities.<br>";

			} 

			

			var aggregates_complete = true;

			var aggregates_match = true;

			var percentage_inputs = $(".maturity-input .percentage");

			var error_inputs = new Array();

			

			// check term maturity inputs

			$(".bid_aggregate_maturity").each( function(i) {

			

				// complete? neither or both can be selected, but not only the first

				var label = $(this).prev(".maturity_label");

				var aggregate_start_val = $(this).val();

				var aggregate_end_val = $(this).next(".bid_aggregate_maturity_end").val();

				if (aggregate_start_val && !aggregate_end_val) {

					aggregates_complete = false;

					$(label).addClass("error");

				} else {

					$(label).removeClass("error");

					

					// check range

					var started = false;

					var current_val = "";

					var set_of_maturities = new Array();

					var these_aggregates_match = true;



					percentage_inputs.each( function(j) {

						$(this).removeClass("error");

						var maturity_year = $(this).prev(".maturity_year").html();

						if (maturity_year >= aggregate_start_val && maturity_year <= aggregate_end_val) {

							set_of_maturities.push(this);

							if (!started) {

								started = true;

								current_val = jQuery.trim($(this).val());

							} else {

								if (jQuery.trim($(this).val()) != current_val) {

									aggregates_match = false;

									these_aggregates_match = false;

									$(label).addClass("error");

								} else {

									$(label).removeClass("error");

								}

							}

						}

					});

					

					if (!these_aggregates_match) {

						jQuery.merge(error_inputs, set_of_maturities);

					}

						

									

				}

				

			});

			

			$(error_inputs).addClass('error');

			

			if (!aggregates_complete) {

				error_msg += "If you select the start of an aggregate range, you must also select the end.<br>";

			}

			if (!aggregates_match) {

				error_msg += "Consecutive maturities must have the same percentages.<br>";

			}

			

			passed = all_entered && aggregates_complete && aggregates_match;

			

			

			break;

			

		

		case 'insurance_input':

		

			

			/* if any of the inputs are filled in, the whole section must be completed */

			

			var one_completed = false;

			var one_blank = false;

			

			var insurance_from = $("input[name='input[insurance_from]']").val();

			var insurance_premium_amount = $("input[name='input[insurance_premium_amount]']").val();

			var insurance_premium_percent = $("input[name='input[insurance_premium_percent]']").val();

			var insurance_maturities_type = $("input[name='input[insurance_maturities_type]']:checked").val();

			var insurance_maturity_years = $("input[name='input[insurance_maturity_years]']").val();

			

			if (insurance_from) {

				one_completed = true;

			} else {

				one_blank = true;

			}

			

			if (insurance_premium_amount || insurance_premium_percent) {

				one_completed = true;

			} else {

				one_blank = true;

			}

			

			if (insurance_maturities_type) {

				one_completed = true;

				if (insurance_maturities_type == 'years' && !insurance_maturity_years) {

					one_blank = true;

				}

			} else {

				one_blank = true;

			}

				

			

			/* if both are true, then at least one was blank and at least one was filled in,

				which is an error */

				

			if (one_completed && one_blank) {

				error_msg = "If any one of the values in this section is completed, then all inputs are required.";

			} else {

				passed = true;

			}

			

			break;

			

			

		

			



	}



	if (debug && !test_this) return true;

	

	if (passed) {

		theErrorBlock.hide();

	} else {

		theErrorBlock.html(error_msg).show();

	}



	return passed;



	

	



}



function pop_aggregate_years(maturity_dropdown, aggregate_dropdown, aggregate_val) {

	var waiting_for_match = true;

	var theHTML = "";

	var maturity_val = $("#"+maturity_dropdown).val();

	

	if (maturity_val) {

		theHTML = "<option value=''>YYYY</option>";

		for (var i=0; i < maturity_years.length; i++) {

			if (waiting_for_match) {

				if (maturity_val == maturity_years[i]) {

					waiting_for_match = false;

				}

			} else {

				theHTML += "<option value='" + maturity_years[i] + "'";

				if (maturity_years[i] == aggregate_val) {

					theHTML += " selected";

				}

				theHTML += ">" + maturity_years[i] + "</option>";

			}

		}

	} else {

		theHTML = "<option value=''>--</option>";

	}

	

	$("#" + aggregate_dropdown).html(theHTML);

	

	repeat_aggregate_year(aggregate_dropdown);

		

}



function repeat_aggregate_year(aggregate_dropdown) {

	theDropdown = $("#"+aggregate_dropdown);

	$(theDropdown).parent().next('.term_maturity').html($(theDropdown).val());

}





			

		
