/******************* Element *******************/
Element = function(id, type) {
	try {
		this.elem = document.getElementById(id);
		this.html = this.elem.innerHTML;
	} catch(e) {
		if (type) {
			this.elem = Element.create(id, type);
			this.html = this.elem.innerHTML;
		}
	}
	this.html = function(html) {
		if (html) this.elem.innerHTML += html;
		return this.elem.innerHTML;
	}
	this.newhtml = function(html) {
		if (html) this.elem.innerHTML = html;
	}
	this.top = this.y = function() {
		var top = 0;
		if (this.elem.offsetParent) {
			top = this.elem.offsetTop;
			while (this.elem = this.elem.offsetParent) top += this.elem.offsetTop;
		}
		return top;
	}
	this.left = this.x = function() {
		var left = 0;
		if (this.elem.offsetParent) {
			left = this.elem.offsetLeft;
			while (this.elem = elem.offsetParent) left += this.elem.offsetLeft;
		}
		return left;
	}
	this.height = this.h = function() {
		if (this.elem.clientHeight) return this.elem.clientHeight;
		else if (this.elem.offsetHeight) return this.elem.offsetHeight;
		else return 0;
	}
	this.width = this.w = function() {
		if (this.elem.clientWidth)return this.elem.clientWidth;
		else if (this.elem.offsetWidth) return this.elem.offsetWidth;
		else return 0;
	}
	this.style = function(styles) { //styles should be a {} of name/value pairs
		for (var style in styles) this.elem.style[style] = styles[style];
	}
	this.destroy = function() {
		try { document.getElementsByTagName('body')[0].removeChild(this.elem) } catch(e) { return false }
	}
}
Element.create = function(id, type) {
	try {
		this.elem = document.createElement(type);
		this.elem.id = id;
		this.elem.style.position = "absolute";
		this.elem.style.display = "none";
		document.getElementsByTagName('body')[0].appendChild(this.elem);
		return this.elem;
	} catch(e) { return false }
}

/******************* Form *******************/
Form = function(action) {Form[action].apply(this, Array.prototype.slice.apply(arguments, [1]))}
Form.forceAlphaNumeric = function(field) { //field = field reference
	for(var i = 0; i < field.value.length; ++i){
		if(field.value.charAt(i).match(/[^a-zA-Z0-9]/)){
			field.value = field.value.replace(field.value.charAt(i), "");
		}
	}
}
Form.forceNumeric = function(field) { //field = field reference
	for(var i = 0; i < field.value.length; ++i){
		if(field.value.charAt(i).match(/[^0-9]/)){
			field.value = field.value.replace(field.value.charAt(i), "");
		}
	}
}
Form.setSelect = function(sel, val) { //sel = select object reference, value = value to select
	for(var i = 0; i < sel.length; ++i) if(sel[i].value == val) sel[i].selected = true;
}
Form.validateField = function(field) { //field needs to be a field reference
	switch (field.getAttribute('require').toLowerCase()) {
		case 'text': //validate that a text field is not empty
			return (field.value != "");
			break;
		case 'email': //validate an email address
			return ((field.value.indexOf("@") != -1) && (field.value.indexOf(".") != -1) && (field.value.length > 5));
			break;
		case 'notinvalid': //validate that a select box selection is not invalid (from csv list in 'invalid' attribute)
			var invalid = field.getAttribute('invalid').split(',');
			for (var i = 0; i < invalid.length; ++i) if (field.selectedIndex == invalid[i]) return false;
			return true;
			break;
		case 'checked': //validate that a checkbox is checked
			return (field.checked);
			break;
		case 'notnone': //validate that a selection was made from a radio group
			var group = field.form[field.name];
			for (var i = 0; i < group.length; ++i) if (group[i].checked) return true;
			return false;
			break;
		case 'custom': //validate field with custom function (function name in 'function' attribute -- should return boolean value)
			return [field.getAttribute('function')](field);
			break;
		default:
			alert('requirement not understood, please correct before use in a production environment.');
			return false;
			break;
	}
}
Form.validate = function(form) { //form = form reference
	var arrProblems = [];
	for (var i = 0; i < form.length; ++i) {
		if (form.elements[i].getAttribute('require')) if (!Form.validateField(form.elements[i])) arrProblems.push(form.elements[i]);
		if (document.getElementById(form.elements[i].name + "_label")) document.getElementById(form.elements[i].name + "_label").style.color = "";
	}
	if (arrProblems.length != 0) {
		Form.displayProblems(arrProblems);
		return false;
	} else {
		Form.disableSubmit(form, 10);
		return true;
	}
}
Form.displayProblems = function(arrProblems) {
	while (arrProblems.length) {
		var field = arrProblems.shift();
		if (document.getElementById(field.name + "_label")) document.getElementById(field.name + "_label").style.color = "#c00";
	}
	var msg = 'Some information seems to be missing!<br /><br />';
	msg += 'Please completely fill in all fields marked in red and submit this form again.';
	//alert(msg);
	Alert(msg);
}
Form.submitTo = function(form, action) { //form = form reference
	form.action = action;
	form.submit();
}
Form.disableSubmit = function(form, seconds) { //form needs to be a form reference, seconds will disable submit for that interval
	for (var i = 0; i < form.length; ++i) if (form.elements[i].type == "submit") {
		form.elements[i].disabled = true;
	}
	setTimeout(function() {Form.enableSubmit(form)}, seconds * 1000);
}
Form.enableSubmit = function(form) { //form = form reference
	for (var i = 0; i < form.length; ++i) if (form.elements[i].type == "submit") form.elements[i].disabled = false;
}



/******************* Alert *******************/
Alert = function(msg) {
	Alert.div = new Element("alertbox", "div");
	Alert.div.style({visibility: "hidden", display: "block", top: "50%", left: "50%", padding: "16px", textAlign: "center", 
			  border: "1px solid #666", borderRight: "2px solid #999", borderBottom: "2px solid #999", background: "#ffd",
			  fontWeight: "bold"});
	Alert.div.newhtml(msg);
	Alert.div.html('<div style="text-align: center; margin-top: 15px;"><input type="button" value="Close" onClick="Alert.dismiss()" /></div>');
	
	Alert.center();
	Alert.div.style({visibility: "visible"});
	window.onresize = Alert.center;
	window.onscroll = Alert.center;
}
Alert.center = function() {
	var topmargin = (findWindowScroll() - (Alert.div.h())/2 + 8) + "px";
	var leftmargin = -((Alert.div.w())/2 + 8) + "px";
	Alert.div.style({marginTop: topmargin, marginLeft: leftmargin});
}
Alert.dismiss = function() { Alert.div.destroy(); }
function findWindowScroll(){
	var ScrollTop = document.body.scrollTop;
	if (ScrollTop == 0){
		if (window.pageYOffset) ScrollTop = window.pageYOffset;
		else ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}
	return ScrollTop;
}