/*
 * Utility Functions
 */
var CMSConstant = {};

function getConstant(name) {
	return(!CMSConstant[name] ? name : CMSConstant[name]);
}

function addEvent(element,eventtype,callback,useCapture) {
	if (element.addEventListener){
		element.addEventListener(eventtype,callback,useCapture);
		return(true);
	} else if (element.attachEvent){
		var result = element.attachEvent("on"+eventtype,callback); 
		return(result);
	} else return(false);
} 

function getElement(id) { 
	return(typeof(id) == 'object' ? id : document.getElementById(id)); 
}

function isEmpty(obj,allow_zero) {
	if (obj == undefined) return(true);
	var value = (typeof(obj) == "string" || obj instanceof String) ? obj : obj.value;
	return(allow_zero ? /^\s*$/.test(value) : /^[0\s]*$/.test(value));
}

function getValue(id) {
	var e = getElement(id);
	return(e ? e.value : null);
}

function setValue(id,value) {
	var e = getElement(id);
	if (e) e.value = value;
	return (e ? true : false);
}

function setVisible(id,status) { 
	var e = getElement(id);
	if (e) e.style.display = status ? '' : 'none'; 
}

function toggleDisplay(id) {
	var e = getElement(id);
	if (e) e.style.display = e.style.display == 'none' ? '' : 'none';
}
/*
 * Dialog Functions
 */
function hideModalDialog() {
	var e = getElement('cms_dialog');
	clearInterval(e.interval);
	document.body.removeChild(getElement('cms_shadow'));
	document.body.removeChild(e);
}
 
function updateModalDialog() {
	var d = document.getElementById('cms_dialog');
	var e = document.getElementById('cms_shadow');
	var w = window.innerWidth || (document.documentElement ? document.documentElement.clientWidth : 0) || (document.body ? document.body.clientWidth : 0);
	var h = window.innerHeight || (document.documentElement ? document.documentElement.clientHeight : 0) || (document.body ? document.body.clientHeight : 0);
	var x = window.pageXOffset || (document.documentElement ? document.documentElement.scrollLeft : 0) || (document.body ? document.body.scrollLeft : 0);
	var y = window.pageYOffset || (document.documentElement ? document.documentElement.scrollTop : 0) || (document.body ? document.body.scrollTop : 0);
	/* Resize shadow */
	e.style.width = w+x+'px';
	e.style.height = h+y+'px';
	/* Move dialog to center */
	d.style.top = y+(h-d.offsetHeight)/2+'px';
	d.style.left = x+(w-d.offsetWidth)/2+'px';
}

function showModalDialog(html) {
	/* Return if an alert exists */
	if (getElement('cms_dialog')) return false;
	/* Create curtain */
	var e = document.createElement('div');
	e.id = 'cms_shadow';
	e.className = 'select-free';
	e.innerHTML = '<!--[if lte IE 6.5]><iframe></iframe><![endif]-->';
	with (e.style) {
		position = 'absolute';
		top = '0px';
		left = '0px';
		background = '#000';
		filter = 'alpha(opacity=20)';
		opacity = '.20';
		zIndex = '2000';
	}
	document.body.appendChild(e);
	/* Create dialog */
	var d = document.createElement('div');
	d.id = 'cms_dialog';
	d.className = 'dialog';
	d.innerHTML = html;
	with (d.style) {
		position = 'absolute';
		width = '400px';
		zIndex = '2001';
	}
	document.body.appendChild(d);
	updateModalDialog();
	d.interval = setInterval(updateModalDialog,200);
}

function showAlert(text) {
	showModalDialog("<table style='width:100%'><tr><td style='text-align:center; padding-right:20px'><img src='images/icon_warning_medium.gif' alt=''/><br /><b>"+getConstant('_WARNING')+"</b></td><td>"+text.replace(/\n/g,"<br />")+"</td></tr><tr><td colspan='2' style='text-align:center; padding-top:5px'><button type='button' onclick='hideModalDialog()'>"+getConstant('_OK')+"</button></td></tr></table>");
}

function showOkCancel(html,callback,ok,cancel) {
	showModalDialog("<table style='width:100%'><tr><td style='text-align:center; padding-right:10px'><img src='images/icon_warning_medium.gif' alt='' /><br /><b>"+getConstant('_WARNING')+"</b></td><td>"+html+"</td></tr><tr><td colspan='2' style='text-align:center; padding-top:5px'><button type='button' id='cms_ok' style='margin-right:30px'>"+ok+"</button><button type='button' id='cms_cn'>"+cancel+"</button></td></tr>");
	getElement('cms_ok').onclick = function() {
		hideModalDialog();
		if (callback) callback(true);
	}
	getElement('cms_cn').onclick = function() {
		hideModalDialog();
		if (callback) callback(false);
	}
}
/*
 * Validation Functions
 */
function validateClearIcons(prefix) {
	prefix = prefix ? prefix+"_" : "";
	var list = document.getElementsByTagName("img");
	for (var i = 0; i < list.length; i++) {
		if (list[i].id && list[i].id.substr(0,4) == "vic_"+prefix) list[i].src = "images/icon_blank.gif";
	}
}

function validateSetIcon(id,valid) {
	var e = getElement("vic_"+id); 
	if (!e) return(false);
	e.src = 'images/'+(valid ? 'icon_blank.gif' : 'icon_warning.gif');
	if (!valid && (e = getElement(id))) e.focus();
}

function validateNotEmpty(id,allow_zero) {
	var e = getElement(id);
	var valid = !isEmpty(e,allow_zero);
	validateSetIcon(e.id,valid);
	return(valid);
}

function validate(id,check_empty,callback) {
	var e = getElement(id);
	var valid = (check_empty && isEmpty(e.value)) || callback(e);
	validateSetIcon(e ? e.id : id,valid);
	return(valid);
}
/*
 * Test Functions
 */
function testEmail(value) {
	return(/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(value));
}