function checkForDefaultDescription(){
	var description = $('description').value; 
	if(description == "Click to enter description"){
		$('description').value = '';
	}
	return false;	
}
function showImportPartDialog(componentId, assemblyComponentId) {

	$('componentId').value = componentId;
	$('assemblyComponentId').value = assemblyComponentId;
	showAsModalDialog('mypartslist');
	$('mypartslist').style.top = (document.documentElement.scrollTop + 75) + 'px';
	return false;

}

function showAddedPartDialog(partNumber, supplierName) {

	document.getElementById('partNumber').innerHTML = partNumber;
	document.getElementById('supplierName').innerHTML = supplierName;

	showAsModalDialog('addedPartDialog');

	return false;

}

function setSupplierInfo(text, li) {
	if (li.id.startsWith('userSupplier')){
		$('useSupplierId').value = getNumericId(li.id);
	}else{
		$('vendId').value = getNumericId(li.id);
	}
}

function showLoadingIcon() {
/*	if (!$('loading')) {
		var l = document.createElement('div');
		l.id = 'loading';
		l.style.display = 'none';
		document.body.appendChild(l);
	} else {
		var l = $('loading');
	}
	l.show();
*/	
}

function hideLoadingIcon() {
/*	$('loading').hide(); */
}


function addPartFromGsPart(partNumber, description, productType, url, vendId, gsPartId, gsCompType, gsQueryId, componentId) {

	DWREngine.setAsync(true);
	showLoadingIcon();
	DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
		'addPartFromGsPart',
		partNumber, description, productType, url, vendId, gsPartId, gsCompType, gsQueryId, componentId,
		function (resp) {
			hideLoadingIcon();
			closeModalDialog();
			showAddedPartDialog(partNumber, resp);
		});
	DWREngine.setAsync(false);

	hideLoadingIcon();
	return false;
}

function showAddComponentDialog() {
//	document.getElementById('partListActionNav').style.display="none";
	document.getElementById('addComponentDialog').style.display="block";
	document.getElementById('addComponentTextBox').focus();
}

function hideAddComponentDialog() {
	document.getElementById('addComponentDialog').style.display="none";
	document.getElementById('addComponentTextBox').value = '';
}


// Function used to handle events when the layout of panes is changed.
function addComponent(componentName, assemblyId) {

	if (componentName == "") {
		return;
	}

	DWREngine.setAsync(true);
	showLoadingIcon();
	DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
		'addComponent',
		assemblyId, componentName, 1, '',
		function (resp) {
			hideLoadingIcon();
			var li = document.createElement('li');
			var splitResp = splitHtmlAndJs(resp);
			li.innerHTML = splitResp.html;
			var list = $('assembly_' + assemblyId + '-componentList');
			list.insertBefore(li, list.firstChild);
			$(list.firstChild).highlight();
			eval(splitResp.js);
		});
	DWREngine.setAsync(false);

	hideAddComponentDialog();

	return false;
	
}

function deleteProject(projectId) {
	if (confirm("Are you sure you want to delete this project?")) {
		DWREngine.setAsync(true);
		showLoadingIcon();
		DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
			'deleteProject',
			projectId,
			function (resp) {
				hideLoadingIcon();
				Element.hide($('project_' + projectId).parentNode);
			});
		DWREngine.setAsync(false);
	}
}


function deleteComponentPart(componentPartId) {
	if (confirm("Are you sure you want to remove this part?")) {
		DWREngine.setAsync(true);
		showLoadingIcon();
		DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
			'deleteComponentPart',
			componentPartId,
			function (resp) {
				hideLoadingIcon();
				if (resp == "{success}") {
					$('componentPart_' + componentPartId).style.display = 'none';
				}
			});
		DWREngine.setAsync(false);
	}
}

function deleteComponent(componentId) {
	if (confirm("Are you sure you want to delete this component?")) {
		DWREngine.setAsync(true);
		showLoadingIcon();
		DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
			'deleteComponent',
			componentId,
			function (resp) {
				hideLoadingIcon();
				if (resp == "{success}") {
					$('component_' + componentId).style.display = 'none';
				}
			});
		DWREngine.setAsync(false);
	}
}

function getNumericId(idString) {
	a = idString.split('_');
	return a[a.length-1];
}	

function addUserPartToComponent(form) {

	var selectedPartsToImport = 0;
	if(form.userPartId.length){
		for(var i=0; i<form.userPartId.length;i++){
			if(form.userPartId[i].checked == true){
				selectedPartsToImport = 1;
			}
		}
	}else{
		if(form.userPartId.checked == true){
			selectedPartsToImport = 1;
		}
	}
	
	if(selectedPartsToImport == 1){
		var componentId = form.componentId.value;
		DWREngine.setAsync(true);
		showLoadingIcon();
		DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
			'addUserPartToComponent',
			DWRUtil.serializeForm(form),
			function (resp) {
				hideLoadingIcon();
				var li = document.createElement('li');
				var splitResp = splitHtmlAndJs(resp);
				li.innerHTML = splitResp.html;
				var list = $('component_' + componentId + '-partList');
				list.insertBefore(li, list.firstChild);
				eval(splitResp.js);
				closeModalDialog();
			});
		DWREngine.setAsync(false);
		return false;
	}else{
		alert('You must select a part to import.');
		return false;
		
	}
}

function deleteUserPart(userPartId) {

	if (confirm('Are you sure you want to delete this part?')) {
		DWREngine.setAsync(true);
		showLoadingIcon();
		DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
			'deleteUserPart',
			userPartId,
			function (resp) {
				hideLoadingIcon();
				Element.hide($('userPart_' + userPartId).parentNode);
			});
		DWREngine.setAsync(false);
	}
}

function setPreferredPart(componentId, assemblyComponentId, componentPartId) {
	//alert(assemblyComponentId + "@" + componentPartId);
	
	DWREngine.setAsync(true);
	showLoadingIcon();
	DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
		'setPreferredPart',
		assemblyComponentId, componentPartId,
		function (resp) {

			hideLoadingIcon();
			
			componentParts = $('component_' + componentId).getElementsBySelector('.componentPart');
			for (i = 0; i < componentParts.length; i++) {
				// Show the "preferred" image for the newly selected component part
				$('componentPart_' + getNumericId(componentParts[i].id) + '-preferred-image').hide();
				$('componentPart_' + getNumericId(componentParts[i].id) + '-preferred-radio').show();
				$('componentPart_' + getNumericId(componentParts[i].id) + '-preferred-radio').checked = false;
			}

			// Show the "preferred" image for the newly selected component part
			$('componentPart_' + componentPartId + '-preferred-image').show();
			$('componentPart_' + componentPartId + '-preferred-radio').hide();
		});
	DWREngine.setAsync(false);
}

/*
 * Reset the contact add / edit form
 */
function resetContactForm(form) {

	// Clear out the form fields that were not pre-filled.
	form.name.value = "";
	form.email.value = "";
	form.mobile1.value = "";
	form.title.value = "";

	// Reset the form fields that were pre-filled to their
	// original values. the orig* fields are hidden fields
	// that are included on the form and are populated when
	// the form is renderered.
	form.phone1.value = form.origPhone1.value;
	form.fax1.value = form.origFax1.value;
	form.organization.value = form.origOrganization.value;
	form.url.value = form.origUrl.value;
}

function addContact(userSupplierId, form) {
	if (form.name.value.length == 0) {
		alert('Name is required');
		return false;
	}

	DWREngine.setAsync(true);
	showLoadingIcon();
	DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
		'addContact',
		userSupplierId, DWRUtil.serializeForm(form),
		function (resp) {
			hideLoadingIcon();
			var li = document.createElement('li');
			li.innerHTML = resp;
			$('userSupplier_' + userSupplierId + '-contactList').appendChild(li);
			closeModalDialog();
			resetContactForm(form);
		});
	DWREngine.setAsync(false);
}

function deleteUserContact (userContactId) {

	if (confirm('Are you sure you want to delete this contact?')) {
		DWREngine.setAsync(true);
		showLoadingIcon();
		DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
			'deleteUserContact',
			userContactId, 
			function (resp) {
				hideLoadingIcon();
				Element.hide($('userContact_' + userContactId).parentNode);
			});
		DWREngine.setAsync(false);
	}
}


function addProjectNote(projectId, noteText) {
	if (noteText.length == 0) {
		alert('Your note is too short.');
		return false;
	}

	DWREngine.setAsync(true);
	showLoadingIcon();
	DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
		'addProjectNote',
		projectId, noteText,
		function (resp) {
			hideLoadingIcon();
			var li = document.createElement('li');
			li.innerHTML = resp;
			var list = 	$('project_' + projectId + '-notes');
			list.insertBefore(li, list.firstChild);
			$('note').value = "";
			$('addNote').hide();
			$(list.firstChild).highlight();

			htmlEditor.getDoc().body.innerHTML = '';	
		});
	DWREngine.setAsync(false);
}

function addNote(noteTypeId, foreignId, noteText, uniqueId) {

	if (noteText.length == 0) {
		alert('Your note is too short.');
		return false;
	}

	DWREngine.setAsync(true);
	showLoadingIcon();
	DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
		'addNote',
		noteTypeId, foreignId, noteText,
		function (resp) {
			hideLoadingIcon();

			$('addNote-' + uniqueId).hide();
			$('noteText-' + uniqueId).clear();

			var li = document.createElement('li');
			li.innerHTML = resp;
			var listToAddTo = $('noteList-' + uniqueId);
			listToAddTo.insertBefore(li, listToAddTo.firstChild);
			$(listToAddTo.firstChild).highlight();
		});
	DWREngine.setAsync(false);
}

function deleteNote (noteId) {
	if (confirm('Are you sure you want to delete this note?')) {
		
		DWREngine.setAsync(true);
		showLoadingIcon();
		DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
			'deleteNote',
			noteId,
			function (resp) {
				hideLoadingIcon();
				Element.hide($('note_' + noteId).parentNode);
			});
		DWREngine.setAsync(false);
	}
}

function onProjectChange(projectSelect) {
	var projectId = projectSelect.options[projectSelect.selectedIndex].value;
	
	DWREngine.setAsync(true);
	showLoadingIcon();
	DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
		'getComponentsForProject',
		projectId,
		function (resp) {
			hideLoadingIcon();
			$('componentId').innerHTML = resp;
		});
	DWREngine.setAsync(false);				
	
}

function specSearch() {
	var searchComponentId = $('searchComponentId').value;
	var searchTerm = $('searchTerm').value;
	if(searchTerm.length > 0){
		setCookie("GsComponentId", searchComponentId, "0.007", "TRUE");  
		document.location.href='/productfinder/findproducts?query=' + escape(searchTerm);
	}else{
		alert('Please enter a valid search term.');
	}
}
function showProductSearchDialog(componentId){
	$('searchComponentId').value = componentId;
	showAsModalDialog('productSearch');
	$('frmProductSearch').searchTerm.focus();
	return false;
}

function resetProductSearchDialog(){
	$('frmProductSearch').searchTerm.value= '';
	return false;
}

function setLastProjectViewedCookie(projectId) {
	setCookie("GsLastProjectViewed", projectId, "1", "TRUE");  
}

function setVendId(text, li) {
	$('frmAddSupplier').vendId.value = getNumericId(li.id);
}

function showAddSupplierDialog() {

	showAsModalDialog('addSupplierDialog');
	$('frmAddSupplier').supplierName.focus();
	return false;
}

function showEditSupplierDialog() {
	
	showAsModalDialog('editSupplierDialog');
	$('frmEditSupplier').organization.focus();
	return false;
}

function showAddPartDialog() {

	showAsModalDialog('addPartDialog');
	$('frmAddPart').supplierName.focus();
	return false;
}

function showEditContactDialog() {
	
	showAsModalDialog('editContactDialog');
	$('frmEditContact').firstName.focus();
	return false;
}

function addSupplierToNewSuppliersList(newSupplierName, resp) {

	var li = document.createElement('li');
	li.innerHTML = resp;
	
	var suppliers = $('supplierList').select('li');
	
	var pos = 0;
	// Find the right spot (alphabetically) to file this supplier. 
	for (var i = 0; i < suppliers.length; i++) {
		var curLi = suppliers[i];
		var supplierNames = curLi.select('[class="supplierName"]');
		if (supplierNames.length > 0) {
			var supplierName = supplierNames[0].firstChild.innerHTML;
			if (newSupplierName.toLowerCase() < supplierName.toLowerCase()) {
				$('supplierList').insertBefore(li, curLi);
				pos = i;
				break;
			}
		}
	}
	if (pos == 0) {
		$('supplierList').insertBefore(li, $('supplierList').firstChild);	
	}
	$(li).highlight();
	if ($('suppliersOfInterest')) {
		$('suppliersOfInterest').show();
	}
}

function resetAddSuppliersForm(form) {
	form.supplierName.value = "";
	form.vendId.value = "";
	form.useSupplierId.value = "";
	form.url.value = "http://";
	form.url.disabled = false;
	form.isNonGsSupplier.value = "0";
	form.noWebsite.checked = false;
	$('moreAddSupplier').hide();
	closeModalDialog();
}	

function addGsSupplier(supplierName, gsVendId) {
	DWREngine.setAsync(true);
	showLoadingIcon();
	DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
		'addSupplierFromGsSupplier',
		gsVendId, 0,
		function (resp) {
			hideLoadingIcon();
			//resetAddSuppliersForm(form);
			addSupplierToNewSuppliersList(supplierName, resp);
		});
	DWREngine.setAsync(false);
}

function addSupplier(form) {

	var supplierName = form.supplierName.value;
	if (supplierName.length == 0) {
		alert('Supplier Name is required.');
		return false;
	}
	if (form.vendId.value > 0) {
		DWREngine.setAsync(true);
		showLoadingIcon();
		DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
			'addSupplierFromGsSupplier',
			form.vendId.value, form.projectId.value,
			function (resp) {
				hideLoadingIcon();
				resetAddSuppliersForm(form);
				addSupplierToNewSuppliersList(supplierName, resp);
				if ($('addSupplierMessage')) {
					$('addSupplierMessage').hide();
				}
			});
		DWREngine.setAsync(false);
	} else if (form.useSupplierId.value > 0){
		DWREngine.setAsync(true);
		showLoadingIcon();
		DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
			'addSupplierToProject',
			form.useSupplierId.value, form.projectId.value,
			function (resp) {
				hideLoadingIcon();
				resetAddSuppliersForm(form);
				addSupplierToNewSuppliersList(supplierName, resp);
				if ($('addSupplierMessage')) {
					$('addSupplierMessage').hide();
				}
			});
		DWREngine.setAsync(false);
	} else if (form.isNonGsSupplier.value == "1") {
		if ((form.url.value.length == 0 ||
			form.url.value == "http://") &&
			form.noWebsite.checked == false)  {
			alert('Website is required.');
			return false;	
		}
		DWREngine.setAsync(true);
		showLoadingIcon();
		DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
			'addSupplier',
			form.supplierName.value, form.url.value, form.projectId.value,
			function (resp) {
				hideLoadingIcon();
				resetAddSuppliersForm(form);
				addSupplierToNewSuppliersList(supplierName, resp);
				if ($('addSupplierMessage')) {
					$('addSupplierMessage').hide();
				}
			});
		DWREngine.setAsync(false);
	} else {
		$('moreAddSupplier').show();
		form.isNonGsSupplier.value = "1";
	}

	// Clear out the hidden vendor Id field, 
	// so that we don't mistakenly link the 
	// next supplier to an old one.
}

function deleteSupplier(userSupplierId) {
	if (confirm('Are you sure you want to delete this supplier?\r\nAll of the parts that you\'ve saved from this supplier will be lost.')) {
		DWREngine.setAsync(true);
		showLoadingIcon();
		DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
			'deleteUserSupplier',
			userSupplierId,
			function (resp) {
				hideLoadingIcon();
				Element.hide($('userSupplier_' + userSupplierId).parentNode);
			});
		DWREngine.setAsync(false);
	}
}

function editSupplier(form) {
		DWREngine.setAsync(true);
		showLoadingIcon();
		DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
			'editSupplier',
			DWRUtil.serializeForm(form),
			function (resp) {
				$('userSupplier_'+form.supplierId.value).innerHTML = resp;
				closeModalDialog();
				hideLoadingIcon();
			});
		DWREngine.setAsync(false);
}

function editContact(form) {

		DWREngine.setAsync(true);
		showLoadingIcon();
		DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
			'editUserContact',
			DWRUtil.serializeForm(form),
			function (resp) {
				closeModalDialog();
				//alert(resp);
				$('userContact_'+form.userContactId.value).innerHTML = resp;
				hideLoadingIcon();
			});
		DWREngine.setAsync(false);

}

function saveSupplier(gsVendId) {

	DWREngine.setAsync(true);

	// Note: the gsVendId - 0 used below was to force the value
	// to be numeric and not a string. This certainly doesn't
	// seem perfect, but it appears to get the job done.
	DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
		'addSupplierFromGsSupplier',
		gsVendId - 0, 0,
		function (resp) {
			showAsModalDialog('saveSupplierConfirm');
		}		
		);
	DWREngine.setAsync(false);
}

function resetAddPartForm(form) {
	form.partNumber.value = "";
	$('moreAddSupplier').hide();
}	

function displayAddPartMessages(){
	$('partListHeader').show();
	$('addPartMessage').hide();
}

function addPart(form) {

	var partNumber = form.partNumber.value;
	if (partNumber == 0) {
		alert('Part Number is required.');
		return false;
	}
	
	var supplierName = form.supplierName.value;

	if (supplierName.length == 0) {
		alert('Supplier Name is required.');
		return false;
	}

	var vendId = form.vendId.value;
	var userSupplierId = form.useSupplierId.value;

	if (userSupplierId > 0){
		DWREngine.setAsync(true);
		showLoadingIcon();
		DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
			'addPart',
			partNumber, userSupplierId,
			function (resp) {
				hideLoadingIcon();
				hideAddPartDialog();
				resetAddSuppliersForm(form);
				resetAddPartForm(form);
				addPartToNewPartsList(partNumber, resp);
				displayAddPartMessages();
			});
		DWREngine.setAsync(false);
		hideLoadingIcon();
		return false;
	} else if (vendId > 0 ) {
		DWREngine.setAsync(true);
		showLoadingIcon();
		DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
			'addPartFromGsSupplier',
			partNumber, vendId,
			function (resp) {
				hideLoadingIcon();
				hideAddPartDialog();
				resetAddSuppliersForm(form);
				resetAddPartForm(form);
				addPartToNewPartsList(partNumber, resp);
				displayAddPartMessages();
			});

		DWREngine.setAsync(false);
		return false;
	} else if (form.isNonGsSupplier.value == "1") {
		DWREngine.setAsync(true);
		showLoadingIcon();
		DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
			'addPartFromSupplier',
			partNumber, supplierName, form.url.value, 
			function (resp) {
				hideLoadingIcon();
				hideAddPartDialog();
				resetAddSuppliersForm(form);
				resetAddPartForm(form);
				addPartToNewPartsList(partNumber, resp);
				displayAddPartMessages();
			});

		DWREngine.setAsync(false);
		hideLoadingIcon();
		return false;		
	} else{
		$('moreAddSupplier').show();
		form.isNonGsSupplier.value = "1";
	}
}

function hideAddPartDialog() {
	closeModalDialog('addPartDialog');
}

// get the nth child element of a node ignoring all text and comment nodes
function getChildElementByIndex(parentNode, index) {
	var child = parentNode.firstChild;
	var currentIndex = 0;
	
	while (child != null) {
		if (child.nodeType == Node.ELEMENT_NODE) {
			if (currentIndex >= index) {
				break;
			}
			currentIndex++;
		}
		child = child.nextSibling;
	}
	
	return child;
}

function addPartToNewPartsList(newPartNumber, resp) {

	var li = document.createElement('li');
	li.innerHTML = resp;
	var partList = $('partList');
	partList.insertBefore(li, getChildElementByIndex(partList, 1));
}

function addProjectTask(projectId, projectTaskName, completeDate) {
	// Validate the title
	if (projectTaskName.length == 0) {
		alert("You must enter a task title.");
		return false;
	}
	
	DWREngine.setAsync(true);
	showLoadingIcon();
	DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
		'addProjectTask',
		projectId, projectTaskName,
		function (resp) {
			//hideLoadingIcon();
			var li = document.createElement('li');
			li.innerHTML = resp;
			var list = 	$('project_' + projectId + '-tasks');
			list.appendChild(li);
			$(li).highlight();
			if ($('no-pending-tasks')) {
				$('no-pending-tasks').hide();
			}

			// Reset the add task form
			$('taskName').value = "";
			$('addTask').hide();
		});
	DWREngine.setAsync(false);
}

function updateProjectTask(projectId, projectTaskId, complete, projectTaskCheckbox) {
	DWREngine.setAsync(true);
	showLoadingIcon();
	DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
		'updateProjectTask',
		projectId, projectTaskId, complete,
		function (resp) {
			//hideLoadingIcon();
								
			var li = document.createElement('li');
			li.innerHTML = resp;
			
			var taskDiv = $('task_' + projectTaskId);
			var completedTaskDiv = $('task_' + projectTaskId + '_completed');
			
			if (complete == true) {
				// Marking a task as complete
				var completedList =	$('project_' + projectId + '-completed-tasks');
				var list = $('project_'+ projectId + '-tasks');
				completedList.insertBefore(li, completedList.firstChild);				
				list.removeChild(taskDiv.parentNode);
				
			} else {
				// Marking a task as still active.
				
				var completedTaskDiv = $('task_' + projectTaskId + '_completed');
				var completedList = $('project_' + projectId + '-completed-tasks');
				var list = $('project_' + projectId + '-tasks');
				list.insertBefore(li, list.firstChild);
				
				// Remove the DOM element
				completedList.removeChild(completedTaskDiv.parentNode);
			}
		});
	DWREngine.setAsync(false);			
}

function deleteTask(projectId, projectTaskId) {
	if (confirm('Are you sure you want to delete this task?')) {
		DWREngine.setAsync(true);
		DWREngine._execute("/ajax/myglobalspec/myprojectsajax.cfc", null, 
			'deleteProjectTask',
			projectTaskId,
			function (resp) {
				var taskDiv = $('task_' + projectTaskId);
				var list = $('project_' + projectId + '-tasks');
				list.removeChild(taskDiv.parentNode);
			});
		DWREngine.setAsync(false);
	}
}




// Takes html content and divides it into html and javascript parts.
// The caller can then append the html to a document and exec the js directly
function splitHtmlAndJs(input) {
	// match everything between a script block
	var findScriptsPattern = RegExp("<script[^>]*>[\\w\\W]*?<" + "/script>", "gim");
	
	// used to strip out the script tag
	var removeScriptsPattern = RegExp("<script[^>]*>|<" + "/script>", "gim");
	
	var html = "";
	var js = "";
	
	var lastIndex = 0;
	var match = findScriptsPattern.exec(input);
	
	while (match != null) {
		match = match.toString(); //need to do this for some reason or else .length fails
		
		var thisIndex = findScriptsPattern.lastIndex - match.length;
		var lastContent = input.substring(lastIndex, thisIndex);
		lastIndex = findScriptsPattern.lastIndex;
		
		html += lastContent + "\r\n";
		js += match.replace(removeScriptsPattern, "") + "\r\n";
		
		match = findScriptsPattern.exec(input);
	}
	
	return { html: html, js: js };
}
