// ===============
// Namespace stuff

if ((typeof fieldstone == "undefined") || (fieldstone == null)) {
    /**
     * The fieldstone object is the global (namespace) object used by the FOTI
     * JavaScript Library.  All fieldstone objects reside within this object scope.
     */
    var fieldstone = {};
}

/**
 * Create the specified namespace(s)
 * @static
 * @param  {String | Array} s Namespaces to create
 * @return {Object}  A reference to the last
 * namespace object created or an empty global
 * object if the namespace couldn't be created
 */
fieldstone.namespace = function(s) {
    var a, o, i, j, d;
    // Determine if s is string or array and
    // force it to an array
    if (typeof s === 'string') {
        a = [s];
    }
    else if (s.length !== undefined) {
        a = s;
    }
    else {
        // Neither a string or an array was passed in, so exit
        return {};
    }
    for (i = 0; i < a.length; i++) {
        d = a[i].split(".");
        o = fieldstone;
        // fieldstone is implied, so it is ignored if it is included
        for (j = (d[0] == "fieldstone") ? 1 : 0; j < d.length; j++) {
            o[d[j]] = o[d[j]] || {};
            o = o[d[j]];
        }
    }
    return o;
};

// ================
// Validation stuff
fieldstone.validation = function() {
	return {
		
		isValidDate : function(value) {

		    /*
		    
		    Purpose: Validate that the value falls within the SQL Server smalldatetime range.
		    
		    Note: Day and month values may have a leading zero if the value is a single digit.
		    
		    Note: Year must be a four digit value. 
		     
		    Note: I'm using a two-step process because the Javascript Date constructor is a joke, i.e. thinks the
		    year "08" is "1908" and thinks "2/31/2009" is "3/2/2009".
		    
		    */
		
		    if (!value || value == '') { return false; }
		    
		    regExPattern = /((^(10|12|0?[13578])([/])(3[01]|[12][0-9]|0?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3}))$)|(^(11|0?[469])([/])(30|[12][0-9]|0?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3}))$)|(^(0?2)([/])(2[0-8]|1[0-9]|0?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3}))$)|(^(0?2)([/])(29)([/])([2468][048]00)$)|(^(0?2)([/])(29)([/])([3579][26]00)$)|(^(0?2)([/])(29)([/])([1][89][0][48])$)|(^(0?2)([/])(29)([/])([2-9][0-9][0][48])$)|(^(0?2)([/])(29)([/])([1][89][2468][048])$)|(^(0?2)([/])(29)([/])([2-9][0-9][2468][048])$)|(^(0?2)([/])(29)([/])([1][89][13579][26])$)|(^(0?2)([/])(29)([/])([2-9][0-9][13579][26])$))/;
		    
		    if (value.match(regExPattern))  {
			// Tested
		    
			var d = new Date(value);
			if (d == null || d == "Invalid Date") { return false; }
			
			var dMin = new Date("1/1/1900").getTime();
			var dMax = new Date("6/6/2079 11:59").getTime();
			
			return (d.getTime() >= dMin && d.getTime() <= dMax);
		    
		    } else {
			// Tested
			
			return false;
		    }
		    
		},				
		
		isValidEmail : function(value) {
			/*
				Returns: boolean
			*/
			
			if (!value || value == '') { return false; }
			
			var regExPattern = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
			return value.match(regExPattern);
		},
		
		isValidPhone : function(value) {
			/*
				Returns boolean
			*/
			
			if (!value || value == '') { return false; }
			
			// Remove all non-digits and check that the length is at least 10.
			var tmp = value.replace(/[^\d]/g, "");
			    
			if (tmp.length >= 10) {
				// Tested both ways
				return (tmp.substr(0, 3) != "000");        
			} else {
				// Tested
				return false;
			}
		}, 
		
		isValidZip : function(value) {
			/*
				Tests for a 5 digit U.S. zip code
			
				Returns boolean
			*/
			if (!value || value == '') { return false; }
			
			var regExPattern = /^[\d]{5}$/g;
			return value.match(regExPattern);
		}
	}
}();

// ===========
// Popup stuff
fieldstone.popup = function() {

    return {

        byWidthHeight : function(url, w, h, location, scrollbars, menubar, toolbar, resizable, status, title)
        {
            // Parameter url: URL for the new window
            // Parameter w: Width of window
            // Parameter h: Height of window
            
            var location_ = (location && location != "undefined") ? location : "no";
            var scrollbars_ = (scrollbars && scrollbars != "undefined") ? scrollbars : "yes";
            var menubar_ = (menubar && menubar != "undefined") ? menubar : "no";
            var toolbar_ = (toolbar && toolbar != "undefined") ? toolbar : "no";
            var resizable_ = (resizable && resizable != "undefined") ? resizable : "yes";
            var status_ = (status && status != "undefined") ? status : "yes";
            var title_ = (title && title != "undefined") ? title : "PopUp";

            var winl = (screen.width-w)/2;
            var wint = (screen.height-75-h)/2; // 75 because of bar at the bottom of the screen
            if (winl < 0) winl = 0;
            if (wint < 0) wint = 0;
        	
            var windowprops = "height="+h+",width="+w+",top="+wint+",left="+winl+",location="+location_+"," +
	            "scrollbars="+scrollbars_+",menubar="+menubar_+",toolbar="+toolbar_+"," + 
	            "resizable="+resizable_+",status="+status_;
        	
            //window.alert(windowprops);
            window.open(url, title_, windowprops);
        },

        byPercent : function(url, percentOfScreen, location, scrollbars, menubar, toolbar, resizable, status, title)
        {
            // Parameter url: URL for the new window
            // Parameter percentOfScreen: A value between 0 and 100 that will size the new window as a percentage of 
            // the available screen width/height.
        	
            var location_ = (location && location != "undefined") ? location : null;
            var scrollbars_ = (scrollbars && scrollbars != "undefined") ? scrollbars : null;
            var menubar_ = (menubar && menubar != "undefined") ? menubar : null;
            var toolbar_ = (toolbar && toolbar != "undefined") ? toolbar : null;
            var resizable_ = (resizable && resizable != "undefined") ? resizable : null;
            var status_ = (status && status != "undefined") ? status : null;
            var title_ = (title && title != "undefined") ? title : "PopUp";
        	
            var w = screen.width * (percentOfScreen * 0.01);
            var h = screen.height * (percentOfScreen * 0.01);
        	
            fieldstone.popup.byWidthHeight(url, w, h, location, scrollbars, menubar, toolbar, resizable, status, title);
        }
    }
}();

// ===============
// Analytics stuff
fieldstone.analytics = function() {
	
	return {
		
		recordEvent : function(value) {
			_gaq.push(value);
		},
		
		recordPageLoad : function() {
			
		}
		
	}
	
}();

		