
Type.registerNamespace("Awc.WestOnline.Rules.UI");

Awc.WestOnline.Rules.UI.Common = new function()
{
    this.IsValidEmail = function(email) { 
        return email.match(/^[\w\.\+-]+@[\w-]+\.[\w\.-]+$/);
    };

    this.IsValidPhone = function(phone) { 
        return phone.match(/^\+{0,1}([0-9]{0,1}|\s|\-|\(|\)){1,18}$/);
    };
    
    this.IsValidPostcode = function(postcode) { 
        return postcode.match(/^\d{4}$/);
    };
    
    this.IsValidDate = function(date) {
        return date.match(/(\d{1,2})[/](\d{1,2})[/](\d{4})/gi);
    };
    
    this.IsValidTFN = function(tfn) {
        return tfn.match(/(\d{3})[\s-]?(\d{3})[\s-]?(\d{3})[\s-]?/);
    };
    
    this.IsValidAccountNumber = function(number) {
        return number.match(/\d{1,9}/);
    };
    
    this.IsNumeric = function(number, requiredLength) {
        return !number.match(/\D/) && number.length == requiredLength;
    };
    
    this.IsValidBSB = function(bsb) {
        return bsb.match(/\d{3}[\s-]?\d{3}[\s-]?/);
    };
    
    this.IsPOBoxAddress = function(address) {
        return address.match(/(PO|P.O|P O)\sbox/i);
    };
    
    this.IsValidAccountDesignation = function(account) {
        return (account.length <= 24 && !account.match(/\btrust\b/gi));
    };
        
    this.IsNumericAndSpaces = function(number) {
        return !number.match(/([^[0-9\s]+)/);
    };
    
    this.IsValidDrivingLicence = function(driverlicence) {
		return driverlicence.match(/^\w{1,9}$/);
    };
    
    this.Popup = function(page, height, width) {
        var winHeight = (typeof(height) == 'undefined' ? 500 : height);
        var winWidth = (typeof(width) == 'undefined' ? 750 : width);
	    var childWindow = window.open(page,'childWindow','resizable=yes,scrollbars=1,height=' + winHeight + ',width=' + winWidth);
		childWindow.focus();
		return;
	}
	
	this.ParseDate = function(ambiguousdate) {
	    if (ambiguousdate.constructor == String) {

	        //Separators
		    var SLASH = "/";
		    var SPACE = " ";
			
		    // Check for ambiguous date format (dd/mm/yyyy) and change to (mm/dd/yyyy) so JS functions
		    // will interpret it correctly.
			var re = new RegExp(/(\d{1,2})[-\/\. ](\d{1,2})[-\/\. ](\d{2,4})/gi);
		    var result = re.exec(ambiguousdate);
			
		    var year = new String(new Date().getFullYear()).substr(2,2);
		    
		    var yearPrefix = '';
		    		    
		    if (result != null) 
    	    {	
    	        //If the date is only 2 digits determine wheter to append an 19, or 20
    	        if (result[3] != null && result[3].length == 2)
		        {
		            var stringResult= new String(result[3]);
		            yearPrefix = (stringResult >= year) ? '19' : '20'; 
		        }
        		
        		if (result[1] == '31' && result[2].match(/(2|4|6|9|11)/gi) != null) {
					return null;
				}
				else if (result[1] == '30' && result[2] == '2') {
					return null;
				}
				else if (result[1] == '29' && result[2] == '2' && !(result[3] % 4 == 0 && ( result[3] % 100 != 0 || result[3] % 400 == 0))) {
					return null;
				}
				
    	        //Format: mm/dd/yyyy eg 11/01/2007
                ambiguousdate = result[2] + SLASH + result[1] + SLASH + yearPrefix + result[3];
		    }
	        else
	        {
	            re = new RegExp(/(\d{1,2})[-\/\. ]([a-zA-Z]{3})[-\/\. ](\d{2,4})/gi);
	            //This occurs when the client press on place orders.
	            var result = re.exec(ambiguousdate); 
                if(result != null){
                    //If the date is only 2 digits determine wheter to append an 19, or 20
    	            if (result[3] != null && result[3].length == 2)
		            {
                        var stringResult= new String(result[3]);
		                yearPrefix = (stringResult >= year) ? '19' : '20'; 
		            }
		            
		            if (result[1] == '31' && result[2].match(/(feb|apr|jun|sep|nov)/gi) != null) {
						return null;
					}
					else if (result[1] == '30' && result[2].match(/(feb)/gi) != null) {
						return null;
					}
					else if (result[1] == '29' && result[2].match(/(feb)/gi) != null && !(result[3] % 4 == 0 && ( result[3] % 100 != 0 || result[3] % 400 == 0))) {
						return null;
					}
                    //Format: dd MMM yyyy eg 12 Mar 2007
	                ambiguousdate = result[1] + SPACE + result[2] + SPACE + yearPrefix + result[3];
	            }   
	        }
		    
			var milliseconds = Date.parse(ambiguousdate); // Parse string and return no. of milliseconds.
            
			if (!isNaN(milliseconds)) 
				return new Date(milliseconds);
			
		} else if (ambiguousdate.constructor == Date) {
			return ambiguousdate;
		}
		return null;
	}
}

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();