// JavaScript Document

function setupForms(){
	for (var x = 0; x < document.forms.length; x++) {
		if (document.forms[x].className == "jumpstart") {
			document.forms[x].create.onclick = createLink;
            document.forms[x].test.onclick = testLink;
            document.forms[x].highlight.onclick = highlightLink;
            document.forms[x].erase.onclick = eraseLink;
		} else {
			if (document.forms[x].className == "jumpstart ovid") {
				document.forms[x].create.onclick = createLink;
				document.forms[x].test.onclick = testLink;
				document.forms[x].highlight.onclick = highlightLink;
				document.forms[x].erase.onclick = eraseLink;
			}
		}
		if (document.forms[x].highlight != undefined && navigator.appName == "Microsoft Internet Explorer") {
			document.forms[x].highlight.value = "Copy Link";
		}
		if (document.forms[x].result != undefined && document.forms[x].result.value != "" && navigator.appName != "Microsoft Internet Explorer") {
			changeVisibility("copy-note_" + document.forms[x].getAttribute("id").split("_")[1]);
		}
	}
}

function createLink() {
	var url = this.form.url.value;
	var alertMessage = validateField(this.form);
	if (url == "" || (this.form.className == "jumpstart ovid" && this.form.database.value == "")) {
		alert(alertMessage + ".");
	} else {
		if (this.form.result.value == "") {
			changeVisibility("copy-note_" + this.form.getAttribute("id").split("_")[1]);
		}
		this.form.result.value = this.form.linkPrefix.value + url + this.form.linkSuffix.value;
		if (this.form.className == "jumpstart ovid") {
			this.form.result.value = this.form.result.value + this.form.database.value;
		}
	}
}

function testLink() {
	var alertMessage = validateField(this.form);
	if (this.form.result.value == "") {
		alert(alertMessage + " and click \"Create Link\".");
	} else {
		var newwindow = window.open(this.form.result.value);
		if (window.focus) {
			newwindow.focus()
		};
	}
}

function highlightLink() {
	var alertMessage = validateField(this.form);
	if (this.form.result.value == "") {
		alert(alertMessage + " and click \"Create Link\".");
	} else {
		this.form.result.select();
		if(navigator.appName == "Microsoft Internet Explorer"){
			var therange = this.form.result.createTextRange();
			therange.execCommand("Copy");
		}
	}
}

function eraseLink(){
	if (this.form.result.value != "") {
		changeVisibility("copy-note_" + this.form.getAttribute("id").split("_")[1]);
	}
}

function changeVisibility(pTagID){
	if (navigator.appName != "Microsoft Internet Explorer") {
		if (document.getElementById(pTagID).className == "copy-note invisible") {
			document.getElementById(pTagID).className = "copy-note";
		} else if (document.getElementById(pTagID).className == "copy-note") {
			document.getElementById(pTagID).className = "copy-note invisible";
		}
	}
}

function validateField(form){
	var input = form.linkInput.value;
	var inputFirstLetter = input.substring(0,1);
	var article = "";
	if (inputFirstLetter != "A" && inputFirstLetter != "E" && inputFirstLetter != "I" && inputFirstLetter != "O" && inputFirstLetter != "U"){
		article = "a";
	} else {
		article = "an";
	}
	if (form.className == "jumpstart ovid") {
		var alertMessage = "Please select a database from the Database drop-down menu and enter " + article + " " + input + " into the " + input + " box";
	} else {
		var alertMessage = "Please enter " + article + " " + input + " into the " + input + " box";
	}
	return alertMessage;
}
