function confirm_delete(msg) {
	var answer = confirm(msg);
	if(answer == false) {
		return false;
		}
	return true;
}

function confirmation(msg, target) {
	var answer = confirm(msg);
	if(answer == true) {
		window.location = "http://gandhari.org/" + target;
		}
	return false;
}

function check_pass() {
	var pass1 = trim(document.form.pass1.value);
	var pass2 = trim(document.form.pass2.value);
	if(pass1 != pass2) {
		alert("Your passwords do not match!");
		return false;
	}
	return true;
}

function check_fields() {
	var username = trim(document.signup.username.value);
	var pass1 = trim(document.signup.p1.value);
	var pass2 = trim(document.signup.p2.value);
	var test1 = (username.length < 1) ? true : false;
	var test2 = (pass1.length < 6) ? true : false;
	var test3 = (pass1 != pass2) ? true : false;
	var test4 = (trim(document.signup.name.value).length < 1) ? true : false;
	var test5 = (trim(document.signup.email.value).length < 1) ? true : false;

	if(test1) {
		alert("You did not enter a Username");
	}
	else if(test2) {
		alert("Password does not meet require length");
		document.signup.p1.value = "";
		document.signup.p2.value = "";
		document.signup.p1.focus();
	}
	else if(test3) {
		alert("Passwords do not match, retype passwords");
		document.signup.p1.value = "";
		document.signup.p2.value = "";
		document.signup.p1.focus();
	} 
	else if(test4) {
		alert("You did not enter a Name");
	} 
	else if(test5) {
		alert("You did not enter an Email");
	}
	// if any of the requirements fail, return false
	if(test1 || test2 || test3 || test4 || test5) { return false; }
}

function check_bib_form() {
	if(document.theForm.subject.selectedIndex == 0) {
		alert("Please select one subject!");
		document.theForm.subject.focus();
		return false;
	} else if(document.theForm.type.selectedIndex == 0) {
		alert("Please select a type!");
		document.theForm.type.focus();
		return false;
	} else if(typeof document.theForm.subject2 != "undefined") {
		if(document.theForm.subject2.selectedIndex == 0) {
			alert("Please select one subject!");
			document.theForm.subject2.focus();
			return false;
		} else if(document.theForm.type2.selectedIndex == 0) {
			alert("Please select a type!");
			document.theForm.type2.focus();
			return false;
		}
	}
	
	return true;
}

function sort_generate() {
	var sort_string = "";
	var theType = document.theForm.type.options[document.theForm.type.selectedIndex].value;
	if(theType.length == 0) { theType = "nothing"; }
	var aa = new Object();
	aa["Article"] = "02";
	aa["Book"] = "01";
	aa["Catalogue"] = "07";
	aa["Digital"] = "10";
	aa["Dissertation"] = "04";
	aa["Essay"] = "13";
	aa["Handout"] = "12";
	aa["InCollection"] = "02";
	aa["Lecture"] = "09";
	aa["Letter"] = "14";
	aa["Newspaper"] = "08";
	aa["Notes"] = "15";
	aa["Paper"] = "11";
	aa["Periodical"] = "08";
	aa["Review"] = "03";
	aa["Thesis"] = "05";
	aa["nothing"] = "";

	// remove the "The" and "A" from the beginning of titles
	var articleTitle = document.theForm.article_title.value;
	var aArray = articleTitle.split(" ");
	if(aArray[0] == "A") {
		articleTitle = articleTitle.substring(2, articleTitle.length);
	}
	if(aArray[0] == "The") {
		articleTitle = articleTitle.substring(4, articleTitle.length);
	}

	var pubTitle = document.theForm.publication_title.value;
	var pArray = pubTitle.split(" ");
	if(pArray[0] == "A") {
		pubTitle = pubTitle.substring(2, pubTitle.length);
	}
	if(pArray[0] == "The") {
		pubTitle = pubTitle.substring(4, pubTitle.length);
	}

	if(trim(document.theForm.author_last.value) == "") {
		sort_string = "0_";
		sort_string += trim(document.theForm.date.value);
		sort_string += trim(document.theForm.to_date.value);
		sort_string += "_" + aa[theType];
		sort_string += trim(document.theForm.script_code.value);
		sort_string += articleTitle.substring(0,5);
		sort_string += pubTitle.substring(0,5);
		
	} else {
		//uppercase the author last names
		//*fix for the vons and vans sorting
		var last_name = trim(document.theForm.author_last.value);
		var last_name_ch = last_name.substring(0,1);
		var last_name_theRest = last_name.substring(1,last_name.length);
		last_name = last_name_ch.toUpperCase() + last_name_theRest; 
		sort_string = last_name + "_";
		sort_string += trim(document.theForm.author_first.value) + "_";
		if(trim(document.theForm.add_authors.value) == "") {
			sort_string += "0_";
		} else {
			sort_string += trim(document.theForm.add_authors.value) + "_";
		}
		sort_string += trim(document.theForm.date.value);
		sort_string += trim(document.theForm.to_date.value);
		sort_string += "_" + aa[theType];
		sort_string += trim(document.theForm.script_code.value);
		sort_string += articleTitle.substring(0,5);
		if(pubTitle == "") {
			sort_string += "_";
		} else {
			sort_string += pubTitle.substring(0,5);
		}
	}
	document.theForm.sort.value = sort_string;
}

/* http://www.breakingpar.com/bkp/home.nsf/Doc?OpenNavigator&U=87256B14007C5C6A87256AFB0013C722 */

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
}

