window.addEvent('domready',function() {
$('Calculate').addEvent('click', function() {
	var x=1;
	$('endmsg').innerHTML="<div align='center'><h3>Loading...</h3)<br><img src='/images/loading.gif'>";
	$('Calculate').disabled = true;
	$('Calculate').value = "Loading...";
	var options = {}
	$('ccform').getFormElements().each(function(el){
		var name = el.name;
		var value = el.getValue();
		if (name.match("name_") && value=="")
			el.value="Creditor "+x;
		if (name.match("name_")) x++;
 		options['update']='log_update';
 		options['evalScripts']=true;
		if (value === false || !name || el.disabled) options[value] = false;
		else options[value] = true;
	});
	$('ccform').send(options);
});

addField();
var Tips4 = new Tips($$('.Tips0'), {
	className: 'custom',
	offsets: {'x': -145, 'y': -30}
});

});

var limit=25;
 
 var fn = function() {
 	$('Calculate').value="Success!";
	var box = $('endmsg');
	var fx = new Fx.Style(box, 'background-color', {
		duration: 800,
		transition: Fx.Transitions.Quad.easeOut
	}).start('#ffffff','#296517');
	box.setStyle('color','#ffffff');
	box.setStyle('border','1px solid #000000');
	endmsgNormal.delay(15000);
}

function hl_name(id) {
	var box = $('name_'+id);
	box.setStyle('background-color','#FF9999');
	var sc = box.getStyle('background-color');
	var fx = new Fx.Style(box, 'background-color', {
		duration: 800,
		transition: Fx.Transitions.Quad.easeOut
	}).start(sc,'#ffffff');
}

var endmsgErr = function() {
 	$('Calculate').disabled=false;
 	$('Calculate').value='Go!';
	var box = $('endmsg');
	box.setStyle('color','#ffffff');
	box.setStyle('border','1px solid #000000');
	var fx = new Fx.Style(box, 'background-color', {
		duration: 800,
		transition: Fx.Transitions.Quad.easeOut
	}).start('#ffffff','#6B1115');
	endmsgNormal.delay(5000);
}

var endmsgNormal = function() {
 	$('Calculate').disabled=false;
 	$('Calculate').value='Go!';
	var box = $('endmsg');
	var sc = box.getStyle('background-color');
	box.setStyle('color','#4F6B72');
	box.setStyle('border','1px solid #ffffff');
	var fx = new Fx.Style(box, 'background-color', {
		duration: 800,
		transition: Fx.Transitions.Quad.easeOut
	}).start(sc,'#ffffff');
}
function addSelect(nm) {
  var input = document.createElement("select");
  input.id = nm;
  input.name = nm;
  for (i=1; i<29; i++) {
  	var option = document.createElement("option");
	option.text=i;
  	input.appendChild(option);
  }
  return input;
}
function addSpan(el, nm) {
	var input = document.createElement("span");
	input.id= nm;
	input.innerHTML='-';
	el.appendChild(input);
}
function addInput(nm, sz) {
  var input = document.createElement("input");
  input.id = nm;
  input.name = nm;
  input.type = "text"; //Type of field - can be any valid input type like text,file,checkbox etc.
  input.size = sz;
  return input;
}
//Add more fields dynamically.
function addField() {
 area="cctable";
 
 if(!document.getElementById) return; //Prevent older browsers from getting any further.
 var field_area = document.getElementById(area);
 var all_trs = field_area.getElementsByTagName("tr"); //Get all the input fields in the given area.
 //Find the count of the last element of the list. It will be in the format '<field><number>'. If the 
 //  field given in the argument is 'friend_' the last id will be 'friend_4'.
 var last_item = all_trs.length - 1;
 var last = all_trs[last_item].id;
 var count = Number(last.split("_")[1]) + 1;
 if (isNaN(count)) count=1;
 cls = (count%2)?'spec':'specalt';
 //If the maximum number of elements have been reached, exit the function.
 //  If the given limit is lower than 0, infinite number of fields can be created.
 if(count > limit && limit > 0) {
 	alert("Sorry, only "+limit+" entries allowed.");
 	return;
 } 
 if(document.createElement) { //W3C Dom method.
	var tr    = document.createElement('tr');
	var td1   = document.createElement('th');
	var td3   = document.createElement('td');
	var td4   = document.createElement('td');
	var td5   = document.createElement('td');//minpay
	var td6   = document.createElement('td');//new bal
	var td7   = document.createElement('td');//new min payment
	var td8   = document.createElement('td');//payoff date
	var td9   = document.createElement('td');//remove

  tr.id="cc_"+count;
//  td1.appendChild(addSelect('due_'+count));
  td1.appendChild(addInput('name_'+count,10));
  td1.className= cls;
  td1.setAttribute('scope', 'row');
//  td2.appendChild(addInput('name_'+count,10));
  td3.appendChild(addInput('pv_'+count,7));
  td4.appendChild(addInput('apr_'+count,3));
  td5.appendChild(addInput('minpay_'+count,7));
  addSpan(td6,'newbal_'+count);
  addSpan(td7,'newminpay_'+count);
  addSpan(td8,'payoffdate_'+count);
  td9.innerHTML='<span id="extra_'+count+'"><img src="/images/remove.png" class="pointer" onclick="removeLine(this);" id="Remove" alt="Remove this Creditor" /><img src="/images/blank.gif" width="10" height="1" alt="" /></span>';
  if (cls=='specalt') {
//    td2.setAttribute('class', 'alt');
    td3.className='alt';
    td4.className='alt';
    td5.className='alt';
    td6.className='alt';
    td7.className='alt';
    td8.className='alt';
    td9.className='alt';
  }
  tr.appendChild(td1);
//  tr.appendChild(td2);
  tr.appendChild(td3);
  tr.appendChild(td4);
  tr.appendChild(td5);
  tr.appendChild(td6);
  tr.appendChild(td7);
  tr.appendChild(td8);
  tr.appendChild(td9);
	
  field_area.appendChild(tr);
  $('name_'+count).focus();

 } else {
 	alert("Sorry, your brower does not support our application.");
 }
}

function removeRemoves(){
	$$('#Remove').setStyle('visibility', 'hidden');
}
function removeLine(el) {
	if (confirm("Are you sure you wish to permanenty delete this Creditor?"))
	el.parentNode.parentNode.parentNode.parentNode.removeChild(el.parentNode.parentNode.parentNode);
}