/**
 * FBD Namespace
 */  
if (!FBD) {
	var FBD = {};
}

/**
 * Add useful methods to all (DOM) elements
 */ 
Element.addMethods({
	isVisibleOnTheScreen: function(element) {
		element = $(element);
		if (!element.visible()) return false;
		var isVisible = true;
		element.ancestors().each(function(ancestor) {
			if (!ancestor.visible()) {
				isVisible = false;
				throw $break;
			}
		});
		return isVisible;
	}
});

/*isVisibleOnTheScreenFalse: function(element) {
		
		var isVisible = false;
		
	}
});*/




/**
 * Add some useful methods to string objects.
 */
Object.extend(String.prototype, {
	convertToAmount: function() {
		var aDotParts = this.split('.');
		var intPart = aDotParts[0];
		
		// count dots
		if (aDotParts.size() > 2) {
			throw new Error('Badly formed number: ' + this.inspect())
		}
		
		// maybe there are already comas in a number string - check it 
		if (intPart.include(','))
		{
			var aComaParts = intPart.split(',');
			if (aComaParts[0].length > 3) {
				throw new Error('Badly formed number: ' + this.inspect())
			}
			aComaParts.slice(1).each(function(part) {
				if (part.length != 3) {
					throw new Error('Badly formed number: ' + this.inspect())
				}
			}.bind(this));
		}
		else
		{
			// no comas so create your own
			intPart = intPart._convertIntPart()
	
		}
		
		if (2 == aDotParts.size() && (aDotParts[1].length > 0)) {
			intPart = intPart + '.' + aDotParts[1];
		} else {
		
			intPart = intPart + '.00';
		}
		
		
		return intPart
	},
	
	convertToIntegerAmount: function() {
		var aDotParts = this.split('.');
		var intPart = aDotParts[0];
		
		// count dots
		if (aDotParts.size() > 2) {
			throw new Error('Badly formed number: ' + this.inspect())
		}
		
		// maybe there are already comas in a number string - check it 
		if (intPart.include(','))
		{
			var aComaParts = intPart.split(',');
			if (aComaParts[0].length > 3) {
				throw new Error('Badly formed number: ' + this.inspect())
			}
			aComaParts.slice(1).each(function(part) {
				if (part.length != 3) {
					throw new Error('Badly formed number: ' + this.inspect())
				}
			}.bind(this));
		}
		else
		{
			// no comas so create your own
			intPart = intPart._convertIntPart()
	
		}
		
		return intPart
	},
	
	convertToInteger: function() {
		return new Number(this.replace(/,/g, ''));
	},
	
	_convertIntPart: function() {
		var result = '';
		var sInt = this;
		var len = sInt.length; 

		if (len <= 3) {
			return this;
		}
		
		var comaIdx = len % 3;
		
		if (comaIdx) {
			result = sInt.substr(0, comaIdx) + ',';
		}
		for (; comaIdx < len; comaIdx += 3 ) {
			result += sInt.substr(comaIdx, 3) + ',';
		}
		
		// cut last coma off
		return result.substr(0, result.length-1); 
	},
	
	isNumber: function() {
		return !this.blank() && /^\d+$/.test(this);
	},
	
	isFloat: function() {
		return !this.blank() && /^\d*(\.\d{1,2})?$/.test(this);
	},
	
	isFormatedFloat: function() {
		return !this.blank() && /^\d{0,3}(,\d{3}){0,2}(\.\d{1,2})?$/.test(this);	
	},
	
	isDate: function() {
		return /^\d{1,2}\/\d{1,2}\/\d{4}$/.test(this);
	},
	
	isEmail: function() {
		return /^\w+(\.\w+)*@(\w+\.)+\w{2,5}$/.test(this);
	},
	
	/*
	//This function tests for Irish characters
	isFormattedName: function() {
	//HEX CODES REPRESENTING GAELIC UNICODE CHARS
		return /^[a-zA-Z\u00E0\u00E1\u00E8\u00E9\u00EC\u00F2\u00F9\u00C0\u00C1\u00C8\u00C9\u00CC\u00D2\u00D3\u00D9\']{0,2}[\u00E0\u00E1\u00E8\u00E9\u00EC\u00F2\u00F9\u00C0\u00C1\u00C8\u00C9\u00CC\u00D2\u00D3\u00D9a-zA-Z-\s]+$/.test(this);
	}*/
	
	isFormattedName: function() {
		return /^[a-zA-Z\']{0,2}[a-zA-Z-\s]+$/.test(this);
	}
	
	
});

/**
 * Method to validate the numerical values of free text entered dates
 * Used in conjunction with isDate which is simply a format check. 
 */
function isValidDate(day, month, year) 
{
    if (month < 1 || month > 12) 
    {
        return false;
    }
    if (day < 1 || day > 31) {
        return false;
    }
    if ((month == 4 || month == 6 || month == 9 || month == 11) &&
        (day == 31)) {
        return false;
    }
    if (month == 2) {
        var leap = (year % 4 == 0 &&
                   (year % 100 != 0 || year % 400 == 0));
        if (day>29 || (day == 29 && !leap)) {
            return false;
        }
    }
    return true;
}

/**
 * Add some useful methods to Date objects.
 */
Object.extend(Date.prototype, {
	setFromString: function(sDate) {
		// assumed that sDate has a 'dd/mm/yyyy' format
		var dateParts = sDate.split('/');
		this.setFullYear(dateParts[2], dateParts[1] - 1, dateParts[0]);

		return this;
	}
});


/**
 * Settings
 */
FBD.Settings = {
	ICON_HELP_RIGHT_CAR_SRC: '/media/FBD/images/righthanddrive.jpg', 
	ICON_TICK_SRC:       '/media/FBD/images/icon_tick01.jpg',
	ICON_ERROR_SRC:      '/media/FBD/images/icon_close3.jpg',
	ICON_HELP_SRC:       '/media/FBD/images/icon_help.gif',
	FOCUS_INDICATOR_SRC: '/media/FBD/images/icon_help.gif',
	ERROR_SOURCE_BOX_ID: 'errors-list', 
	ERROR_BOX_ID:        'error-box', 
	HELP_BOX_ID:         'help-box',
	TEXT: {
		DELETE_ADDED_DRIVERS_CONFIRMATION: 'Are you sure you wish to delete all additional drivers added?', 
		GENERIC_ERROR_MSG: 'One or more errors have occurred. Please correct these errors before moving to the next section', 
		PAYMENT_TICK_THE_TERMS_MSG: 'You must tick the terms to confirm all data present is correct.',
		DD_TICK_THE_TERMS_MSG: 'You must tick the box to confirm you have read and agreed to the direct debit terms and conditions',
		HOME_TOO_MANY_HIVALUE_ITEMS: 'You cannot add more hi-value items'
	}
}

