//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 tm 	= $('#tm').val();
	
	if (tm == 1) {
		// This is a daily policy, so enable the "Number of Days" dropdown.
		$('#dn option:first').val('1').text('1');
		$('#dn').removeAttr('disabled'); 
				
	} else {
		
		$('#dn option:first').val('0').text('-- N/A --');
		$('#dn option:first').attr("selected", true);
		$('#dn').attr('disabled', 'disabled');
	}
}






// UPDATE THESE TO jQuery
// Set a form target to a new target.  Use this to open a form in a new window, like this:
// <form action="whatever.php" method="post" onsubmit="return setFormTarget(this, '_blank');">
function setFormTarget(formname, new_target) {
	formname.target = new_target;
}

// Changes the Licence Expire Month to match the Birth Month selected by the user
function changeLicExpMonth() {	// used in insured.php
	document.getElementById("licence_expiration_month").options[document.getElementById("birth_month").selectedIndex].selected = true;	
}

// Changes the Licence Expire Day to match the Birth Day selected by the user
function changeLicExpDay() {	// used in insured.php
	document.getElementById("licence_expiration_day").options[document.getElementById("birth_day").selectedIndex].selected = true;	
}

// Changes the Licence State to match the Address State selected by the user
function changeLicState() {	// used in insured.php
	document.getElementById("ls1").options[document.getElementById("st1").selectedIndex].selected = true;	
}

// This function will toggle the display of a div to on or off.
function toggleDivDisplay(div_id) {
	var objStyle = document.getElementById(div_id).style;
	var objDisplay = objStyle.display;

	if (objDisplay == "none") {
		objStyle.display = "block";
	} else {
		objStyle.display = "none";
	}
}

// This function is used on insured.php to toggle new policy options on or off.
function toggleOrganizationInsured(organization_div_id, insured_div_id, form_element_id) {
	
	if (document.getElementById(organization_div_id)) {
		var organizationStyle = document.getElementById(organization_div_id).style.display;
	}
	if (document.getElementById(insured_div_id)) {
		var insuredStyle = document.getElementById(insured_div_id).style.display;
	}
	if (document.getElementById(form_element_id)) {
		var radioValue = document.getElementById(form_element_id).value;
	}
	
	if (radioValue == 't' && organizationStyle == 'none') {
		toggleDivDisplay(organization_div_id);
		if (insuredStyle == 'block') {
			toggleDivDisplay(insured_div_id);
		}
	} else if (radioValue == 'f' && insuredStyle == 'none') {
		toggleDivDisplay(insured_div_id);
		if (organizationStyle == 'block') {
			toggleDivDisplay(organization_div_id);
		}
	}
}



/******************************************************
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/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 = '';
}



