//<![CDATA[

var state = 0;
var pmtcount = 1;
var tblids = 100;
var custom = true;

function openMenu() {
        if (state == 0) {
          state++;
		  //state++;
		  handleState();
		  return;
        }
		
		if (state == 1) {
		  state++;
		  handleState();
		  return;
		}
		
		if (state == 2) {
		  state = 0;
		  handleState();
		}
		
		if (state == 3) {
		  state = 0;
		  handleState();
		}
}

function backMenu() {
		
		if (state == 1) {
		  state--;
		  handleState();
		  return;
		}
		
		if (state == 2) {
		  state--;
		  handleState();
		  return;	
		}
		if (state == 3) {
		  state = 1;
		  handleState();
		}
}

function calculate() {
	var len = document.getElementById('sch').value;
	var amount = document.getElementById('amthold').value;
	state = 3;
	handleState();
	
	var current = new Date();
	var month = current.getMonth()+1;
	var day = current.getDate();
	
	var year = current.getFullYear();
	
	var output = "<table width=\"90%\"><tr><td><input type=\"button\" value=\"Close\" onclick=\"openMenu()\"><input type=\"button\" value=\"Back\" onclick=\"backMenu()\"></td></tr>";
	
	for(var x = 1; x <= len; x++)
	{
		month++;
		if(month == 13)
		{month = 1; year++;}
		
		if(month == 2)
		{
			if(day > 28)
			{day = 28;}
		}
		if(month == 4 || month == 6 || month == 9 || month == 11)
		{
			if(day > 30)
			{day = 30;}
		}
		var fulldate =  month + "/" + day + "/" + year;
		
		output += "<tr><td>" + month + "/" + day + "/" + year + "<input type=\"hidden\" name=\"cd" + x + "\" value=\"" + fulldate + "\">";
		output += "</td><td>Amount: <input type=\"text\" name=\"n" + x + "\" size=\"12\" value=\"" + amount + "\"></td></tr>";
		day = current.getDate();
	}
			
	
	output += "</table><br>";
	document.getElementById('fixed').innerHTML = output;
	document.getElementById('type').value = "fixed";
	
}

function handleState() {
        if (state == 0) {
		  document.getElementById('repeat').style.display = "none";
		  document.getElementById('openRepeat').style.display = "block";
		  document.getElementById('done').style.display = "none";
		  document.getElementById('radio').style.display = "none";
		  document.getElementById('sub').style.display = "none";
		  document.getElementById('fixed').style.display = "none";
        }
		if (state == 1) {
		  document.getElementById('repeat').style.display = "none";
		  document.getElementById('openRepeat').style.display = "none";
		  document.getElementById('done').style.display = "block";
		  document.getElementById('radio').style.display = "block";
		  document.getElementById('sub').style.display = "none";
		  document.getElementById('fixed').style.display = "none";
		}                                              
		if (state == 2) {
		  document.getElementById('repeat').style.display = "block";
		  document.getElementById('openRepeat').style.display = "none";
		  document.getElementById('done').style.display = "block";
		  document.getElementById('radio').style.display = "none";
		  document.getElementById('sub').style.display = "block";
		  document.getElementById('fixed').style.display = "none";
		  custom = true;
		  document.getElementById('type').value = "cust";
		}
		if(state == 3) {
		  document.getElementById('repeat').style.display = "none";
		  document.getElementById('openRepeat').style.display = "none";
		  document.getElementById('done').style.display = "block";
		  document.getElementById('radio').style.display = "none";
		  document.getElementById('sub').style.display = "block";
		  document.getElementById('fixed').style.display = "block";
		  custom = false;
		  document.getElementById('type').value = "fixed";
		}
}

function addPayment() {
	var amount = document.getElementById('amthold').value;
	if(pmtcount < 12)
	{
		pmtcount++;
		var pmthtml = "<table width=\"90%\" id=\"";
			pmthtml += tblids;
			pmthtml += "\" ><tr class=\"pmt\" ><td width=\"50%\">"; 
			pmthtml += pmtcount;
			pmthtml += ". Payment Date: <p class=\"small\" onclick=\"removePayment(";
			pmthtml += tblids;
			pmthtml += ")\">remove<\/p><\/td><td width=\"50%\"><input id=\"d";
			pmthtml += pmtcount;
			pmthtml += "\" type=\"text\" value=\"\" name=\"theDate";
			pmthtml += pmtcount;
			pmthtml += "\"><img width=\"23px\" height=\"23px\" src=\"images\/minical.gif\" onclick=\"displayCalendar(document.forms[0].theDate";
			pmthtml += pmtcount;
			pmthtml += ",'mm/dd/yyyy',this)\" \/>Amount: <input type=\"text\" name=\"amt" + pmtcount + "\" size=\"12\" value=\"" + amount + "\"></td></tr><\/td><\/tr><\/table>";
		
		var content = document.getElementById('repeat').innerHTML;
		
		content += pmthtml;
	
		document.getElementById('repeat').innerHTML = content;
	
		tblids++;
	}
}

function removePayment(tblid) {
      var Parent = document.getElementById(tblid);
      while(Parent.hasChildNodes())
      {
		Parent.removeChild(Parent.firstChild);
      }
	  pmtcount--;
}
	  
handleState(); 

function validateForm(sched)
{
    if(custom == true)
	{
		for(var x = 1; x <= pmtcount; x++)
		{   
			var n = "d" + x;
			
			if(document.getElementById(n).value == "")
			{
				alert('Payment date field ' + x + ' is incomplete, please enter a date.');
				return false;
			}
		}
	}
}

function TestMenu()
{
	dRequest("test");
}

function open_help()
{
	window.open("payonline_help.htm","_blank","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=yes, width=500, height=400")
}

function dRequest(x) {
    document.getElementById('dbstuff').innerHTML = '<i>Establishing Connection</i><br /><img src="images/ajax-loader.gif" />';
	new Ajax.Request("http://www.checkmyhospitalbill.com/asptest.asp",
	{
		method: 'post',
		postBody: 'test',
		onComplete: showDBstuff
	});
}
function showDBstuff(req) {
	document.getElementById('dbstuff').innerHTML = req.responseText;
}

/*
var winRef;
function open_print_friendly()
{
	winRef=window.open("print_confirmation.htm","_blank","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=yes, width=600, height=700")
	send_print_friendly();
}

function send_print_friendly()
{
	//var input = document.getElementById('confirmtbl').innerHTML;
	//window.open("print_confirmation.htm","_blank","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=yes, width=600, height=700")
	//document.getElementById('print').innerHTML = input;
	var g=document.getElementById("confirmtbl").innerHTML;
	winRef.document.getElementById("printtbl").innerHTML = g;
}*/
	 
//]]>

