// JavaScript Document 
 
//check field value
function checkNull(obj, field){
   if(obj.value.trim()==""){
        markInvalid(obj);
    if( field =='' )
        alert("Please enter a value in the highlighted field");
    else
        alert("Please enter a value for " + field);
        return false;
   }
   obj.style.color="#ffffff";
    obj.style.backgroundColor = "#303030";
    return true;
}
 
 
function checkRange(obj, field, min){
   if(obj.value.trim()!=""){
     if((obj.value)<min || isNaN(obj.value))
     {
        markInvalid(obj);
    if( min == 1)
        alert(field + " must be greater than 0");
    else
        alert(field + " must be equal or greater than 0");
        return false;
     }
     else//valide
     {
        obj.style.color="#ffffff";
        obj.style.backgroundColor = "#303030";
        return true;
     }
   }
}
//focus and style change for indicating error
function markInvalid(obj){
        obj.style.color="#ff0000";
        obj.style.backgroundColor = "#ffffff";       
        obj.focus();
        obj.select();
}
//trim function for string
String.prototype.trim = function(){return this.replace(/^\s+|\s+$/g,"");}
 
 
function ConfirmSubmit()
{
    txtamountDesired=document.getElementById("Viewloancalc_amountDesired");
    txtnrOfYears=document.getElementById("Viewloancalc_nrOfYears"); 
    txtinterestRate=document.getElementById("Viewloancalc_interestRate");    
    txtmonths = document.getElementById("Viewloancalc_TextBox4"); 
    
    if(checkNull(txtamountDesired,  "Loan Amount")!=true)return false;    
 
 
    // if both are empty
    if( (txtnrOfYears.value.trim() == '' && txtmonths.value.trim() == '') )    
    {
    alert("Please enter number of years or months");
    markInvalid(txtmonths);
    markInvalid(txtnrOfYears);
    return false;
    }
    /*
    //cant provide both
    if( (txtnrOfYears.value.trim() != '' && txtmonths.value.trim() != '') )    
    {
    alert("Please enter either number of years or months");
    markInvalid(txtmonths);
    markInvalid(txtnrOfYears);
    return false;
    }*/
 
    if( txtnrOfYears.value.trim() !='' && checkRange(txtnrOfYears, "Number of years", 0) != true ) return false; 
    if( txtmonths.value.trim() !='' && checkRange(txtmonths, "Number of months", 1) != true ) return false; 
  
   
    if(checkNull(txtinterestRate,"Interest rate")!=true)return false;    
        
    if(checkRange(txtamountDesired, "Loan Amount", 1)!=true)return false;    
    if(checkRange(txtinterestRate, "Interest rate", 0)!=true)return false;
    
    return true;
}
 
 
function ConfirmSubmit2()
{
    txtaddMonthly=document.getElementById("Viewloancalc_addMonthly");
    txtaddYearly=document.getElementById("Viewloancalc_addYearly"); 
    txtaddOnce=document.getElementById("Viewloancalc_addOnce");    
 
    
    if(checkNull(txtaddMonthly)!=true)return false;      
    if(checkNull(txtaddYearly)!=true)return false;    
    if(checkNull(txtaddOnce)!=true)return false;    
 
        
    if(checkRange(txtaddMonthly, "Additional Monthly payment",0)!=true)return false;    
    if(checkRange(txtaddYearly, "Additional Yearly payment", 0)!=true)return false;
    if(checkRange(txtaddOnce, "Additional One Time payment", 0)!=true)return false;
    
    return true;
}
function mon(val1){
            if(val1=="Viewloancalc_TextBox4"){
                var months;
                var year;
                months=document.getElementById(val1).value;
                year=months/12;
                document.getElementById("Viewloancalc_nrOfYears").value=year;
            }
            if(val1=="Viewloancalc_nrOfYears"){
                var months;
                var year;
                year=document.getElementById(val1).value;
                months=year*12;
                document.getElementById("Viewloancalc_TextBox4").value=months;
            }
        }