var choices
var preloader = new Image;
preloader.src = "images/buttons/uploadnow.gif";

function imageIs() {
	var material = document.imageForm.imagePath.value;
	if(material == "") {
		alert("Please select an file on your hard drive before you submit the form");
		return false;
	} else {
		return true;
	}
}
function cityIs(alertmsg) {
	var material = document.imageForm.city.value;
	if(material == "") {
		if(alertmsg) {
			alert("You have not filled in the City field");
		}
		return false;
	} else {
		return true;
	}
}
function dontTouchThat() {
	alert("Please press the Browse button to select an image on your hard drive");
	document.imageForm.imagePath.blur();
}
function pcIs(alertmsg) {
	var obj =  document.imageForm.postalcode;
	if(obj.value == "") {
		if(alertmsg) {
			alert("You have not entered a postal/zip code");
		}
		return false;
	} else {
		return true;
	}
}
function titleIs() {
	var obj =  document.imageForm.title;
	if(obj.value == "") {
		alert("You have not filled in the Title field");
		return false;
	} else {
		if(obj.value.length > 30) {
			alert("You have made the title too long");
			return false;
		} else {
			return true;
		}
	}
}
function catIs() {
	if(document.imageForm.category.selectedIndex == 0) {
		alert("You have not selected a category");
		return false;
	} else {
		return true;
	}
}
function filenameIs() {
	if(document.imageForm.newfilename.value == "") {
		alert("You have not entered a filename");
		return false;
	} else {
		return true;
	}
}
function provIs(alertmsg) {
	if(document.imageForm.province.selectedIndex == 0) {
		if(alertmsg) {
			alert("You have not selected a state or province");
		}
		return false;
	} else {
		return true;
	}
}
function dateIs() {
	var menus = ["day","month","year"];
	for(var x=0;x<menus.length;x++) {
		var obj = eval("document.imageForm."+menus[x]);
		if(obj.selectedIndex == 0) {
			alert("You have not completely filled in the date");
			return false;
		}
	}
	return true;
}
function validate(destination, showOthers) {
	if(showOthers) {
		if(imageIs() && titleIs() && catIs() && companyIs()) {
			killTheAnchor("uploadnow", destination);
		}
	} else {
		if(imageIs()) {
			killTheAnchor("uploadnow", destination);
		}
	}
}
function validateSpecial() {
	if(labelIs() && titleIs() && catIs()) {
		document.imageForm.submit();
	}
}
function areYouSure(who) {
	if(confirm("Click OK to block this record from being displayed in the website, or Cancel to cancel")) {
		top.location.href = "block.asp?it="+who;
	}
}
function fNameIs() {
	if(document.imageForm.firstname.value == "") {
		alert("You have not filled in the client's first name");
		return false;
	} else {
		return true;
	}
}
function lNameIs() {
	if(document.imageForm.lastname.value == "") {
		alert("You have not filled in the client's last name");
		return false;
	} else {
		return true;
	}
}
function emailIs() {
	if(document.imageForm.email.value == "") {
		alert("You have not filled in the e-mail field");
		return false;
	} else {
		return true;
	}
}
function phoneIs() {
	if(document.imageForm.homephone.value == "") {
		alert("You have not filled in the client's home phone number");
		return false;
	} else {
		return true;
	}
}
function passIs() {
	if(document.imageForm.password.value == "") {
		alert("You have not filled in a password");
		return false;
	} else {
		return true;
	}
}
function labelIs() {
	if(document.imageForm.label.value == "") {
		alert("You have not filled in an image ID");
		return false;
	} else {
		return true;
	}
}
function addressIs(alertmsg) {
	if(document.imageForm.address.value == "") {
		if(alertmsg) {
			alert("You have not filled in an address");
		}
		return false;
	} else {
		return true;
	}
}
function companyIs() {
	if(document.getElementById("newcompany")) {
		var newcompany = document.getElementById("newcompany").value;
	} else {
		var newcompany = document.getElementById("company").value;
	}
	var newlength = newcompany.length;
	if(document.imageForm.companylist.selectedIndex == 0 && newlength < 1) {
		alert("You have not indicated a company");
		return false;
	} else {
		return true;
	}
}
function worktitleIs() {
	if(document.imageForm.title.value == "") {
		alert("You have not filled in a work title");
		return false;
	} else {
		return true;
	}
}
function questionIs() {
	if(document.imageForm.question.value == "") {
		alert("You have not filled in a secret question");
		return false;
	} else {
		return true;
	}
}
function answerIs() {
	if(document.imageForm.answer.value == "") {
		alert("You have not filled in an answer to the secret question");
		return false;
	} else {
		return true;
	}
}
function projectIs() {
	var status = document.imageForm.clientproject.type;
	switch(status) {
		case "text":
		var that = document.imageForm.clientproject.value;
		if(that == "") {
			alert("You have not typed in a client project");
			return false;
		}
		break;
		case "select-one":
		var that = document.imageForm.clientproject.selectedIndex;
		var choose = document.imageForm.clientproject.options[0].text.indexOf("Choose");
		if(that == 0 && choose != -1) {
			alert("You have not selected a client project");
			return false;
		}
		default:
		return true;
	}
	return true;
}
function emailGood() {
	var at = document.imageForm.email.value.indexOf("@");
	var dot = document.imageForm.email.value.indexOf(".");
	var extension = document.imageForm.email.value.length - 3;
	if(at == -1 || dot == -1 || dot > extension) {
		alert("You have entered an invalid e-mail address");
		return false;
	} else {
		return true;
	}
}
function switchCom(send) {
	var mynumber = document.getElementById("companylist").value;
	var before = mynumber;
	var choose = document.getElementById("companylist").options[0].text.indexOf("Add");
	mynumber = parseInt(mynumber);
	if(before == "nucom") {
		mynumber = 0;
	}
	if(mynumber != 0) {
		var raw = document.imageForm.companylist.options[document.imageForm.companylist.selectedIndex].value;
		raw = raw.split("|");
		var cooked = raw[0];
		document.imageForm.newcompany.value = cooked;
	} else {
		document.imageForm.newcompany.value = "";
	}
	if(send == 1) {
		document.imageForm.submit();
	}
}

function checkAddress() {
	if(!addressIs(false) || !cityIs(false) || !provIs(false) || !pcIs(false)) {
		return false;
	} else {
		return true;
	}
}
function validateOther() {
	if(phoneIs() && emailIs() && fNameIs() && lNameIs() && passIs() && worktitleIs() && companyIs() && emailGood()) {
		if(document.imageForm.companylist.selectedIndex != 0 && document.imageForm.newcompany ) {
			document.imageForm.newcompany.value = document.imageForm.companylist.options[document.imageForm.companylist.selectedIndex].text;
		}
		if(checkAddress()) {
			document.imageForm.submit();
		} else {
			var question = "Failure to fill in the client's mailing address will result in no mailing address showing up in any quotes or invoices you send to this client. Click OK to submit the form anyway, or Cancel to cancel";
			if(confirm(question)) {
				document.imageForm.submit();
			}
		}
	}
}
function validateShort() {
	if(emailIs() && fNameIs() && lNameIs() && questionIs() && answerIs() && emailGood()) {
		document.imageForm.submit();
	}
}
function validateOne() {
	var content = document.box.comment.value;
	if(content == "") {
		alert("You have not written a comment");
	} else {
		document.box.submit();
	}
}
function validateWM() {
	if(imageIs()) {
		document.imageForm.submit();
	}
}
function myID(who) {
	for(f=0;f<document.imageForm.length;f++) {
		var needle = document.imageForm.elements[f].id;
		if(needle == who) {
			return f;
		}
	}
	return "dodo";
}
function fillChoices(filler) {
	choices = filler;
}
function fillNewCP(where) {
	var what = prompt("Enter the name of the new client project and click OK, or click Cancel to cancel", "");
	where.value = what;
}
function cleanAndSpew() {
	document.getElementById("clientproject").selectedIndex = 0;
	var uplimit = document.getElementById("clientproject").length - 1;
	for(var t=uplimit;t>=0;t--) {
		document.getElementById("clientproject").options[t] = null;
	}
	var pos = document.imageForm.companylist.selectedIndex;
	if(document.getElementById("client")) {
		var secretcode = document.getElementById("companylist").options[document.getElementById("companylist").selectedIndex].value;
		document.getElementById("client").value = secretcode;
	}
	if(document.getElementById("company")) {
		var filler = document.getElementById("companylist").options[document.getElementById("companylist").selectedIndex].text;
		document.getElementById("company").value = filler;
	}
	document.getElementById("clientproject").options[0] = new Option("Choose a client project...", "General");
	var mylist = choices[pos];
	if(pos > 0 && mylist.length > 1) {
		for (i=1;i<mylist.length;i++) {
			var init = mylist[i];
			var uplimit = 25;
			if(init.length > uplimit) {
				var label = init.substr(0, (uplimit - 3));
				label += "...";
			} else {
				var label = init;
			}
			document.getElementById("clientproject").options[i] = new Option(label, mylist[i]);
		}
	}
}
function adjustWatermark() {
	var pos = document.imageForm.companylist.selectedIndex;
	var mylist = choices[pos];
	var j = parseInt(mylist[0]);
	if(j == 1) {
		document.getElementById("watermark").style.visibility = "visible";
	} else {
		document.getElementById("watermark").style.visibility = "hidden";
	}
}
function clearTheOptions() {
	var pos = document.imageForm.companylist.selectedIndex;
	var mylist = choices[pos];
	var y = myID("clientproject");
	var el = document.getElementById("imageForm").elements;
	var doofus = el[y].parentNode;
	if(pos > 0 && mylist.length > 1) {
		doofus = el[y].parentNode;
		doofus.removeChild(el[y]);
		var cp = document.createElement("select");
		cp.setAttribute("id", "clientproject");
		doofus.appendChild(cp);
		document.getElementById("clientproject").name = "clientproject";
		document.getElementById("clientproject").options[0] = new Option("Choose a client project...", "General");
		for (i=1;i<mylist.length;i++) {
			document.getElementById("clientproject").options[i] = new Option(mylist[i], mylist[i]);
		}
	} else {
		doofus.removeChild(el[y]);
		var cp = document.createElement("input");
		if(navigator.appName.indexOf("Microsoft") == -1 && navigator.platform.indexOf("Mac") == -1) {
			cp.setAttribute("type", "text");
		}
		cp.setAttribute("id", "clientproject");
		doofus.appendChild(cp);
		document.getElementById("clientproject").name = "clientproject";
	}
	var j = parseInt(mylist[0]);
	if(j == 1) {
		document.getElementById("watermark").style.visibility = "visible";
	} else {
		document.getElementById("watermark").style.visibility = "hidden";
	}
}
function killTheAnchor(who, where, what) {
	if(document.imageForm.imagePath && !document.imageForm.imagePath.value) return false;

	if(document.imageForm.companylist) {
		if(document.imageForm.companylist.selectedIndex == 0) {
			alert("If you do not select a company, this image will be uploaded to the gallery of the default company");
		}
	}
	if(document.imageForm.upload) {
		document.imageForm.upload.src = "images/buttons/"+what+".gif";
	}
	document.imageForm.action = where;
	changeLinkHref(who, "#");
	document.imageForm.submit();
}

function changeLinkHref(id,newHref) {
	if (document.links.length > 0) {
		if (document.getElementById) {
			document.getElementById(id).href = newHref;
		}
		else if (document.all) {
			document.all[id].href = newHref;
		}
	}
}
function showXtraFields() {
	var currStatus = document.getElementById("newcom").style.visibility;
	if(currStatus == "hidden") {
		document.getElementById("newcom").style.visibility = "visible";
	} else {
		document.getElementById("newcom").style.visibility = "hidden";
	}
}
function showNewCP(what) {
	var currStatus = what.selectedIndex;
	if(currStatus == 0) {
		document.getElementById("newcp").style.visibility = "visible";
	} else {
		document.getElementById("newcp").style.visibility = "hidden";
	}
}
function validateEdit(where) {
	if(titleIs() && catIs() && companyIs() && filenameIs()) {
		vrealign("imageForm", where);
	}
}
function vrealign(what,how) {
	var theOtherThing = String(what);
	var that = eval("document."+theOtherThing);
	that.action = how;
	that.submit();
}
