/*
* This is the CMinder namespace.  All js objects and methods should be placed in this
* namespace to avoid collisions with other functions and objects.
*/
// Utility requires CMinder.Module version 0.1
CMinder.Module.require("CMinder.Module", 0.1);

// Create a namespace for our Class module
CMinder.Module.createNamespace("CMinder.ValidationUtilities");

CMinder.ValidationUtilities = {
/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
    // Declaring valid date character, minimum year and maximum year
    dtCh: "/",
    minYear: 1900,
    maxYear:2100,

    isInteger:function(s){
        var pattern = /^[0-9]+$/;
        var b = pattern.test(s);        
        return b;
    },

    stripCharsInBag:function(s, bag){
	    var i;
        var returnString = "";
        // Search through string's characters one by one.
        // If character is not in bag, append to returnString.
        for (i = 0; i < s.length; i++){   
            var c = s.charAt(i);
            if (bag.indexOf(c) == -1) returnString += c;
        }
        return returnString;
    },

    daysInFebruary:function(year){
	    // February has 29 days in any year evenly divisible by four,
        // EXCEPT for centurial years which are not also divisible by 400.
        return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
    },
    getDaysArray:function(n) {
        var dArr=new Array();
	    for (var i = 1; i <= n; i++) {
		    dArr[i] = 31
		    if (i==4 || i==6 || i==9 || i==11) {dArr[i] = 30}
		    else if (i==2) {dArr[i] = 29}
       } 
       return dArr;
    },

    isDate:function(dtStr){
	    var daysInMonth = this.getDaysArray(12)
	    var pos1=dtStr.indexOf(this.dtCh)
	    var pos2=dtStr.indexOf(this.dtCh,pos1+1)
	    var strMonth=dtStr.substring(0,pos1)
	    var strDay=dtStr.substring(pos1+1,pos2)
	    var strYear=dtStr.substring(pos2+1)
	    var validationResult = {
           isValid: false,
           message: ''
        }
	    
	    strYr=strYear
	    if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	    if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	    for (var i = 1; i <= 3; i++) {
		    if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	    }
	    month=parseInt(strMonth)
	    day=parseInt(strDay)
	    year=parseInt(strYr)
	    if (pos1==-1 || pos2==-1){
		    validationResult.message = "The date format should be : mm/dd/yyyy.";
		    return validationResult;
	    }
	    if (strMonth.length<1 || month<1 || month>12){
		    validationResult.message = "Please enter a valid month.";
		    return validationResult;
	    }
	    if (strDay.length<1 || day<1 || day>31 || (month==2 && day>this.daysInFebruary(year)) || day > daysInMonth[month]){
		    validationResult.message = "Please enter a valid day.";
		    return validationResult;
	    }
	    if (strYear.length != 4 || year==0 || year<this.minYear || year>this.maxYear){
		    validationResult.message = "Please enter a valid 4 digit year between " + this.minYear + " and " + this.maxYear + ".";
		    return validationResult;
	    }	    

	    if ((dtStr.indexOf(this.dtCh,pos2 + 1)==-1) && (this.isInteger(this.stripCharsInBag(dtStr, this.dtCh)))){
		    validationResult.isValid = true;    
		    return validationResult;
	    }        

        validationResult.message = "Please enter a valid date.";        
        return validationResult;
    }
}
