function add_listitem( list, head, body ) {
	var li = document.createElement( 'li' ), it;
	
	it = document.createElement( 'strong' );
	it.appendChild( document.createTextNode( head ) );
	li.appendChild( it );
	
	it = document.createElement( 'br' );
	li.appendChild( it );

	li.appendChild( document.createTextNode( body ) );
}

function ax_evt( ax, msg ) {
	
	if (msg == 'error' || msg == 'timeout') {
		alert( 'Cannot connect to server! ('+ msg +')' );
		return;
	}
	
	eval( unescape( msg ) );

	if (res.ret == 0 && res.msg != '') { //nak
		alert( res.msg );
	}
	else if (res.ret == 1 && res.msg != '') { //ack
		alert( res.msg );
	}
}

function cm_form_submit( evt ) {
	var obj = (window.event ? window.event.srcElement : evt.target);
	
	if (obj.is_submitted !== undefined && obj.is_submitted == true) {
		return false;
	}

	var comment = document.getElementById( 'comment' );
	var handle = document.getElementById( 'handle' );
	
	if (comment.value.length < 8) {
		alert( 'Input a valid comment!' );
		return false;
	}
	
	var inp = obj.parentNode.getElementsByTagName( 'input' );
	var params = [];
	
	params.push( 'comment='+ escape( comment.value ) );
	params.push( 'handle='+ escape( handle.value ) );
	
	for (var i = 0; i < inp.length; i++) {
		switch (inp[i].name) {
			case 'sender': {
				params.push( 'sender='+ escape( inp[i].value ) );
				break;
			}
			case 'optval': {
				params.push( 'optval='+ escape( inp[i].value ) );
				break;
			}
			case 'optcro': {
				params.push( 'optcro='+ escape( inp[i].value ) );
				break;
			}
			case 'itemid': {
				params.push( 'itemid='+ escape( inp[i].value ) );
				break;
			}
		}
	}
	params = params.join( '&' );
	var ax = new ajax( ax_evt, 15000 );
	ax.post( '/dynamic/ax_add_comment.php', params, true );
	
	return false;
}

function cm_form_setup() {
	var frm = document.getElementById( 'comment_form' );

	if (frm !== null) {
		var btn = frm.getElementsByTagName( 'button' );
		btn[0].onclick = cm_form_submit;
		
		frm.onkeypress = function( evt ) {
			var obj = (window.event ? window.event.srcElement : evt.target);
			
			if (obj.value.length > 2000) {
				alert( 'You have reached the character limit for this comment!' );
				return false;
			}
			
			return true;
		};
	}
}