/*********************************************************************************
//-----------------------------------------------------------------------------
// Class......: Validation Util
//-----------------------------------------------------------------------------
// Notes......: This file contains the validation javascript functions.
// Req........: - References are made to a static name 'JSValidationUtil'
//                -> JSValidationUtil = new JSValidationUtil();           
//-----------------------------------------------------------------------------
 ********************************************************************************/

// CONSTRUCTOR
function JSValidationUtil() {

// COMMON VALIDATION FUNCTIONS

	//-----------------------------------------------------------------------------
	/** 
	 * Common way of displaying validation missing messages
	 */
    this.constructMissingMessage = function(element) {
        return "Missing Required Fields: " + element + "\n";
    }

	//-----------------------------------------------------------------------------
	/** 
	 * Common way of displaying validation messages
	 */
    this.constructInvalidMessage = function(element) {
        return "Invalid Fields: " + element + "\n";
    }
    
	//-----------------------------------------------------------------------------
	/** 
	 * Common way of display step error messages
	 */
    this.constructStepError = function(step, message) {
        return "Step " + step + ": " + message + "\n";
    } ;
      
    //-----------------------------------------------------------------------------
    /**
     * Sets the status for instructions
     */
    this.setInstStatus = function(step, status) {
    	var elementName = 'instStep' + step;
    	var className = '';
    	if(status) {
			className = 'instComplete';
    	}
    	else {
    		className = 'instIncomplete';
    	}

	   	document.getElementById(elementName).className = className;	    	
    };
    
	//-----------------------------------------------------------------------------
	/** 
	 * Common error check and alert 
	 */
    this.checkError = function(errorMessage) {
        if(errorMessage == "") {
            return true;
        }
        else {
            alert(errorMessage);
            return false;
        }
    }

// CUSTOMISED FUNCTIONS

	// Extended check for a positive Decimal Type
	this.isPositiveDecimal = function(value, decimalPlaces) {
		if(value != '') {
            if(this.isDecimal(value, decimalPlaces)) {
                // Test if the value is positive
                if(value > 0.00) {
                    return true;
                }
                else {
                    return false;
                }
            }
            else {
                return false;				
            }
        }
        else {
            return true;
        }            
	}
	
	// Extended check for a positive Decimal Type
	this.isPositiveDecimalAnyPlaces = function(value) {
		if(value != '') {
            if(this.isDecimalAnyPlaces(value)) {
                // Test if the value is positive
                if(value >= 0.00) {
                    return true;
                }
                else {
                    return false;
                }
            }
            else {
                return false;				
            }
        }
        else {
            return true;
        }            
	}	

	// Extended check for a positive Integer Type
	this.isPositiveInteger = function(value) {
  		if(value != '') {
            if(this.isInteger(value)) {
                // Test if the value is positive
                if(value > 0) {
                    return true;
                }
                else {
                    return false;
                }			
            }
            else {
                return false;				
            }
        }		
        else {
            return true;
        }                        
	}

// COMMON GENERIC FUCTIONS

	// This function checks if a given value is a Decimal Type
	// FORMAT: XXXXXXXXX.XXXXXX
	this.isDecimalAnyPlaces = function(value) {
		return !isNaN(parseFloat(value));
	}

	// This function checks if a given value is a Decimal Type
	// FORMAT: XXXXXXXXX.decimalPlaces
	this.isDecimal = function(value, decimalPlaces) {
		var dotCount = 0;
		var decimalPlacesCount = 0;
		
		// Convert to string
		value = '' + value;		
		
		if((value != '') && (value != null)) {
		
			// Iterate through the value, and check for each character
			for(i=0; i < value.length; i++) {
				var character = value.charAt(i)
				
				// If a dot exists, start counting
				if(dotCount > 0) {
					decimalPlacesCount++;
					// If the counted number of decimal places is greater than specified
					// return a false
					if(decimalPlacesCount > decimalPlaces) {
						return false;
					}
				}
				
				// If a character is a '.', record it
				if(character == '.') {
					dotCount++;
				}
				else {
					// If its not a number, return false
					if(!this.isNumber(character)) {
						return false;
					}
				}
			}
		}
		else {
			return false;
		}
			
	
		// Only one decimal point is allowed
		if(dotCount > 1) {
			return false;
		}
		else {
			return true;		
		}
	}
	
	// This function checks if a given value is an Integer Type
	// FORMAT: XXXXXXXXX
	this.isInteger = function(value) {
		var dotCount = 0;
		
		if((value != '') && (value != null)) {
			// Iterate through the value, and check for each character
			for(i=0; i < value.length; i++) {
				var character = value.charAt(i)
				
				// If its not a number, return false
				if(!this.isNumber(character)) {
					return false;
				}
			}
		}
		else {
			return false;
		}		
	
		return true;
	}
	
	// This function checks if a given value (one character) is an integer
	this.isNumber = function(value) {
		if((value >= -9) && (value <= 9)) {
			return true;
		}
		else {
			return false;
		}
	}
	
	// This function checks if a given value is an email
	this.isEmail = function(emailStr){
		if(emailStr.length== 0){
			return true;
		}
		if(!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(emailStr))
			return false
		return true	
	}
	
	// Common Date Validation Constants and Functions
	this.dtCh= "/";
	this.minYear=1900;
	this.maxYear=2100;
	this.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 );
	}
	
	this.DaysArray = function(n) {
		for (var i = 1; i <= n; i++) {
			this[i] = 31
			if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
			if (i==2) {this[i] = 29}
	   }
	   return this
	}
	
	// This function checks if a given value conforms to the Date Format
	// FORMAT: dd/mm/yyyy
	this.isDateString = function(dtStr){
		if(dtStr.length== 0){
			return true;
		}
		var daysInMonth = this.DaysArray(12)
		var pos1=dtStr.indexOf(dtCh)
		var pos2=dtStr.indexOf(dtCh,pos1+1)    
		var strYear=dtStr.substring(pos2+1)    
		var strDay=dtStr.substring(0,pos1)
		var strMonth=dtStr.substring(pos1+1,pos2)
		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){
			return false
		}
		if (strMonth.length<1 || month<1 || month>12){
			return false
		}
		if (strDay.length<1 || day<1 || day>31 || (month==2 && day> this.daysInFebruary(year)) || day > this.daysInMonth[month]){
			return false
		}
		if (strYear.length != 4 || year==0 || year<this.minYear || year>this.maxYear){
			return false
		}
		if (dtStr.indexOf(dtCh,pos2+1)!=-1 || this.isInteger(ASCommonUtil.stripCharsInBag(dtStr, dtCh))==false){
			return false
		}
	return true
	}
	
	// This function checks that the first date specified is less than or equal to the second date specified
	// FORMAT: dd/mm/yyyy
	this.compareDates = function(dateStart, dateEnd, equalFlag){
		var pos1=dateStart.indexOf(this.dtCh)
		var pos2=dateStart.indexOf(this.dtCh,pos1+1)    
		var dateStartYear=dateStart.substring(pos2+1)    
		var dateStartDay=dateStart.substring(0,pos1)
		var dateStartMonth=dateStart.substring(pos1+1,pos2)
	
	    pos1=dateEnd.indexOf(this.dtCh)
		pos2=dateEnd.indexOf(this.dtCh,pos1+1)    
		var dateEndYear=dateEnd.substring(pos2+1)    
		var dateEndDay=dateEnd.substring(0,pos1)
		var dateEndMonth=dateEnd.substring(pos1+1,pos2)
	
	    // If total match
	    if(dateStart == dateEnd) {
	        return equalFlag;
	    }
	    // Year is more - false
	    if(dateStartYear > dateEndYear) {
	        return false;
	    }
	    // Year is less - true
	    else if(dateStartYear < dateEndYear) {
	        return true;
	    }
	    // Year equal, month is more - false
	    else if(dateStartMonth > dateEndMonth) {
	        return false;
	    }
	    // Year equal, month is less - true
	    else if(dateStartMonth < dateEndMonth) {
	        return true;
	    }
	    // Year equal, month equal, day is more - false
	    else if(dateStartDay > dateEndDay) {
	        return false;
	    }
	    // Year equal, month equal, day is less - true
	    else if(dateStartDay < dateEndDay) {
	        return true;
	    }    
	    // Year equal, month equal, day equal - true;
	    else {
	        return equalFlag;
	    }
	}
};