function finishLogin(response){
	i = response.split('=>');
	success = i[0];
	global  = i[1];
	userID  = i[2];
	
	if (success == 'true'){
		$('u').value = userID;
		$('username_li').innerHTML = '<a href="/'+global+'">'+global+'</a>&nbsp;&nbsp;[ <a id="logout" href="javascript:logout();">Log Out</a> ]&nbsp;';
		
		$('slogin').style.display = 'none';
		$('sregister').style.display = 'none';
		$('username_li').style.display = 'inline';
		$('smanage').style.display = 'inline';
		
		hideLightbox();
		try{toggleCommentForm();} catch (e) {}
	} else {
		$('message').innerHTML = '<strong style="color:red">Incorrect username/password!</strong>';	
	}
}

function finishCITLink(response,op){
	i = response.split('=>');
	
	success = i[0];
	
	if (op == 'citlist'){
		if (success == 0){
			alert('Sorry, CIT link failed. Error 1');
			return;
		}
		else {
			ch = i[1].split('?');
			ch = ch.sort();

			$('cit_chars').options.length = 0;

			for (x=0; x<ch.length; x++){
				chars = ch[x].split(',');
				$('cit_chars').options[$('cit_chars').length] = new Option(chars[0] + ' ('+chars[2]+')' ,chars[1]);
			}
			
			var arrayPageSize = getPageSize();
			var arrayPageScroll = getPageScroll();
			
			xc = parseInt($('x').value)-315;
			yc = (parseInt(arrayPageScroll[1]) + parseInt($('y').value)) - 15;
		
			$('cit_list').style.display = 'block';
			$('cit_list').style.left = xc;
			$('cit_list').style.top = yc;
			$('cit_list').style.width = '300px';
		}	
	}

	if (op == 'citlink'){
		if (success == 0){
			alert('Sorry, CIT link failed. Error 2.');
			return;
		}
		else {
			buildID = i[1];
			citlink = '<a href="javascript:;" onClick="linkCIT(0,\''+buildID+'\');">';
			citlink+= '<img src="/img/cit_unlink.gif" title="Unlink this build from CIT" alt="Unlink this build from CIT" />';
			citlink+= '</a>';

			$('cit'+buildID).innerHTML = citlink;
			$('cit_list').style.display = 'none';

			alert('CIT Build successfully linked!');
			return;
		}
	}

	if (op == 'citdel'){
		buildID = i[1];
		
		if (success == 0){
			alert('Sorry, unlink operation failed');
			return;
		}
		else{
			citlink = '<a href="javascript:;" onClick="linkCIT(1,\''+buildID+'\');">';
			citlink+= '<img src="/img/cit_link.gif" title="Link this build to a CIT character" alt="Link this build to a CIT character" />';
			citlink+= '</a>';
			$('cit'+buildID).innerHTML = citlink;
			
			alert('Build successfully unlinked from CIT');
			return;
		}
	}
}

function finishDeleteBuild(response,n){
	i = response.split('=>');
	success = i[0];
	
	if (success == 0){
		alert("Delete failed!");	
	} else {
		alert(n + ' deleted!');
		window.location='/manage';
	}
}

function finishDupeBuild(response){
	if (response == '0'){
		alert("Build Duplication Failed! :(");	
	} else {
		alert('Build duplicated!');
		window.location='/manage';
	}
}

function finishStartSave(response){
	i = response.split('=>');
	exists = i[0];

	$('save_name').value = form.charname.value;
	
	if (exists != 'false'){
		
		buildIDs = i[0].split(',');;	
		names = i[1].split(',');
		levels = i[2].split(',');
		extras = i[3];

		$('save_exists').style.display = 'block';
		
		tbl = $('save_builds');
		
		if (tbl.rows.length > 1){
			len = tbl.rows.length;
			for (t = 1; t <= len; t++){
				try {
					tbl.deleteRow(1);
				} catch(e){ }
			}
		}
		
		for (x=0;x<buildIDs.length;x++){
			lastRow = tbl.rows.length;
			var row = tbl.insertRow(lastRow);
			
			url = "<a href=\"http://www.cohplanner.com/?a=load&id="+buildIDs[x]+"\" target=\"_blank\">";
			
			cell = row.insertCell(0);
			cell.innerHTML = "<input type=\"radio\" name=\"save_ov\" value=\""+buildIDs[x]+"\" style=\"border: 0;\">";
			
			cell = row.insertCell(1);
			cell.innerHTML = 'Replace <b>' + url + names[x] + '</a></b> [Level ' + levels[x] + ']';
		}
		
		if (extras != 0){
			extra = extras.split(',');
			
			document.check_save.save_pvp.value = extra[0];
			document.check_save.save_origin.value = extra[1];
			document.check_save.save_server.value = extra[2];
			document.check_save.save_private.value = extra[3];
		}
		
	} else {
		tbl = $('save_builds');
		
		if (tbl.rows.length > 1){
			len = tbl.rows.length;
			for (t = 1; t <= len; t++){
				try {
					tbl.deleteRow(1);
				} catch(e){ }
			}
		}
		$('save_exists').style.display = 'none';
	}
								
	showLightbox('savecheck','0,0');
}

function finishCheckCIT(response){
	i = response.split('=>');
	
	if (i[0] != 0){
		ch = i[1].split('?');
		ch = ch.sort();

		$('save_cit').options.length = 0;

		// one empty one
		$('save_cit').options[$('save_cit').length] = new Option('Select CIT Character:','0');
		
		for (x=0; x<ch.length; x++){
			chars = ch[x].split(',');
			if (chars[0] == $('save_name').value){
				$('save_cit').options[$('save_cit').length] = new Option(chars[0] + ' ('+chars[2]+')' ,chars[1], true);
			} else {
				$('save_cit').options[$('save_cit').length] = new Option(chars[0] + ' ('+chars[2]+')' ,chars[1], false);
			}
		}
		
		$('cit_li').style.display = 'block';
		$('save_cit').style.visibility = 'visible';
	}	
}

function finishImgDel(response,bID){
	if (response == '0'){
		alert('There was an error deleting the avatar.\n\nPlease log out and log back in, and try again!');
		return;
	} else {
		alert('Avatar deleted successfully!');
		$('img'+bID).innerHTML = '<a href="javascript:;" onClick="toggleImgUpload('+bID+');"><img src="/img/img_add.gif" /></a>';
	}
}

function AjaxObject() {
	this.createRequestObject = function() {
		var ro;
		if ( window.XMLHttpRequest) {
			try { ro = new XMLHttpRequest();
			}			
			catch (e) { ro = new ActiveXObject("Microsoft.XMLHTTP"); 
			}
		}
		else {	ro = new ActiveXObject("Microsoft.XMLHTTP");
		}
		return ro;	
	}
	this.sndReq = function(action, url) {
		this.http.open(action,url);
		this.http.onreadystatechange = this.handleResponse;
		this.http.send(null);
	}
	this.handleResponse = function() {
		if ( me.http.readyState == 4) {
			var rawdata = me.http.responseText.split("|");
				
			for ( var i = 0; i < rawdata.length; i++ ) {
								
				var item = (rawdata[i]).split("=>");
								
				if (item[0] != "") {
					type = item[0];
					form = document.planner;
					
					switch (type){
						
						case 'select':
							pool = item[1];
							ids = item[2];
							names = item[3];							
	
							sear = document.sea;
																				
							if (pool == '1'){ 
								p = 'primary';
							} 
							else if (pool == '2') {
								p = 'secondary';	
							}
							else if (pool == '4') {
								p = 'app';	
							}
														
							ids = ids.split(',');
							names = names.split(',');							
							
							eval("form."+p+".options.length = 1;");
							
							for (x=0; x<ids.length; x++){
								eval("form."+p+".options[form."+p+".options.length] = new Option('"+names[x]+"','"+ids[x]+"');");
							}
						break;
						
						case 'pools_search':
													
							pool = item[1];
							ids = item[2];
							names = item[3];							
	
							sear = document.sea;
																				
							if (pool == '1'){ 
								p = 'primary';
							} else {
								p = 'secondary';	
							}
														
							ids = ids.split(',');
							names = names.split(',');							
							
							eval("sear."+p+".options.length = 1;");
							
							for (x=0; x<ids.length; x++){
								eval("sear."+p+".options[sear."+p+".options.length] = new Option('"+names[x]+"','"+ids[x]+"');");
							}
						
						break;
						
						case 'html':
							pool = item[1];						
							ids  = item[2];
							names= item[3];
												
							names = names.split(',');
							ids = ids.split(',');

							for (x=0; x<names.length; x++){
								
								$(pool+(x+1)).name = ids[x];
								$(pool+(x+1)).innerHTML = names[x];
								
								if ((pool+(x+1)) == 'sec1' && form.load.value == 0){
									choosePower(ids[x], 'sec1');
								}
								
								r = $(pool+(x+1)+'_i');
								r.childNodes[0].href = "javascript:showDetail('"+ids[x]+"');";
								
							}
							
							if (form.at.value == 11 || form.at.value == 12){
								disp = '';
								form.app.disabled = true;
								$('app_div').style.display = 'none';
							} else {
								disp = 'none';
								form.app.disabled = false
								$('app_div').style.display = '';
							}
							
						break;
						
						case 'span':
							name = item[1];
							id   = item[2];
							val  = item[3];
							e    = String(item[4]);

							level = left($('selection'+id).innerHTML, 4);
							level = level.replace('(','');
							level = level.replace(')','');
	
							$('seltext'+id).innerHTML = urldecode(name);
							$('selection'+id).name = urldecode(val);
							$('li_'+id+'_1').innerHTML = "<a href=\"javascript:showEnhDiv('"+val+"','li_"+id+"_1');\"><img id=\""+val+"-"+level+"\" alt=\""+level+"\" src=\"/img/enh/Empty.gif\" /></a>";
						break;
						
						case 'kspan': // special for khelds
							name = item[1];
							id   = item[2];
							level  = item[3];
							e    = String(item[4]);

							for (x=25;x<=40;x++){
								if ($('seltext'+x).innerHTML == ''){
	
									$('seltext'+x).innerHTML = urldecode(name);
									$('selection'+x).name = urldecode(e);
	
									$('selection'+x).innerHTML = '('+level+')'+ $('selection'+x).innerHTML;
	
									img = "<a href=\"javascript:showEnhDiv('"+e+"','li_"+x+"_1');\"><img id=\""+e+"-"+level+"\" alt=\""+level+"\" src=\"/img/enh/Empty.gif\" /></a>";
	
									$('li_'+x+'_1').innerHTML = img;
	
									return;
								}
							}
							makeSlotsAvailable();
						break;
						
						case 'kspan2':
							name = item[1];
							id   = item[2];
							level= item[3];
							e    = String(item[4]);
	
							if (level.length == 1){
								level = '0' + String(level);
							}
	
							g = name.split('|');
	
							didit = 0;
	
							for (x=25;x<=40;x++){
								if ($('seltext'+x).innerHTML == '' && didit == 0){
	
									$('seltext'+x).innerHTML = urldecode(name);
									$('selection'+x).name = urldecode(id);
	
									$('selection'+x).innerHTML = $('selection'+x).innerHTML.replace(/\(\d+\)/,'');
									$('selection'+x).innerHTML = '('+level+')'+ $('selection'+x).innerHTML;
	
									img = "<a href=\"javascript:showEnhDiv('"+id+"', 'li_"+x+"_1');\"><img id=\""+id+"-"+level+"\" alt=\""+level+"\" src=\"/img/enh/Empty.gif\" /></a>";
									
									$('li_'+x+'_1').innerHTML = img;
	
									didit = 1;
								}
							}
							makeSlotsAvailable();
						break;
						
						case 'enh':
							enhs  = urldecode(item[1]).split(',');
							powID = item[2];
							liID  = item[3];
							names = urldecode(item[4]).split(',');
							hos = urldecode(item[5]).split(',');
							ios = urldecode(item[6]).split(',');
							ioes = urldecode(item[7]).split(',,');
							
							e_name = names[0];
	
							out = '';
	
							for ( g = 0; g < enhs.length; g++ ){
								out += "<a href=\"javascript:chooseEnh('"+liID+"','"+enhs[g]+"');\" title=\""+enhs[g]+"\"><img src=\"/img/enh/" + enhs[g] + ".gif\" /></a>";
							}
							
							out += "<a href=\"javascript:chooseEnh('"+liID+"','Empty');\" title=\"Empty\"><img src=\"/img/enh/Empty.gif\" /></a>";
							
							if (hos != 0){
								out += "<br />";
								for (h = 0; h < hos.length; h++){
									if (hos[h].length > 0){
										out += "<a href=\"javascript:chooseEnh('"+liID+"','"+hos[h]+"');\" title=\""+ho_list[hos[h]]+"\"><img src=\"/img/hos/" + hos[h] + ".gif\" /></a>";
									}
								}
							}
							
							if (ios != 0){
								out += "<br />";
								last = '';
								for (i = 0; i < ios.length; i++){
									if (ios[i].length > 0){
										if (last != ios[i]){
											out += '<br /><strong>' + ios[i] + '</strong><br />';
										}
										out += "<a href=\"javascript:chooseEnh('"+liID+"','IO-"+ios[i]+"-"+ioes[i]+"');\" title=\""+ios[i]+": "+ioes[i]+"\"><img src=\"/img/ios/" + ios[i] + ".png\" /></a>";
										last = ios[i];
										//out += "<a href=\"javascript:showIOSets('"+ios[i]+"');\" title=\""+ios[i]+"\"><img src=\"/img/ios/" + ios[i] + ".png\" /></a>";
									}
								}
							}
	
							$('enh_name').innerHTML = e_name;
							$('enh_imgs').innerHTML = out;
							
							xy = form.x.value + ',' + form.y.value;
							setTimeout("showLightbox('enh','"+xy+"');",150);
						break;
						
						case 'detail':
							name	= urldecode(item[1]);
							detail	= urldecode(item[2]);
							val		= item[3];
							e		= String(item[4]);
				
							data   = urldecode(val).split('+');			
							descr  = urldecode(data[0]);

							if (detail == ''){ detail = 'No Data'; }
							if (descr  == ''){ descr  = 'No Data'; }

							$('details_h1').innerHTML   = '&nbsp;' + name;
							$('details_span2').innerHTML= "(" + detail + ")";
							$('details_span').innerHTML = descr + "<br />";
							$('details_div').innerHTML  = '';
							if (data[2].length > 0){
								$('details_div').innerHTML  += "<b style=\"color:red\">Recharge:</b> " + data[2] + "<br />";
							}
							if (data[3].length > 0){
								$('details_div').innerHTML  += "<b style=\"color:red\">End Cost:</b> " + data[3] + "<br />";
							}
							bix = data[4].replace("|1x0(None)",'');
							bix = bix.replace("|0x0(None)",'');
							bix = bix.replace("|0x0()",'');
							bix = bix.replace("0x0(None)",'');
							bix = bix.replace("0x0()",'');
							bix = bix.replace("1x0(None)",'');
							if (bix.length > 0){
								bix = bix.replace(/\|/g, ', ');
								$('details_div').innerHTML  += "<b style=\"color:red\">Brawl Index:</b> " + bix + " <small>(Total: " + data[5] + ")</small><br />";
							}
							
							$('details_div').innerHTML  += "<br /><b>Enhancements:</b><br />";
							enhs = data[1].split(',');
							img = '';
							for (x=0;x < enhs.length; x++){
								img +=	"<img src=\"/img/enh/"+enhs[x]+".gif\">&nbsp;";
							}
							$('details_div').innerHTML  += img + "<br />";
							
							xy = form.x.value + ',' + form.y.value;
		
							showLightbox('details',xy);	
						break;
						
						case 'user':
							name = urldecode(item[1]);			

							if (name == 'false') {
								$('reg_msg').style.display = 'block';
								$('reg_msg').style.color = 'red';
								$('reg_msg').innerHTML = 'Please select a different username.';
							} 
							else {
								$('reg_msg').style.display = 'block';
								$('reg_tbl').style.display = 'none';
								$('reg_msg').style.color = 'green';
								$('reg_msg').innerHTML = 'Account Created!<br>You may now login!<br>';
								$('reg_msg').innerHTML+= '<a href="javascript:hideLightbox();doShowLogin();">Login Now</a><br><br>';
							}
						break;
						
						case 'save':
							name = urldecode(item[1]);
							alert(name);
						break;
						
						case 'load':
							dat = urldecode(item[1]);
						
							if (dat==0){
								alert('Build Load Failed!');
								hideLightBox();
							} else {
								load = item[18];

								a		 = item[2];
								p		 = item[3];
								s		 = item[4];
								atpower	 = item[5];
								enh_count= item[6];
								enh_tot	 = item[7];	
								level	 = item[8];
								slotss	 = item[9];
								pool1	 = item[10];
								pool2	 = item[11];
								pool3	 = item[12];
								pool4	 = item[13];
								appp	 = item[14];
								name	 = item[15];
								enh_level= item[16];
								atslots  = item[17];

								form = document.planner;

								if (load == 1){
									pname = item[19];
									sname = item[20];
									gbl   = item[21];
									$('charname').innerHTML = name.toUpperCase() + $('charname').innerHTML;
									$('primary').innerHTML = pname;
									$('secondary').innerHTML = sname;
									$('globalID').innerHTML = '<a href="/'+gbl+'">'+gbl+'</a>';
								}
								else if (load == 2){
									// nuffin'
								}
								else {

									form.at.value = a;
									
									form.charname.value  = name;
									form.load.value 	 = 1;

									form.pool1.value = pool1;
									form.pool2.value = pool2;
									form.pool3.value = pool3;
									form.pool4.value = pool4;
									
									fillPrimSecLoad();
									
									form.primary.value = p;
									form.secondary.value = s;
									form.app.value = appp;
									
									fillPowers('prim','primary');
									fillPowers('sec','secondary');
									fillPowers('app','app');
									fillPowers('pool1_','pool1');
									fillPowers('pool2_','pool2');
									fillPowers('pool3_','pool3');
									fillPowers('pool4_','pool4');
									
									form.atpower.value   = atpower;
									form.atslots.value   = atslots;
									form.enh_count.value = enh_count;
									form.enh_tot.value   = enh_tot;
									form.enh_level.value = enh_level;
									form.level.value     = level;
									form.slots.value     = slotss;
								}
								
								lines = dat.split("*");
								
								// fill out the powers and slots
								iocheck=0;
								for (x=0;x<40;x++){
									ldata = lines[x].split('>>');
																		
									if ((x+1) == ldata[0] && ldata[1] != 'undefined'){
										 if (ldata[1].length > 20){
                                         	$('seltext'+(x+1)).innerHTML = left(ldata[1],17) + '...';
                                         } else {
                                         	$('seltext'+(x+1)).innerHTML = ldata[1];
                                         }

										$('selection'+(x+1)).name = ldata[2];	
										
										check = 0;
										
										for (l=4;l<=9;l++){
											if (!ldata[l].match(/0:0/)){
												
												liArr = ldata[l].split(':');
												
												li = liArr[0];
												lie = liArr[1];
												lil = liArr[2];
												lit = lil;

												if (lie.match('hos')){
													if (!lie.match(/img\/hos/)){
														lie = lie.replace('hos/','/img/hos/');
													}
												} else if (lie.match('ios')){

													if (iocheck == 0 && load == 0){
														$('show_ios').checked = true;
														$('exp').value = 'long';
													}

													iocheck = 1;
													
													liez = lie.split('"');
													lie = liez[0];
													lit = '('+lil+'): '+ (lie.replace(/img\/ios\//,'')).replace(/\.png/,'') +': '+liez[1].replace(/&#44;/,',');
												}else{

													if (!lie.match(/img\/enh/)){
														lie = lie.replace('img/','img/enh/');
													}
												}

												if (load == 0){
													$(li).innerHTML = "<a href=\"javascript:showEnhDiv('"+ldata[2]+"','"+li+"');\"><img id=\""+ldata[2]+lil+"\" alt=\""+lil+"\" title=\""+lit+"\" src=\"/"+lie+"\"></a>";
												} 
												else if (load == 2){
													lie = lie.replace('img/enh/','img/enh/18_');
													lie = lie.replace('gif','png');
													
													lie = lie.replace('img/hos/','img/hos/18_');
													lie = lie.replace('gif','png');
													
													lie = lie.replace('img/ios/','img/ios/18_');

													if (lie.match('enh')){
														lil = lie.replace('img/enh/18_','');
														lil = lil.replace('.png','');
														lil = liArr[2] + ': ' + lil;
													}
													
													if (lie.match('hos')){
														lil = lie.replace('img/hos/18_','');
														lil = lil.replace('.png','');
														lil = liArr[2] + ': ' + lil;
													}

													if (lie.match('ios')){
														lil = lie.replace('img/ios/18_','');
														lil = lil.replace('.png','');
														lil = liArr[2] + lit.replace(/\([0-9]+\)/,'');
													}
													
													$(li).innerHTML = "<img id=\""+ldata[2]+lil+"\" title=\""+lil+"\" alt=\""+lil+"\" title=\""+lit+"\" src=\"/"+lie+"\">";
												} 
												else {
													$(li).innerHTML = "<img id=\""+ldata[2]+lil+"\" alt=\""+lil+"\" title=\""+lit+"\" src=\"/"+lie+"\">";
												}
												
											} else {
												liArr = ldata[l].split(':');												
												li = liArr[0];
											
												if (slotss > 0 && !li.match(/_1$/) && ldata[2] != 'undefined' && check == 0 && enh_level < level){
													//$(li).innerHTML = "<a href=\"javascript:placeEmptyEnh('"+li+"','"+ldata[2]+"')\"><img src=\"/img/enh/new.gif\"></a>";
													check = 1;
												}
											}
										}
									}
								}

								if (load == 0){
									if (a == 11 || a == 12){
										showKheldPowers(1);	
									} else {
										showKheldPowers(2);
									}
									
									makePowerAvailable(level, 'prim',   14);
									makePowerAvailable(level, 'sec',    12);
									makePowerAvailable(level, 'pool1_', 4);
									makePowerAvailable(level, 'pool2_', 4);
									makePowerAvailable(level, 'pool3_', 4);
									makePowerAvailable(level, 'pool4_', 4);
									makePowerAvailable(level, 'app', 	4);
									
									if (enh_level == 50 && slotss == 0){
									} else {
										makeSlotsAvailable();
									}
									
									$('atbg').style.background = "url('/img/at"+a+".png') top left no-repeat";

									powerCompare();
								}
								
								// change the stylesheet
								if (load < 2){
									if (a <= 5){
										setStyle('villain');
									} else {
										setStyle('hero');
									}
								}

								if (item[18] == 1){
									hideLightbox();
								}
							}
							
						break;
						
						case 'login':
							success = item[1];
							global  = item[2];
							userID  = item[3];
							form = document.planner;
							
							if (success == 'true'){
								$('username_li').innerHTML = '<a href="/'+global+'">'+global+'</a>&nbsp;&nbsp;[ <a id="logout" href="javascript:logout();">Log Out</a> ]&nbsp;';
								form.u.value = userID;
								
								$('slogin').style.display = 'none';
								$('sregister').style.display = 'none';
								$('username_li').style.display = 'inline';
								$('smanage').style.display = 'inline';

								hideLightbox();
								toggleCommentForm();
							} else {
								$('message').innerHTML = '<span style="color:red">Incorrect username/password!</span>';	
							}
						
						break;
						
						case 'logout':
						
							form = document.planner;
													
							$('slogin').style.display = 'inline';
							$('sregister').style.display = 'inline';
							$('username_li').style.display = 'none';
							$('smanage').style.display = 'none';
							
							form.u.value = '';

							toggleCommentForm();

						break;
						
					}
				}
			}
		}
	}
	var me = this;
	this.http = this.createRequestObject();
}
