var help_win='';
function open_help ( help_panel, x, y )
{
	help_win=window.open(help_panel,'login_help','height='+y+',width='+x+',left=0,top=0');
}
function close_help ()
{
	if ( help_win != '' ) { help_win.close(); }
}
function open_panel (panel, x, y) {
	panel_win=window.open(panel, 'panel','height='+y+',width='+x+',left=0,top=0,scrollbars=no,resizable=yes');
}
function open_panel_scroll (panel, x, y) {
	panel_win=window.open(panel, 'panel','height='+y+',width='+x+',left=0,top=0,scrollbars=yes,resizable=yes');
}
function limit_num_characters ( id, max_char ) {
	var tbox = document.getElementById(id);
	var v = tbox.value;

	if ( v.length > max_char ) {
		tbox.value = v.substring(0, max_char);
		alert ( 'You cannot enter more than ' + max_char + ' characters' );
	}
}
function duplicate_selection ( source, destination ) {
	var sel_src = document.getElementById(source);
	var sel_dest = document.getElementById(destination);
	sel_dest.selectedIndex = sel_src.selectedIndex ;
}

function duplicate_entry ( source, destination ) {
	var text_src = document.getElementById(source);
	var text_dest = document.getElementById(destination);
	text_dest.value = text_src.value ;
}

function JumpTo ( a ) {
	document.location.href = '#' + a;
}

// the following functinos are specifically for the Insert New Task and Update Task pages
function update_unit_price ( gst_rate ) {
	var value1 = document.getElementById('quantity_actual').value;
	var value2 = document.getElementById('unit_price').value;
	var sel_dest = document.getElementById('calc_amount_actual');
	var sel_hidden = document.getElementById('amount_actual');
	
	value2 = trim_dollar ( value2 ); // remove the $ symbol
	var total = value1 * value2;

	sel_hidden.value = total;
	sel_dest.innerHTML = dollar_format ( total );
	
	calc_chargable_price ( gst_rate );
	
	if ( total == 0 ) {
		document.getElementById('task_completed').checked = false;
		document.getElementById('task_completed').disabled = true;
	} else {
		document.getElementById('task_completed').disabled = false;
	}
}

function calc_actual_price ( gst_rate ) {
	var value1 = document.getElementById('quantity_actual').value;
	var value2 = document.getElementById('unit_price').value;
	var sel_dest = document.getElementById('calc_amount_actual');
	var sel_hidden = document.getElementById('amount_actual');
	
	value2 = trim_dollar ( value2 ); // remove the $ symbol
	var total = value1 * value2;

	sel_hidden.value = total;
	sel_dest.innerHTML = dollar_format ( total );
	
	calc_chargable_price ( gst_rate );
	
	if ( total == 0 ) {
		document.getElementById('task_completed').checked = false;
		document.getElementById('task_completed').disabled = true;
	} else {
		document.getElementById('task_completed').disabled = false;
	}
}

function calc_chargable_price ( gst_rate ) {
	var value1 = document.getElementById('quantity_chargable').value;
	var value2 = document.getElementById('unit_price').value;
	var sel_dest = document.getElementById('calc_amount_charged');
	var sel_hidden = document.getElementById('amount_charged');
	
	value2 = trim_dollar ( value2 ); // remove the $ symbol
	var total = value1 * value2;

	sel_hidden.value = total;
	sel_dest.innerHTML = dollar_format ( total ) ;
	
	calc_gst ( gst_rate );
	
	if ( total == 0 ) {
		document.getElementById('display_on_invoice').checked = false;
		document.getElementById('display_on_invoice').disabled = false;
	} else {
		document.getElementById('display_on_invoice').checked = true;
		document.getElementById('display_on_invoice').disabled = true;
	}
}

function calc_discount ( max_discount, gst_rate ) {
	var discount_requested = document.getElementById('discount');
	var price = document.getElementById('quantity_chargable');
	var max_discount_price = price.value * max_discount / 100;
	if ( discount_requested.value > max_discount_price ) {
		alert ( 'The maximum discount allowed is: $' + max_discount_price + '!');
		discount_requested.value = max_discount_price;
	}
	
	calc_gst ( gst_rate );
}

function calc_gst ( gst_rate ) {
	var price = document.getElementById('calc_amount_charged').innerHTML;
	var discount = document.getElementById('calc_discount').innerHTML;
	var sel_dest = document.getElementById('calc_gst');
	var sel_hidden = document.getElementById('gst');

	price = trim_dollar ( price ); // remove the $ symbol
	discount = trim_dollar ( discount ); // remove the $ symbol
	var gst = ( price - discount ) * gst_rate / 100;

	sel_hidden.value = gst;
	sel_dest.innerHTML = dollar_format ( gst ) ;
	calc_total ( );
}

function dollar_format ( num ) {
	num = Math.round( num * 100 ) / 100;
	num = '$' + num;  // convert num into a string so that split works
	
	var num_array = num.split('.');

	if ( num_array.length == 1 ) num = num + '.00';
	else {
		var frac = num_array[1];
		
		if ( frac == 0 ) num = num + '00';
		else if ( frac < 10 ) num = num + '0';
	}
	return num;
}

function trim_dollar ( num ) { 
	if ( num.slice(0,1) == '$' ) num = num.slice(1);
	return num;
}

function calc_total ( ) {
	var price = document.getElementById('calc_amount_charged').innerHTML;
	var discount = document.getElementById('calc_discount').innerHTML;
	var gst = document.getElementById('calc_gst').innerHTML;
	
	price = price.slice(1); // remove the $ symbol
	discount = discount.slice(1); // remove the $ symbol
	gst = gst.slice(1); // remove the $ symbol
	
	total = parseFloat(price) - parseFloat(discount) + parseFloat(gst);
	
	document.getElementById('calc_total_price').innerHTML = dollar_format ( total ) ;
}
