//
//  serious-utils.js - version 0.1 - Serious 0.0.1
//
//  Copyright (c) 2009. Serious.
//  All rights reserved.
//

var Serious;
if (!Serious) Serious = {};
if (!Serious.Constant) Serious.Constant	= {};
Serious.Constant.DEBUG					= true;
////////////////////////////////////////////////////////////////////////////////
//
// Serious.Utils
//
////////////////////////////////////////////////////////////////////////////////
if (!Serious.Utils) Serious.Utils = {};

Serious.Utils.formModified = false;

Serious.Utils.dispatchEvent = function(e, type) {
	jQuery('#'+e).trigger(type);
}

Serious.Utils.setFormModified = function(val){
	Serious.Utils.formModified = (val != 0)? true: false;
};

Serious.Utils.setFormCurrent = function(val){
	Serious.Utils.setFormModified(0);
};

Serious.Utils.warnFormModified = function(e){
	if (Serious.Utils.formModified==true) {
		returnValue = "This form contains unsaved changes.\nYou will LOSE YOUR UPDATES if you press ok.\n\nTo save your work, press CANCEL below, then click\nthe UPDATE BUTTON in the page.";
		if (!e) {
				e = window.event;
		}
		else
			if (e) {
				e.returnValue = returnValue;
			}
			else {
				return returnValue;
		}
	}
};

Serious.Utils.disableField = function(elementId) {
	jQuery('#'+elementId).attr("disabled","disabled");
};

Serious.Utils.enableField = function(elementId) {
	jQuery('#'+elementId).removeAttr("disabled");
};

Serious.Utils.toggle = function(elementId){
	jQuery('#'+elementId).toggle();
};

Serious.Utils.show = function(elementId) {
	jQuery('#'+elementId).show();
};

Serious.Utils.hide = function(elementId) {
	jQuery('#'+elementId).hide();
};

Serious.Utils.isNumeric = function(testCase){
	var regularExpressionNumeric=/(^\d+$)|(^\d+\.\d+$)/
	if (regularExpressionNumeric.test(testCase)) {
		return false;
	}
	else {
		return true;
	}
};

Serious.Utils.addLoadListener = function(listener){
	jQuery(document).ready(listener);
};

Serious.Utils.addUnloadListener = function(listener){
	try
	{
		element = jQuery(window).get();
		if (element.addEventListener)
			element.addEventListener("beforeunload", listener, true);
		else if (element.attachEvent)
			element.attachEvent("onbeforeunload", listener);
	}
	catch (e) {}
};

Serious.Utils.submitForm = function(url, Id, elements, callback) {
	var	data = jQuery.makeArray(jQuery(elements));
	jQuery(Id).load(url,data,callback,'html');
}

Serious.Utils.log = function(log){
	if (Serious.Constant.DEBUG==true) console.log(log);
}

////////////////////////////////////////////////////////////////////////////////
//
// String
//
////////////////////////////////////////////////////////////////////////////////
String.prototype.trim = function() {
    return this.replace( /^\s+|\s+$/, "" );
}

////////////////////////////////////////////////////////////////////////////////
//
// Array
//
////////////////////////////////////////////////////////////////////////////////
Array.prototype.contains = function (element) {
	for (var i = 0; i < this.length; i++) {
		if (this[i] == element) {
			return true;
		}
	}
	return false;
}

////////////////////////////////////////////////////////////////////////////////
//
// Date
//
////////////////////////////////////////////////////////////////////////////////
Date.prototype.format = function(format) {
	var returnStr = format;
	var replaceStr = Date.replaceStrings;
	var replaceChr = Date.replaceChars;
	var curSeq;

	if(this.toString() == "NaN" || this.toString() == "Invalid Date"){
		return '';
	}
	for(curSeq in replaceStr){
		if(returnStr.indexOf(curSeq) >= 0){
			return replaceStr[curSeq].call(this);
		}
	}
	for(curSeq in replaceChr){
		returnStr = returnStr.replace(curSeq,replaceChr[curSeq].call(this));
	}
	return returnStr;
};

Date.prototype.IsDate = function() {
	return (this.toString() != "NaN" && this.toString() != "Invalid Date");
};

Date.namedReferences = {
	shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
	longMonths: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
	shortDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
	longDays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
};

Date.replaceStrings = {
	// Complete Formats (abbreviated to avoid reserved word conflicts)
	shor: function(){
		//return String("d/m/yy");
		return String(this.getDate()+'/'+(this.getMonth() + 1)+'/'+this.getFullYear().substr(2));
	},
	lon: function(){
		//return String("mmmm d, yyyy");
		return String(Date.namedReferences.longMonths[this.getMonth()]+' '+this.getDate()+', '+this.getFullYear());
	},
	mediu: function(){
		//return String("mmm d, yyyy");
		return String(Date.namedReferences.shortMonths[this.getMonth()]+' '+this.getDate()+', '+this.getFullYear());
	},
	ful: function(){
		//return String("dddd, mmmm d, yyyy");
		return String(Date.namedReferences.longDays[this.getDay()]+', '+Date.namedReferences.longMonths[this.getMonth()]+' '+this.getDate()+', '+this.getFullYear());
	}
};

Date.replaceChars = {
	// Day
	dddd: function() { return Date.namedReferences.longDays[this.getDay()]; },
	ddd: function() { return Date.namedReferences.shortDays[this.getDay()]; },
	dd: function() { return (this.getDate() < 10 ? '0' : '') + this.getDate(); },
	d: function() { return this.getDate(); },
	// Month
	mmmm: function() { return Date.namedReferences.longMonths[this.getMonth()]; },
	mmm: function() { return Date.namedReferences.shortMonths[this.getMonth()]; },
	mm: function() { return (this.getMonth() < 9 ? '0' : '') + (this.getMonth() + 1); },
	m: function() { return (this.getMonth() + 1); },
	// Year
	yyyy: function() { return this.getFullYear(); },
	yy: function() { return ('' + this.getFullYear()).substr(2); },
	// Time
	tt: function() { return this.getHours() < 12 ? 'AM' : 'PM'; },
	t: function() { return this.getHours() < 12 ? 'A' : 'P'; },
	hh: function() { return (this.getHours() < 10 || (12 < this.getHours() < 22) ? '0' : '') + (this.getHours() < 10 ? this.getHours() + 1 : this.getHours() - 12); },
	HH: function() { return (this.getHours() < 10 ? '0' : '') + this.getHours(); },
	h: function() { return this.getHours() == 0 ? 12 : (this.getHours() > 12 ? this.getHours() - 12 : this.getHours()); },
	H: function() { return this.getHours(); },
	ii: function() { return (this.getMinutes() < 10 ? '0' : '') + this.getMinutes(); },
	i: function() { return this.getMinutes(); },
	ss: function() { return (this.getSeconds() < 10 ? '0' : '') + this.getSeconds(); },
	s: function() { return ( this.getSeconds()); },
	l: function() { return ( this.getMilliseconds()); },
	L: function() { return ( this.getMilliseconds()); }
};

////////////////////////////////////////////////////////////////////////////////
//
// jQuery
//
////////////////////////////////////////////////////////////////////////////////
/*
jQuery.datepicker.setDefaults({
	dateFormat: 'yyyy-mm-dd',
	changeYear: true,
	changeMonth: true,
	yearRange: '2000:2012'
});
*/
////////////////////////////////////////////////////////////////////////////////
//
// Function
//
////////////////////////////////////////////////////////////////////////////////
Function.prototype.inheritsFrom = function( parentClassOrObject ){ 
	if ( parentClassOrObject.constructor == Function )
	{
		//Normal Inheritance
		this.prototype = new parentClassOrObject;
		this.prototype.constructor = this;
		this.prototype.parent = parentClassOrObject.prototype;
	}
	else
	{
		//Pure Virtual Inheritance
		this.prototype = parentClassOrObject;
		this.prototype.constructor = this;
		this.prototype.parent = parentClassOrObject;
	} 
	return this;
}