var CMSConstant = {};

function getConstant(name) {
	return(!CMSConstant[name] ? name : CMSConstant[name]);
}

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';
}
/*
 * Utility Functions
 */
function cms_alert_update() {
	var e = document.getElementById("cms_alert_curtain");
	var d = document.getElementById("cms_alert");
	var de = document.documentElement;
	/* Resize curtain */
	e.style.width = de.clientWidth+de.scrollLeft+"px";
	e.style.height = de.clientHeight+de.scrollTop+"px";
	/* Move dialog to center */
	d.style.top = de.scrollTop+(de.clientHeight-d.offsetHeight)/2+"px";
	d.style.left = de.scrollLeft+(de.clientWidth-d.offsetWidth)/2+"px";
}

function cms_alert(text) {
	/* Return if an alert exists */
	if (getElement("cms_alert")) return(false);
	/* Create curtain */
	var e = document.createElement("div");
	e.id = "cms_alert_curtain";
	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 = '16000';
	}
	document.body.appendChild(e);
	/* Create dialog */
	var d = document.createElement("div");
	d.id = "cms_alert";
	d.className = "dialog";
	d.innerHTML = "<table style='width:100%'><tr><td style='text-align:center'><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='var e = getElement(\"cms_alert\"); clearInterval(e.interval); document.body.removeChild(getElement(\"cms_alert_curtain\")); document.body.removeChild(e);'>"+getConstant("_OK")+"</button></td></tr></table>";
	with (d.style) {
		position = "absolute";
		width = "320px";
		zIndex = '16001';
	}
	document.body.appendChild(d);
	cms_alert_update();
	d.interval = setInterval(cms_alert_update,100);
	var beep = getElement("cms_beep");
	//if (beep) beep.play(2);
}

/*
 * 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));
}
