function checkEmail($f) {
// optional 2nd argument: "quiet" suppresses alert messages	
	$str = $f.value;
	filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/;
	if (!filter.test($str) && $str.length > 0) {
		if (arguments[1]!="quiet") {
			errorBubble($f,"error_bubble_small","<b>Sorry!</b><br />There appears to be a problem with your email address.");
		}
		return false;
	} else {
		return true;
	}
}

function checkUsername($f) {
	// optional 2nd argument: "quiet" suppresses alert message	
	$str = $f.value;
	filter=/[^\w\d]/g;
	if (filter.test($str) && $str.length > 0) {
		if (arguments[1]!="quiet") {
			alert("Please enter only letters and numbers here, with no spaces.");
		}
		$f.value = $f.value.replace(/[^\w\d]/g,"");
		return false;
	} else {
		return true;
	}
}

function del_client_confirm() {
	$f = document.forms["admin"].elements;
	$message = "";
	for ($i=0; $i < $f.length; $i++) {
		if ($f[$i].type=="checkbox" && $f[$i].checked) {
			if ($message.length < 1) {
				$message = "Please confirm you really want to delete the following clients and their files:\n";
			}
			$username = $f[$i].name.split("_");
			$username = $username[2];
			$message += " - " + $username + "\n";
		}
	}
	if ($message.length > 0) {
		if (confirm($message)) {
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

function check_del_file($file) {
// disable related form elements
	$f = document.forms["edit"].elements;
	if ($f["del_file_" + $file].checked) {
		$f["new_file_" + $file].disabled = true;
		$f["new_descrip_" + $file].disabled = true;
	} else {
		$f["new_file_" + $file].disabled = false;
		$f["new_descrip_" + $file].disabled = false;
	}
}

function check_del_image($file) {
// disable related form elements	
	$f = document.forms["edit"].elements;
	if ($f["del_file_" + $file].checked) {
		$f["main_image_" + $file].checked = false;
		$f["main_image_" + $file].disabled = true;
	} else {
		$f["main_image_" + $file].disabled = false;
	}
	check_main_image($file);	// make sure at least one image is specified as main image
}

function check_main_image($img) {
// uncheck all other "main image" checkboxes, or check first if none are
	$f = document.forms["edit"].elements;
	if ($f["main_image_" + $img].checked) {
		for ($i=0; $i < $images.length; $i++) {	// uncheck all others
			if ($images[$i] != $img) {
				$f["main_image_" + $images[$i]].checked = false;
			}
		}
	} else {	// check first image not marked for deletion
		// first make sure there's not another one already checked
		$checked = false;
		for ($i=0; $i < $images.length; $i++) {
			if ($f["main_image_" + $images[$i]].checked) {
				$checked = true;
			}
		}
		if (!$checked) {
			for ($i=0; $i < $images.length; $i++) {
				if (!$f["del_file_" + $images[$i]].checked) {
					$f["main_image_" + $images[$i]].checked = true;
					$f["main_image_" + $images[$i]].disabled = false;
					break;
				}
			}
		}
	}
}

function checkForm() {	// for client edit page
	$f = document.forms["edit"].elements;
	$error = false;
	$confirm = false;
	$error_message = "Sorry, there is a problem with your submission:\n\n";
	$confirm_message = "Please confirm the following deletions:\n\n";
	
// check form, issue alert or delete confirmation

// username
	if ($f["new_user"] && $f["new_user"].value.length < 4) {
		$error = true;
		$error_message += " - Please enter a username 4 to 30 characters in length.\n";
	}

// password
	if ($f["passwd"].value.length > 0 || $f["passwd_confirm"].value.length > 0) {
		if ($f["passwd"].value != $f["passwd_confirm"].value || $f["passwd"].value.length < 4) {
			$error = true;
			$error_message += " - Please be sure your password and password confirmation match, and are at least 4 characters in length.\n";
		}
	} else {
		if ($f["new_user"] && ($f["passwd"].value.length < 4 || $f["passwd_confirm"].value.length < 4)) {
			$error = true;
			$error_message += " - Please be sure your password and password confirmation match, and are at least 4 characters in length.\n";
		}
	}
	
// Check for valid email syntax
	if (
		($f["new_user"] && $f["email"].value.length < 5) ||
		!checkEmail($f["email"],"quiet")) {
		$error = true;
		$error_message += " - Please enter a valid email address.\n";
	}

// Delete confirmations
	if (!$error) {
		for ($i=0; $i < $f.length; $i++) {
			if ($f[$i].name.substr(0,4)=="del_" && $f[$i].checked) {
				$confirm = true;
				$confirm_message += " - " + ucfirst($f[$i].name.substring(4, $f[$i].name.indexOf("_", 5))) + " " + $f[$i].name.substr($f[$i].name.indexOf("_",5) + 1) + "\n";
			}
		}	// end for
	} else {	// error
		alert($error_message);
		return false;
	}

	if ($confirm) {
		wait();
		return confirm($confirm_message);
	} else {
		wait();
		return true;
	}
	
}

function get_images() {
	// returns array of image ids
	$f = document.forms["edit"].elements;
	$img = new Array();
	for ($i=0; $i < $f.length; $i++) {	// uncheck all others
		if ($f[$i].name.indexOf("main_image_") > -1) {
			$img[$img.length] = $f[$i].name.substr($f[$i].name.lastIndexOf("_")+1);
		}
	}
	return $img;
}

function projectLoadMedium($img) {
	document.getElementById("project_img_med").src = $img;
	$img = $img.replace(/_m\.jpg/,".jpg");
	document.getElementById("project_img_link").href = $img;
}

function wait() {
	document.getElementById("wait").style.display = "inline";
	setInterval("writeWait()",3000);
}

function writeWait() {
	document.getElementById("wait").innerHTML += ".";
}

// PHP Clones

function ucfirst(str) {
	return str.substr(0,1).toUpperCase() + str.substr(1);
}

function preg_match(regex,str) {
	return regex.test(str);
}

function in_array(needle, haystack) {
	if (typeof(haystack) != "object" || !haystack.push("") || needle.toString().length < 1) {
		return false;
	} else {
		for (i=0; i<haystack.length; i++) {
			if (needle == haystack[i]) {
				return true;
				break;
			}
		}
		return false;
	}
}