/**************************
* Clones
**************************/

function markclone ( cid, rowid, mark ) {
	var con = new ajax();
	var pre = ( mark ) ? '' : 'un';
	
	con.resource.onreadystatechange = function() {
		if ( con.ready() ) {
			change_data( 'cname' + rowid, con.response() );
		}
		else {
			change_data( 'cname' + rowid, '<i>Saving...</i> <img src="images/loading.gif" alt="" />' );
		}
	}
	
	con.connect( 'data.php?act=' + pre + 'mark&row=' + rowid + '&cid=' + cid + '&sid=' + sid );
}

/**************************
* Ratings
**************************/

function rate ( rowid, cloneid ) {
	var form = eval( 'document.rateform_' + rowid );
	var off  = form.offensive.value;
	var def  = form.defensive.value;
	var leg  = form.legitimacy.value;
	var over = form.overall.value;
	
	if ( off == 0 || def == 0 || leg == 0 || over == 0 ){
		alert( 'Please select a rating for each area.' );
	}
	else {
		var con = new ajax();
		con.add( 'offense', off );
		con.add( 'defense', def );
		con.add( 'legit'  , leg );
		con.add( 'overall', over );
	
		con.resource.onreadystatechange = function () {
			if ( con.ready() ) {
				change_data( 'rate_' + rowid, con.response() );
				ratings( cloneid, rowid );
			}
			else {
				form.send.disabled = "true";
				form.offense.disabled = "true";
				form.defense.disabled = "true";
				form.legitimacy.disabled = "true";
				form.overall.disabled = "true";
				form.send.value = "Sending...";
			}
		}
		
		 con.connect( 'data.php?act=rate&cid=' + cloneid );
	}
}

function dropdown ( fieldname, title ) {
	return '<select name="' + fieldname + '">' +
  '<option value="0">' + title + '</option>' +
  '<option value="10">10</option>' +
  '<option value="9">9</option>' +
  '<option value="8">8</option>' +
  '<option value="7">7</option>' +
  '<option value="6">6</option>' +
  '<option value="5">5</option>' +
  '<option value="4">4</option>' +
  '<option value="3">3</option>' +
  '<option value="2">2</option>' +
  '<option value="1">1</option>' +
'</select>';
}

function rateform ( cloneid, rowid ) {
	var formname = "rateform_" + rowid;
	var form     = '<form name="' + formname + '" onsubmit="rate(' + rowid + ', \'' + cloneid + '\');return false">' +
  dropdown( 'offensive', 'Offense' ) + ' / ' +
  dropdown( 'defensive', 'Defense' ) + ' / ' +
  dropdown( 'legitimacy', 'Legitimacy' ) + ' / ' +
  dropdown( 'overall', 'Overall' ) + ' <img src="http://www.shadowdiablo.com/images/point.gif" alt="" /> ' +
  '<input id="send" name="send" type="submit" value="Submit" class="button" />' +
'</form>';

	return form;
}

function cancelrate ( rowid ) {
	document.getElementById( 'rate_' + rowid ).innerHTML = ratecache[ rowid ];
}

function ratings ( cid, rowid ) {
	var con = new ajax();
	
	switchlinks( 'ratings', cid, rowid );
	
	con.resource.onreadystatechange = function () {
		if ( con.ready () ) {
			change_data( 'info' + rowid, rateform(cid, rowid) + '<hr />' + con.response() );
		}
		else {
			change_data( 'info' + rowid, '<i>Loading Ratings...</i><br /><img src="images/loading.gif" alt="" />' );
		}
	}
	
	con.connect( 'data.php?act=ratings&cid=' + cid );
}

/**************************
* Comments
**************************/

var commentcache = new Array();
var detailscache = new Array();
var insertcache  = new Array();

function switchlinks ( selected, charid, rowid ) {
	commentcache[ rowid ] = document.getElementById( 'cnumber' + rowid ).innerHTML;
	
	if ( ! detailscache[ rowid ] ) {
		detailscache[ rowid ] = document.getElementById( 'info' + rowid ).innerHTML;
	}
	
	try {
		if ( eval( 'document.addform' + rowid + '.comment.value' ) ) {
			insertcache[ rowid ] = eval('document.addform' + rowid + '.comment.value');
		}
	} catch(e) {}

	var links = document.getElementById('links' + rowid).getElementsByTagName('a');
	
	for ( c = 0; c < links.length; c++ ) {
		links[c].className = '';
	}
	
	switch ( selected ) {
		case 'details':
			links[0].className = 'selected';
			break;
		case 'comments':
			links[1].className = 'selected';
			break;
		case 'ratings':
			links[2].className = 'selected';
			break;
		case 'addcomment':
			links[3].className = 'selected';
			break;
	}
}

function comments ( id, rowid ) {
	switchlinks( 'comments', id, rowid );
	
	var con = new ajax();
	con.resource.onreadystatechange = function () {
		if ( con.ready() ) {
			change_data( 'info' + rowid, con.response() );
		}
		else {
			change_data( 'info' + rowid, '<i>Loading Comments</i><br /><img src="images/loading.gif" alt="" />' );
		}
	}
	
	con.connect( 'data.php?act=comments&cid=' + id + '&rowid=' + rowid );
}

function checkform ( rowid ) {
	var form = eval( 'document.addform' + rowid + '');
	var comm = form.comment.value.replace(/\+/g, '%20');
	var cloneid = form.cid.value;
	
	if ( ! comm ) {
		alert( 'You must enter a comment to add to this clone.' );
		return;
	}
	
	var con = new ajax();
	con.add( 'comment', comm );
	
	con.resource.onreadystatechange = function () {
		if ( con.ready() ) {
			if ( con.response() ) {
				alert( con.response() );
			}
			else {
				comments( cloneid, rowid );
				document.getElementById( 'cnumber' + rowid ).innerHTML = parseInt( document.getElementById( 'cnumber' + rowid ).innerHTML ) + 1;
			}
		} else {
			form.submitbutton.disabled = "true";
			form.username.disabled = "true";
			form.comment.disabled = "true";
			form.submitbutton.value = "Posting...";
		}
	}
	
	con.connect( 'data.php?act=comments&cid=' + cloneid );
}

function addcomment ( cid, rowid ) {
	switchlinks( 'addcomment', cid, rowid );
	
	var commentform = '<form name="addform' + rowid + '" onsubmit="checkform(' + rowid + ');return false">' +
  '<input type="hidden" name="cid" value="' + cid + '" />' +
  '<textarea name="comment" rows="16" cols="72" class="textfield"></textarea><br />' +
  '<input name="submitbutton" type="submit" value="Add My Comment" class="button" />' +
'</form>';
  
	change_data( 'info' + rowid, commentform );
	eval( 'document.addform' + rowid + '.comment.focus();' );
  
	try {
		if ( insertcache[ rowid ] ) {
			eval('document.addform' + rowid + '.comment.value = \'' + insertcache[ rowid ] + '\';' );
		}
	} catch(e) {}
}

function editcomment ( number, rowid, time ) {
	if ( typeof editcomment.openEdits == 'undefined' ) {
		editcomment.openEdits = new Array();
	}
	
	for ( c = 0; c < editcomment.openEdits.length; c++ ) {
		if ( editcomment.openEdits[c] == number + '-' + rowid ) {
			finishedit(number, rowid, time);
			editcomment.openEdits.splice(c, 1);
			change_data('link-' + rowid + '-' + number, 'Edit Comment' );
			return;
		}
	}
	
	change_data('link-' + rowid + '-' + number, 'Save Comment');
	
	editcomment.openEdits[editcomment.openEdits.length] = number + '-' + rowid;
	
	if ( navigator.userAgent.indexOf('MSIE') != -1 ) {
		var comment = document.getElementById(rowid + '-' + number).innerText;
	}
	else {		
		var comment = document.getElementById(rowid + '-' + number).textContent;
	}
	
	var form = '<form name="edit-' + rowid + '-' + number + '">' +
  '<textarea name="comment" rows="13" cols="71" class="textfield">' + comment + '</textarea>' +
  '</form>';
  
  change_data(rowid + '-' + number, form);
}

function finishedit ( number, rowid, time ) {	
	if ( ! document.forms['edit-' + rowid + '-' +number].comment.value ) {
		return;
	}
	
	document.forms['edit-' + rowid + '-' + number].disabled = "true";
	
	var con = new ajax();
	con.add('editcomment', document.forms['edit-' + rowid + '-' + number].comment.value);
	
	con.resource.onreadystatechange = function () {
		if ( con.ready () ) {
			change_data(rowid + '-' + number, con.response() );
		}
	}
	
	con.connect('data.php?act=comments&edit=' + time + '&cid=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' );
}

function deletecomment ( time, rowid ) {
	if ( ! confirm( 'Are you sure you want to delete this comment?' ) ) {
		return;
	}
	
	var con = new ajax();
	
	con.resource.onreadystatechange = function () {
		if ( con.ready() ) {
			comments( con.response(), rowid );
			document.getElementById('cnumber' + rowid).innerHTML = parseInt(document.getElementById('cnumber' + rowid).innerHTML) - 1;
		}
	}
	
	con.connect( 'data.php?act=comments&delete=' + time + '&cid=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' )
}

/**************************
* General
**************************/

function details ( cid, rowid ) {
	switchlinks( 'details', cid, rowid );
	change_data( 'info' + rowid, detailscache[ rowid ] );
}

function helpwindow () {
	window.open( 'help.php', 'help', 'top=100,left=100,height=400,width=450,menubar=no,scrollbar=yes' );
	return false;
}