function custom_alert(msg, title) {				
	alert_box = $('custom_alert');
	
	if(!alert_box) {			
		alert_box 		= new Element('div', { className: 'custom_alert', id: 'custom_alert' });
		alert_body 		= new Element('div', { id: 'alert_body' });
		alert_header	= new Element('div', { id: 'alert_header', onclick: '$("custom_alert").hide(); '});
		alert_content	= new Element('div', { id: 'alert_content' });
		
		alert_box.insert(alert_body);
		alert_body.insert(alert_header);
		alert_body.insert(alert_content);
		
		document.body.appendChild(alert_box);		
	}	
	
	title = !title ? 'Message' : title;
	$('alert_header').update(title);
	
	$('alert_content').innerHTML = msg;
	
	center_box(alert_box);
	
	if( !$('custom_alert').visible() ) 
		$('custom_alert').show();
}

function overlay(cb) { 
	ovr = $('tb-overlay');
	
	if(!ovr) {
		ovr = new Element('div', { id: 'tb-overlay' });
		document.body.appendChild(ovr);
	}
	
	Event.observe(ovr, 'click', function() {
		$('tb-overlay').remove();
			
		if(cb)
			cb();
	});
	
	ovr.show();
	
	return ovr;
}

function center_box(elt, pos) {
	elt = $(elt);
	
	window_dimensions	= document.viewport.getDimensions(); 
	scroll_offsets 		= document.viewport.getScrollOffsets();	
	
	ofs_left			= scroll_offsets.left 		+ 	(window_dimensions.width 	- 	elt.getWidth()) 	/ 2;
	ofs_top				= scroll_offsets.top 		+ 	(window_dimensions.height 	- 	elt.getHeight()) 	/ 2;
	
	if(ofs_left < 0) 	ofs_left 	= 0;
	if(ofs_top < 0)		ofs_top 	= 0;
			
	elt.setStyle({
		position: 	'absolute',
		zIndex: 	'1001',
		left:		ofs_left 	+ 'px',
		top:		ofs_top		+ 'px'
	});		

	switch(pos) {
		case 'fixed':
			elt.setStyle({
				position: 	'fixed',
				left:		(window_dimensions.width 	- 	elt.getWidth()) 	/ 2 	+ 'px',
				top: 		(window_dimensions.height 	- 	elt.getHeight()) 	/ 2		+ 'px'
			});
		break;
	}
}

function bind_forms() {
	$$('form.bind_ajax').each(function(elt) { bind_form(elt) });
}

function bind_form(frm) {	
	frm = $(frm);
	
	if( frm.getAttribute('bound') == '1' )
		return false;
		
	frm.setAttribute('bound', '1');
	
	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;		
	});
}

function form_handler(frm, callback) {
	f = function(o) {	
		xmlDoc 		= m_xml.create_doc(o.responseText);
		
		id			= m_xml.get_text(xmlDoc, 'id');
		msg 		= m_xml.get_text(xmlDoc, 'message');
		ttl			= m_xml.get_text(xmlDoc, 'title');
		redirect	= m_xml.get_text(xmlDoc, 'redirect');
		delay		= m_xml.get_text(xmlDoc, 'wait');	
				
		if(redirect && delay)
			setTimeout("window.location='" + redirect + "'", parseInt(delay) * 1000);
			
		if(msg)
			custom_alert(msg, ttl ? ttl : '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);
	}
	
	frm = $(frm);
		
	new Ajax.Request(frm.getAttribute('action') + '?j=' + ut(), { 
		postBody: frm.serialize(), 
		method: 'post', 
		onSuccess: f 
	}); 	
}

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