
	function convertIsbn10toIsbn13(isbn10) {

		// drop check digit from existing ISBN-10
			isbn13 = isbn10.substring(0, 9);
		
		// add prefix '978' to the resulting 9-digit number
			isbn13 = '978' + isbn13;
		
		// recalculate the check digit
			weightingFactors = '131313131313';
			totalSum = 0;

			for (counter = 0; counter < 12; counter++) {
				totalSum = totalSum + (parseInt(isbn13.substring(counter, counter + 1), 10) * parseInt(weightingFactors.substring(counter, counter + 1), 10));
			}

			totalSumDividedByModulus = totalSum / 10;
			remainder = totalSumDividedByModulus - parseInt(totalSumDividedByModulus, 10);
			
			if (remainder == 0) checksum = 0;
			else {
				checksum = Math.round(10 - (remainder * 10));
			}

		// create final isbn
			isbn13 = isbn13 + '' + checksum;
			
			return isbn13;

	}

// new code follows

	function convertISBN(isbnIn,hyphenate) {
	
		// Set default variables and cleanup ISBN
		isbnIn = isbnIn.replace(/[-\s]/g,"").toUpperCase();
		var isbnnum = isbnIn;
		var isbn10exp = /^\d{9}[0-9X]$/;
		var isbn13exp = /^\d{13}$/;
		var isbnlen = isbnnum.length;
		var total = 0;
	
		// Preliminary validation
	
			if (isbnlen == 0) {
				alert("Please enter an ISBN to convert in the first box.");
				return false;
			}
		
			if (!(isbn10exp.test(isbnnum)) && !(isbn13exp.test(isbnnum))) {
				if ((isbnlen != 10) && (isbnlen != 13)) {
					alert("This ISBN is invalid." + "\n" + "It contains " + isbnlen + " characters.");
				}
				else {
					alert("This ISBN is invalid." + "\n" + "It contains invalid characters.");			
				}
				return false;
			}
	
		// Convert a 10-digit ISBN
	
			if (isbnlen == 10) {
				
				// Test for 10-digit ISBNs: formulated number must be divisible by 11
	
				for (var x=0; x<9; x++) { 
					total = total+(isbnnum.charAt(x)*(10-x)); 
				}
		
				// check digit
					z = isbnnum.charAt(9);
					if (z == "X") { z = 10; }
		
				// validate ISBN
					if ((total+z*1) % 11 != 0) {
						// modulo function gives remainder
							z = (11 - (total % 11)) % 11;
							if (z == 10) { z = "X"; }
							alert("This 10-digit ISBN is invalid." + "\n" + "The check digit should be " + z + ".");
							return false;
					}
					else {
						// convert the 10-digit ISBN to a 13-digit ISBN
							isbnnum = "978" + isbnnum.substring(0,9);
							total = 0;
							for (var x=0; x<12; x++) {
								if ((x % 2) == 0) { y = 1; }
								else { y = 3; }
								total = total+(isbnnum.charAt(x)*y);
							}		
							z = (10 - (total % 10)) % 10;
					}		
			}
	
		// Validate & convert a 13-digit ISBN
	
			else { 
				for (var x=0; x<12; x++) {
					if ((x % 2) == 0) { y = 1; }
					else { y = 3; }
					total = total+(isbnnum.charAt(x)*y);
				}
	
				// check digit
					z = isbnnum.charAt(12);
	
				// validate ISBN		
					if ((10 - (total % 10)) % 10 != z) {
						// modulo function gives remainder
							z = (10 - (total % 10)) % 10; 
							alert("This 13-digit ISBN is invalid." + "\n" + "The check digit should be " + z + ".");
							return false;
					}
					else {
						// convert the 13-digit ISBN to a 10-digit ISBN
							if ((isbnnum.substring(0,3) != "978")) {
								alert("This 13-digit ISBN does not begin with \"978\"" + "\n" + "It cannot be converted to a 10-digit ISBN.");
								return false;
							}
							else {
								isbnnum = isbnnum.substring(3,12);
								total = 0;
								for (var x=0; x<9; x++) {
									total = total+(isbnnum.charAt(x)*(10-x));
								}
								z = (11 - (total % 11)) % 11;
								if (z == 10) { z = "X"; } 
							}
					}
		}
	
		if (hyphenate == true) {
			bAlert = true;
			form.isbn_in.value = hyphenate(form.isbn_in.value);
			bAlert = false;
			return(hyphenate(isbnnum+z));
		}
		else {
			return(isbnnum+z);
		} 
	
	}

	function hyphenate(isbn) {
	
		var prefix;
		
		if (isbn.length == 13)	// for 13-digit ISBNs
		{
			prefix = isbn.substring(0,3) + "-";
			isbn = isbn.substring(3,13);
		}
		else { prefix = ''; }
		
		var d = eval(isbn.substring(0,1));	// one digit
		var d2 = eval(isbn.substring(1,3));	// two digits
		var d4 = eval(isbn.substring(1,5));	// four digits
		var objRegExp = "";
		
		switch(d) {
			case 0:
		    case 3:
		    case 4:
		/*
		0 = English-speaking areas
		3 = German-speaking areas
		4 = Japan
		*/
			switch(true) {
				case (d2 < 20):
					objRegExp = /(\d)(\d{2})(\d{6})(\w)/;
				   break;
			   case (d2 < 70):
					objRegExp = /(\d)(\d{3})(\d{5})(\w)/;
					break;
			   case (d2 < 85):
					objRegExp = /(\d)(\d{4})(\d{4})(\w)/;
					break;
			   case (d2 < 90):
					objRegExp = /(\d)(\d{5})(\d{3})(\w)/;
					break;
			   case (d2 < 95):
					objRegExp = /(\d)(\d{6})(\d{2})(\w)/;
					break;
			   case (d2 <= 99):
					objRegExp = /(\d)(\d{7})(\d)(\w)/;
					break;
			   default:
				   break;
			}
			break;
		
			case 1:
		/*
		1 = English-speaking areas
		*/
			switch(true) {
				case (d4 < 1000):
					objRegExp = /(\d)(\d{2})(\d{6})(\w)/;
				   break;
			   case (d4 < 4000):
					objRegExp = /(\d)(\d{3})(\d{5})(\w)/;
					break;
			   case (d4 < 5500):
					objRegExp = /(\d)(\d{4})(\d{4})(\w)/;
					break;
			   case (d4 < 8698):
					objRegExp = /(\d)(\d{5})(\d{3})(\w)/;
					break;
			   case (d4 < 9990):
					objRegExp = /(\d)(\d{6})(\d{2})(\w)/;
					break;
			   case (d4 <= 9999):
					objRegExp = /(\d)(\d{7})(\d)(\w)/;
					break;
			   default:
				   break;
			}
			break;
		
		    case 2:
		/*
		2 = French-speaking areas
		*/
			switch(true) {
				case (d2 < 20):
					objRegExp = /(\d)(\d{2})(\d{6})(\w)/;
				   break;
			   case (d2 < 70):
					objRegExp = /(\d)(\d{3})(\d{5})(\w)/;
					break;
			   case (d2 < 84):
					objRegExp = /(\d)(\d{4})(\d{4})(\w)/;
					break;
			   case (d2 < 90):
					objRegExp = /(\d)(\d{5})(\d{3})(\w)/;
					break;
			   case (d2 < 95):
					objRegExp = /(\d)(\d{6})(\d{2})(\w)/;
					break;
			   case (d2 <= 99):
					objRegExp = /(\d)(\d{7})(\d)(\w)/;
					break;
			   default:
				   break;
			}
			break;
		  
		    case 9:
		/*
		90 = Dutch/Flemish-speaking
		*/ 
		  if (isbn.substring(1,2) == 0) {
			d2 = isbn.substring(2,4);
			switch(true) {
				case (d2 < 20):
					objRegExp = /(\d{2})(\d{2})(\d{5})(\w)/;
				   break;
			   case (d2 < 50):
					objRegExp = /(\d{2})(\d{3})(\d{4})(\w)/;
					break;
			   case (d2 < 70):
					objRegExp = /(\d{2})(\d{4})(\d{3})(\w)/;
					break;
			   case (d2 < 80):
					objRegExp = /(\d{2})(\d{5})(\d{2})(\w)/;
					break;
			   case (d2 <= 81):
					objRegExp = /(\d{2})(\d{6})(\d)(\w)/;
					break;
			   default:
				break;
			}
		  }
		
		/*
		965 = Israel
		*/
		  if (isbn.substring(1,3) == 65) { 
			d2 = isbn.substring(3,5);
			switch(true) {
				case (d2 < 20):
					objRegExp = /(\d{3})(\d{2})(\d{4})(\w)/;
				   break;
			   case (d2 < 70):
					objRegExp = /(\d{3})(\d{3})(\d{3})(\w)/;
					break;
			   case (d2 < 90):
					objRegExp = /(\d{3})(\d{4})(\d{2})(\w)/;
					break;
			   case (d2 <= 95):
					objRegExp = /(\d{3})(\d{5})(\d)(\w)/;
					break;
			   default:
				   break;
			}
		  }  
		
		/*
		981 = Singapore
		*/ 
		  if (isbn.substring(1,3) == 81) {
			d2 = isbn.substring(3,5);
			switch(true) {
				case (d2 < 20):
					objRegExp = /(\d{3})(\d{2})(\d{4})(\w)/;
				   break;
			   case (d2 < 30):
					objRegExp = /(\d{3})(\d{3})(\d{3})(\w)/;
					break;
			   case (d2 <= 40):
					objRegExp = /(\d{3})(\d{4})(\d{2})(\w)/;
					break;
			   default:
				   break;
			}
		  }  
		  else { break; }
		
			   break;
		
			default:
				break;
		}
		
		if (objRegExp != "") {
			  isbn = prefix + isbn.replace(objRegExp, "$1-$2-$3-$4");
		}
		else { 
			if (bAlert == true) { alert("Unable to hyphenate this ISBN!"); }
			isbn = (prefix + isbn).replace(/[-]/g,"");
		}
		return isbn;
	
	}
