function cancelLightview() {
	Lightview.hide();
}

function submitRepfinderDropdown(form, selection) {
	if (selection == "") {
		return false;
	} else {
		$(form).submit();
	}
}

function toggleFieldTypeSize(type_id) {
	if (type_id == 2) {
		$('field_type_size_div').style.display = 'block';
	} else {
		$('field_type_size_div').style.display = 'none';
	}
}

function populateListDatasourceColumns(theSelectMenu, sourceId, sourceColumn) {
	new Ajax.Request('/admin/rep_finder/ajax/get_source_columns.cfm', {
	  method: 'get',
	  parameters: {source_id: sourceId},
	  onSuccess: function(transport) {
		theSelectMenu.options.length = 0;
		theSelectMenu.options[0] = new Option('', '');
	  
	  	columns = transport.responseText.split(',');
	  	
		for(var i=0; i<columns.length; i++){
			if (columns[i] != 'ID') {
				theSelectMenu.options[theSelectMenu.options.length] = new Option(columns[i], columns[i]);
				if (sourceColumn == columns[i]) { theSelectMenu.options[theSelectMenu.options.length - 1].selected = true; }
			}
		}
	  }
	});
}

function toggleIAmASelectList(listSelection, sourceId, sourceColumn) {
	if (listSelection == "datasource") {
		$('list_datasource_source_div').style.display = 'block';
		$('list_datasource_column_div').style.display = 'none';
		$('list_datasource_aoi_div').style.display = 'none';
		$('list_datasource_direct_div').style.display = 'none';
	}

	if (listSelection == "datasource_column") {
		populateListDatasourceColumns($('iama_select_list_column'), sourceId, sourceColumn);

		$('list_datasource_source_div').style.display = 'block';
		$('list_datasource_column_div').style.display = 'block';
		$('list_datasource_aoi_div').style.display = 'none';
		$('list_datasource_direct_div').style.display = 'none';
	}

	if (listSelection == "aoi") {
		$('list_datasource_source_div').style.display = 'none';
		$('list_datasource_column_div').style.display = 'none';
		$('list_datasource_direct_div').style.display = 'none';
		$('list_datasource_aoi_div').style.display = 'block';
	}

	if (listSelection == "direct") {
		$('list_datasource_source_div').style.display = 'none';
		$('list_datasource_column_div').style.display = 'none';
		$('list_datasource_aoi_div').style.display = 'none';
		$('list_datasource_direct_div').style.display = 'block';
	}
}

function toggleAOISelectList(listSelection, sourceId, sourceColumn) {
	if (listSelection == "datasource") {
		$('list_datasource_source_div').style.display = 'block';
		$('list_datasource_column_div').style.display = 'none';
		$('list_datasource_direct_div').style.display = 'none';
	}
	
	if (listSelection == "datasource_column") {
		populateListDatasourceColumns($('aoi_select_list_column'), sourceId, sourceColumn);

		$('list_datasource_source_div').style.display = 'block';
		$('list_datasource_column_div').style.display = 'block';
		$('list_datasource_direct_div').style.display = 'none';
	}

	if (listSelection == "direct") {
		$('list_datasource_source_div').style.display = 'none';
		$('list_datasource_column_div').style.display = 'none';
		$('list_datasource_direct_div').style.display = 'block';
	}
}

function checkInvalidCharacters(str, type) {
	if (type == "strict alphanumeric") { var theMatch = /^[a-zA-Z0-9]+$/; }
	if (type == "medium alphanumeric") { theMatch = /^[\_a-zA-Z0-9]+$/; }
	if (type == "loose alphanumeric") { theMatch = /^[a-zA-Z0-9\,\@\_\=\-\/\(\) ]+$/; }
	if (type == "alpha") { theMatch = /^[a-zA-Z]+$/; }
	if (type == "strict alpha lowercase") { var theMatch = /^[a-z ]+$/; }
	if (type == "numeric") { theMatch = /^[0-9]+$/; }
	
	if (str.match(theMatch) || str.length == 0) {
		if ($('lightviewMessageDiv')) {
			$('lightviewMessageDiv').setAttribute("class", "tableLoaded");
			$('lightviewMessageDiv').className = "tableLoaded";
			$('lightviewMessageDiv').innerHTML = "Characters OK";
		} else {
			$('messageDiv').setAttribute("class", "tableLoaded");
			$('messageDiv').className = "tableLoaded";
			$('messageDiv').innerHTML = "Characters OK";
		}	
	} else {
		if ($('lightviewMessageDiv')) {
			$('lightviewMessageDiv').setAttribute("class", "tableLoading");
			$('lightviewMessageDiv').className = "tableLoading";
			$('lightviewMessageDiv').innerHTML = "Invalid Characters Present";
			new Effect.Highlight('lightviewMessageDiv');
			new Effect.Pulsate('lightviewMessageDiv', { pulses: 5, duration: 1.5 });
		} else {
			$('messageDiv').setAttribute("class", "tableLoading");
			$('messageDiv').className = "tableLoading";
			$('messageDiv').innerHTML = "Invalid Characters Present.";
			new Effect.Highlight('messageDiv');
			new Effect.Pulsate('messageDiv', { pulses: 5, duration: 1.5 });
		}
	}
}

function checkIAmANameExists(iamaName) {
	new Ajax.Request('/admin/rep_finder/ajax/check_iamaname_exists.cfm', {
	  method: 'get',
	  parameters: {iama_id: 0, iama_name: iamaName},
	  onSuccess: function(transport) {
	       if (transport.responseText == 1) {
	       		if ($('lightviewMessageDiv')) {
					$('lightviewMessageDiv').setAttribute("class", "tableLoading");
					$('lightviewMessageDiv').className = "tableLoading";
					$('lightviewMessageDiv').innerHTML = "System Name Already Exists";
					new Effect.Highlight('lightviewMessageDiv');
					new Effect.Pulsate('lightviewMessageDiv', { pulses: 5, duration: 1.5 });
	       		} else {
					$('messageDiv').setAttribute("class", "tableLoading");
					$('messageDiv').className = "tableLoading";
					$('messageDiv').innerHTML = "System Name Already Exists.";
					new Effect.Highlight('messageDiv');
					new Effect.Pulsate('messageDiv', { pulses: 5, duration: 1.5 });
	       		}
	       } else {
	       		if ($('lightviewMessageDiv')) {
					$('lightviewMessageDiv').setAttribute("class", "tableLoaded");
					$('lightviewMessageDiv').className = "tableLoaded";
					$('lightviewMessageDiv').innerHTML = "System Name OK";
	       		} else {
					$('messageDiv').setAttribute("class", "tableLoaded");
					$('messageDiv').className = "tableLoaded";
					$('messageDiv').innerHTML = "System Name OK";
	       		}

			checkInvalidCharacters(iamaName, 'strict alpha lowercase');
	       }
	  }
	});
}

function checkIAmALabelExists(iamaLabel) {
	new Ajax.Request('/admin/rep_finder/ajax/check_iamalabel_exists.cfm', {
	  method: 'get',
	  parameters: {iama_id: 0, iama_label: iamaLabel},
	  onSuccess: function(transport) {
	       if (transport.responseText == 1) {
	       		if ($('lightviewMessageDiv')) {
					$('lightviewMessageDiv').setAttribute("class", "tableLoading");
					$('lightviewMessageDiv').className = "tableLoading";
					$('lightviewMessageDiv').innerHTML = "Name Already Exists";
					new Effect.Highlight('lightviewMessageDiv');
					new Effect.Pulsate('lightviewMessageDiv', { pulses: 5, duration: 1.5 });
	       		} else {
					$('messageDiv').setAttribute("class", "tableLoading");
					$('messageDiv').className = "tableLoading";
					$('messageDiv').innerHTML = "Name Already Exists.";
					new Effect.Highlight('messageDiv');
					new Effect.Pulsate('messageDiv', { pulses: 5, duration: 1.5 });
	       		}
	       } else {
	       		if ($('lightviewMessageDiv')) {
					$('lightviewMessageDiv').setAttribute("class", "tableLoaded");
					$('lightviewMessageDiv').className = "tableLoaded";
					$('lightviewMessageDiv').innerHTML = "Name OK";
	       		} else {
					$('messageDiv').setAttribute("class", "tableLoaded");
					$('messageDiv').className = "tableLoaded";
					$('messageDiv').innerHTML = "Name OK";
	       		}

			checkInvalidCharacters(iamaLabel, 'loose alphanumeric');
	       }
	  }
	});
}

function checkAOINameExists(aoiName) {
	new Ajax.Request('/admin/rep_finder/ajax/check_aoiname_exists.cfm', {
	  method: 'get',
	  parameters: {aoi_id: 0, aoi_name: aoiName},
	  onSuccess: function(transport) {
	       if (transport.responseText == 1) {
	       		if ($('lightviewMessageDiv')) {
					$('lightviewMessageDiv').setAttribute("class", "tableLoading");
					$('lightviewMessageDiv').className = "tableLoading";
					$('lightviewMessageDiv').innerHTML = "Area of Interest Already Exists";
					new Effect.Highlight('lightviewMessageDiv');
					new Effect.Pulsate('lightviewMessageDiv', { pulses: 5, duration: 1.5 });
	       		} else {
					$('messageDiv').setAttribute("class", "tableLoading");
					$('messageDiv').className = "tableLoading";
					$('messageDiv').innerHTML = "Area of Interest Already Exists.";
					new Effect.Highlight('messageDiv');
					new Effect.Pulsate('messageDiv', { pulses: 5, duration: 1.5 });
	       		}
	       } else {
	       		if ($('lightviewMessageDiv')) {
					$('lightviewMessageDiv').setAttribute("class", "tableLoaded");
					$('lightviewMessageDiv').className = "tableLoaded";
					$('lightviewMessageDiv').innerHTML = "Name OK";
	       		} else {
					$('messageDiv').setAttribute("class", "tableLoaded");
					$('messageDiv').className = "tableLoaded";
					$('messageDiv').innerHTML = "Name OK";
	       		}

			checkInvalidCharacters(aoiName, 'loose alphanumeric');
	       }
	  }
	});
}

function checkTableNameExists(sourceTable) {
	new Ajax.Request('/admin/rep_finder/ajax/check_table_exists.cfm', {
	  method: 'get',
	  parameters: {source_id: 0, source_table: sourceTable},
	  onSuccess: function(transport) {
	       if (transport.responseText == 1) {
	       		if ($('lightviewMessageDiv')) {
					$('lightviewMessageDiv').setAttribute("class", "tableLoading");
					$('lightviewMessageDiv').className = "tableLoading";
					$('lightviewMessageDiv').innerHTML = "Table Already Exists";
					new Effect.Highlight('lightviewMessageDiv');
					new Effect.Pulsate('lightviewMessageDiv', { pulses: 5, duration: 1.5 });
	       		} else {
					$('messageDiv').setAttribute("class", "tableLoading");
					$('messageDiv').className = "tableLoading";
					$('messageDiv').innerHTML = "Table Already Exists.";
					new Effect.Highlight('messageDiv');
					new Effect.Pulsate('messageDiv', { pulses: 5, duration: 1.5 });
	       		}
	       } else {
	       		if ($('lightviewMessageDiv')) {
					$('lightviewMessageDiv').setAttribute("class", "tableLoaded");
					$('lightviewMessageDiv').className = "tableLoaded";
					$('lightviewMessageDiv').innerHTML = "Table Name OK";
	       		} else {
					$('messageDiv').setAttribute("class", "tableLoaded");
					$('messageDiv').className = "tableLoaded";
					$('messageDiv').innerHTML = "Table Name OK";
	       		}

			checkInvalidCharacters(sourceTable, 'medium alphanumeric');
	       }
	  }
	});
}

function checkSourceNameExists(sourceName) {
	new Ajax.Request('/admin/rep_finder/ajax/check_source_exists.cfm', {
	  method: 'get',
	  parameters: {source_id: 0, source_name: sourceName},
	  onSuccess: function(transport) {
	       if (transport.responseText == 1) {
	       		if ($('lightviewMessageDiv')) {
					$('lightviewMessageDiv').setAttribute("class", "tableLoading");
					$('lightviewMessageDiv').className = "tableLoading";
					$('lightviewMessageDiv').innerHTML = "Datasource Name Already Exists";
					new Effect.Highlight('lightviewMessageDiv');
					new Effect.Pulsate('lightviewMessageDiv', { pulses: 5, duration: 1.5 });
	       		} else {
					$('messageDiv').setAttribute("class", "tableLoading");
					$('messageDiv').className = "tableLoading";
					$('messageDiv').innerHTML = "Datasource Name Already Exists.";
					new Effect.Highlight('messageDiv');
					new Effect.Pulsate('messageDiv', { pulses: 5, duration: 1.5 });
	       		}
	       } else {
	       		if ($('lightviewMessageDiv')) {
					$('lightviewMessageDiv').setAttribute("class", "tableLoaded");
					$('lightviewMessageDiv').className = "tableLoaded";
					$('lightviewMessageDiv').innerHTML = "Datasource Name OK";
	       		} else {
					$('messageDiv').setAttribute("class", "tableLoaded");
					$('messageDiv').className = "tableLoaded";
					$('messageDiv').innerHTML = "Datasource Name OK";
	       		}

			checkInvalidCharacters(sourceName, 'loose alphanumeric');
	       }
	  }
	});
}

function checkFieldNameExists(sourceId, sourceTable, fieldName) {
	new Ajax.Request('/admin/rep_finder/ajax/check_field_exists.cfm', {
	  method: 'get',
	  parameters: {source_id: sourceId, source_table: sourceTable, field_name: fieldName},
	  onSuccess: function(transport) {
	       if (transport.responseText == 1) {
	       		if ($('lightviewMessageDiv')) {
					$('lightviewMessageDiv').setAttribute("class", "tableLoading");
					$('lightviewMessageDiv').className = "tableLoading";
					$('lightviewMessageDiv').innerHTML = "FIeld Already Exists";
					new Effect.Highlight('lightviewMessageDiv');
					new Effect.Pulsate('lightviewMessageDiv', { pulses: 5, duration: 1.5 });
	       		} else {	       
					$('messageDiv').setAttribute("class", "tableLoading");
					$('messageDiv').className = "tableLoading";
					$('messageDiv').innerHTML = "FIeld Already Exists.";
					new Effect.Highlight('messageDiv');
					new Effect.Pulsate('messageDiv', { pulses: 5, duration: 1.5 });
				}
	       } else {
	       		if ($('lightviewMessageDiv')) {
					$('lightviewMessageDiv').setAttribute("class", "tableLoaded");
					$('lightviewMessageDiv').className = "tableLoaded";
					$('lightviewMessageDiv').innerHTML = "Field Name OK";
	       		} else {
					$('messageDiv').setAttribute("class", "tableLoaded");
					$('messageDiv').className = "tableLoaded";
					$('messageDiv').innerHTML = "Field Name OK";
				}
	            
				checkInvalidCharacters(fieldName, 'medium alphanumeric');
	       }
	  }
	});
}

function submitEditFieldForm(event) {
	Event.stop(event);
	
	Lightview.show({
		href: '/admin/rep_finder/ajax/edit_field.cfm',
		rel: 'ajax',
		options: {
		  title: 'results',
		  menubar: false,
		  topclose: true,
		  autosize: true,
		  ajax: {
			parameters: Form.serialize('fEditFieldForm'),
			onComplete: function() {
				window.location.href = window.location.href + "&message=Column Successfully Updated.";
			}

		  }
		}
	  });
}

function submitAddDataForm(event) {
	Event.stop(event);
	
	Lightview.show({
		href: '/admin/rep_finder/ajax/add_data.cfm',
		rel: 'ajax',
		options: {
		  title: 'results',
		  menubar: false,
		  topclose: true,
		  autosize: true,
		  ajax: {
			parameters: Form.serialize('fAddDataForm'),
			onComplete: function() {
				window.location.href = window.location.href + "&message=Data Added Successfully.";
			}

		  }
		}
	  });
}

function submitAddColumnForm(event) {
	Event.stop(event);
	
	Lightview.show({
		href: '/admin/rep_finder/ajax/add_column.cfm',
		rel: 'ajax',
		options: {
		  title: 'results',
		  menubar: false,
		  topclose: true,
		  autosize: true,
		  ajax: {
			parameters: Form.serialize('fAddColumnForm'),
			onComplete: function() {
				window.location.href = window.location.href + "&message=Column Added Successfully.";
			}

		  }
		}
	  });
}

function submitDeleteDatasourceForm(event) {
	Event.stop(event);
	
	Lightview.show({
		href: '/admin/rep_finder/ajax/delete_datasource.cfm',
		rel: 'ajax',
		options: {
		  title: 'results',
		  menubar: false,
		  topclose: true,
		  autosize: true,
		  ajax: {
			parameters: Form.serialize('fDeleteDatasourceForm'),
			onComplete: function() {
				window.location.href = window.location.href + "&message=Datasource Deleted Successfully.";
			}

		  }
		}
	  });
}

function submitUnDeleteDatasourceForm1(event) {
	Event.stop(event);
	
	Lightview.show({
		href: '/admin/rep_finder/ajax/undelete_datasource.cfm',
		rel: 'ajax',
		options: {
		  title: 'results',
		  menubar: false,
		  topclose: true,
		  autosize: true,
		  ajax: {
			parameters: Form.serialize('fUnDeleteDatasourceForm'),
			onComplete: function() {
				$('fUnDeleteDatasourceForm').observe('submit', submitUnDeleteDatasourceForm2);
			}

		  }
		}
	  });
}

function submitUnDeleteDatasourceForm2(event) {
	Event.stop(event);
	
	Lightview.show({
		href: '/admin/rep_finder/ajax/undelete_datasource.cfm',
		rel: 'ajax',
		options: {
		  title: 'results',
		  menubar: false,
		  topclose: true,
		  autosize: true,
		  ajax: {
			parameters: Form.serialize('fUnDeleteDatasourceForm'),
			onComplete: function() {
				window.location.href = window.location.href + "&message=Datasource UnDeleted Successfully.";
			}

		  }
		}
	  });
}

function submitCreateDatasourceForm(event) {
	Event.stop(event);
	
	Lightview.show({
		href: '/admin/rep_finder/ajax/create_datasource.cfm',
		rel: 'ajax',
		options: {
		  title: 'results',
		  menubar: false,
		  topclose: true,
		  autosize: true,
		  ajax: {
			parameters: Form.serialize('fCreateDatasourceForm'),
			onComplete: function() {
				window.location.href = window.location.href + "&message=Datasource Created Successfully.";
			}

		  }
		}
	  });
}

function submitCreateIAmAForm(event) {
	Event.stop(event);
	
	Lightview.show({
		href: '/admin/rep_finder/ajax/create_iama.cfm',
		rel: 'ajax',
		options: {
		  title: 'results',
		  menubar: false,
		  topclose: true,
		  autosize: true,
		  ajax: {
			parameters: Form.serialize('fCreateIAmAForm'),
			onComplete: function() {
				window.location.href = window.location.href + "&message=I Am A Created Successfully.";
			}

		  }
		}
	  });
}

function submitDeleteIAmAForm(event) {
	Event.stop(event);
	
	Lightview.show({
		href: '/admin/rep_finder/ajax/delete_iama.cfm',
		rel: 'ajax',
		options: {
		  title: 'results',
		  menubar: false,
		  topclose: true,
		  autosize: true,
		  ajax: {
			parameters: Form.serialize('fDeleteIAmAForm'),
			onComplete: function() {
				window.location.href = window.location.href + "&message=I Am A Deleted Successfully.";
			}

		  }
		}
	  });
}

function submitUnDeleteIAmAForm1(event) {
	Event.stop(event);
	
	Lightview.show({
		href: '/admin/rep_finder/ajax/undelete_iama.cfm',
		rel: 'ajax',
		options: {
		  title: 'results',
		  menubar: false,
		  topclose: true,
		  autosize: true,
		  ajax: {
			parameters: Form.serialize('fUnDeleteIAmAForm'),
			onComplete: function() {
				$('fUnDeleteIAmAForm').observe('submit', submitUnDeleteIAmAForm2);
			}

		  }
		}
	  });
}

function submitUnDeleteIAmAForm2(event) {
	Event.stop(event);
	
	Lightview.show({
		href: '/admin/rep_finder/ajax/undelete_iama.cfm',
		rel: 'ajax',
		options: {
		  title: 'results',
		  menubar: false,
		  topclose: true,
		  autosize: true,
		  ajax: {
			parameters: Form.serialize('fUnDeleteIAmAForm'),
			onComplete: function() {
				window.location.href = window.location.href + "&message=I Am A UnDeleted Successfully.";
			}

		  }
		}
	  });
}

function submitEditIAmAForm(event) {
	Event.stop(event);
	
	Lightview.show({
		href: '/admin/rep_finder/ajax/edit_iama.cfm',
		rel: 'ajax',
		options: {
		  title: 'results',
		  menubar: false,
		  topclose: true,
		  autosize: true,
		  ajax: {
			parameters: Form.serialize('fEditIAmAForm'),
			onComplete: function() {
				window.location.href = window.location.href + "&message=I Am A Successfully Updated.";
			}

		  }
		}
	  });
}


/* */

function submitCreateAOIForm(event) {
	Event.stop(event);
	
	Lightview.show({
		href: '/admin/rep_finder/ajax/create_aoi.cfm',
		rel: 'ajax',
		options: {
		  title: 'results',
		  menubar: false,
		  topclose: true,
		  autosize: true,
		  ajax: {
			parameters: Form.serialize('fCreateAOIForm'),
			onComplete: function() {
				window.location.href = window.location.href + "&message=Area of Interest Created Successfully.";
			}

		  }
		}
	  });
}

function submitDeleteAOIForm(event) {
	Event.stop(event);
	
	Lightview.show({
		href: '/admin/rep_finder/ajax/delete_aoi.cfm',
		rel: 'ajax',
		options: {
		  title: 'results',
		  menubar: false,
		  topclose: true,
		  autosize: true,
		  ajax: {
			parameters: Form.serialize('fDeleteAOIForm'),
			onComplete: function() {
				window.location.href = window.location.href + "&message=Area of Interest Deleted Successfully.";
			}

		  }
		}
	  });
}

function submitUnDeleteAOIForm1(event) {
	Event.stop(event);
	
	Lightview.show({
		href: '/admin/rep_finder/ajax/undelete_aoi.cfm',
		rel: 'ajax',
		options: {
		  title: 'results',
		  menubar: false,
		  topclose: true,
		  autosize: true,
		  ajax: {
			parameters: Form.serialize('fUnDeleteAOIForm'),
			onComplete: function() {
				$('fUnDeleteAOIForm').observe('submit', submitUnDeleteAOIForm2);
			}

		  }
		}
	  });
}

function submitUnDeleteAOIForm2(event) {
	Event.stop(event);
	
	Lightview.show({
		href: '/admin/rep_finder/ajax/undelete_aoi.cfm',
		rel: 'ajax',
		options: {
		  title: 'results',
		  menubar: false,
		  topclose: true,
		  autosize: true,
		  ajax: {
			parameters: Form.serialize('fUnDeleteAOIForm'),
			onComplete: function() {
				window.location.href = window.location.href + "&message=Area of Interest UnDeleted Successfully.";
			}

		  }
		}
	  });
}

function submitEditAOIForm(event) {
	Event.stop(event);
	
	Lightview.show({
		href: '/admin/rep_finder/ajax/edit_aoi.cfm',
		rel: 'ajax',
		options: {
		  title: 'results',
		  menubar: false,
		  topclose: true,
		  autosize: true,
		  ajax: {
			parameters: Form.serialize('fEditAOIForm'),
			onComplete: function() {
				window.location.href = window.location.href + "&message=Area of Interest Successfully Updated.";
			}

		  }
		}
	  });
}

function submitDefineRenderForm(event) {
	Event.stop(event);
	
	Lightview.show({
		href: '/admin/rep_finder/ajax/define_render.cfm',
		rel: 'ajax',
		options: {
		  title: 'results',
		  menubar: false,
		  topclose: true,
		  autosize: true,
		  ajax: {
			parameters: Form.serialize('fDefineRenderForm'),
			onComplete: function() {
				window.location.href = window.location.href + "&message=Output Format Updated";
			}

		  }
		}
	  });
}