// compSub.js custom object for submitting all types of components


var CompSub = Class.create();

CompSub.prototype = {

	initialize:function(owner,bodyElement,type) {
		if(type==undefined){type='any';}
		this.owner=owner;
		this.bodyElement=$(bodyElement);
		this.type=type;
		
		this.componentTypeID=0;
		this.level=0;
		this.classID=0;
		this.serverID=0;
		
		global.lootSubmitGUID=(global.lootSubmitGUID==undefined?1:global.lootSubmitGUID++);
		this.GUID=global.lootSubmitGUID;
		
		this.render();

	},
	
	render:function() {
	
		s='<div class="error"><b>AND THE LORD SAID UNTO HIS CHOSEN PEOPLE:<BR><BR><I> "THOU SHALT NOT SUBMIT COMPONENTS AWARDED BY YOUR PILOT TRAINERS (INCLUDING THOSE DISTRIBUTED BY THE DESPICABLE KASH NUNES) NOR REACTORS LOOTED PRIOR TO JULY 26, 2007, LEST YE BE STRICKEN DOWN AND TROD INTO THE VERY DUST FROM WHENCE YE CAME!"</i><BR><br>LOVE,<br> NarfBlinko</b><br><br><a href="http://forums.station.sony.com/swg/posts/list.m?topic_id=272527">Pilot Profession Roadmap Reward Items</a></div>';
		s+='<div class="lootSubmitStatus" id="elem_compSub_' + this.GUID + '_status"></div>';		
		s+='<form id="form_compSub_' + this.GUID + '" onSubmit="return false" onReset="return false">';
		s+='<div class="lootSubmitFormRow"><label class="lootSubmitFormLabel">Server</label>';
		s+='<select class="lootSubmitFormInput" id="ctrl_compSub_' + this.GUID + '_server"  style="width:200px;"></select></div>';
		s+='<div class="lootSubmitFormRow"><label class="lootSubmitFormLabel">Component Type</label>';
		s+='<select class="lootSubmitFormInput" id="ctrl_compSub_' + this.GUID + '_componentType" style="width:200px;"></select>&nbsp;<i>hotkeys: a,b,c,d,e,r,s,w</i></div>';
		s+='<div class="lootSubmitFormRow"><label class="lootSubmitFormLabel">RE Level</label>';
		s+='<select class="lootSubmitFormInput" id="ctrl_compSub_' + this.GUID + '_level" style="width:200px;"></select>&nbsp;<i>hotkeys: 0-9</i></div>';
		s+='<div class="lootSubmitFormRow"><label class="lootSubmitFormLabel">Class</label>';
		s+='<select class="lootSubmitFormInput" id="ctrl_compSub_' + this.GUID + '_class" style="width:600px;"></select></div>';
		if(global.user.username.toLowerCase() == 'cactus-jack') {
			s+='<div class="lootSubmitFormRow"><label class="lootSubmitFormLabel">MLC Project</label>';
			s+='<input type="checkbox" id="ctrl_compSub_' + this.GUID + '_forMLC" value="1" CHECKED></div>';
		}
		s+='<div class="lootSubmitFormRow" id="elem_compSub_' + this.GUID + '_attributeContainer"><label class="lootSubmitFormLabel">Attributes</label>';
		s+='<div id="elem_compSub_' + this.GUID + '_attribs"></div><br><br>';
		s+='<div class="lootSubmitFormRow"><label class="lootSubmitFormLabel">&nbsp;</label>';
		s+='<input type="submit" id="ctrl_compSub_' + this.GUID + '_execute" value="submit loot" />&nbsp;&nbsp;';
		s+='<input type="reset" id="ctrl_compSub_' + this.GUID + '_reset" value="cancel" />';	
		s+='</div>';
		s+='</form>';
	
		Element.update(this.bodyElement,s);	
		
		this.formElement=$('form_compSub_' + this.GUID);
		this.attributeElement=$('elem_compSub_' + this.GUID + '_attribs');
		this.attributeContainerElement=$('elem_compSub_' + this.GUID + '_attributeContainer');
		this.statusElement=$('elem_compSub_' + this.GUID + '_status');
		
		this.serverControl=$('ctrl_compSub_' + this.GUID + '_server');
		this.componentTypeControl=$('ctrl_compSub_' + this.GUID + '_componentType');
		this.levelControl=$('ctrl_compSub_' + this.GUID + '_level');
		this.classControl=$('ctrl_compSub_' + this.GUID + '_class');
		
		this.executeControl=$('ctrl_compSub_' + this.GUID + '_execute');
		this.resetControl=$('ctrl_compSub_' + this.GUID + '_reset');
		
		this.serverControlChangeObserver = this.onServerControlChange.bindAsEventListener(this);
		Event.observe(this.serverControl,'change',this.serverControlChangeObserver,false);
		
		this.typeControlChangeObserver = this.onComponentTypeControlChange.bindAsEventListener(this);
		Event.observe(this.componentTypeControl,'change',this.typeControlChangeObserver,false);
		
		this.levelControlChangeObserver = this.onLevelControlChange.bindAsEventListener(this);
		Event.observe(this.levelControl,'change',this.levelControlChangeObserver,false);
		
		this.classControlChangeObserver = this.onClassControlChange.bindAsEventListener(this);
		Event.observe(this.classControl,'change',this.classControlChangeObserver,false);
		Event.observe(this.classControl,'focus',function(){global.fieldFocus=true},false);
		Event.observe(this.classControl,'blur',function(){delete global.fieldFocus},false);			
		
		this.formSubmitObserver = this.onFormSubmit.bindAsEventListener(this);
		Event.observe(this.formElement,'submit',this.formSubmitObserver,false);
		
		this.formResetObserver=this.onFormReset.bindAsEventListener(this);
		Event.observe(this.formElement,'reset',this.formResetObserver,false);
		
		this.setServerOptions();
		this.setServer(this.getDefaultServer());
		this.setComponentTypeOptions();
		this.setLevelOptions();
		this.setClassOptions();
		this.setAttributeOptions();
		
		this.hideStatus();
	},
	
	setServerOptions:function() {
		var o = this.serverControl;		
		var l = $H(global.lookup['server']);
		o.options[0]=new Option('choose server...',0,false,false);
		l.each(function f(x,j){
			var k=j+1;
			o.options[k]=new Option(l[k],k,false,false);
		});
	},
	
	setComponentTypeOptions:function()  {
		if(this.type=='loot') {
			var a=[1,4,7,9,3,8,2,5];
		}
		
		var o=this.componentTypeControl;
		o.options[0]=new Option('choose type...',0,false,false);
		for(var i=0;i<a.length;i++) {
			var c=global.lookup.components[a[i]];
			o.options[i+1] = new Option(c.name,a[i],false,false);
		}
	},
	
	setLevelOptions:function() {
		var o=this.levelControl;
		o.options[0]=new Option('choose level..',0,false,false);
		for(var i=1;i<=10;i++){
			o.options[i]=new Option('Level ' + i,i,false,false);
		}
	},
	
	setClassOptions:function(){
		var o=this.classControl;
		o.options.length=0;
		o.options[0]=new Option('select class...',0,false,false);
		if(this.componentTypeID && this.level) {
			var a=global.lookup.components[this.componentTypeID].levels[this.level];
			for(var i=0;i<a.length;i++){
				o.options[i+1]=new Option(a[i].name,a[i].classid,false,false);
			}
			this.classControl.style.width="";
		} else {
			this.classControl.style.width="200px";
		}
	},	
	
	setAttributeOptions:function(){
		if(this.componentTypeID==0){
			Element.hide(this.attributeContainerElement);
			return;
		}
		
		var a=global.lookup.components[this.componentTypeID].attributes;
		var s='';
		for(var i=0;i<a.length;i++){
			s+='<div class="lootSubmitSubTextField"><input style="width:60px;text-align:center;" id="ctrl_compSub_' + this.GUID + '_attribute' + a[i].attributeid + '" /><br>' + a[i].shortname + '</div>';
		}
		Element.update(this.attributeElement,s);
		
		this.attributes=new Array();
		for(var i=0;i<a.length;i++){
			var att=new Object();
			att.id=a[i].attributeid;
			att.shortname=a[i].shortname;
			att.control=$('ctrl_compSub_' + this.GUID + '_attribute' + a[i].attributeid);			
			if(att.shortname=='Armor'){
				Event.observe(att.control,'blur',this.onArmorControlBlur.bindAsEventListener(this),false);
			}
			Event.observe(att.control,'focus',function(){global.fieldFocus=true},false);
			Event.observe(att.control,'blur',function(){delete global.fieldFocus},false);			
			this.attributes.push(att);	
		}			
		Element.show(this.attributeContainerElement);
	},
	
	setExecuteControlStatus:function(){
		var a=((this.level!==0 && this.componentTypeID!==0 && this.classID!==0 && this.serverID!==0)?true:false);
		var b=true;
		if(!_(this.attributes)){b=false;} 
		else  {
			var ar=this.attributes;
			for(var i=0;i<ar.length;i++){
				if(ar[i].control.value==''){b=false;}
			}
		}
		this.executeControl.disabled=(a&&b?false:true);
		return true;
	},
	
	setStatus:function(s,t){
		this.statusElement.className="lootSubmitStatus";
		Element.addClassName(this.statusElement,"status" + t);
		Element.update(this.statusElement,s);
		this.showStatus();
	},
	
	hideStatus:function(){
		//this.statusElement.visibility='hidden';
		Element.hide(this.statusElement);
	},
	
	showStatus:function(){
		//this.statusElement.visibility='visible';
		Element.show(this.statusElement);
	},
	
	setServer:function(s){
		this.serverControl.value=s;
		this.serverID=s;
	},
	
	setComponentType:function(t){
		this.componentTypeControl.value=t;
		this.componentTypeID=t;
		this.setClassOptions();
		this.setAttributeOptions();	
	},
	
	setLevel:function(l){
		this.levelControl.value=l;
		this.level=l;
		this.setClassOptions();
		this.setAttributeOptions();
	},	
		
	setClass:function(c){
		this.classControl.value=c;
		this.classID=c;
	},
	
	onArmorControlBlur:function(e){
		var a=this.attributes;
		for(var i=0;i<a.length;i++) {
			var n=a[i].shortname;
			if(n=='Hitpoints'){var hpI=i;} 
			else if (n=='Drain'){var fcI=i;break;}
			else if (n=='Mass'){var fcI=i;}
		}
		a[hpI].control.value=Event.element(e).value;
		a[fcI].control.focus();
		setTimeout("global.fieldFocus=true",50);
	},
	
	onServerControlChange:function(e){
		this.setServer($F(this.serverControl));
	},
	
	onComponentTypeControlChange:function(e){
		this.setComponentType($F(this.componentTypeControl));
	},
	
	onLevelControlChange:function(e){
		this.setLevel($F(this.levelControl));
	},
	
	onClassControlChange:function(e){
		this.setClass($F(this.classControl));
	},
	
	onFormSubmit:function(e){
		this.execute();
		return false;
	},
	
	onFormReset:function(e){
		this.clearAttributes();
		this.removeConfirmMode();
		this.hideStatus();
	},
	
	onKeyPress:function(e) {
		if(global.fieldFocus || !this.owner.isOpen) { return }
		var k = (e.charCode) ? e.charCode : ((e.which) ? e.which : e.keyCode);
		var n=k-48;
		var l=false;
		var c=false;
		var oU={97:1,98:4,99:7,100:9,101:3,114:8,115:2,119:5};
		var oL={65:1,66:4,67:7,68:9,69:3,82:8,83:2,87:5};
		if(n==0) {
			l=10;
		} else if (n > 0 && n < 10) {
			l=n;
		}
		
		if(l){this.setLevel(l);this.classControl.focus();}		
		if(oU[k]){this.setComponentType(oU[k]);}		
		if(oL[k]){this.setComponentType(oL[k]);}		
	},
	
	setConfirmMode:function(){
		this.confirmMode=true;
		this.executeControl.value="confirm submission";
	},
	
	removeConfirmMode:function(){
		delete this.confirmMode;
		this.executeControl.value="submit loot";
	},
	
	clearAttributes:function(e){
		var a=this.attributes;
		for(var i=0;i<a.length;i++){a[i].control.value='';}
	},
	
	clearForm:function(e){
		this.removeConfirmMode();
		this.hideStatus();
		this.setServer(0);
		this.setComponentType(0);		
		this.setLevel(0);
		this.clearAttributes();
		this.setServer(this.getDefaultServer());
	},
	
	getDefaultServer:function(){
		if(_(global.user) && _(global.user.toons) && _(global.user.toons[0])) {
			return(parseInt(global.user.toons[0].serverid));
		} else {
			return 0;
		}
	},
	
	execute:function(){
		//this.setStatus("Loot submission isn't quite ready for prime time yet.  Raja has a couple more methods to knock out.  It should be available for public use by late this evening.","Error");
		//return false;		
		
		var execute_Response=function(obj){
			if(obj.error==1){
				this.setStatus(obj.errortext,'Error');
				return;
			}
			
			if(obj.confirm==1){
				if(obj.data=='ok') {
					var a=obj.confirmmessages;
					var s='<b>Record Loot Submission</b><ul>';
					for(var i=0;i<a.length;i++){
						s+='<li>' + a[i] + '</li>';
					}
					s+='</ul>Because of these record breaking values the SpaceLoot loot administration staff may request screenshot verification of the component.<br><br>To ensure that your record stands, please make sure that the email address in your <a href="javascript:void(0);" onClick="command.parse(\'/user\');">user profile</a> is valid and that you are prepared to upload a screenshot if requested.<br><br>If we cannot contact you or you cannot verify the component, it may be deleted or modified.  Thanks for submitting your data!';
					Element.update('dspNotify',s);
					winNotify.open();				
				} else {
					this.setConfirmMode();
					var a=obj.confirmmessages;
					var s="<b>Confirmation Required</b><ul>";
					for(var i=0;i<a.length;i++){
						s+='<li>' + a[i] + '</li>';
					}
					s+='</ul>Please check the values carefully.  If everything is in order, click "confirm" at the bottom of the form to complete the submission.';
					this.setStatus(s,"Confirm");
				}
			}
			
			if(obj.data=='ok'){
				this.setStatus('Component added.  Analysis is available on the details tab','Normal');
				this.clearAttributes();
				this.componentTypeControl.focus();
				var o={componentID:this.componentTypeID,
					   username:global.user.username,
					   order:'dateSubmitted',
					   direction:1};
				tabLootBrowser.set(o);
				tabLootBrowser.execute(true);
			}
			
			setPanelHeight();
		}
	
		this.setStatus("Communicating with server...","Normal");
		var a=this.attributes;
		var args=new Object();
		args.object="loot";
		args.method="save";
		args.classID=this.classID;
		args.componentTypeID=this.componentTypeID;
		args.serverID=this.serverID;
		args.level=this.level;
		if(global.user.username.toLowerCase() == 'cactus-jack'){
			if($F('ctrl_compSub_' + this.GUID + '_forMLC')==1){
				args.mlc=1;
			}
		}
		if(_(this.confirmMode)){
			args.confirm=1;
			this.removeConfirmMode();
		}
		for(var i=0;i<a.length;i++){
			args['attribute_' + a[i].id] = a[i].control.value;
		}

		http('POST','index.cfm',execute_Response.bind(this),args);
		return false;
	}
	
}