// JavaScript Document
if (!String.prototype.trim) {
	String.prototype.trim = function() {
		var str = this.replace(/^\s*/, '');
		return str.replace(/\s*$/, '');
	}
}

if (!Array.prototype.has) {
	Array.prototype.has = function(value) {
		for (var i = 0; i < this.length; i++) {
			if (this[i] == value) {
				return true;
			}
		}
		return false;
	}
}

if (!Array.prototype.push) {
	Array.prototype.push = function(value) {
		this[this.length] = value;
	}
}

_popupNames = new Array();
_popups = new Array();

function __winPopup(url, winName, title, width, height) {
	if (!winName) {
		winName = '__popup__';
	}
	if (!width) width = 400;
	if (!height) height = 400;
	var left = window.screen.availWidth / 2 - width / 2;
	var top = window.screen.availHeight / 2 - height / 2;

	var win = dhtmlmodal.open(winName, "iframe", url, title, 'width=' + width + 'px,height=' + height + 'px,center=1,resize=1,scrolling=1', 'recal')
	if (!_popups[winName]) {
		_popupNames.push(winName);
	}
	_popups[winName] = win;
	
	return win;
	
}

function winPopupX(url, winName, title, width, height) {
	var p = window;
	while (p.parent != null && p.parent != p) {
		p = p.parent;
	}

	if (p != null) {
		return p.__winPopup(url, winName, title, width, height);
	}
}

function winPopup(url, winName, width, height, title) {
	if (!title) title = ':: Elixir ::';
	return winPopupX(url, winName, title, width, height);
}

function modalPopup(url, winName, width, height) {
	if (!winName) {
		winName = '__popup__';
	}
	if (window.showModalDialog) {
		return window.showModalDialog(url, '', 'dialogWidth:' + width + 'px;dialogHeight:' + height + 'px');
	} else {
		return window.open(url, '', 'height=' + height + ',width=' + width + ',toolbar=no,directories=no,status=no,menubar=no,scrollbars=1,resizable=no,modal=yes');
	}
}

function resizeWindow(width, height) {
	window.resizeTo(width, height);
	window.moveTo(window.screen.availWidth / 2 - width / 2, window.screen.availHeight / 2 - height / 2);
}

function closeWin(win) {
	for (var i = 0; i < _popupNames.length; i++) {
		var iframe = _popups[_popupNames[i]].getElementsByTagName('IFRAME')[0];
		if (iframe.contentWindow == win) {
			_popups[_popupNames[i]].hide();
		}
	}
}

function closePopup() {
	if (window.opener) {
		window.opener.closeWin(window);
	} else if (window.parent) {
		window.parent.closeWin(window);
	}
}

function openerUrl(url) {
	if (window.opener) {
		window.opener.location = url;
		window.close();
	} else {
		redirect(url);
	}
}

function checkForm(form) {
	var fields = '';
	for (var i = 0; i < form.elements.length; i++) {
		var elem = form.elements[i];
		var value = elem.value.trim();
		var name = elem.name.toLowerCase();
		if (elem.type.toLowerCase() == 'text' || elem.type.toLowerCase() == 'textarea') {
			if (name.indexOf('email') >= 0 && value != '' && !value.match(/^\w+([\.-]\w+)?@\w+([\.-]\w+)+$/)) {
				fields += '\n' + ((elem.title == '') ? 'Email' : elem.title);
			}
			else if (name.indexOf('postcode') >= 0 && value != '' && !value.match(/^\d+$/)) {
				fields += '\n' + ((elem.title == '') ? 'Postcode' : elem.title);
			}
			else if (value == '' && elem.title != '') {
				fields += '\n' + elem.title;
			}
		} else if (elem.type.toLowerCase() == 'select-one') {
			if (elem.title != '' && (elem.value == 0 || elem.value == '')) {
				fields += '\n' + elem.title;
			}
		}
	}
	if (fields != '') {
		alert('Please fill or verify following fields: \n\n' + fields);
		return false;
	}
	return true;
}

function inputPhone(event, obj) {
	var chars = new Array(
		45, // "-"
		32, // space
		40, // "("
		41 // ")"
	);
	return inputNumber(event, obj, false, false, chars);
}

function inputInteger(event, obj, length) {
	return inputNumber(event, obj, false, length);	
}

function inputFloat(event, obj) {
	return inputNumber(event, obj, true);
}

function inputNumber(event, obj, decimals, length, chars) {
	if (!length) length = 999;
	if (!chars) {
		chars = new Array();
	}
	var allowed = new Array(
		9,	// tab
		8,	// backspace
		13,	// enter
		46	// dot "."
	);
	var which = event.which;
	var key = event.keyCode;
	if (typeof(which) == "undefined") {
		which = key;
		key = 0;
	}
	if (which == 46 && obj.value.indexOf(".") == -1 && decimals && obj.value.length < length) {
		return true;
	}
	if (!((which >= 48 && which <= 57 && obj.value.length < length) || which == 13 || (key >= 37 && key <= 40) || chars.has(which) || (key >= 112 && key <= 123) || event.ctrlKey || allowed.has(key))) {
		event.returnValue = false;
		return false;
	}
	return true;
}

function redirect(url) {
	window.location = url;
}