function custom_alert(msg, title) {
	alert_box = $('custom_alert');
	
	if(!alert_box) {
		alert_box = Builder.node('div', { className: 'custom_alert', id: 'custom_alert' }, [
			Builder.node('div', { id: 'alert_body' }, [
				Builder.node('div', { id: 'alert_header', onclick: 'unregister_popup("#custom_alert"); $("custom_alert").hide(); '}),
				Builder.node('div', { id: 'alert_content' })
			])
		]);
		document.body.appendChild(alert_box);
	}

//	if(!alert_box) {
//		alert_header = new Element('div', { id: 'alert_header', onclick: 'unregister_popup("#custom_alert"); $("custom_alert").hide(); '})
//		alert_content = new Element('div', { id: 'alert_content' });
//		alert_body = new Element('div', { id: 'alert_body' }).update([alert_header, alert_content]);
//		alert_box = new Element('div', { className: 'custom_alert', id: 'custom_alert' }).update(alert_body);	
//		document.body.appendChild(alert_box);		
//	}
	
	title = !title ? 'Message' : title;
	
	title = "&nbsp;";
	
	$('alert_header').innerHTML 	= title;
	$('alert_content').innerHTML 	= msg;
	
	center_box(alert_box);
	
	if(!$('custom_alert').visible()) $('custom_alert').show();
	
	//Effect.Shake('custom_alert', { duration: 0.25, distance: 2 });
	//setTimeout("$('custom_alert').hide()", 4000);
	
	register_popup("#custom_alert");
}

alt_items = new Array();
function alt(a) {
	if(alt_items[a])
		alt_items[a]++;
	else
		alt_items[a] = 1;
	
	return alt_items[a] % 2 ? 'alt' : '';
}

function form_update() {
	args 	= form_update.arguments;
	url		= args[0];
	frm 	= args[1];	
	cb		= args[2];
	
	f = function(o) {
		xmlDoc 		= create_xml_doc(o.responseText);
		items 		= xmlDoc.getElementsByTagName('item');	
		
		// if there are results, only need the first
		for(x = 0; x < items.length && x < 1; x++) {
			for(y = 0; y < items[x].childNodes.length; y++) {
				cn = items[x].childNodes[y];	
				tn = cn.tagName; 				// tag name
				vl = getText(items[x], tn);		// tag value	
				
				if(frm.elements[tn])
					$(frm.elements[tn]).setValue(vl);
			}
		}	

		if(cb && typeof cb == 'function')
			cb(items);
	}

	search_values = '';
	for(x = 3; x < args.length; x++)
		search_values = '&' + args[x] + '=' + escape($F(args[x]));
	
	new Ajax.Request(url + search_values, {   
		method: 'get',   
		onSuccess: f
	});	
}

function overlay(close_id) { 
	ovr = $('tboverlay');
	
	if(!ovr) {
		//ovr = Builder.node('div', { id: 'tboverlay' }, "&nbsp;");
		ovr = new Element('div', { 'id': 'tboverlay' }).update("&nbsp;");
		document.body.appendChild(ovr);
	}
	
	ovr.onclick = function() { 
		$('tboverlay').hide();
		
		if(close_id)
			$(close_id).hide() 
	};
	
	ovr.show();
	
	return 'tboverlay';
}

function center_box(element){
    try {
        element = $(element);
    } catch(e) {
        return;
    }

    var my_width  = 0;
    var my_height = 0;

    if (typeof( window.innerWidth ) == 'number') {
        my_width  = window.innerWidth;
        my_height = window.innerHeight;
    } else if (document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight )) {
        my_width  = document.documentElement.clientWidth;
        my_height = document.documentElement.clientHeight;
    } else if (document.body && ( document.body.clientWidth || document.body.clientHeight )) {
        my_width  = document.body.clientWidth;
        my_height = document.body.clientHeight;
    }

    element.style.position = 'absolute';
    element.style.zIndex   = 1001;

    var scrollY = 0;

    if ( document.documentElement && document.documentElement.scrollTop ) {
        scrollY = document.documentElement.scrollTop;
    } else if ( document.body && document.body.scrollTop ) {
        scrollY = document.body.scrollTop;
    } else if ( window.pageYOffset ) {
        scrollY = window.pageYOffset;
    } else if ( window.scrollY ) {
        scrollY = window.scrollY;
    }

    var elementDimensions = Element.getDimensions(element);

    var setX = ( my_width  - elementDimensions.width  ) / 2;
    var setY = ( my_height - elementDimensions.height ) / 2 + scrollY;

    setX = ( setX < 0 ) ? 0 : setX;
    setY = ( setY < 0 ) ? 0 : setY;

    element.style.left = setX + "px";
    element.style.top  = setY + "px";

    element.style.display  = 'block';
}

popup_list = new Array();
function register_popup(ptype) {
	for(x = 0; x < popup_list.length; x++) {
		if(popup_list[x] == ptype)
			return true;
	}
		
	setTimeout(function() { popup_list[popup_list.length] = ptype; }, 10);
}

function unregister_popup(ptype) {
	for(x = 0; x < popup_list.length; x++) {
		if(popup_list[x] == ptype)
			popup_list.splice(x, 1);
	}
}

function check_popup_clicks(event) {					
	popups = $$(popup_list);
	
	var e = Event.element(event); 
	
	for(x = 0; x < popups.length; x++) {			
		if(e.descendantOf(popups[x]))
			return true;
	}
	
	unregister_list = new Array();
		
	for(x = 0; x < popups.length; x++) {
		if(popups[x].visible()) {
			popups[x].hide();	
			
			if(popups[x].id)
				unregister_list[unregister_list.length] = "#" + popups[x].id;							
		}
	}
	
	for(x = 0; x < unregister_list.length; x++)
		unregister_popup(unregister_list[x]);
	
	return true;
}

Event.observe(document, 'click', check_popup_clicks);

function bind_forms() {
	frms = $$('.bind_ajax');
	for(x = 0; x < frms.length; x++)	
		bind_form(frms[x]);
}

function bind_form(frm) {
	if(typeof frm.onsubmit == 'function') {
		callback = frm.getAttribute('onsubmit');
		frm.onsubmit = function() { }
		frm.setAttribute('callback', callback);
	}	
		
	Event.observe(frm, 'submit', function(event) {
		elt = Event.element(event);
		
		if(JSV.Validate.checkForm(event))
			form_handler(elt);		
				
		Event.stop(event);
		
		return false;		
	});
}

// ajax submits a form to it's action
function form_handler(frm, callback) {
	f = function(o) {				
		xmlDoc 		= create_xml_doc(o.responseText);
		
		id			= getText(xmlDoc, 'id');
		message 	= getText(xmlDoc, 'message');
		title		= getText(xmlDoc, 'title');
		redirect	= getText(xmlDoc, 'redirect');
		delay		= getText(xmlDoc, 'wait');	
				
		if(redirect && delay)
			setTimeout("window.location='" + redirect + "'", parseInt(delay) * 1000);
			
		if(message)
			custom_alert(message, title ? title : 'Form Submission');					
		
		if(id && frm.elements['id'])
			frm.elements['id'].value = id;		
		
		if(frm.getAttribute('callback')) {
			cb = frm.getAttribute('callback');
			
			if(window.execScript) 
				window.execScript(cb); 
			else
				eval(cb);
		}
		
		if(callback && typeof callback == 'function')
			callback(o);
	}
		
	new Ajax.Request(frm.getAttribute('action') + '?j=' + ut(), { postBody: $(frm).serialize(), method: 'post', onSuccess: f }); 	
}

function loader(ptr, limg, lmsg) {
	img1 			= $(document.createElement('img'));
	img1.src 		= limg;
	
	div1			= $(document.createElement('div'));
	div1.addClassName('loader');
	
	div1.appendChild(img1);
	
	if(lmsg) {
		div2			= $(document.createElement('div'));
		div2.innerHTML 	= lmsg;
		div1.appendChild(div2);
	}
	
	ptr.appendChild(div1);
	
	return div1;
}

function dbg(t) {
	d = $('dbg');
	
	if(!d) {
		d = $(document.createElement('div'));	
		document.body.appendChild(d);
		d.setAttribute('id', 'dbg');	
		d.setStyle({
			position: 'absolute',
			bottom: '5px',
			left: '5px',
			width: '600px',
			padding: '4px',
			backgroundColor: 'white',
			border: '1px solid red'
		});
		
		d.onclick = function() { this.innerHTML = '' }
	}
	
	d.innerHTML += t.replace(/</g,"&lt;").replace(/>/g,"&gt;") + '<hr>';
}

function ut() {
	var foo = new Date;
	var unixtime_ms = foo.getTime();
	var unixtime = parseInt(unixtime_ms / 1000); 
	return unixtime;
}