/* Javascript for BuybackWeb */

String.prototype.trim = function() {
	return this.replace(/^\s*(.*?)\s*$/, "$1");
};

String.prototype.wellformed_isbn = function() {	// assumes value has been cleaned
	return /^\d{9}[xX\d]$/.test(this) || /^\d{13}$/.test(this);
}

String.prototype.valid_isbn = function() {	// assumes value has been cleaned
	if (!/^0{10}$/.test(this) && /^\d{9}[xX\d]$/.test(this)) {
		var sum = 0;
		for (var i = 0; i < 9; ++i)
			sum += (10 - i) * eval(this.charAt(i));
		sum += (this.charAt(9) == 'x' || this.charAt(9) == 'X') ? 10 : eval(this.charAt(9));
		return (sum % 11) == 0;
	}
	else if (!/^0{13}$/.test(this) && /^\d{13}$/.test(this)) {
		var sum = 0;
		for (var i = 0; i < 13; ++i) {
			var n = eval(this.charAt(i));
			if ((i % 2) == 1) n *= 3;
			sum += n;
		}
		return (sum % 10) == 0;
	}
	else return false;
}

String.prototype.wellformed_email = function() {
	return /^[a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~-]*(\.[a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(([a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9]+)?){1,63}\.)+([a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9]+)?){2,63}$/.test(this);
}


function CanLogIn(frm) {
	/* If already logged in, ok */
	var ctl = frm.FVEMAIL;
	if (!ctl) return true;
	if (ctl.value.trim() == '') {
		alert('Please enter your Email Address.');
		ctl.focus();
		return false;
	}
	ctl = frm.FVPASSWORD;
	if (ctl.value.trim() == '') {
		alert('Please enter your password.');
		ctl.focus();
		return false;
	}
	return true
}

function CanSearch(frm) {
	var ctl = frm.FVISBN;
	var s = ctl.value.trim();
	if (s == '') {
		alert('Please enter one or more ISBNs to search for.\nIf entering multiple ISBNs, separate '
			+ 'them with commas and/or spaces.');
		ctl.focus();
		return false;
	}
	var bad = new Array();
	var ISBNs = s.replace(/\s+|(\s*,\s*)/g, ' ').trim();
	if (ISBNs == '') {
		alert('Please enter one or more ISBNs to search for.\nIf entering multiple ISBNs, separate '
			+ 'them with commas and/or spaces.');
		ctl.focus();
		return false;
	}
	ISBNs = ISBNs.split(' ');

	for (var i = 0; i < ISBNs.length; i++) {
		var isbn = ISBNs[i].replace(/-/g, '');
		if (!isbn.wellformed_isbn()) {
			bad[bad.length] = ISBNs[i];
		}
		else if (!isbn.valid_isbn()) {
			bad[bad.length] = ISBNs[i];
		}
	}
	if (bad.length == ISBNs.length) {
		alert('None of the ISBNs you entered appear to be valid textbook ISBNs.');
		return false;
	}
	if (bad.length) {
		if (bad.length == 1)
			s = '"' + bad[0] + '" is not a valid ISBN - ignore it and search for the others?';
		else {
			s = '';
			for (var i = 0; i < bad.length; i++) {
				if (i == 0) s = '"' + bad[i] + '"';
				else {
					if (i == bad.length-1) s += ' and';
					else s += ',';
					s += ' "' + bad[i] + '"';
				}
			}
			s += ' are not valid ISBNs - ignore them and search for the others?';
		}
		if (!confirm(s)) {
			ctl.focus();
			return false;
		}
	}
	return true;
}

function CanAddBooks(frm) {
	// Can ALWAYS add, even if 
	return true;
}

if (!g_min_buyback) var g_min_buyback = 10.0;	/* warning: means cannot have 0.00 as min */
var AmUpdating = false;

function CanUpdate(frm) {
	if (AmUpdating) {
		// Wants to update
		var idx = 1;
		while (true) {
			var ctl = eval("frm.FVSEQ_" + idx);
			if (!ctl) break;
			if (ctl.checked)
				return true;
			idx++;
		}
		alert("Nothing to do - no books selected for removal.");
		return false;
	}
	else {
		// Wants to check out.
		var total = 0.0, idx = 1;
		while (true) {
			var ctl = eval("frm.price_" + idx);
			if (!ctl) break;
			total += parseFloat(ctl.value);
			idx++;
		}
		if (total < g_min_buyback) {
			alert("There must be at least $" + g_min_buyback.toFixed(2) + " in the cart for a buyback.");
			return false;
		}
	}
	return true;
}

function CanFinalize(frm) {
	with (document.frmAddress) {
		var ctl = FVFIRSTNAME;
		if (ctl.value.trim() == '') {
			ctl.focus();
			alert('Please enter your First Name.');
			return false;
		}
		ctl = FVLASTNAME;
		if (ctl.value.trim() == '') {
			ctl.focus();
			alert('Please enter your Last Name.');
			return false;
		}
		ctl = FVADDRESS1;
		if (ctl.value.trim() == '') {
			ctl.focus();
			alert('Please enter your Address Line 1.');
			return false;
		}
		ctl = FVCITY;
		if (ctl.value.trim() == '') {
			ctl.focus();
			alert('Please enter your City.');
			return false;
		}
		ctl = FVSTATE;
		if (ctl.selectedIndex < 1) {
			ctl.focus();
			alert('Please select your State.');
			return false;
		}
		ctl = FVZIP;
		var s = ctl.value.trim();
		if (s == '') {
			ctl.focus();
			alert('Please enter your Zip Code.');
			return false;
		}
		if (!/^\d{5}(-\d{4})?$/.test(s)) {
			ctl.focus();
			alert('Please enter a valid Zip Code.');
			return false;
		}
		ctl = FVEMAIL;
		var s = ctl.value.trim();
		if (s == '') {
			ctl.focus();
			alert('Please enter your Email Address.');
			return false;
		}
		if (!s.wellformed_email()) {
			ctl.focus();
			alert('Please enter a valid Email Address.');
			return false;
		}
		if (FVPAYMENT && FVPAYMENT.length) {
			if (!FVPAYMENT[0].checked && !FVPAYMENT[1].checked) {
				alert("Please select a Payment Option.");
				return false;
			}
			if (FVPAYMENT[1].checked) {
				ctl = FVPAYPALEMAIL;
				s = ctl.value.trim();
				if (s.length == 0) {
					ctl.focus();
					alert("Please enter Your PayPal Email Address.");
					return false;
				}
				var s2 = FVVERIFYPAYPALEMAIL.value.trim();
				if (s2.length == 0) {
					FVVERIFYPAYPALEMAIL.focus();
					alert("Please Verify Your PayPal Email Address.");
					return false;
				}
				if (s2 != s) {
					FVVERIFYPAYPALEMAIL.focus();
					alert("Verify Your PayPal Email Address does not match Your PayPal Email Address.");
					return false;
				}

				if (FVCAPTCHA) {
					s = FVCAPTCHA.value.trim();
					if (s.length == 0) {
						FVCAPTCHA.focus();
						alert("Please enter the characters from the image.");
						return false;
					}
				}
			}
		}
	}
	return true;
}

function CanContactUs(frm) {
	var ctl = frm.email;
	var s = ctl.value.trim();
	if (s == '') {
		ctl.focus();
		alert('Please enter your Email Address so we can respond to you.');
		return false;
	}
	else if (!s.wellformed_email()) {
		ctl.focus();
		alert('Please enter a valid Email Address.');
		return false;
	}

	ctl = frm.subject;
	if (ctl.value.trim() != '') return true;

	if (frm.message.value.trim() == '') {
		ctl.focus();
		alert('Please enter the subject and/or message you want to contact us about.');
		return false;
	}

	return true;
}

function ShowPaypal(vis) {
	if (!document.getElementById) return;
	var email = document.getElementById('paypalEmail');
	var vemail = document.getElementById('paypalVerifyEmail');
	var captcha = document.getElementById('paypalCaptcha');
	if (!email || !vemail) return;
	if (vis) {
		email.className = '';
		vemail.className = '';
		if (captcha) captcha.className = '';
	}
	else {
		email.className = 'hide';
		vemail.className = 'hide';
		if (captcha) captcha.className = 'hide';
	}
}

