/* Prototypes */

//prototype to test strings if they're blank or not
String.prototype.isBlank = function() 
{
	return /^ *$/.test(this);
}

//prototype to trim strings
String.prototype.trim = function()
{
	return this.replace(/(^ +| +$)/g, "");
}

//prototype to see if a string is numeric
String.prototype.isNumeric = function()
{
	return /^-?([0-9]+(\.[0-9]+){0,1}|\.[0-9]+)$/.test(this);
}

//prototype to see if a string is a valid email
String.prototype.isEmail = function()
{
	return /^([a-zA-Z0-9_\.\-])+\@([a-zA-Z0-9.\-])+(\.[a-zA-Z0-9]{2,4})+$/.test(this);
}

//prototype to encode a string for use in a regular expression.
//this is useful for the string.replace method, since the first
//argument normally takes a RegExp object.
String.prototype.regexEncode = function()
{
	return this.replace(/([.*+?^${}()|[\]\/\\])/g, "\\$1");
}

//HTML encoding
String.prototype.htmlEncode = function()
{
	return this.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
}

//date format
Date.prototype.toShortDateString = function()
{
	return (this.getMonth() + 1).toString() + "/" + this.getDate().toString()  + "/" + this.getFullYear().toString();
}

//easy way to test for weekends
Date.prototype.isWeekend = function()
{
	return this.getDay() == 0 || this.getDay() == 6;
}

//find the number of days between dates
Date.prototype.daysBetween = function (dateToCompare) {
    var difference =
        Date.UTC(dateToCompare.getFullYear(), dateToCompare.getMonth(), dateToCompare.getDate(), 0, 0, 0)
      - Date.UTC(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0);
	  
    return Math.floor(Math.abs(difference / 1000 / 60 / 60 / 24));
}

//number format
Number.prototype.numberFormat = function() {
	
	var parts = this.toString().split(".");
	var number = "";
	
	for (var i = parts[0].length - 1; i >= 0; i--)
	{		
		if ((parts[0].length - i) % 3 == 0 && i > 0)
		{
			number =  "," + parts[0].charAt(i) + number;
		}
		else
		{
			number = parts[0].charAt(i) + number;
		}
	}
	
	if (parts.length > 1)
	{
		number += "." + parts[1];
	}
	
	return number;
}

/* Validation */

function ValidateNotEmpty(field, fieldName)
{
	if (field.value.isBlank())
	{
		alert("The " + fieldName + " is required.");
		field.focus();
		return false;
	}
	
	return true;
}

function ValidateAlphabetic(field, fieldName)
{
	var pattern = /[^a-zA-Z]/;
	
	if (pattern.test(field.value))
	{
		alert("The " + fieldName + " can only contain alphabetic characters.");
		field.focus();
		return false;
	}
	
	return true;
}

function ValidateNumeric(field, fieldName, allowDecimal)
{
	if (!field.value.isNumeric())
	{
		alert("The " + fieldName + " must be numeric.");
		field.focus();
		return false;
	}
	
	if (!allowDecimal && field.value.indexOf(".") != -1)
	{
		alert("The " + fieldName + " must be a whole number.");
		field.focus();
		return false;
	}
	
	return true;
}

function ValidateName(field, fieldName)
{
	//only alphabetic characters, hyphens, 
	//apostrophes, and spaces are allowed
	var pattern = /[^-a-zA-Z' ]/;
	
	if (pattern.test(field.value))
	{
		alert("The " + fieldName + " can only contain letters, hyphens, apostrophes, or spaces.");
		field.focus();
		return false;
	}
	
	//it must start and end with a letter
	pattern = /(^[^a-zA-Z]|[^a-zA-Z]$)/;
	
	if (pattern.test(field.value))
	{
		alert("The " + fieldName + " must start and end with a letter.");
		field.focus();
		return false;
	}
	
	return true;
}

function ValidateDateTime(field, fieldName)
{
	var pattern = /^\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{2} [AP]M$/;
	
	if (!pattern.test(field.value))
	{
		alert("The " + fieldName + " must be in the form of: mm/dd/yyyy hh:mm TT, where TT is either AM or PM.");
		field.focus();
		return false;
	}
	
	var parts = field.value.split(" ");
	var dateParts = parts[0].split("/");
	var timeParts = parts[1].split(":");
	
	var month = parseInt(dateParts[0], 10);
	var day = parseInt(dateParts[1], 10);
	var year = parseInt(dateParts[2], 10);
	var hours = parseInt(timeParts[0], 10);
	var minutes = parseInt(timeParts[1], 10);
	
	if (!IsDateValid(month, day, year))
	{
		alert("The date entered is invalid.");
		field.focus();
		return false;
	}
	
	if (hours < 1 || hours > 12 || minutes > 59)
	{
		alert("The time entered is invalid.");
		field.focus();
		return false;
	}
	
	return true;
}

function IsDateValid(month, day, year)
{
	if (month.toString().isBlank() 
		|| day.toString().isBlank() 
		|| year.toString().isBlank()
		|| !month.toString().isNumeric() 
		|| !day.toString().isNumeric() 
		|| !year.toString().isNumeric() 
		|| month.toString().indexOf(".") != -1
		|| day.toString().indexOf(".") != -1
		|| year.toString().indexOf(".") != -1
		|| year.toString().length != 4)
	{
		return false;
	}
	
	var date = new Date(year, month - 1, day, 0, 0, 0, 1);
	return date.getMonth() + 1 == month && date.getDate() == day && date.getFullYear() == year;
}

function ApplyCenturyToYear(year)
{
	year = year.toString();
	
	if (year.isBlank())
	{
		return "";
	}
	
	return "20" + year.replace(/^(\d)$/, "0$1"); //non-y3k compliant ;)
}

function ShowAlertDialog(text, okCallback)
{
	Dialog.alert(text, {ok: okCallback || false, okLabel: "OK", windowParameters: {className: "alphacube", effectOptions: {duration: .25 } } });
}

function ShowConfirmDialog(text, okCallback, cancelCallback, width, height)
{
	Dialog.confirm(text, {
		ok: okCallback,
		okLabel: "OK",
		cancel: cancelCallback, 
		windowParameters: {
			className: "alphacube",
			width: width, 
			height: height,
			effectOptions: {duration: .25 }
		} 
	});
}

function ShowElementContentWindow(element, container, allowResize, width, height)
{
	element = $(element);
	container = $(container);
	
	var w = new Window("win", { resizable: allowResize, className: "alphacube", effectOptions: {duration: .25 } });
	w.setContent(element, true, true);
	
	if (width)
	{
		w.setSize(width, w.getSize().height);
	}
	
	if (height)
	{
		w.setSize(w.getSize().width, height);
	}
	
	w.toFront(); 
	w.setDestroyOnClose(); 
	w.showCenter(); 
	
	observer = { 
		onDestroy: function(eventName, win) { 
			if (win == w) 
			{ 
				Element.hide(element);
				container.appendChild(element); 
				w = null; 
				Windows.removeObserver(this);
			}
		} 
	};
	
	Windows.addObserver(observer);
}

function FixPng()
{
	if (document.all)
	{
		var pngs = $A(document.getElementsByTagName("img")).findAll(function(element, index) {
			return /\.png/gi.test(element.src);
		});
		
		pngs.each(function(element, index) {
			var span = document.createElement("span");
			
			if (element.id)
				span.id = element.id;
				
			if (element.title)
				span.title = element.title;
			
			Element.setStyle(span, {
				display: "inline-block", 
				width: element.width + "px", 
				height: element.height + "px", 
				float: Element.getStyle(element, "float") ? Element.getStyle(element, "float") : "",
				filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\"" + element.src + "\", sizingMethod=\"scale\")"
			});
			
			element.parentNode.insertBefore(span, element);
			Element.remove(element);
		});
	}
}