/// <reference path="prototype.js" />
var DDM = {
	Version: '1.0.03',
	Copyright: 'Copyright (c)Dynamic Digital Media Group',
	Author: 'Glenn Byron',
	prototypeVersion: parseFloat(Prototype.Version.split(".")[0] + "." + Prototype.Version.split(".")[1])
}

// Prototype now needs to be at least version 1.5.0_rc1, and this function
// does not check for release canidate 1 (rc1)
if((typeof Prototype=='undefined') || DDM.prototypeVersion < 1.5)
      throw("DDM requires the Prototype JavaScript framework >= 1.5.0_rc1");

DDM.isIE = navigator.appVersion.match(/MSIE/) == "MSIE";

// ************************************************
// 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);

// ******************************
// Two functions to get the postion and size 
// of the current window so you can postion 
// things like windows
// 
// getPageScroll()
// getPageSize()
// ******************************
DDM.WindowUtilities = {
 	getPageScroll :function() {
		var yScroll;

		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
		}
		return yScroll;
	},

	// getPageSize()
	// Returns properties with page width, height and window width, height
	// Core code from - quirksmode.org
	// Edit for Firefox by pHaez
	// Edit to return properties and not an array by Glenn Byron @ Dynamic Digital Media Group
	getPageSize: function(){
		var xScroll, yScroll;
	
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
	
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
	
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}

		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
		return {pageWidth: pageWidth, pageHeight: pageHeight, windowWidth: windowWidth, windowHeight: windowHeight};
	}
}
