 
    String.prototype.trim = function() {
        return this.replace(/^\s+|\s+$/g, "");
    }
    
    function check_decimal_places(number)
    {
        alert(number);
        var decimal = number.indexOf(".");
        alert("1 " + decimal);
        if (decimal+3 < number.length){
            alert("1 " + decimal);
            alert("Field must have only 2 decimal places")
            return false
        }
        return true;
    }
    
   
    function input_filterAmt (str, dec, bNeg)
   { // auto-correct input - force numeric data based on params.
    var cDec = '.'; // decimal point symbol
    var bDec = false; var val = "";
    var strf = ""; var neg = ""; var i = 0;
   
    if (str == "") return "";
    parseFloat ("0").toFixed (dec);
    if (bNeg && str.charAt (i) == '-') { neg = '-'; i++; }
   
    for (i; i < str.length; i++)
    {
     val = str.charAt (i);
     if (val == cDec)
     {
      if (!bDec) { strf += val; bDec = true; }
     }
     else if (val >= '0' && val <= '9')
      strf += val;
    }
    strf = (strf == "" ? 0 : neg + strf);
    return parseFloat (strf).toFixed (dec);
} 
    
    function validate_report()
    {        
        if (document.monthlyReportForm.LearnerName.value.trim() == "")
        {
            //show an error message
            alert("Please enter a value for the Learner Name");
            document.monthlyReportForm.LearnerName.focus();
            return false;
        }                 
        
        if (document.monthlyReportForm.TutoringSite.value.trim() == "")
        {
            //show an error message
            alert("Please enter a value for the Tutoring Site");
            document.monthlyReportForm.TutoringSite.focus();
            return false;
        }  
        
        if (!isPositiveInteger(document.monthlyReportForm.Year.value.trim()))
        {
            alert("Please enter a positive integer for the year");
            document.monthlyReportForm.Year.focus();
            return false;
        } 
        
        if (document.monthlyReportForm.TutorName.value.trim() == "")
        {
            //show an error message
            alert("Please enter a value for the Tutor's Name");
            document.monthlyReportForm.TutorName.focus();
            return false;
        }  
        
        if (!isInteger(document.monthlyReportForm.NoOfSessions.value.trim()))
        {
            alert("Please enter a integer for the number of sessions");
            document.monthlyReportForm.NoOfSessions.focus();
            return false;
        }  
        
        var noOfSessions = parseFloat(document.monthlyReportForm.NoOfSessions.value.trim());         
        
        if (document.monthlyReportForm.Email.value.trim().length > 0)
        {        
            if (!isValidEmailAddress(document.monthlyReportForm.Email.value.trim()))
	    {
	        alert("Please enter a valid email address");
	        document.monthlyReportForm.Email.focus();
	        return false;
            }
        }
        
        var enteredTime = 0;
        var week = new Array(5);
        var timetable = document.getElementById("timetable");
        for (i = 1; i <= 7; i++)
        {
            for (j = 2; j <= 6; j++)
            {
                var data = timetable.rows[i].cells[j].firstChild.value;
                if (data.trim().length > 0)
                {
                    if (!isNumeric(data))
		    {
		        alert("'" + data + "'  is not a valid number. Please enter a positive number");		         
		        return false;
		    }
		    else
		    {
		       
		        data = parseFloat(data);
		         
		        if (data < 0)
		        {
		            alert("'" + data + "'  is negative. Please enter a positive number");
		            return false;
		        }
		         
		        if (data > 12)
		        {
		            alert("'" + data + "'  is not valid. Please enter a positive number for hours and decimal place followed by minutes. For eg: 40 minutes = 0.4");
		            return false;
		        }
		        
		        
		        if ((i < 3) && (data > 0))
		        {		            
		            week[j - 2] = 1;
		        }
		         
		        
		        var fracPart = roundNumber(Mod(data, 1.0),2); 		        
		        
                        if (fracPart > 0.6)
                        {
                            alert("Invalid value " + data + ". Please enter value in hours and minutes");
                            return false;
                        }
                                                                         
		    }
		    enteredTime = 1;
                }
            }
        }
        
        if (enteredTime == 1)
        {
            if (noOfSessions == 0)
            {
                alert("The number of sessions is zero.");
                return false;
            }
            
            var weekCount = 0;
            for (k=0; k<5; k++)
            {
                if (week[k] == 1)
                {
                    weekCount = weekCount + 1;
                }
            }
            if (noOfSessions < weekCount)
            {
                alert("Please enter the correct number of sessions.");
                return false;
            }             
        }
        
        if (check_url(document.monthlyReportForm.LiteracyGoals.value.trim()))
        {
            alert("Please do not include any url in the section: literacy goals for this month");
            return false;
        }
        
        if (check_url(document.monthlyReportForm.Skills.value.trim()))
	{
	    alert("Please do not include any url in the section: skills you built this month");
	    return false;
        }
        
        if (check_url(document.monthlyReportForm.MaterialsMethods.value.trim()))
	{
	    alert("Please do not include any url in the section: materials and methods used");
	    return false;
        }
        
        if (check_url(document.monthlyReportForm.Questions.value.trim()))
	{
	    alert("Please do not include any url in the section: Other questions, problems, wonderful things...");
	    return false;
        }
        
        if (check_url(document.monthlyReportForm.OtherActivities.value.trim()))
	{
	    alert("Please do not include any url in the Classes, workshops, or other activities you've attended this month");
	    return false;
        }        
         
        
        return true;
    } 
    
    function check_url(input)
    {
        inputStr = input.toString()
        if (inputStr.length == 0) return false;
        var url_regex = /https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?/;
        if (url_regex.test(inputStr))
        {
            return true;
        }
        return false;
    }
    
    function Mod(X, Y) {
        return X - Math.floor(X / Y) * Y;
    }
    
    function roundNumber(num, dec) {
    	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
    	return result;
    }

        
    function isPositiveInteger(input) 
    {  
        inputStr = input.toString()
        if (inputStr.length == 0) return false;
        for (var i = 0; i < inputStr.length; i++) {
            var char1 = inputStr.charAt(i)
            if (char1 < "0" || char1 > "9") {
                return false;
            } 
            else if ((i == 0) && (char1 == "0")) {
                return false;
            }
        }
        return true;
    }
    
    function isInteger(input) 
    {  
        inputStr = input.toString()
        if (inputStr.length == 0) return false;
        for (var i = 0; i < inputStr.length; i++) {
            var char1 = inputStr.charAt(i)
            if (char1 < "0" || char1 > "9") {
                return false;
            }              
        }
        return true;
    } 
    
    function isNumeric(input) 
    {  
        inputStr = input.toString()
        if (inputStr.length == 0) return false;        
        if (isNaN(inputStr)) {
            return false;	 
	}         
        return true;
    }     
    
function emailHandler(o)
{
    alert("Succesfully mailed the monthly report");
}

/*
 * void
 * failureHandler()
 *
 * This is the handler function called when an ajax call fails
*/
function failureHandler(o)
{
    alert("There was some problem in mailing the monthly report");
}

/*
 * Check if the email address is valid
 *  
*/
function isValidEmailAddress(str) 
{
    var at="@";
    var dot=".";
    var lat=str.indexOf(at);
    var lstr=str.length;
    var ldot=str.indexOf(dot);

    if (str.indexOf(at)==-1)
    {       
        return false;
    }

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
    {       
	return false;
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
    {       
        return false;
    }    

    if (str.indexOf(at,(lat+1))!=-1)
    {        
        return false;
    }

    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
    {        
        return false;
    }

    if (str.indexOf(dot,(lat+2))==-1) 
    {         
        return false;
    }
		
    if (str.indexOf(" ")!=-1)
    {        
        return false;
    }
    return true;					
}




 
