//Common JavaScript functions

// Prevent selection of invalid dates through the select controls 
function adjustDaySelect() {
	// field_prefix is deprecated!
	var daysInMonth = 32 - new Date($('#eyear').val(), $('#emonth').val() - 1,
			32).getDate();
	$('#eday option').attr('disabled', '');
	$('#eday option:gt(' + (daysInMonth - 1) + ')')
			.attr('disabled', 'disabled');

	if ($('#eday').val() > daysInMonth) {
		$('#eday').val(daysInMonth);
	}
}

// This function opens links in an external window in a standards compliant way.
// To use it, code the link like this:
// <a href="document.html" onclick="setTargetBlank(this);">external link</a>
function setTargetBlank(thelink) {
	thelink.target = "_blank";
}

// This function will enable or disable to "Number of Days" dropdown based on
// the Coverage Term value (Daily, 6-month, Annual)
function toggleNumberOfDays() {
	var term_id = $('#term_id').val();

	if (term_id == 1) {
		// This is a daily policy, so enable the "Number of Days" dropdown.
		$('#day_num option:first').val('1').text('1');
		$('#day_num').removeAttr('disabled');

	} else {

		$('#day_num option:first').val('0').text('-- N/A --');
		$('#day_num option:first').attr("selected", true);
		$('#day_num').attr('disabled', 'disabled');
	}
}

function toggleCustomerSelects(first_id, second_id, default_val) {
	$('#' + first_id).change(function() {
		first_val = $('#' + first_id + ' option:selected').val();

		if (first_val == default_val) {
			$('#' + second_id).val(0);
		} else {
			$('#' + second_id).val(first_val);
		}
	});
}


/*******************************************************************************
 * This javascript will control the location of the helpbox on the purchase
 * pages. If the user scrolls, the helpbox will follow so that it is always
 * visible to the user. This was built by taking pieces of code from:
 * http://www.quirksmode.org/js/fixedmenu.html
 * http://www.quirksmode.org/viewport/compatibility.html
 * http://www.quirksmode.org/js/dhtmloptions.html
 ******************************************************************************/

var objHelp; // The helpbox div
var scrolledPixels; // The number of pixels the user has scrolled down.
var minPixels = 165; // The minimum number of pixels the user must scroll
						// before the helbox moves.
var delay = 10; // The delay (in milliseconds) before checking position again.

// This function begins the processing of the helpbox position.
// Called from <body onload="startHepBox();">
function startHelpBox() {
	objHelp = new getObj('helpbox');
	moveHelpBox();
}

// This function is used by startHelpBox() to grab the element properties.
function getObj(name) {
	if (document.getElementById) {
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	} else if (document.all) {
		this.obj = document.all[name];
		this.style = document.all[name].style;
	} else if (document.layers) {
		this.obj = document.layers[name];
		this.style = document.layers[name];
	}
}

// This function does the actual moving of the helpbox.
function moveHelpBox() {
	if (window.innerHeight) { // all except IE
		scrolledPixels = window.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		scrolledPixels = document.documentElement.scrollTop; // IE6 in Strict
																// Mode
	} else if (document.body) { // other IEs
		scrolledPixels = document.body.scrollTop;
	}
	if (scrolledPixels > minPixels) { // User has scrolled more than
										// scrolledPixels
		// Move the helpbox. Must include units ("px") at the end.
		objHelp.style.top = scrolledPixels - minPixels + "px";
	} else if (scrolledPixels < minPixels) { // User has scrolled leess than
												// scrolledPixels
		// Move helpbox back to top.
		// We must do this to reset if the user scrolls back up too fast.
		objHelp.style.top = 0 + "px";
	}
	setTimeout('moveHelpBox()', delay);
}

// This function will grab help info from the DB using AJAX.
function getHelp(help_id) {
	$('#helpbody').load('/buy/ajax/process-help.php', {
		'help_id' : help_id
	});
}

// ############# DEPRECATED FUNCTIONS #############################

// hide a given HTML element
function hideElement(id) {
	var element = document.getElementById(id);
	element.style.display = 'none';
}

// show a given HTML element
function showElement(id) {
	var element = document.getElementById(id);
	element.style.display = '';
}

