//code for running dynamic table used in registration process
var RowCounter = 0;

function toggle()
{
	//Adding a Row advances counter
	if (RowCounter < 10){
		RowCounter = RowCounter + 1;
		
		id = RowCounter;
		Rid = 'Row' + id;
		var tr = document.getElementById(Rid);
		if (tr==null) { return; }
		var bExpand = tr.style.display == '';
		tr.style.display = (bExpand ? 'none' : '');

	} else {
		id = RowCounter;
		Rid = 'Row11';
		var tr = document.getElementById(Rid);
		if (tr==null) { return; }
		var bExpand = tr.style.display == '';
		tr.style.display = '';
		
	}
	
	//Example Text for 1st Row, show header
	if (RowCounter==1)
	{
	Rid = 'Row0';
	var tr = document.getElementById(Rid);
	if (tr==null) { return; }
	var bExpand = tr.style.display == '';
	tr.style.display = (bExpand ? 'none' : '');
	
	Gid = 'GiveTo' + id;
	var tg = document.getElementById(Gid);
	if (tg==null) { return; }
	tg.value = '1'; //Pull this value from dropdownscript if possible
	
	Did = 'Desc' + id;
	var td = document.getElementById(Did);
	if (td==null) { return; }
	td.value = 'Books';
	
	Aid = 'Amount' + id;
	var ta = document.getElementById(Aid);
	if (ta==null) { return; }
	ta.value = '$100';
	
	Qid = 'Quantity' + id;
	var tq = document.getElementById(Qid);
	if (tq==null) { return; }
	tq.value = '10';
	}
	
	tr.focus()
}
function deleterow(id)
{
	rowid = 'Row' + id
	var tr = document.getElementById(rowid);
	if (tr==null) { return; }
	var bExpand = tr.style.display == '';
	tr.style.display = (bExpand ? 'none' : '');

	Did = 'Desc' + id;
	var td = document.getElementById(Did);
	if (td==null) { return; }
	td.value = '';
	
	Aid = 'Amount' + id;
	var ta = document.getElementById(Aid);
	if (ta==null) { return; }
	ta.value = '';
	
	Qid = 'Quantity' + id;
	var tq = document.getElementById(Qid);
	if (tq==null) { return; }
	tq.value = '';
	
	RowCounter = RowCounter - 1;
	//alert(RowCounter);
}


function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function totalCol (col) {
  
	var qty = document.forms[2].elements['itemizeCost[]'][col];
	var cost = document.forms[2].elements['itemizeQuantity[]'][col];
	var total = eval("document.forms[2].elements['itemizeTotal_' + col]");

	if (qty==null) { return; }
	if (cost==null) { return; }
	if (total==null) { return; }

	costval = makenum(cost.value);
		
	total.value = qty.value * costval;
	
	total.value = formatCurrency(total.value);
	
	var totaltest = 0;
	for(x=0;x<10;x++) {
	eval("var total_x = testvalue(document.forms[2].elements['itemizeTotal_' + x])");
	}
	
	for(y=0;y<10;y++) {
	//eval("alert(document.forms[2].elements['itemizeTotal_' + y].value)*1");
  eval("totaltest = totaltest + makenum(document.forms[2].elements['itemizeTotal_' + y].value)*1;");
	
	}	
	
	
	document.forms[2].elements['itemizeTotal_all'].value = formatCurrency(totaltest);

}

function refreshtotal(){
	fundid = 'fundAmount'
	retotalid = 'reTotal'
	var totalall
	var fund = document.getElementById(fundid);
	var retotal = document.getElementById(retotalid);
	if (fund==null) { return; }
	if (retotal==null) { return; }
	
	var total_0 = document.getElementById('itemizeTotal_0');
	var total_1 = document.getElementById('itemizeTotal_1');
	var total_2 = document.getElementById('itemizeTotal_2');
	var total_3 = document.getElementById('itemizeTotal_3');
	var total_4 = document.getElementById('itemizeTotal_4');
	var total_5 = document.getElementById('itemizeTotal_5');
	var total_6 = document.getElementById('itemizeTotal_6');
	var total_7 = document.getElementById('itemizeTotal_7');
	var total_8 = document.getElementById('itemizeTotal_8');
	var total_9 = document.getElementById('itemizeTotal_9');
	
	totalall = makenum(fund.value);
	
	totalall = totalall - makenum(total_0.value);
	totalall = totalall - makenum(total_1.value);
	totalall = totalall - makenum(total_2.value);
	totalall = totalall - makenum(total_3.value);
	totalall = totalall - makenum(total_4.value);
	totalall = totalall - makenum(total_5.value);
	totalall = totalall - makenum(total_6.value);
	totalall = totalall - makenum(total_7.value);
	totalall = totalall - makenum(total_8.value);
	totalall = totalall - makenum(total_9.value);
	totalall = formatcurrency(totalall);
	//alert(totalall);
	
	document.getElementById('itemizeTotal_all').value = totalall;
	

}

function testvalue(field) {
	if (field==null) {return '0'} else {return field.value}
	}

function isNum(str) {
   var test = "0123456789"
   for (i=0; i <= str.length-1; i++) {
      if (test.indexOf(str.charAt(i)) == -1) return false;
   }
   return true;
}

function makenum(num) {
	
	var numval = "";
	numval = num.replace("$","");
	numval = numval.replace(",","");
	if(isNaN(numval)){ 
		return 0
		
	 }
			 
	if(numval>0){
		return numval;
		//alert (numval);
	} else {
		return 0;
	}
	return 0;
	
}


function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }

function submitform(frmname) {
		document.getElementById(frmname).submit();
	
}
