Object.prototype.objectType = function()
{
var obType = String(this.constructor).match(/function\s+(\w+)/);

if(obType) return obType[1];
return "undefined"; // just in case...
};

function getField(fieldPath) {
	if (document.getElementById(fieldPath)!=null) {
		var fieldValue = document.getElementById(fieldPath).value;
		if (fieldValue=="") {
			return fieldValue;
		} else if (!isNaN(Date.parse(fieldValue))) {
			return new Date(Date.parse(fieldValue));
		} else if (!isNaN(Number(fieldValue))) {
			return new Number(fieldValue);
		} else {
			return fieldValue;
		}
	} else {
		return null;
	}
}

f = getField;

function ifEqual(a, b) {
	if (a!=b) {
		return new ValidationLibrary(false);
		//alert('not equal');
	}
	//alert('equal');
	return this;
}

function ifNotEqual(a, b) {
	if (a==b) {
		return new ValidationLibrary(false);
		//alert('not equal');
	}
	//alert('equal');
	return this;
}

function ifLessThan(a, b) {
	
	if (a >= b) {
		return new ValidationLibrary(false);
		//alert('not equal');
	}
	//alert('equal');
	return this;
}

function ifGreaterThan(a, b) {
	
	if (a <= b) {
		return new ValidationLibrary(false);
		//alert('not equal');
	}
	//alert('equal');
	return this;
}

function assertEqual(msg, a, b) {
	if (this.check) {
		if (a!=b) {
			this.error(msg);
		}
	}
	return this;
}

function assertNotEmpty(msg, a) {
	if (this.check) {
		if (a=="") {
			this.error(msg);
		}
	}
	return this;
}

function assertEmpty(msg, a) {
	if (this.check) {
		if (a!="") {
			this.error(msg);
		}
	}
	return this;
}

function assertAllEmpty(msg, arr) {
	if (this.check) {
		var fail = false;
		for (var i = 0; i<arr.length; i++) {
			if (arr[i]!="") {
				fail = true;
			}
		}
		if (fail) {
			this.error(msg);
		}
	}
	return this;
}

function assertAllNotEmpty(msg, arr) {
	if (this.check) {
		var fail = false;
		for (var i = 0; i<arr.length; i++) {
			if (arr[i]=="") {
				fail = true;
			}
		}
		if (fail) {
			this.error(msg);
		}
	}
	return this;
}

function assert(msg) {
	if (this.check) {
		this.error(msg);
	}
	return this;
}

function assertIsDate(msg, a) {
	if (this.check) {
		if (a.objectType()!="Date") {
			this.error(msg);
		}
	}
	return this;
}

function assertIsNumber(msg, a) {
	if (this.check) {
		if (a.objectType()!="Number") {
			this.error(msg);
		}
	}
	return this;
}

function sum(arr) {
	var sum = 0.0;
	var fail = false;
	for (var i = 0; i<arr.length; i++) {
		if(!isNaN(Number(arr[i]))) {
			sum = sum + Number(arr[i]);
		}
	}
	
	return sum;
	
}

function error(msg) {
	var errfield = "errfield";
	var errelement = document.getElementById(errfield);
	if (errelement!=null) {
		errelement.innerHTML = errelement.innerHTML + "<li>" + msg + "</li>";
	}
}

function resetError() {
	var errfield = "errfield";
	var errelement = document.getElementById(errfield);
	if (errelement!=null) {
		errelement.innerHTML = "";
	}
}

function ValidationLibrary(check) {
	this.check = check;
	this.getField = getField;
	this.assertEqual = assertEqual;
	this.assertNotEmpty = assertNotEmpty;
	this.assertEmpty = assertEmpty;
	this.assertAllEmpty = assertAllEmpty;
	this.assertAllNotEmpty = assertAllNotEmpty;
	this.assert = assert;
	this.sum = sum;
	this.ifEqual = ifEqual;
	this.ifNotEqual = ifNotEqual;
	this.ifGreaterThan = ifGreaterThan;
	this.ifLessThan = ifLessThan;
	this.assertIsDate = assertIsDate;
	this.assertIsNumber = assertIsNumber;
	this.error = error;
	this.resetError = resetError;
}
