	function validateForm(loanForm) 
	{
   		if(!check_empty(document.loanForm.loanAmount, "Loan amount cannot be empty!"))
		{
		   	return false;
		}

    	if(!digit(document.loanForm.loanAmount.value))  
	    {
	       alert("Please enter only valid digit for loan amount!");
	       return false;
		}
		  
	    if(!check_empty(document.loanForm.interestRate, "Interest Rate cannot be empty!"))
	       return false;
		
    	if(!digit(document.loanForm.interestRate.value))  
	    {
	       alertMsg("Please enter only valid digit for interest rate!");
	       return false;
		}
		
		if(!check_empty(document.loanForm.loanPeriod, "Loan Period cannot be empty!"))
	      return false;

    	if(!digit(document.loanForm.loanPeriod.value))  
	    {
	       alertMsg("Please enter only valid digit for loan period!");
	       return false;
		}
       calculate();
   }
	
	function check_empty(field,alert_msg) 
	{
    	if(empty(field.value)) 
      	{
       		alertMsg(alert_msg);
      		return false; 
      	}
    return true;
	}
	
	function empty(string) 
	{
    	if(string.length==0)
      	{
       		return true;
     	}
    	else
      	{
       		for(i=0;i< string.length;i++)
         	{ 
				ch=string.substring(i,i+1);
           	
				if (ch != " ") 
            	{
              		return false;
            	}
        	 }
      	return true;   
      	}
	} 
	
	function digit(string) 
	{
    	var i;
    	var chr;
   		
		for (i = 0; i < string.length; i++) 
		{
        	chr = string.substring(i, i+1);
        	
			if (!validDigit(chr)) 
			{
            	return false;
			}
    	}
    	
		return true;
	}
	 
	function validDigit(ch) 
	{
   		if ((ch != ".") && (ch != "0") && (ch != "1") && (ch != "2") && (ch != "3") && (ch != "4") &&
        	(ch != "5") && (ch != "6") && (ch != "7") && (ch != "8") && (ch != "9"))
 		{
        	return false;
		}
    	else 
		{
        	return true;
    	}
	}
	 
	function alertMsg(msg) 
	{
   		window.alert(msg);   		
	} 
	
	function calculate() 
	{
 		TotalLoan = document.loanForm.loanAmount.value;
		Result = TotalLoan * (document.loanForm.interestRate.value/1200);
		down= 1-(1/(Math.pow(((document.loanForm.interestRate.value/1200) + 1), (document.loanForm.loanPeriod.value*12))));
		document.loanForm.payment.value = calcRound(Result/down);
 	}
	
	function calcRound(num) 
	{
		result=Math.floor(num)+".";
     	n = result.length;

     	var cents=100*(num-Math.floor(num))+0.5;

     	result += Math.floor(cents/10);
		result += Math.floor(cents%10);

    	return(result)
    }