/* --------------------------------------------------------------------------------------------------
Revision history:

 Source: http://www.isbn-international.org/converter/isbngen.js
 
 22.6.2005:
 Version 1.0 delivered by Leitess

 2.7.2005:  Version 1.1.
 Invalid onClick function call removed in tag for SUBMIT-button. Caused "scripting error" in IE (only).
 Corrected code change by JG that produced invalid ISBN default logo file name
 Ready to install online.

 11.8.2005: Version 1.2.
  HTML special characters turned into litterals during transfer turned back to HTML

 27.8.2005: Version 1.3
 Fixed bad validation for pub numbers. Now leading zeros are treated ok, when properly defined in ranges.js:
 Wrong: 00-19 -----  Right: 00-09;10-19

 5.6.2006: Version 1.4
 Removed default author and publisher number. No validation when user entering the page the first time
 Using new files for ranges and languages from Brian

*/

// ------------- ISBN HANDLING FUNCTIONS ;

function GetCheck(mNmbr) {
// generate check digit for ISBN 10;
var mSum = 0;
var mWeight=10;
for (var i=0;i<9;i++) {
		m1 = mNmbr.charAt(i);
		mSum = (mSum + (m1 * mWeight));
		mWeight = mWeight - 1;
}
ret = 11 - (mSum % 11);
if ( ret == 10 ) {
	 ret = "X" ;
} else {
		if (ret == 11) ret = 0;
}
return ret;
}

function GetEAN(mNmbr) {
// get EAN from complete ISBN or ISBN without Check digit;
// remove hyphens if any;
mNmbr = "978" + mNmbr.replace(/-/g, "");
mNmbr = mNmbr.substring(0, 12);
// The array of weights to multiply with;
aWeight = new Array(1,3,1,3,1,3,1,3,1,3,1,3)
var mSum = 0;
var mWeight=10;
for (var i=0;i<12;i++) {
		m1 = mNmbr.charAt(i);
		mSum += m1 * aWeight[i];
}
mRemainder = (mSum % 10);
ret = 10 - mRemainder;
if (ret == 10) { ret = 0 }
return mNmbr.toString() + ret.toString();
}

function GetNextISBN (mIsbnStr) {
// get the next valid ISBN;
// if next ISBN gets out of range - returns empty string
// parameter must be a valid ISBN string, with hyphens - no validation is performed
aOld = mIsbnStr.split("-");
var mNewBookNr = 1 * aOld[2] + 1;
var mNewBookStr = "" + "0000000000" + mNewBookNr.toString();
var mNewBookStr2 = mNewBookStr.substring(mNewBookStr.length - aOld[2].length, 99);
mNewISBN = "" + aOld[0] + "" + aOld[1] + "" + mNewBookStr2;
mNewCheck = GetCheck(mNewISBN);
if ( eval(mNewBookStr2) == 0) {;
	return ""
} else {
	return "" + aOld[0] + "-" + aOld[1] + "-" + mNewBookStr2 + "-" + mNewCheck;
}
}

function GetISBN13(mISBN10) {
// get EAN from complete ISBN-10 or ISBN-10 without check digit
// mISBN must contain hyphens
// remove hyphens for checksum
mNmbr = mISBN10.replace(/-/g, "");
mNmbr = "978" + mNmbr.toString();
mNmbr = mNmbr.substring(0, 12);
// The array of weights to multiply with;
aWeight = new Array(1,3,1,3,1,3,1,3,1,3,1,3)
var mSum = 0;
var mWeight=10;
for (var i=0;i<12;i++) {
		m1 = mNmbr.charAt(i);
		mSum += m1 * aWeight[i];
}
mRemainder = (mSum % 10);
ret = 10 - mRemainder;
if (ret == 10) { ret = 0 };
var xtoken = mISBN10.split("-");
return "978-" + xtoken[0].toString() + "-" + xtoken[1].toString() + "-" + xtoken[2].toString() + "-" + ret.toString();
}


function GetISBN13num(mISBN10) {
// get EAN from complete ISBN-10 or ISBN-10 without check digit
// mISBN must contain hyphens
// remove hyphens for checksum
mNmbr = mISBN10.replace(/-/g, "");
mNmbr = "978" + mNmbr.toString();
mNmbr = mNmbr.substring(0, 12);
// The array of weights to multiply with;
aWeight = new Array(1,3,1,3,1,3,1,3,1,3,1,3)
var mSum = 0;
var mWeight=10;
for (var i=0;i<12;i++) {
		m1 = mNmbr.charAt(i);
		mSum += m1 * aWeight[i];
}
mRemainder = (mSum % 10);
ret = 10 - mRemainder;
if (ret == 10) { ret = 0 };
var xtoken = mISBN10.split("-");
return "978" + xtoken[0].toString() + xtoken[1].toString() + xtoken[2].toString() + ret.toString();
}

//SCREEN ALERT FUNCTIONS ;

function GetISBN13info(xPrefix,xPubnr,xBooknr) {;
// display ISBN Info

var xStart =  xPrefix.toString() + xPubnr.toString() + xBooknr.toString();
var xCheck =  GetCheck(xStart);
var mAlert = "";

if (!isValidISBN10(xStart.toString() + xCheck.toString())) {;
		// check for other errors
	  mAlert = mAlert + l[lang].isbn10_err;
} else {;
		var xISBN = xPrefix.toString() + '-' + xPubnr.toString() + '-' + xBooknr.toString() + '-' + xCheck.toString();
		document.write(GetISBN13(xISBN));
};
return false;
}

function GetISBN13value(xPrefix,xPubnr,xBooknr) {;
// display ISBN Info

var xStart =  xPrefix.toString() + xPubnr.toString() + xBooknr.toString();
var xCheck =  GetCheck(xStart);
var mAlert = "";

if (!isValidISBN10(xStart.toString() + xCheck.toString())) {;
		// check for other errors
	  mAlert = mAlert + l[lang].isbn10_err;
} else {;
		var xISBN = xPrefix.toString() + '-' + xPubnr.toString() + '-' + xBooknr.toString() + '-' + xCheck.toString();
		xISBN = GetISBN13num(xISBN);
};
return xISBN;
}


// VALIDATION FUNCTIONS;
// all isValid....() functions return boolean and produce no screen output (except in case of deadly internal errrors);

function isValidPrefix(mPrefix) {;
// validate prefix, depends on gi object;
return (gi.idlist.indexOf(','+mPrefix+',') != -1)
}

function isValidPubnr (mPrefix, mPubnr) {;
// validate Publisher number;
// prefix is not validated here;
if (!mPubnr.match(/^\d+$/)) {
 	 //"not a number";
 	 return false;
}
marea = "area" + mPrefix;
if (! gi[marea]) {
	window.alert("Internal Error: Group number " + mPrefix + " not defined in ranges.js - cannot validate");
	return false;
};
 //window.alert((!gi[marea].pubrange) || (gi[marea].pubrange==""));

if (!gi[marea].pubrange || gi[marea].pubrange == "") {
//	window.alert("No Publisher Numbers yet defined for this Group number - cannot validate");
	return false;
}
temp = GetValidPubnrCode (marea, mPubnr);
return eval(temp)  ;
};

function GetValidPubnrCode (marea, mPubnr) {
temp1 = gi[marea].pubrange.split(";");
// fixed for correct string comparison, and more fixes added to compare string length;
for (i=0; i<temp1.length; i++) {;
	// count lenght without hyphen - must fit;
		 tempy = temp1[i].replace(/-/, "");
		 tempz = tempy.length /2;
		 mPubnrlen = mPubnr.length;
  temp1[i] = "((x>= '" + temp1[i].replace(/-/,"' && x<= '") + "') && "+ parseFloat(mPubnrlen) + "==" + (parseFloat(tempz)) + ")";
  if ( i < temp1.length -1)  temp1[i] = temp1[i] + " || ";
}
temp2 = temp1.join(" ");
temp3 = temp2.replace(/x/g, "'" + mPubnr.toString() + "'");
return temp3;
}

function isValidISBN10(mNmbr) {
// validate complete ISBN-10 number;
// remove hyphens if any
mNmbr = mNmbr.replace(/-/g,"");
if (mNmbr.length != 10) { return false };
var mSum = 0;
var mWeight=10;
for (var i=0;i<9;i++) {
		m1 = mNmbr.charAt(i);
		mSum = mSum + m1 * mWeight;
		mWeight = mWeight - 1;
}
mCheck = mNmbr.charAt(9);
if (mCheck == "X") {
	 mCheck = 10;
} else {
	mCheck = 0 - mCheck * -1
}
mTotal = (0 + mSum + mCheck);
mReturn = ((mSum + mCheck) % 11);
if (mReturn == 0) {
	 return true;
} else {
	return false;
}
}
