/**
*	catalina.js		25-feb-09
*	support: greg@catalina-it.com.au
*
*	Revised 19-may-10 - now just 2 boxes and new calcs
*/

function	recalcAmounts()
{
	var	mn;
	var	prop;
	var apra;
	
	mn = parseInt(document.getElementById('membershipNumber').value);
	if(mn <= 0)
	{
		document.getElementById('proposedPPCA').value = "";
		//document.getElementById('APRAFee').value = "";
	}
	else
	{
		prop = mn * 15 * 52;
		//prop = mn * 4.54*12;  // old
		//apra = prop*2;
		
		prop = parseInt(prop * 100);
		//apra = parseInt(apra * 100);
		
		//if(apra > 265400) apra = 265400;
		
		 dollarAmount('proposedPPCA', prop);
		// dollarAmount('APRAFee',apra);
		//document.getElementById('proposedPPCA').value = formatDecimals(prop, 2);
		//document.getElementById('APRAFee').value = formatDecimals(apra, 2);
	}
}
/**
* used to calc totals in artist show input
*/
function	calculateArtistShowTotals()
{
	cost = parseFloat(document.getElementById('cost').value);
	fee =  parseFloat(document.getElementById('bookingFee').value);
	total = cost + fee;
	//alert("cost " + cost + " fee " + fee + " total " + total);
	
	document.getElementById('totalSpan').innerHTML = formatDecimals(total, 2);
}
/*
*	used on field entry of money - NO NEGATIVE ALLOWED
*/

function currencymask(t)
{

	var patt = /(\d*)\.{1}(\d{0,2})/;
	var donepatt = /^(\d*)\.{1}(\d{2})$/;   // old
	try
	{
		var str = t.value;
	}
	catch(e)
	{
		alert(e.name);
	}
	var result;
	if (!str.match(donepatt))
	{
		result = str.match(patt);
		if (result!= null)
		{
			t.value = t.value.replace(/[^\d]/gi,'');
			str = result[1] + '.' + result[2] ;
			t.value = str;
		}
		else
		{
			if (t.value.match(/[^\d]/gi))
				t.value = t.value.replace(/[^\d]/gi,'');
		}
	}
	t = formatAsMoney(t);
}
/*
*	used on field entry of money BUT ALLOWS NEGATIVE leading
*/

function currencymaskWithNegative(t)
{
	var patt = /(-?\d*)\.{1}(\d{0,2})/;
	var donepatt = /^(-?\d*)\.{1}(\d{2})$/;   //
	try
	{
		var str = t.value;
	}
	catch(e)
	{
		alert(e.name);
	}
	var result;
	if (!str.match(donepatt))
	{
		result = str.match(patt);
			//alert("result 1 = " + result[1] + " result 2 = " + result[2]);

		if (result!= null)
		{
			t.value = t.value.replace(/[^\d-]/gi,'');
			str = result[1] + '.' + result[2] ;
			t.value = str;
		}
		else
		{
			if (t.value.match(/[^\d-]/gi))
				t.value = t.value.replace(/[^\d-]/gi,'');
		}
	}

	t = formatAsMoney(t);

}

/*
*	use for input of integers, including minus sign
*/

function integerMask(t)
{
	// ALL TODO
	var patt = /(\d*)/;
	var donepatt = /^(\d*)-?$/;   // old
	//var donepatt = /^-?(\d*)\.{1}(\d{2})$/
	var str = t.value;
	var result;

	// when doesnt match - ie. when nota all integers
	if (!str.match(donepatt))
	{
		result = str.match(patt);
		if (result!= null)
		{
			t.value = t.value.replace(/[^\d]/gi,'');
		}
		else
		{
			if (t.value.match(/[^\d]/gi))
				t.value = t.value.replace(/[^\d]/gi,'');
		}

	}
}

// use with onChange as no good for reformatting char by char
// gets passed a value which gets returned formatted
//
function formatAsMoney(mnt)
{
	amt = new String(mnt);
	value = formatDecimals(amt, 2);

	if(value = 'N.aN') value = '00';
	return value;

	/// old 27-mar-06 ----
	value = 0;

	try
	{
		// turn into cents then round to integer
		if(isNaN(mnt)) mnt = 0;

		value = formatDecimals(mnt, 2);
	}
	catch(e)
	{
		alert("error in formatAsMoney " + e.name);
	}

	return value;

}

function	formatAsMoneyWhenFinished(mnt)
{
	x = formatDecimals(mnt.value, 2);
	if(x == 'N.aN') x = '.00';   // note differs for integer
	mnt.value = x;
}
function	formatAsIntegerWhenFinished(mnt)
{
	x = (mnt.value);
	if (isNaN(parseInt(x)) == true) x = 0;
	if(x == '') x = 0;
	mnt.value = x;
}

// generic positive number decimal formatting function
// gets an object, and returns a
//

 function formatDecimals(expr, decplaces)
 {

 	neg = 0;

 	// 27-mar-06
	 if(expr.value < 0)
	 {

	 	expr.value = - expr.value;
	 	neg = 1;

	 //	alert("was neg" + expr.value);

	 }



	 // decide if expression negative by trailing - sign
	 // if so we tak it off and remember it
	// neg = 0;
	 len = expr.length;
	 if(len > 1)
	 {
		 last = expr.charAt(len-1);
		 if(last == '-')
		 {
		 	neg = 1;
			expr = expr.substring(0,len-1);
		 }
		 else		// or at start
		 if(expr.charAt(0) == '-')
		 {
		 	neg = 1;
			expr = expr.substring(1,len);
		 }

	 }

         // raise incoming value by power of 10 times the
         // number of decimal places; round to an integer; convert to string
		 // eg     1.35 to 2 dec pts   is 1.35 * (10 to power of 2)  = 135  effectively cents in money
         var amt = (eval(expr) * Math.pow(10,decplaces));

		// floating pt calcs go wrong so that  .405 * 100 = .4049999996
		if(amt > 0)
			amt += .0001;

	     var amt2 = Math.round(amt);

 		var str = "" + amt2;

         // pad small value strings with zeros to the left of rounded number
		 // eg. if we had 2  it would be make 02 for 2 dec places ( ithink - it looks like it will be 002)
        while (str.length < decplaces)
		{
           str = "0" + str;
         }
         // establish location of decimal point
         var decpoint = str.length - decplaces;
         // assemble final result from: (a) the string up to the position of
         // the decimal point; (b) the decimal point; and (c) the balance
         // of the string. Return finished product.
  		val = str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);

  		//if only .89 make 0.89
  		//if(val.length == 3)
  		//{
  			//val = '0' + val;
  		//}

  		if(neg == 1)
		{
			val = '-' + val;
		}
		// dmc weant a leading zero so comment out
		// i dont want to work out why we have a leading zero like 0.00, but I want it off
		// so here we go
		if(val.substring(0,1) == '0')
		{
		//	val = val.substring(1);
		}

		return val;

      }

 <!--
// == This Script Free To Use Providing This Notice Remains == //                                                                 
// == This Script Has Been Found In The http://www.DesignerWiz.com Javascript Public Archive Library == // 
// == NOTICE: Though This Material May Have Been In A Public Depository, Certain Author Copyright Restrictions May Apply == //
--><script language="JavaScript" type="text/javascript">
<!-- Begin
function checkNum(data) {      // checks if all characters 
var valid = "0123456789.";     // are valid numbers or a "."
var ok = 1; var checktemp;
for (var i=0; i<data.length; i++) {
checktemp = "" + data.substring(i, i+1);
if (valid.indexOf(checktemp) == "-1") return 0; }
return 1;
}
function dollarAmount(field, value) { 
var Num;
value = value / 100;
Num = new String(value);
dec = Num.indexOf(".");
end = ((dec > -1) ? "" + Num.substring(dec,Num.length) : ".00");
Num = "" + parseInt(Num);
var temp1 = "";
var temp2 = "";
if (checkNum(Num) == 0) {
alert("This does not appear to be a valid number.  Please try again.");
}
else { 
if (end.length == 2) end += "0";
if (end.length == 1) end += "00";
if (end == "") end += ".00";
var count = 0;
for (var k = Num.length-1; k >= 0; k--) {
var oneChar = Num.charAt(k);
if (count == 3) {
temp1 += ",";
temp1 += oneChar;
count = 1;
continue;
}
else {
temp1 += oneChar;
count ++;
}
}
for (var k = temp1.length-1; k >= 0; k--) {
var oneChar = temp1.charAt(k);
temp2 += oneChar;
}
temp2 = "$" + temp2 + end;
eval("document.getElementById('" + field + "').value = '" + temp2 + "';");
}
}
//  End --></script>


