

function SubscribeTo( theForm, which )
{

	if ( _get_num_seats( theForm ) == 0 ) {
		alert( "Please specify the number of Employee Licenses" );
		return false;
	}

	if ( _get_num_channels( theForm ) == 0 ) {
		alert( "Please select at least one Research Stream" );
		return false;
	}

	theForm.subscription_level.value = which;
	theForm.submit();
}

function selectChannel( frm, chan )
{
	var theForm = eval( 'document.' + frm );
	if ( ! theForm ) {
		return 0;
	}

	var elems = theForm.elements;
	for ( var i = 0; i < elems.length; i++ ) {

		var theElem = elems[ i ];
		if ( theElem.type == 'checkbox' && theElem.value == chan ) {
			theElem.checked = 'checked';
			updatePrices( theForm );
			return 0;
		}
	}
}

function selectAllChannels( theCheck )
{
	var theForm = theCheck.form;
	var elems = theForm.elements;
	var num_elems = elems.length;

	var num_channels = 0;

	for ( var i = 0; i < num_elems; i++ ) {
		var theElem = elems[ i ];
		if ( theElem.type == 'checkbox' && theElem.name != 'selectall' ) {
			theElem.checked = theCheck.checked;
		}
	}

	updatePrices( theForm );
}

function _get_num_seats( theForm )
{
	var num_seats = 0;

	var fldSeats = theForm.num_seats;

	for ( var i = 0; i < fldSeats.length; i++ ) {
		var theSeat = fldSeats[i];
		if ( theSeat.checked ) {
			num_seats = theSeat.value;
			break;
		}
	}

	return num_seats;
}

function _get_num_channels( theForm )
{
	var elems = theForm.elements;
	var num_elems = elems.length;

	var num_channels = 0;

	for ( var i = 0; i < num_elems; i++ ) {
		var theElem = elems[ i ];
		if ( theElem.type == 'checkbox' && theElem.checked == true && theElem.name != 'selectall' ) {
			num_channels++;
		}
	}

	return num_channels;
}

function updatePrices( theForm )
{

	var indiv = document.getElementById( 'indiv_price' );
	var project = document.getElementById( 'proj_price' );
	var annual = document.getElementById( 'ann_price' );

	var num_seats    = _get_num_seats( theForm );
	var num_channels = _get_num_channels( theForm );

	var levels = Array( 'indiv', 'project', 'annual' );
	var base_prices = Array( 1950, 2450, 3500 );
	var mid_diff = Array( 500, 500, 500 );
	var unl_diff = Array( 2450, 2350, 2000 );

	var multi_disc_8 = Array( 100, 550, 1200 );
	var multi_disc_25 = Array( 125, 600, 1400 );
	var multi_disc_unl = Array( 225, 1200, 1500 );

	var discounts = Array( multi_disc_8, multi_disc_25, multi_disc_unl );
	
	var indiv_cost = 0;
	var proj_cost = 0;
	var ann_cost = 0;

	if ( num_seats != 0 && num_channels > 0 ) {

		indiv_cost = base_prices[0];
		proj_cost  = base_prices[1];
		ann_cost   = base_prices[2];

		var seat_index = 0;

		if (num_seats == 25 ) {
			seat_index = 1;

			indiv_cost += mid_diff[0];
			proj_cost  += mid_diff[1];
			ann_cost   += mid_diff[2];
		}
		else if ( num_seats == 'unlimited' ) {
			seat_index = 2;

			indiv_cost += unl_diff[0];
			proj_cost  += unl_diff[1];
			ann_cost   += unl_diff[2];
		}

		if ( num_channels > 1 ) {

			var addl_channels = num_channels - 1;
			var indiv_addl_cost = indiv_cost - discounts[seat_index][0];
			var proj_addl_cost  = proj_cost  - discounts[seat_index][1];
			var ann_addl_cost   = ann_cost   - discounts[seat_index][2];

			indiv_cost += ( addl_channels * indiv_addl_cost );
			proj_cost += ( addl_channels * proj_addl_cost );
			ann_cost += ( addl_channels * ann_addl_cost );
		}
	}
	
	indiv.innerHTML = '$' + indiv_cost;
	project.innerHTML = '$' + proj_cost;
	annual.innerHTML = '$' + ann_cost;
}

function DisplaySubs( theForm ) {

	var elems = theForm.elements;
	var num_elems = elems.length;

	var str = '';

	for ( var i = 0; i < num_elems; i++ ) {
		var theElem = elems[ i ];

		if ( theElem.type == 'checkbox' && theElem.checked == true ) {
			str += "<br />" + theElem.id;
		}
	}
	
	var theDiv = document.getElementById( 'current_subs' );

	if ( ! str ) {
		str = 'Select one or more Channels';
	}
	else {
		str = '<b>Currently Selected:</b><br />' + str;
	}

	if ( theDiv ) {
		theDiv.innerHTML = str;
	}
}


