var SpiralTrac = {
	Helpers: {
		LoadOnEnter: function(event, baseURL, fieldID) {
			if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {
				location.href = baseURL+$('#'+fieldID).val();
				return false;  
			} else {  
				return true;  
			}
		},
		
		DefaultButton: function(event, id) {
			if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {  
				$('#'+id).click(); 
				return false;  
			} else {  
				return true;  
			}
		},

		SubmitOnEnter: function(event, id) {
			if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {  
				$('#'+id).submit(); 
				return false;  
			} else {  
				return true;  
			}
		}
	},
	
	DynamicList: {
		Create: function(id) {
			var field = $('#'+id+'_newName');
			SpiralTrac.DynamicList.AppendToList(id, null, field.val());
			field.val("");
		},
		
		Remove: function(id, fld) {
			//remove this item to the hidden field...
			var hiddenField = $('#'+id);
			var seperator = '|||';
			var checkVal = $(fld).prev('span').text();
			var fldID = $(fld).parent().attr('id');
			
			if(fldID != null && fldID.length > 0){
				checkVal = fldID.replace('display_','');
			}

			if(hiddenField.val().indexOf(seperator+checkVal+seperator) > -1) {	
				var hidVal = hiddenField.val();
				hidVal = hidVal.replace(seperator+checkVal+seperator,seperator);
				hiddenField.val(hidVal);
			}

			//remove the field
			$(fld).parent().fadeOut();
		},

		AppendSelected: function(id) {
			var field = $('#'+id+'_chooseCurrent');
			var fieldSelected = $('#'+id+'_chooseCurrent :selected');
			var fieldVal = field.val();

			if(fieldVal != '' && fieldVal != 'NULL') {
				
				SpiralTrac.DynamicList.AppendToList(id, fieldVal, fieldSelected.text());
			}

			//reset the selected item
			field.val("");
		},

		AppendToList: function(id, displayID, val) {
			//add this item to the hidden field...
			var hiddenField = $('#'+id);
			var seperator = '|||';
			var allowAdd = true;
			var displayName = '';
			var checkVal = val;
			
			if(displayID != null && displayID.length > 0) {
				displayName = 'display_'+displayID;
				checkVal = displayID;
			}

			if(hiddenField.val().length == 0) {
				//if it's the first tag, just add it
				hiddenField.val(seperator+checkVal+seperator);
			} else if(hiddenField.val().toLowerCase().indexOf(seperator+checkVal.toLowerCase()+seperator) == -1) {	
				var hidVal = hiddenField.val();
				hiddenField.val(hidVal+checkVal+seperator);
			} else {
				allowAdd = false;
			}

			//check to see if this can be added to the page
			if(allowAdd) {
				var displayAssigned = $('#'+id+'_displayAssigned');

				template = $('#'+id+'_listTemplate').clone()
							.attr('id',displayName)
				            .fadeIn('slow');		
				template.find('span').text(val);
				displayAssigned.append(template);
			}
		}
	},
	
	ContactData: {
		Generic: {
			Add: function(name) {
				count = $('#'+name+'_count').val();
				count = parseInt(count)+1;
				
				template = $('#'+name+'_template').clone()
							.attr('id',name+'_'+count)
				            .fadeIn('slow');
				
				template.find("[name^='"+name+"_']:input").each(function(i) {
					fld = $(this);
					fldName = fld.attr('name');
					fld.attr('name', fldName+'_'+count);
				});
				
				itemList = $('#'+name+'_list');
				itemList.append(template);
				
				$('#'+name+'_count').val(count);
				
				return template;
			},
			
			Remove: function(name, fld) {
				$(fld).parent().find("[name^='"+name+"_']:input").each(function(i) {
					input = $(this);
					input.val('');
				});
				
				$(fld).parent().hide();
			}
		},
		
		Position: {
			Add: function() {
				SpiralTrac.ContactData.Generic.Add('position');
			},
			
			Remove: function(fld) {
				SpiralTrac.ContactData.Generic.Remove('position', fld);
			}
		},
		
		Email: {
			Add: function() {
				SpiralTrac.ContactData.Generic.Add('email');
			},
			
			Remove: function(fld) {
				SpiralTrac.ContactData.Generic.Remove('email_address', fld);
			}
		},
		
		Phone: {
			Add: function() {
				SpiralTrac.ContactData.Generic.Add('phone');
			},
			
			Remove: function(fld) {
				SpiralTrac.ContactData.Generic.Remove('phone_number', fld);
			}
		},
		
		Address: {
			Add: function() {
				template = SpiralTrac.ContactData.Generic.Add('address');
				
				//setup the max length script for textarea's
				template.find('textarea.limited').maxlength();
			},
			
			Remove: function(fld) {
				SpiralTrac.ContactData.Generic.Remove('address', fld);
			}
		},

		Contact: {
			Add: function() {
				template = SpiralTrac.ContactData.Generic.Add('contact');
			},

			Remove: function(fld) {
				SpiralTrac.ContactData.Generic.Remove('contact', fld);
			}
		},

		Company: {
			Add: function() {
				template = SpiralTrac.ContactData.Generic.Add('company');
			},

			Remove: function(fld) {
				SpiralTrac.ContactData.Generic.Remove('company', fld);
			}
		}
	},
	
	InlineEditing: {
		Setup: function() {
			$('.editable').each(function() {
				if($(this).attr('id')) {
					var field = $(this);
					//alert(this);
					field.attr('title','click to edit field');
					field.bind("click", SpiralTrac.InlineEditing.OnClick);
				}
			});
		},
		
		OnClick: function() {
			var fld = $(this);
			var width = fld.width()+14;
			var style = 'width:'+width+'px;';
			var fldTemplate = $.template('<input id="${id}_editor" class="editing" type="text" value="${text}" style="${style};" onkeydown="SpiralTrac.InlineEditing.OnEnter(event, this,\'${id}\')" onblur="SpiralTrac.InlineEditing.OnBlur(this,\'${id}\')" />');
			
			fld.after( fldTemplate , {
			     id: fld.attr('id'),
			     text: fld.text(),
			     style: style
			});
			
			fld.hide();

			$('#'+fld.attr('id')+'_editor').focus();
		},
		
		OnEnter: function(event, field, id) {
			if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {
				SpiralTrac.InlineEditing.UpdateData(field, id);
				return false;  
			} else {  
				return true;  
			}
		},

		OnBlur: function(field, id) {
			SpiralTrac.InlineEditing.UpdateData(field, id);
		},
		
		UpdateData: function(field, id) {
			var fld = $(field);
			var updateFld = $('#'+id);
			var val = fld.val();

			updateFld.text(val);
			fld.remove(); 
			updateFld.show();
		}
	}
}