function Add()
{
    var newRow;
	try
	{
		newRow = document.getElementById("InvoiceTable").insertRow();
	}
	catch(e)
	{
		newRow = document.createElement('TR');
		document.getElementById("InvoiceTable").appendChild(newRow);
	}

	for(var num=0;num<4;num++)
	{
		var newCell;
		try
		{
			newCell = document.createElement('TD');
			newRow.appendChild(newCell);
		}
		catch(e)
		{
			newCell = newRow.insertCell();
		}
	}
	
	var tagTextDescription = document.createElement('SPAN');
    tagTextDescription.innerHTML="<b>Invoice Number :</b>";
    tagTextDescription.className="";
    newRow.cells[0].appendChild(tagTextDescription);

	var tagText;
	try
	{
		tagText = document.createElement('<input name="TextBox" type="text" class="style1"/>');
	}
	catch(e)
	{
		tagText	= document.createElement('INPUT');
		tagText.className="style1";
		tagText.name="TextBox";		
	}
	newRow.cells[1].appendChild(tagText);
	
	var tagAmountDescription = document.createElement('SPAN');
    tagAmountDescription.innerHTML="<b>Amount :</b>";
    tagAmountDescription.className="";
    newRow.cells[2].style.textAlign="right";
    newRow.cells[2].appendChild(tagAmountDescription);

	var tagAmount;
	try
	{
		tagAmount = document.createElement('<input name="Amount" type="text" class="style1" style="text-align:right"/>');
	}
	catch(e)
	{
		tagAmount = document.createElement('INPUT');
		tagAmount.className="style1";
		tagAmount.name="Amount";
		tagAmount.style.textAlign="right";
	}
	tagAmount.onblur = function() {CheckAmount.apply(null, [tagAmount]);  };
    newRow.cells[3].appendChild(tagAmount);
}

function Process()
{
	var strTransNum = "";
	var intAmount = 0;
    var InvoiceNum = document.getElementsByName('TextBox');
    var InvoiceAmount = document.getElementsByName('Amount');
    
    if ( document.getElementById('Name').value== null || document.getElementById('Name').value=='')
    {
        alert("Customer Name cannot be blank.\n");
        document.getElementById('Name').focus();
        return;
    }
    
    if (InvoiceNum[0].value == null || InvoiceNum[0].value=='')
    {
        alert("Invoice Number cannot be blank.\n");
        InvoiceNum[0].focus();
	    InvoiceNum[0].select();
        return;
    }
    if (InvoiceAmount[0].value == null || InvoiceAmount[0].value == '')
    {
        alert("Amount cannot be blank.\n");
        InvoiceAmount[0].focus();
	    InvoiceAmount[0].select();
        return;
    }
    
	for(var num=0;num<InvoiceNum.length;num++)
	{
	    var boolInvoiceNum=(InvoiceNum[num].value == null || InvoiceNum[num].value=='');
	    var boolInvoiceAmount=(InvoiceAmount[num].value == null || InvoiceAmount[num].value == '');
	    		
	    if (boolInvoiceNum == boolInvoiceAmount)
	    {
	        if (!boolInvoiceAmount)
	        {
	            intAmount += parseFloat(InvoiceAmount[num].value);
	        }
	        if (!boolInvoiceNum)
	        {
	            strTransNum = strTransNum + InvoiceNum[num].value + ",";
	        }
	    }
	    else
	    {
	        if (boolInvoiceAmount)
	        {
	            alert("Amount cannot be blank.\n");
	            InvoiceAmount[num].focus();
			    InvoiceAmount[num].select();
	            return;
	        }
	        
	        if (boolInvoiceNum)
	        {
	            alert("Invoice Number cannot be blank.\n");
	            InvoiceNum[num].focus();
			    InvoiceNum[num].select();
	            return;
	        }
	    }		
	}
	strTransNum = strTransNum.substring(0,strTransNum.length-1);

    document.getElementById('InvoiceDiv').style.display='none';
	document.getElementById('ProcessDiv').style.display='block';
	
    var processURL = "ProcessPage.php?No="+strTransNum+"&TotalAmount="+Format2Decimal(intAmount);
    processURL = processURL + "&Name=" + document.getElementById("Name").value; 
    document.getElementById('ProcessPage').src=processURL;
}

function Back()
{
    document.getElementById('ProcessDiv').style.display='none';
    document.getElementById('ProcessPage').src=null; 
    document.getElementById('InvoiceDiv').style.display='block';
}

function CheckAmount(obj)
{
    var reg = new RegExp("^\\d{1,20}(\\.\\d{1,2})?$");
    var intTotalAmount = 0.00;
    var InvoiceAmount = document.getElementsByName('Amount');
    
    if (obj.value!=null && obj.value!='')
    {
        if (!reg.test(obj.value))
        {
            alert('The amount must be digit with 2 decimal.');
            if (document.all)
            {
                obj.focus();
            }
            else
            {
                setTimeout( function(){ obj.focus(); }, 0);
            }
        }
        else
        {
            for(var num=0;num<InvoiceAmount.length;num++)
	        {
	            if (InvoiceAmount[num].value != null && InvoiceAmount[num].value != '')
	            {
	                intTotalAmount += parseFloat(InvoiceAmount[num].value);
	            }
	        }
	        
	        document.getElementById('totalAmount').innerHTML = Format2Decimal(intTotalAmount);
        }
    }
}
function OpenPage()
{
	 window.open("Pay-Invoices.html",'','width=700,height=650,status=no,resizable=yes,top=100,left=200,location=no,menubar=no,scrollbars=no,toolbar=no');
}

function Format2Decimal(num)
{
    num = num.toString();
    var startIndex=0;
    var strdigit=0;
    if(num.indexOf(".")==-1)
    {
        num = num + ".00";
    }
    else
    {
        startIndex=num.lastIndexOf(".");
        strdigit=num.substr(startIndex);
        if(strdigit.length==2)
        {
            num = num + "0";
        }
    }
    return num;
}

var obj;
var obj2;

function Print()
{ 
    obj=document.getElementById('ResultMessage').parentNode;
    obj2=document.getElementById('ResultMessage');
    obj.removeChild(obj2);  
	window.print();   
    setTimeout(DisplayBtn,1000);
}
function DisplayBtn()
{
   obj.appendChild(obj2);
}

function validate(theform)
{
	if (theform.email.value == "")
	{
		alert("Please enter Email address.\n\n");
	    if (document.all)
        {
            document.getElementById('email').focus();
        }
        else
        {
            setTimeout( function(){ document.getElementById('email').focus(); }, 0);
        }
		return false
	}
	if (theform.email.value.indexOf ('@',0) == -1 || theform.email.value.indexOf ('.',0) == -1)
	{
		alert("The Email field requires a \"@\" and a \".\" to be used.\n Please re-enter your E-mail address.\n\n");
	     if (document.all)
        {
            document.getElementById('email').focus();
        }
        else
        {
            setTimeout( function(){ document.getElementById('email').focus(); }, 0);
        }
		return false
	 }
	 return true;
}
