//DO NOT REMOVE THE FOLLOWING COMMENT
//<![CDATA[<%@Language=JScript%><%

// Provide access to the global namespace from within object methods.
var global = this;

// This function is NOT a class.  It is a syntactic shortcut for CREATING classes.
function Class(Members) {
	var baseClass;
	if(Members.inherits) {
		baseClass = new global[Members.inherits];
	}
	var func = function () {
		this.parentClass = baseClass;
		if(Members.inherits) {
			this.baseClass = baseClass;
		}
		for(var Member in Members) {
			this[Member] = Members[Member];
		}
		this.isA = function (ParentClass) {
			return(ParentClass.prototype.isPrototypeOf(this));
		}
		this.extend = function(obj) {
			for(var Member in this) {
				obj[Member] = this[Member];
			}
		}
	}
	if(Members.inherits) {
		func.prototype = baseClass;
		if(Members.inherits) {
			func.prototype[Members.inherits] = baseClass;
		}

	}
	return(func);
}
Function.prototype.extend = function(obj) {
	var x = new this;
	for(var Member in x) {
		obj[Member] = x[Member];
	}
}
Function.prototype.isA = function (ParentClass) {
		return(ParentClass.prototype.isPrototypeOf(this.prototype));
}


//Extend the built-in Javascript objects.
//pad adds leading zeros.
Number.prototype.pad = function (num) {
	var padding = "";
	for(var c = 1; c <= num; c++) 
		padding += "0";
		
	padding += this.toString();
	return(padding.substr(padding.length -num));
}

//toVBDate actually returns a string, in a format that VB can convert into a date.
Date.prototype.toVBDate = function () {
	return(this.getFullYear().pad(4) + "/" + (this.getMonth() + 1).pad(2) + "/" + this.getDate().pad(2) + " " 
		+ this.getHours().pad(2) + ":" + this.getMinutes().pad(2) + ":" + this.getSeconds().pad(2));
}


// Teach the Javascript Date object how to parse ISO 8601.
Date._parse = Date.parse;
Date.parse = function(value) {
	// First, parse it with the usual method.
	var result = this._parse(value);
	
	// If that fails, try parsing it as an ISO 8601 value.
	if(isNaN(result)) {
		var re = /^((((\d\d\d\d)\-(\d\d)\-(\d\d))(T(((\d\d)\:(\d\d)\:(\d\d))(\.(\d+))?))?)((Z)|(([\+\-])((\d\d)\:(\d\d)))?)?)?$/;
		
		if(re.test(value)) {
			parts = re.exec(value);
			var datePart = parts[3];
			var timePart = parts[9];
			
			if(datePart) {
				// Replace the ISO 8601 delimiters (-) with values Javascript recognizes ("/"), then try parsing again.
				result = Date._parse(datePart.replace(/\-/g, "/") + (timePart ? " " + timePart : ""));
			}
		}
		// Lastly, try to parse a date that looks like this: "2007-09-18 4:10 PM"
		else {
			re = /^((\d\d\d\d)\-(\d?\d)\-(\d?\d))(.*)/;
			parts = re.exec(value);
			if(parts) {
				var datePart = parts[1];
				var timePart = parts[5];
				if(datePart) {
					result = Date._parse(datePart.replace(/\-/g, "/") + " " + parts[5]);
				}
			}
		}
	}
	return(result);
}

Date.parseAsDate = function(value) {
	return(new Date(this.parse(value)));
}

Date.prototype.toXml = function() {
	if(!isNaN(this.valueOf())) {
		return this.getFullYear().toString().pad(4)
				+ "-" 
				+ (this.getMonth()+1).toString().pad(2)
				+ "-"
				+ this.getDate().toString().pad(2)
				+ (this.getHours() + this.getMinutes() + this.getSeconds() != 0 ? 
					"T"
					+ this.getHours().toString().pad(2)
					+ ":"
					+ this.getMinutes().toString().pad(2)
					+ ":"
					+ this.getSeconds().toString().pad(2)
					: 
					""
				);
	}
}

function raiseError (number, description, source) {
	if(typeof(global.ASPRaiseError) != "undefined") {
		global.ASPRaiseError(number, description, source);
	}
	else {
		var err = new Error(number, description);
		err.source = source;
		throw(err);
	}
}

String.prototype.trim = function() {
	return this.replace(/^\s*(.*?)\s*$/g, "$1");
}

String.prototype.pad = function(num) {
	var padding = ""
	for(var c = 1; c <= num; c++) 
		padding += "0";
		
	padding += this;
	return padding.substr(padding.length - num);
}

//DO NOT REMOVE THE FOLLOWING COMMENT
//%>]]>

