// *** general ISBN functions ***
// strip out characters that are not valid in ISBNs
function stripNonISBNChars (input) {
	var result = "";
	result = "" + input;
	result = result.replace(new RegExp("[^0-9xX]","g"),"");
	return result;
}

// validates any ISBN. returns true if valid, default return is false.
function validateISBN (input) {
	var result = false;
	var inputstr = "" + stripNonISBNChars(input);
	if (10 == inputstr.length) {
		result = validateISBN10(input)
	}
	if (13 == inputstr.length) {
		result = validateISBN13(input)
	}
	return result;
}

// convert an ISBN to the alternative length (10 or 13 digits), where possible.
function ConvertISBN (input) {
	var result = "";
	if (validateISBN(input)) {
		input = stripNonISBNChars(input);
		if (10 == input.length) {
			result = ISBN10To13(input);
		} else if (13 == input.length) {
			result = ISBN13To10(input);
		} else {
			result = input;
		}
	} else {
		result = input;
	}
	return result;
}

// *** Z-Cataloguing ISBN functions ***
// add the @or operator so that both are searched for.
function ZConvertISBNSearch (input) {
	var result = "";
	if (validateISBN(input)) {
		input = stripNonISBNChars(input);
		var output = ConvertISBN (input);
		if (input != output) {
			result = "@or " + input + " " + output;
		} else {
			result = input;
		}
	} else {
		result = input;
	}
	return result;
}

// *** functions involving ISBN conversions ***
// convert from a 10-digit ISBN to a 13-digit ISBN
function ISBN10To13 (origISBN) {
  var stringISBN = "" + origISBN;
  var checkdigit = 0;
  var cdtemp = 0;
  var result = "";
  if (10 == stringISBN.length) {
    result = stringISBN.substring(0,9);
    result = "978" + result;
    result += checkDigit13(result);
  } else {
  	result = origISBN;
 	}
  return result;
}
// convert from a 13 digit ISBN to a 10 digit ISBN, if possible
function ISBN13To10 (origISBN) {
  var stringISBN = "" + origISBN;
  var checkdigit = 0;
  var cdtemp = 0;
  var result = "";
  if (13 == stringISBN.length) {
  	if ("978" == stringISBN.substring(0,3)){
			result = stringISBN.substring(3,12);
			result += checkDigit10(result);
		} else {
  		result = origISBN;
 		}
  } else {
  	result = origISBN;
 	}
  return result;
}

// *** functions involving check digit calculations ***
//calculate the check digit for a 10 digit ISBN
function checkDigit10 (input) {
	var cdtemp = 0;
	var checkdigit = 0;
	var result = "";
	for (var i = 0; i < 9; i++) {
		cdtemp = cdtemp + (parseInt(input.charAt(i)) * (10 - i));
	}
	cdtemp = cdtemp % 11;
	checkdigit = 11 - cdtemp;
	if (11 == checkdigit) {
		result = "0";
	} else {
		if (10 == checkdigit) {
			result = "X";
		} else {
			result = checkdigit;
		}
	}
	return result;
}
//calculate the check digit for a 13 digit ISBN
function checkDigit13 (input) {
	var cdtemp = 0;
	var checkdigit = 0;
	var result = "";
	for (var i = 0; i < 12; i++) {
		if (0 == i % 2) {
			cdtemp = cdtemp + parseInt(input.charAt(i));
		} else {
			cdtemp = cdtemp + (parseInt(input.charAt(i)) * 3);
		}
	}
	cdtemp = cdtemp % 10;
	checkdigit = 10 - cdtemp;
	if (10 == checkdigit) {
		result = "0";
	} else {
		result = checkdigit;
	}
	return result;
}

// *** functions involving ISBN validation ***
// validates a 10-digit ISBN. returns true if valid, default return is false.
function validateISBN10 (input) {
	var inputstr = "" + stripNonISBNChars(input);
  var result = false;
 	if (10 == inputstr.length) {
 		var ISBNstart = "" + inputstr.substring(0,9);
 		var checkDigit = "" + inputstr.substring(9,10);
 		if (checkDigit10(ISBNstart) == checkDigit) {
 			result = true;
 		}
 	}
  return result;
}
// validates a 13-digit ISBN. returns true if valid, default return is false.
function validateISBN13 (input) {
	var inputstr = "" + stripNonISBNChars(input);
  var result = false;
 	if (13 == inputstr.length &&
 		( ("978" == inputstr.substring(0,3)) || ("979" == inputstr.substring(0,3)) )
 	) {
 		var ISBNstart = "" + inputstr.substring(0,12);
 		var checkDigit = "" + inputstr.substring(12,13);
 		if (checkDigit13(ISBNstart) == checkDigit) {
 			result = true;
 		}
 	}
  return result;
}


var stripAddUpdISBNPunc = true;
// function for validating ISBN fields that may contain multiple ISBNs
function isbnCheckDigit(theISBN) {
	var input = "";
	var isbn = "";
	var result = "";
	var isValid = true;
	var message = "";

	if (typeof theISBN == "object") {
		input = theISBN.value;
	} else {
		input = unescape("" + theISBN);
	}

	isbn = input.replace(new RegExp("[^0-9xX ]","g"),"");
	// Remove preceding whitespaces
	while (isbn.substring(0,1) == " ") {
		isbn = isbn.substring(1);
	}
	// Remove trailing whitespaces
	while (isbn.substring(isbn.length - 1) == " ") {
		isbn = isbn.substring(0,isbn.length - 1);
	}

	if (validateISBN(isbn)){
		result = stripNonISBNChars(isbn);
	} else {
		if (0 < isbn.length){
			for (var i = 0; i < isbn.length && isValid;) {
				if (0 != result.length) {
					result += " ";
				}
				if (validateISBN(stripNonISBNChars(isbn.substring(i)).substring(0,13))) {
					result += stripNonISBNChars(isbn.substring(i)).substring(0,13);
					//message += "\r\n13 " + stripNonISBNChars(isbn.substring(i)).substring(0,13);

					// increment i for 13 non-space characters
					for (var j = 0; j < 13; i++) {
						if (isbn.charAt(i) == " "){
						} else {
							j++;
						}
					}
				} else if (validateISBN(stripNonISBNChars(isbn.substring(i)).substring(0,10))) {
					result += stripNonISBNChars(isbn.substring(i)).substring(0,10);
					//message += "\r\n10 " + stripNonISBNChars(isbn.substring(i)).substring(0,10);

					// increment i for 10 non-space characters
					for (var j = 0; j < 10; i++) {
						if (isbn.charAt(i) == " "){
						} else {
							j++;
						}
					}
				} else {
					message += "\r\n" + trans["ISBN_FOLLOWING_INVALID"] + ": \r\n" + isbn.substring(i);
					result += isbn.substring(i);
					isValid = false;
				}
			}
		}
		else {
			isValid = false;
		}
	}

	if (!stripAddUpdISBNPunc){
		result = input;
	}

	if (typeof theISBN == "object") {
		if ("" != message) {
			alert(message);
		}
		theISBN.value = result;
	} else {
		if (isValid == false) {
			if (document.gform.gw_title) {
				document.gform.gw_title.value = input;
			} else if (document.gform.gw_TITLE) {
				document.gform.gw_TITLE.value = input;
			}
		} else {
			if (document.gform.gw_isbn) {
				document.gform.gw_isbn.value = result;
			} else if (document.gform.gw_ISBN) {
				document.gform.gw_ISBN.value = result;
			}
			if (document.gform.gw_title) {
				document.gform.gw_title.value = "";
			} else if (document.gform.gw_TITLE) {
				document.gform.gw_TITLE.value = "";
			}
		}
	}
}

function getFirstIsbn(isbnList) {
	var input = "";
	var isbn = "";
	var result = "";
	var isValid = true;
	var message = "";

	input = isbnList;
	isbn = input.replace(new RegExp("[^0-9xX]","g"),"");

	if (validateISBN(isbn)){
		result = stripNonISBNChars(isbn);
	} else {
		for (var i = 0; (i < isbn.length) && ("" == result);i++) {
			if (validateISBN(stripNonISBNChars(isbn.substring(i)).substring(0,13))) {
				result = stripNonISBNChars(isbn.substring(i)).substring(0,13);
			} else if (validateISBN(stripNonISBNChars(isbn.substring(i)).substring(0,10))) {
				result = stripNonISBNChars(isbn.substring(i)).substring(0,10);
			} else {
			}
		}
	}
	return result;
}


function newIsbnEqSearch(isbns){
	isbns = removeDupIsbns(isbns,",");
	var result = "";
	result += "ISBN = ";
	var isbnArr = isbns.split(",");
	for (var i = 0; i < isbnArr.length; i++){
		if (result.length < 185){
			if (0 < i){
				result += ",";
			}
			result += isbnArr[i];
		}
		if ((validateISBN(isbnArr[i])) && (result.length < 185)){
			result += "," + ConvertISBN(isbnArr[i]);
		}
	}
	return result;
}

function newIsbnCoSearch(isbn){
	var result = "";
	if (validateISBN(isbn)){
		var searchPrefix = "ISBN CO ";
		result += "((" + searchPrefix + isbn + ")";
		result += " OR ";
		result += "(" + searchPrefix + ConvertISBN(isbn) + "))";
	}
	return result;
}

function newIsbnFtSearch(isbns){
	var result = "";
	result += "(";
	var isbnArr = isbns.toUpperCase().split(" OR ");
	for (var i = 0; i < isbnArr.length; i++){
		if (0 < i){
			result += " OR ";
		}
		result += isbnArr[i]
		if (validateISBN(isbnArr[i])){
			result += " OR " + ConvertISBN(isbnArr[i]);
		}
	}
	result += ").ISBN.";
	return result;
}

function parseBasicIsbnSearch(orig){
	var origWords = orig.split(" ");
	var result = "";
	if ((3 == origWords.length) && ("ISBN" == origWords[0].toUpperCase())){
		if ("=" == origWords[1]){
			result = newIsbnEqSearch(origWords[2]);
		}
		else if ("CO" == origWords[1].toUpperCase()) {
			result = newIsbnCoSearch(origWords[2]);
		}
	}
	if (0 == result.length) {
		if ((8 < orig.length) && ("(" == orig.substr(0,1)) && (").ISBN." == orig.substr(orig.length - 7).toUpperCase())){
			var FTcontent = orig.substr(1,orig.length-8);
			if (-1 == FTcontent.indexOf(")")){
				result = newIsbnFtSearch(FTcontent);
			}
		}
		else if ((6 < orig.length) && (".ISBN." == orig.substr(orig.length - 6).toUpperCase())) {
			var FTcontent = orig.substr(0,orig.length-6);
			result = newIsbnFtSearch(FTcontent);
		}
	}
	if (0 == result.length) {
		result = orig;
	}
	return result;
}

function removeDupIsbns(inString,seperator){
	var isbns = inString.split(seperator);
	var result = "";
	for (var i = 0; i < isbns.length; i++){
		if ((0 > result.indexOf(isbns[i])) && (0 > result.indexOf(ConvertISBN(isbns[i])))){
			if ("" != result){
				result += seperator;
			}
			result += isbns[i];
		}
	}
	return result;
}

function catalogSearchISBN(textAreaName, destination) {
  //Since textarea contents can have spaces in them, we need to put each line in quotes.
  var myOutput = "";
  var myArray = removeDupIsbns(textAreaName.value,"\r\n").split("\r\n");
  for (var i = 0; i < myArray.length; i++) {
    if (myArray[i] != ""){
      if ("*" == myArray[i].charAt(myArray[i].length - 1)) {
				if (0 < myOutput.length){
					myOutput += " OR ";
				}
        myOutput += '(' + myArray[i].substring(0,myArray[i].length - 1) + '*)';
      } else {
				if (0 < myOutput.length){
					myOutput += " OR ";
				}
				myOutput += '(' + myArray[i] + ')';
        if (validateISBN(myArray[i])){
        	myOutput += ' OR ' + ConvertISBN(myArray[i]) + '';
        }
      }
    }
  }
  destination.value = "(" + myOutput + ").ISBN.";
}