/// <reference path="jquery.js" />
var DdmSys = {
	Version: '2.0.0',
	Copyright: 'Copyright (c)Dynamic Digital Media Group',
	Author: 'Glenn Byron',
	jQueryVersion: parseFloat(jQuery.fn.jquery.split(".")[0] + "." + jQuery.fn.jquery.split(".")[1])
}

// jQuery needs to be at least version 1.2
if ((typeof jQuery == 'undefined') || DdmSys.jQueryVersion < 1.2)
	throw ("DdmSys requires the jQuery JavaScript framework >= 1.2");

Object.extend = function(destination, source) {
	for (var property in source)
		destination[property] = source[property];
	return destination;
};

// ************************************************
// Extend JavaScript String object
//
// The following functions have been added
// to the JavaScirpt String function.
//
// trimLeft()
// trimRight()
// trim()
// endsWith()
// startsWith()
// padLeft()
// padRight()
// cleanSeperators() - used to remove common seperators in phone numbers, etc.
// cleanCurrency()
// restoreCurrency()
// removeCommas() - for numbers
// addCommas() - for numbers
// restoreUSPhone()
// isNumeric()
// isSignedInteger()
// isPostiveInteger()
// isUSDate()
// isUSZipCode()
// isUSPhone()
// ************************************************
Object.extend(String.prototype, {
	trimLeft: function() { return this.replace(/^\s+/, ''); },
	trimRight: function() { return this.replace(/\s+$/, ''); },
	trim: function() { return this.trimRight().trimLeft() },

	endsWith: function(s) {
		if (this.length == 0 || this.length < s.length) return false;
		return (this.substr(this.length - s.length) == s);
	},
	startsWith: function(s) {
		if (this.length == 0 || this.length < s.length) return false;
		return (this.substr(0, s.length) == s);
	},

	padLeft: function(chr, num) {
		var re = new RegExp(".{" + num + "}$");
		var pad = "";

		do {
			pad += chr;
		} while (pad.length < num)
		return re.exec(pad + this);
	},

	padRight: function(chr, num) {
		var re = new RegExp("^.{" + num + "}");
		var pad = "";

		do {
			pad += chr;
		} while (pad.length < num)
		return re.exec(this + pad);
	},

	cleanSeperators: function() {
		return this.replace(/[\(\)\.\-\/\s,]/g, "");
	},

	cleanCurrency: function() {
		var objRegExp = /\(/;
		var strMinus = '';

		if (objRegExp.test(this)) strMinus = '-';

		objRegExp = /\)|\(|[,]/g;
		var inputValue = this.replace(objRegExp, '');

		if (inputValue.indexOf('$') <= 0) {
			inputValue = inputValue.substring(1, inputValue.length);
		}
		return strMinus + inputValue;
	},

	restoreCurrency: function() {
		var objRegExp = /-?[0-9]+\.[0-9]{2}$/;

		if (objRegExp.test(this)) {
			objRegExp.compile('^-');
			var inputValue = this.addCommas();
			if (objRegExp.test(inputValue)) {
				inputValue = '(' + inputValue.replace(objRegExp, '') + ')';
			}
			return '$' + inputValue;
		} else
			return this;
	},

	removeCommas: function() {
		var objRegExp = /,/g;
		return this.replace(objRegExp, '');
	},

	addCommas: function() {
		var inputValue = this.trim();

		len = inputValue.indexOf(".");
		if (len == -1) {
			len = inputValue.length;
			out = "";
		} else {
			out = inputValue.substring(len);
		}

		for (var i = 0; i < len; i++) {
			if (i != 0 && i % 3 == 0) {
				out = "," + out
			}
			out = inputValue.charAt(len - i - 1) + out
		}
		return out
	},

	restoreUSPhone: function() {
		if (this == '') return this;
		var n1 = this.substring(0, 3)
		var n2 = this.substring(3, 6)
		var n3 = this.substring(6, 10)
		return ("(" + n1 + ") " + n2 + "-" + n3)
	},

	//****************************
	//Check for types of things
	//****************************
	isNumeric: function() {
		return /^[-+]?\d+(\.\d+)?$/.test(this.trim());
	},

	isSignedInteger: function() {
		return /^[-+]?\d+$/.test(this.trim());
	},

	isPostiveInteger: function() {
		return /^\d+$/.test(this.trim());
	},

	isUSDate: function() {
		var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/

		//check to see if in correct format
		if (!objRegExp.test(this))
			return false; //doesn't match pattern, bad date
		else {
			var strSeparator = this.substring(2, 3) //find date separator
			var arrayDate = this.split(strSeparator); //split date into month, day, year

			var arrayLookup = {
				'01': 31, '03': 31, '04': 30, '05': 31, '06': 30, '07': 31,
				'08': 31, '09': 30, '10': 31, '11': 30, '12': 31
			};

			var intDay = parseInt(arrayDate[1], 10);

			//check if month value and day value agree
			if (arrayLookup[arrayDate[0]] != null) {
				if (intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
					return true;
			}

			var intMonth = parseInt(arrayDate[0], 10);
			if (intMonth == 2) {
				var intYear = parseInt(arrayDate[2]);
				if (intDay > 0 && intDay < 29) {
					return true;
				} else if (intDay == 29) {
					if ((intYear % 4 == 0) && (intYear % 100 != 0) || (intYear % 400 == 0)) {
						// year div by 4 and ((not div by 100) or div by 400) -> ok
						return true;
					}
				}
			}
		}
		return false; //any other values, bad date
	},

	isUSZipCode: function() {
		//--------------------------------------------------
		//DESCRIPTION: Validates 5 digit format or zip+4
		//-------------------------------------------------
		var objRegExp = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
		return objRegExp.test(this);
	},

	isUSPhone: function() {
		var cleanNumber = this.cleanSeperators();
		if ((cleanNumber.isPostiveInteger() && cleanNumber.cleanSeperators().length == 10) || cleanNumber == '') {
			return true;
		} else {
			return false;
		}
	}
}, false);
