<!--//

function emailCheck(theField){
	var j = theField.value.length;
	var k = j-1;
	var l = j+1;
	var firstChar = 'N';
	var dot = 0;
	var thingy = 0;
	for(var i = 0; i < j; i++){
		var ch = theField.value.substring(i, i+1);
		if (ch == " "){
			alert('Please enter a valid email address.');
			theField.focus();
			theField.select();
			return false;
		}
		if (i ==0 && ch != "@"){
			firstChar = 'Y';
		}
		if (i !=0 && ch == "@"){
			thingy++;
			l = i+1;
			continue;
		}
		if (i != l && i != k && ch == "." && thingy != 0){
			dot++;
			continue;
		}
	}
	if(j > 0){
		if (firstChar == 'N' || dot == 0 || thingy == 0 || thingy > 1){
			alert('Please enter a valid email address.');
			theField.focus();
			theField.select();
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
}

function validate_alphanumeric(theField) {
	var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_."
	var ok = "yes";
	var temp;
	for (var i=0; i<theField.value.length; i++) {
		temp = "" + theField.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}

	if (ok == "no") {
		alert("Your first and last name, username, and password should contain letters and numbers only.");
		theField.focus();
		theField.select();
		return false;
	}else{
		return true;
	}
}

function verify_form(form,formtype){
//formtype == 1: page = add.php, edit.php
//formtype == 2: page = add2.php
//formtype == 3: page = add3.php, edit2.php
//formtype == 4: page = add2.php (back), edit2.php (back) //TK FINISH
//formtype == 5: page = admin/add_user.php
//formtype == 6: page = admin/add_resource.php, admin/edit_resource.php
//formtype == 61: page = admin/add_group.php, admin/edit_group.php, admin/add_security.php, admin/edit_security.php, admin/add_doctype.php, admin/edit_doctype.php, admin/add_application.php, admin/edit_application.php, 
//formtype == 7: page = admin/add_company.php, admin/edit_company.php
//formtype == 8: page = admin/edit_user.php
//formtype == 9: page = admin/categories.php
//formtype == 10: page = admin/edit_password.php
//formtype == 11: page = admin/add_collection.php
//formtype == 19: page = admin/add_project.php
	if(formtype == 1){
		if(isFieldBlank(form.post_title)){
			alert('Please give this contribution a title.');
			form.post_title.focus();
		}else if(isTextareaBlank(form.post_description)){
			alert('Please give this contribution a description.');
			form.post_description.focus();
		}else{
			form.submit();
		}
	}
	if(formtype == 2){
		if(!radiocheck(form.tem_type)){
			alert('Please select a template type');
		}else{
//			alert('checked');
			form.submit();
		}
	}
	if(formtype == 3){
		if(isTextareaBlank(form.textpart)){
			alert('Please complete the text portion of this contribution.');
			form.textpart.focus();
		}else{
			form.submit();
		}
	}

	if(formtype == 5){
		if(isFieldBlank(form.fname) || isFieldBlank(form.lname) || isFieldBlank(form.email) || isFieldBlank(form.username) || isFieldBlank(form.password) || isFieldBlank(form.cpassword)){
			alert('Please complete the required form items.');
		}else if(form.username.value.length < 3){
			alert('Your username should be at least three characters long.');
			form.username.focus();
		}else if(!validate_alphanumeric(form.username)){
			return;
		}else if(form.password.value.length < 6){
			alert('Your password should be at least six characters long.');
			form.password.focus();
		}else if(!validate_alphanumeric(form.password)){
			return;
		}else if(form.password.value != form.cpassword.value){
			alert('Your passwords do not match.');
			form.cpassword.focus();
		}else{
			form.submit();
		}
	}
	if(formtype == 6){

		var description = form.description.value;
		if(isFieldBlank(form.title)){
			alert('Please enter a title.');
			form.title.focus();
		}else if(isTextareaBlank(form.description)){
			alert('Please enter a description.');
			form.description.focus();
		}else if(isFieldBlank(form.directory)){
			alert('Please enter a directory.');
			form.directory.focus();
		}else{
			var butter = form.directory.value;
			var n = butter.length;
			var a = butter.substring(0,1);
			var b = butter.substring(n-1,n);
			if(a != "/") {
				butter = "/"+butter;
			} 
			if(b != "/"){
				butter = butter+"/";
			} 
			form.directory.value = butter;

			form.submit();
		}
	}
	if(formtype == 61){

		var description = form.description.value;
		if(isFieldBlank(form.title)){
			alert('Please enter a title.');
			form.title.focus();
		}else if(isTextareaBlank(form.description)){
			alert('Please enter a description.');
			form.description.focus();
		}else{
			form.submit();
		}
	}

	if(formtype == 7){
		if(isFieldBlank(form.title)){
			alert('Please enter a title.');
			form.title.focus();
		}else{
			form.submit();
		}
	}
	if(formtype == 8){
		//var h = form.group_id2.value;
		if(isFieldBlank(form.fname)){
			alert('Please enter your first name.');
			form.fname.focus();
		
		}else if(isFieldBlank(form.lname)){
			alert('Please enter your last name.');
			form.lname.focus();

		}else if(isFieldBlank(form.email)){
			alert('Please enter your email.');
			form.email.focus();

		}else if (!isFieldBlank(form.new_company)){
			if (confirm("Are you sure you wish to add a new company?")) {
				form.submit();
			} else { 
			return;
			}
		}else if(isFieldBlank(form.username)){
			alert('Please enter your username.');
			form.username.focus();
		}else if(!validate_alphanumeric(form.username)){
			return;

		}else if (form.company_switch.value == 0){
			if(isFieldBlank(form.new_company) && form.company.selectedIndex == 0){
			alert ('Please enter a new company.');
			form.new_company.focus();
			}
			else{
				form.submit();
			}	
		}else if (form.company_switch.value == 1){
			if(isFieldBlank(form.new_company) && isFieldBlank(form.the_company_name)){
			alert ('Please enter a new company.');
			form.new_company.focus();
			}
			else{
				form.submit();
			}	

		//}else if (h > -2) {
		//	 if (confirm("Are you sure you wish to remove user from this group?\nDoing so will erase all users data and is undo-able.\nIf not, please change the remove group value to No Changes.")) {
		//		form.submit();
		//	} else { 
		//		return;
		//	}

		} else {
			form.submit();
			return;
		}
	}
	
	if(formtype == 9){
		if(isFieldBlank(form.new_sec)){
			alert('Please enter a section title.');
			form.new_sec.focus();
		}else{
			form.submit();
		}
	}
	if(formtype == 10){
		if(isFieldBlank(form.password)){
			alert('Please enter a password.');
			form.password.focus();
		}else{
			form.submit();
		}
	}
	if(formtype == 11){
//to do -- check that description value < 256 chars
		var description = form.description.value;
		if(isFieldBlank(form.title)){
			alert('Please enter a title.');
			form.title.focus();
		}else if(isTextareaBlank(form.description)){
			alert('Please enter a description.');
			form.description.focus();
		}else if(isFieldBlank(form.directory)){
			alert('Please enter a directory.');
			form.directory.focus();
		}else{
			var butter = form.directory.value;
			var n = butter.length;
			var a = butter.substring(0,1);
			var b = butter.substring(n-1,n);
			if(a != "/") {
				butter = "/"+butter;
			} 
			if(b != "/"){
				butter = butter+"/";
			} 
			form.directory.value = butter;

			form.submit();
		}
	}

	if(formtype == 12){
//to do -- check that description value < 256 chars
		var security_title = form.security_title.value;
		if(isTextareaBlank(form.security_title)){
			alert('Please enter a Security Title.');
			form.security_title.focus();
		}else{
			form.submit();
		}
	}

	if(formtype == 13){
		if(isFieldBlank(form.oldpassword)){
			alert('Please enter your old password.');
			form.oldpassword.focus();
		}else if(isFieldBlank(form.newpassword)){
			alert('Please enter your new password.');
			form.newpassword.focus();
		}else if(!validate_alphanumeric(form.newpassword)){
			return;
		}else if(isFieldBlank(form.re_newpassword)){
			alert('Please reenter your new password.');
			form.re_newpassword.focus();
		}else if(form.newpassword.value != form.re_newpassword.value){
			alert('Please reenter your new passwords. Your new passwords do not match.');
			form.newpassword.focus();
		}else{
			form.submit();
		}
	}

	if(formtype == 14){
		if(isFieldBlank(form.fname)){
			alert('Please enter your first name.');
			form.fname.focus();
		
		}else if(isFieldBlank(form.lname)){
			alert('Please enter your last name.');
			form.lname.focus();

		}else if(isFieldBlank(form.email)){
			alert('Please enter your email.');
			form.email.focus();
		}else if(isFieldBlank(form.username)){
			alert('Please enter your username.');
			form.username.focus();
		}else if(!validate_alphanumeric(form.username)){
			return;
		} else {
			form.submit();
		}
	}

	if(formtype == 15){
		if(form.group_id.value==-2){
			alert('There are no more groups to add.');
			form.group_id.focus();
		
		}else if(isFieldBlank(form.duration)){
			alert('Please enter the duration of the subscription.');
			form.duration.focus();

		} else {
			form.submit();
		}
	}
	if(formtype == 16){
		if(isFieldBlank(form.subject)){
			alert('Please enter a subject.');
			form.subject.focus();
		}else if(isFieldBlank(form.message)){
			alert('Please enter a message.');
			form.message.focus();
		} else {
			form.submit();
		}
	}
// This appears to be the form for NEW users.
// 27 May 2004 - Force them to add address, city and country details too
	if(formtype == 17){
		if(isFieldBlank(form.fname) || isFieldBlank(form.lname) || isFieldBlank(form.new_company) || isFieldBlank(form.address1) || isFieldBlank(form.city) || isFieldBlank(form.country) || isFieldBlank(form.email) || isFieldBlank(form.username) || isFieldBlank(form.password) || isFieldBlank(form.cpassword)){
//		if(isFieldBlank(form.fname) || isFieldBlank(form.lname) || isFieldBlank(form.new_company) || isFieldBlank(form.email) || isFieldBlank(form.username) || isFieldBlank(form.password) || isFieldBlank(form.cpassword)){
			alert('Please complete the required form items.');
		}else if(!emailCheck(form.email)){
			return;
		}else if(form.username.value.length < 3){
			alert('Your username should be at least three characters long.');
			form.username.focus();
		}else if(!validate_alphanumeric(form.username)){
			return;
		}else if(form.password.value.length < 6){
			alert('Your password should be at least six characters long.');
			form.password.focus();
		}else if(!validate_alphanumeric(form.password)){
			return;
// 20 Oct 2005 - Make sure person does not enter a ' character as it can mess up the database here.
		}else if(!validate_alphanumeric(form.fname)){
			return;
		}else if(!validate_alphanumeric(form.lname)){
			return;
		}else if(form.password.value != form.cpassword.value){
			alert('Your passwords do not match.');
			form.cpassword.focus();
		}else{
			form.submit();
		}
	}
	if(formtype == 18){
		if(isFieldBlank(form.username)){
			alert('Please enter your username.');
		}else{
			form.submit();
		}
	}
	if(formtype == 19){
		if(isFieldBlank(form.title)){
			alert('Please enter a project title.');
		//}else if(isFieldBlank(form.directory)){
		//	alert('Please enter a directory.');
		//	form.directory.focus();
		}else{
			//var butter = form.directory.value;
			//var n = butter.length;
			//var a = butter.substring(0,1);
			//var b = butter.substring(n-1,n);
			//if(a != "/") {
			//	butter = "/"+butter;
			//} 
			//if(b != "/"){
			//	butter = butter+"/";
			//} 
			//form.directory.value = butter;

			form.submit();
		}
	}
}


function isFieldBlank(theField){
	if(theField.value.length == 0)		
		return true;		
	else			
		return false;	
}

function isTextareaBlank(theField){
	if(theField.value == "")		
		return true;		
	else			
		return false;	
}

function radiocheck(theField){
	typecheck = "no";
	o = 0;
	while(theField[o]){
		if(theField[o].checked){
			typecheck = "yes";
		}
		o++;
	}
	if(o == 0){
		if(theField.checked){
			typecheck = "yes";
		}
	}
	if(typecheck == "no"){
		return false;
	} else {
		return true;
	}
}

function closer(){
	window.Close();
}

function company_check(){
	if(!isFieldBlank(add_user.new_company)){
		//alert (add_user.new_company.value);
		add_user.company_id.selectedIndex = 0;
	}	

}

function company_check1(){
	if(!isFieldBlank(add_user.new_company)){
		add_user.the_company_name.value = "";
	}	

}

function company_check2(){
	
	if(!isFieldBlank(add_user.the_company_name)){
		add_user.new_company.value = "";
	}	

}

function login_check(){
	if(!isFieldBlank(search_users.login)){
		search_users.the_last_name.value = "";
	}	

}

function lastname_check(){
	
	if(!isFieldBlank(search_users.the_last_name)){
		search_users.login.value = "";
	}
}

function a_pm_othercheck(theSelect, theField){
//	alert(theSelect);
//	alert(document.forms[0].video_decoder.name);
	var o = theSelect.selectedIndex;
	var p = theSelect.options[o].value;
	if(p == "0"){
		theField.focus();
	}	
}
//-->
