window.onerror = null;
var newPopUpWindow = null;

function openPopUpWindow(popUpWindowPath, popUpWindowName, popUpWindowProperties) {   
	if (newPopUpWindow) if (!newPopUpWindow.closed) newPopUpWindow.close();      
	newPopUpWindow = window.open(popUpWindowPath, popUpWindowName, popUpWindowProperties);
	newPopUpWindow.focus();
}

function validate_createuser() {
	var f = document.forms.thisForm;
	if (f.twlogin.value.length == 0) {
		alert('Login is a required field.  Please enter a login before proceeding.');
		f.twlogin.focus();
		return false;
	} else if (f.twfname.value.length == 0) {
		alert('First Name is a required field.  Please enter a login before proceeding.');
		f.twfname.focus();
		return false;
	} else if (f.twlname.value.length == 0) {
		alert('Last Name is a required field.  Please enter a login before proceeding.');
		f.twlname.focus();
		return false;
	} else if (f.twemail.value.length == 0) {
		alert('Email Address is a required field.  Please enter a login before proceeding.');
		f.twemail.focus();
		return false;
	} else {
		return true;
	}
}

function validate_newentry() {
	var f = document.forms.thisForm;
	if (f.twnewentrytopic.value == 0) {
		alert('You must choose a topic before proceeding.');
		return false;
	} else if (f.twnewentrymaintext.value.length == 0) {
		alert('The new main entry text you entered was empty.');
		return false;
	} else {
		f.post.value = 1;
		return true;
	}
		
}

function validate_editentry() {
	var f = document.forms.thisForm;
	if (f.tweditentrytopic.value == 0) {
		alert('You must choose a topic before proceeding.');
		return false;
	} else if (f.tweditentrymaintext.value.length == 0) {
		alert('The main entry text you entered was empty.');
		return false;
	} else {
		f.post.value = 1;
		return true;
	}
		
}

function validate_attachfiles() {
	var f = document.forms.thisForm;
	var okay = false;
	if (f.file1.value.length > 0) {
		if (f.file1keywords.value.length == 0) {
			alert('Please provide a few keywords or a brief description for File 1.');
		} else if (isValidExtension(f.file1.value) != -1) {
			okay = true;
		}
	} else if (f.file2.value.length > 0) {
		if (f.file2keywords.value.length == 0) {
			alert('Please provide a few keywords or a brief description for File 2.');
		} else if (isValidExtension(f.file2.value) != -1) {
			okay = true;
		}
	} else if (f.file3.value.length > 0) {
		if (f.file3keywords.value.length == 0) {
			alert('Please provide a few keywords or a brief description for File 3.');
		} else if (isValidExtension(f.file3.value) != -1) {
			okay = true;
		}
	} else if (f.file4.value.length > 0) {
		if (f.file4keywords.value.length == 0) {
			alert('Please provide a few keywords or a brief description for File 4.');
		} else if (isValidExtension(f.file4.value) != -1) {
			okay = true;
		}
	} 
	
	if (okay) {
		f.b1.value = "Uploading ...";
		f.b1.disabled = true;
		f.b2.disabled = true;
		f.submit();
	}
}

function validate_attachdxf() {
	var f = document.forms.thisForm;
	var okay = false;
	if (f.dxf.value.length > 0) {
		if (f.dxfkeywords.value.length == 0) {
			alert('Please provide a few keywords or a brief description of your file.');
		} else if (isValidDXFExtension(f.dxf.value) != -1) {
			okay = true;
		}
	}
	if (okay) {
		f.b1.value = "Uploading ...";
		f.b1.disabled = true;
		f.submit();
	}
}

function validate_attachpreview() {
	var f = document.forms.thisForm;
	var okay = false;
	if (f.thumbnail.value.length > 0) {
		if (f.dxfkeywords.value.length == 0) {
			alert('Please provide a few keywords or a brief description of your file.');
		} else if (isValidPreviewExtension(f.thumbnail.value) != -1) {
			okay = true;
		}
	} else {
		if (f.dxfkeywords.value.length == 0) {
			alert('Please provide a few keywords or a brief description of your file.');
		} else {
			okay = true;
		}
	}
	if (okay) {
		f.b1.value = "Uploading ...";
		f.b1.disabled = true;
		f.submit();
	}
}

function cancel_newentry() {
	document.location = '/index.cfm';
}

function cancel_editentry() {
	document.location = '/index.cfm';
}

function preview_newentry() {
	var f = document.forms.thisForm;
	f.action = '/actions/create_new_entry.cfm?preview=1';
	f.submit();
}

function preview_editentry() {
	var f = document.forms.thisForm;
	f.action = '/actions/edit_entry.cfm?preview=1';
	f.submit();
}

function attachfiles_newentry() {
	var f = document.forms.thisForm;
	f.addattachment.value = 1;
	f.submit();
	
}

function attachfiles_editentry() {
	var f = document.forms.thisForm;
	f.addattachment.value = 1;
	f.submit();
	
}

function attachfiles_dialog(id, edit) {
	var path = '';
	if (edit) {
		path = '/membersonly/attach_files.cfm?twentryid=' + id + '&edit=1';
	} else {
		path = '/membersonly/attach_files.cfm?twentryid=' + id + '&edit=0';
	}
	openPopUpWindow(path,'AttachFiles','width=660,height=325,toolbar=no,scrollbars=no,resizable=no,menubar=no,titlebar=no,status=no,directories=no,location=no,top=250,left=325');
}	

function isValidPasswd(pw, pw2, login, fn, ln){
	if (pw.length == 0){
		alert("The new password you entered was empty.");
		return -1;
	} else if (pw != pw2) {
		alert("Your new password doesn't match.");
		return -1;
	} else if (pw.length > 32) {
		alert("Your new password must be no greater than 32 characters.");
		return -1;
	} else if (pw.length < 6) {
		alert("Your new password must be at least 6 characters.");
		return  -1;
	} else if (login.length != 0 && (login.indexOf(pw) >= 0 || pw.indexOf(login) >= 0)){
		alert("Your new password is too similar to your login.");
		return -1;
	} else if ((fn.length > 2) && (pw.indexOf(fn) >= 0) ){
		alert("Your new password is too similar to your first or last name.");
		return -1;
	} else if ( (ln.length > 2) && (pw.indexOf(ln) >=0) ){
		alert("Your new password is too similar to your first or last name.");
		return -1;
	} else {
		return 0;
	}
}

function isValidExtension(extension) {
	extension = extension.toLowerCase();
	if (extension.indexOf('.avi') == -1 && extension.indexOf('.jpg') == -1 && extension.indexOf('.jpeg') == -1 && extension.indexOf('.gif') == -1 && extension.indexOf('.htm') == -1 && extension.indexOf('.html') == -1 && extension.indexOf('.mov') == -1 && extension.indexOf('.mpeg') == -1 && extension.indexOf('.pdf') == -1 && extension.indexOf('.mov') == -1 && extension.indexOf('.wma') == -1 && extension.indexOf('.wmv') == -1) {
		alert('Only avi\'s, jpeg\'s, gif\'s, htm\'s, html\'s, mov\'s,  mpeg\'s, pdf\'s, wma\'s and wmv\'s are allowed.');
		return -1;
	} else {
		return 0;
	}
}

function isValidDXFExtension(extension) {
	extension = extension.toLowerCase();
	if (extension.indexOf('.dxf') == -1 && extension.indexOf('.dwg') == -1) {
		alert('Only dxf\'s & dwg\'s are allowed.');
		return -1;
	} else {
		return 0;
	}
}

function isValidPreviewExtension(extension) {
	extension = extension.toLowerCase();
	if (extension.indexOf('.jpg') == -1) {
		alert('Only jpg\'s are allowed.');
		return -1;
	} else {
		return 0;
	}
}

window.onerror = null;
var newPopUpWindow = null;

function openPopUpWindow(popUpWindowPath, popUpWindowName, popUpWindowProperties) {   
	if (newPopUpWindow) if (!newPopUpWindow.closed) newPopUpWindow.close();      
	newPopUpWindow = window.open(popUpWindowPath, popUpWindowName, popUpWindowProperties);
	newPopUpWindow.focus();
}
