<?php

	$php_self = $_SERVER['PHP_SELF'];

?>

  <script type="text/javascript">
	// validate form

	function validate_form() {

		valid = true;

		if ( document.form.artist.value == '' && document.form.contact.value == '' && document.form.company.value == '' ) {

			alert ( "Please make sure you filled in at least the Artist Name or the Contact Name or the Company name. Thank you." );

			valid = false;

		}

		if ( document.form.payment.selectedIndex == 0 ) {

			alert ( "Please enter the Payment Type. Thank you." );

			valid = false;

		}

		return valid;

	}

	function add()

	{

<?php

	echo '		document.form.action = "'.$php_self.'?do=save";

';

?>

		if ( validate_form() == true ) {

			document.form.submit();

			document.form.save.disabled = true;

			document.form.saveandprint.disabled = true;

			return true;

		}

	}

	function update()

	{

/*		document.form.target = this; 	// removed so it wouldn't open a new page everytime one saves */

		document.target = this;

<?php

	echo '		document.form.action = "'.$php_self.'?do=update";

';

?>

		if ( validate_form() == true ) {

			document.form.submit();

			document.form.save.disabled = true;

			document.form.saveandprint.disabled = true;

			return true;

		}



	}

	

	function update_and_print()

	{

/*		document.form.target = 'invoice'; // removed this cause it stopped working automatically */

<?php

	echo '		document.form.action = "'.$php_self.'?do=update&do2=print&id='.$_GET['id'].'";

';

?>

		if ( validate_form() == true ) {

			document.form.submit();

			document.form.save.disabled = true;

			document.form.saveandprint.disabled = true;

			return true;

		}

	}

	function add_and_print()

	{

/*		document.form.target = 'invoice'; // removed this cause it stopped working automatically */

<?php

	echo '		document.form.action = "'.$php_self.'?do=save&do2=print&id='.$_GET['id'].'";

';

?>

		if ( validate_form() == true ) {

			document.form.submit();

			document.form.save.disabled = true;

			document.form.saveandprint.disabled = true;

			return true;

		}

	}



	// print the plain white invoice

	function PrintInvoice(iframe) {

		iframe.focus();

		iframe.print();

	}

	// auto-fill date

	function fill_date(date,line) {

  		document.form[line].value = date;

	}

  	// auto-fill contact and company name from artist name

  	function InvoiceName() {

  		document.form.contact.value = document.form.artist.value;

  		document.form.company.value = document.form.artist.value;

  	}

  	// calculate line-subtotal

  	function l_subtotal(qty,rate,line) {

  		document.form['l'+line+'_subtot'].value = qty*rate;

		do_the_math(document.form.subtot_notax.value,document.form.tax.value,document.form.deposit.value);

  	}

  	// get timers' time

  	function get_timers() {

	  	for (var x = 1; x <= 10; x++) {

			document.form['l'+x+'_start'].value = document.form['h_l'+x+'_start'].value + ":" + document.form['m_l'+x+'_start'].value + ":" + document.form['ap_l'+x+'_start'].value;

			document.form['l'+x+'_end'].value = document.form['h_l'+x+'_end'].value + ":" + document.form['m_l'+x+'_end'].value + ":" + document.form['ap_l'+x+'_end'].value;

			if ( document.form['l'+x+'_start'].value == "::" )

				document.form['l'+x+'_start'].value = '';

			if ( document.form['l'+x+'_end'].value == "::" )

				document.form['l'+x+'_end'].value = '';

	  	}

  	}

  	// create start time and end time for database

	function timers(what,which,field) {

		if ( document.form[field].value == "" ) {

			if ( document.form['h_'+field].value == "" )

				var h = "--";

			else

				var h = document.form['h_'+field].value;

			if ( document.form['m_'+field].value == "" )

				var m = "--";

			else

				var m = document.form['m_'+field].value;

			if ( document.form['ap_'+field].value == "" )

				var ap = "--";

			else

				var ap = document.form['ap_'+field].value;

			if ( which == "h" ) {

				document.form[field].value = what + ':'+m + ':'+ap;

			} else if ( which == "m" ) {

				document.form[field].value = h+':' + what + ':'+ap;

			} else if ( which == "ampm" ) {

				document.form[field].value = h+':' + m+':' + what;

			}

		} else {

			var time = document.form[field].value.split(":");

			if ( which == "h" ) {

				document.form[field].value = what + ":" + time[1] + ":" + time[2];

			} else if ( which == "m" ) {

				document.form[field].value = time[0] + ":" + what + ":" + time[2];

			} else if ( which == "ampm" ) {

				document.form[field].value = time[0] + ":" + time[1] + ":" + what;

			}

			// calculate how many hours based on the start time and end time of the session

			var line = field.split("_")[0]; // returns 'start' or 'end' from the field in use

			var num_s = document.form[line+'_start'].value.replace(':','.'); 	// replaces the first semicolon with a dot

			var num_e = document.form[line+'_end'].value.replace(':','.');		// so you get something like '01.30:pm'

			var am_or_pm_s = num_s.split(":")[1]; // returns 'am' or 'pm'

			var am_or_pm_e = num_e.split(":")[1]; // returns 'am' or 'pm'

			var start_time = parseFloat(num_s.replace('30','5'));

			var end_time = parseFloat(num_e.replace('30','5'));

			// I am covering all grounds and only ignoring a 24hr long shit cause nobody works 24 hours straight! crazy!

			if ( am_or_pm_s == 'am' && am_or_pm_e == 'pm' ) {

				if ( start_time == '12' && am_or_pm_s == 'am' )

					document.form[line+'_qty'].value = parseFloat(start_time) + parseFloat(end_time); 				// 12am-2pm=14h

				else if ( start_time == '12.5' && am_or_pm_s == 'am' )

					document.form[line+'_qty'].value = parseFloat(start_time) + parseFloat(end_time) - 1; 			// 12.5am-2pm=13.5h

				else if ( ( end_time == '12' || end_time == '12.5' ) && am_or_pm_e == 'pm' )

					document.form[line+'_qty'].value = parseFloat(end_time) - parseFloat(start_time); 				// 4am-12pm=8h

				else

					document.form[line+'_qty'].value = parseFloat(end_time) + ( 12 - parseFloat(start_time) );		// 9am-5pm=8h	

			} else if ( am_or_pm_s == 'pm' && am_or_pm_e == 'am' ) {

				if ( ( end_time == '12' || end_time == '12.5' ) && am_or_pm_e == 'am' )

					document.form[line+'_qty'].value = parseFloat(end_time) - parseFloat(start_time);				// 2pm-12am=10h

				else if ( start_time == '12' && am_or_pm_s == 'pm' )

					document.form[line+'_qty'].value = parseFloat(start_time) + parseFloat(end_time);				// 12pm-4am=16h

				else if ( start_time == '12.5' && am_or_pm_s == 'pm' )

					document.form[line+'_qty'].value = parseFloat(start_time) + parseFloat(end_time) - 1;			// 12.5pm-4am=15.5h

				else

					document.form[line+'_qty'].value = ( 12 - parseFloat(start_time) ) + parseFloat(end_time);		// 8pm-2am=6h

			} else if ( am_or_pm_s == 'am' && am_or_pm_e == 'am' ) {

				if ( start_time == '12' && am_or_pm_s == 'am' ) {

					document.form[line+'_qty'].value = parseFloat(end_time) - ( 12 - parseFloat(start_time) );		// 12am-2am=2h

				} else if ( start_time == '12.5' && am_or_pm_s == 'am' ) {

					document.form[line+'_qty'].value = parseFloat(end_time) - ( 13 - parseFloat(start_time) );		// 12.5am-2am=1.5h

				} else if ( ( end_time == '12' || end_time == '12.5' ) && am_or_pm_e == 'am' ) {

					document.form[line+'_qty'].value = ( 12 - parseFloat(start_time) ) + parseFloat(end_time);		// 2am-12am=22h

				} else {

					if ( parseFloat(end_time) >= parseFloat(start_time) )

						document.form[line+'_qty'].value = parseFloat(end_time) - parseFloat(start_time);			// 11am-8am=3h

					else

						document.form[line+'_qty'].value = 24 - ( parseFloat(start_time) - parseFloat(end_time) );	// 11am-10am=23h

				}

			} else if ( am_or_pm_s == 'pm' && am_or_pm_e == 'pm' ) {

				if ( start_time == '12' && am_or_pm_s == 'pm' ) {

					document.form[line+'_qty'].value = parseFloat(end_time) - (	12 - parseFloat(start_time) );		// 12pm-4pm=4h

				} else if ( start_time == '12.5' && am_or_pm_s == 'pm' ) {

					document.form[line+'_qty'].value = parseFloat(end_time) - (	13 - parseFloat(start_time) );		// 12.5pm-4pm=3.5h

				} else if ( ( end_time == '12' || end_time == '12.5' ) && am_or_pm_e == 'pm' ) {

					document.form[line+'_qty'].value = ( 12 - parseFloat(start_time) ) + parseFloat(end_time);		// 4pm-12pm=20h

				} else {

					if ( parseFloat(end_time) >= parseFloat(start_time) )

						document.form[line+'_qty'].value = parseFloat(end_time) - parseFloat(start_time);			// 5pm-9pm=4h

					else

						document.form[line+'_qty'].value = 24 - ( parseFloat(start_time) - parseFloat(end_time) );	// 11pm-10pm=23h

				}

			}

			if ( document.form[line+'_rate'].value != '' ) {

				l_subtotal(document.form[line+'_qty'].value,document.form[line+'_rate'].value,line.replace('l',''));

		  		do_the_math(document.form.subtot_notax.value,document.form.tax.value,document.form.deposit.value);

		  	}

		}

	}

	// add piano tuning price

	function pianotuning (piano,field) {

		if ( piano == "piano" ) {

			var line = field.split("_");

			var rate = line[0]+'_rate';

			var qty  = line[0]+'_qty';

			document.form[rate].value = 80;

			document.form[qty].value = 1;

			var l_subtot = line[0]+'_subtot';

			document.form[l_subtot].value = 80;

		}

	}

	// caculate subtotal without taxes

	function subtotal_notax() {

<?php

	for ($i=1;$i<11;$i++) {

echo '  		var v'.$i.' = document.form.l'.$i.'_subtot.value;

  		if ( v'.$i.' == "" ) { v'.$i.' = 0; }

';

	}

echo '  		document.form.subtot_notax.value = parseFloat(v1)+parseFloat(v2)+parseFloat(v3)+parseFloat(v4)+parseFloat(v5)+parseFloat(v6)+parseFloat(v7)+parseFloat(v8)+parseFloat(v9)+parseFloat(v10);';

?>

  		document.form.totalowed.value = document.form.subtot_notax.value;

	}

	// caculate subtotal after taxes

	function addTax(notax,taxrate) {

		if ( document.form.addtax.checked ) {

			var tax = notax / 100 * parseFloat(taxrate);

			document.form.taxes.value = Math.round(tax*100)/100;

			document.form.subtot_aftax.value = Math.round((parseFloat(notax) + parseFloat(tax))*100)/100;

			document.form.totalowed.value = Math.round(document.form.subtot_aftax.value*100)/100;

		} else {

			document.form.taxes.value = "";

			document.form.subtot_aftax.value = "";

			document.form.totalowed.value = Math.round(notax*100)/100;

		}

	}

	// caculate totals (owed and paid) after subtracting deposit (if any)

	function grandtotal(dep) {

		if ( document.form.totalpaid.value != 'unpaid' ) {

			if ( dep != "" ) {

				if ( document.form.subtot_aftax.value == "" ) {

					document.form.totalowed.value = Math.round((document.form.subtot_notax.value - parseFloat(dep))*100)/100;

					document.form.totalpaid.value = Math.round((parseFloat(document.form.totalowed.value) + parseFloat(dep))*100)/100;

				} else if ( document.form.subtot_aftax.value != "" ) {

					document.form.totalowed.value = Math.round((document.form.subtot_aftax.value - parseFloat(dep))*100)/100;

					document.form.totalpaid.value = Math.round((parseFloat(document.form.totalowed.value) + parseFloat(dep))*100)/100;

				}

			} else {

				if ( document.form.subtot_aftax.value == "" ) {

					document.form.totalowed.value = Math.round(document.form.subtot_notax.value*100)/100;

					document.form.totalpaid.value = Math.round(document.form.subtot_notax.value*100)/100;

				} else if ( document.form.subtot_aftax.value != "" ) {

					document.form.totalowed.value = Math.round(document.form.subtot_aftax.value*100)/100;

					document.form.totalpaid.value = Math.round(document.form.subtot_aftax.value*100)/100;

				}

			}

		}

	}

	// do all the math every time something is changed

	function do_the_math(notax,taxrate,dep) {

		subtotal_notax();

		addTax(notax,taxrate);

		grandtotal(dep);

	}

	// zero totalpaid if it is unpaid

	function verify_payment() {

		if ( document.form.payment.value == "unpaid" )

			document.form.totalpaid.value = "unpaid";

		if ( document.form.payment.value != "unpaid" )

			document.form.totalpaid.value = document.form.totalowed.value;

	}

	-->

  </script>

  <noscript>Sorry but you need JavaScript enabled for this to work!</noscript>
