/* Virtual Bookstore */

String.prototype.trim = function() {
	return this.replace(/^\s*(.*?)\s*$/, '$1');
};

String.prototype.isEmail = function() {
	var emailReg = /^[a-zA-Z_0-9\.\-]+\@([a-zA-Z_0-9\-]+\.)+[a-zA-Z_0-9\-]+$/;
	return emailReg.test(this.trim());
};

String.prototype.isVBPassword = function() {
	var regex = /^[a-zA-Z0-9]{5,10}$/;
	return regex.test(this.trim());
};

String.prototype.isCCN = function(kind) {
	var s = this.replace(/[\s-]+/g, '');
	if (s == '') return false;
	if (kind == 'V') {
		if (/^\*{9}\*{3}?\d{4}$/.test(s)) return true; /* unchanged */
		if (! /^4\d{12}\d{3}?$/.test(s) ) return false;
	}
	else if (kind == 'M') {
		if (/^\*{12}\d{4}$/.test(s)) return true; /* unchanged */
		if (! /^5[1-5]\d{14}$/.test(s) ) return false;
	}
	else if (kind == 'D') {
		if (/^\*{12}\d{4}$/.test(s)) return true; /* unchanged */
		if (! /^6011\d{12}$/.test(s) ) return false;
	}
	else if (kind == 'F') {
		if (/^\*{11}\d{4}$/.test(s)) return true; /* unchanged */
		if (! /^3[47]\d{13}$/.test(s) ) return false;
	}
	else return false;
	var sum = 0;
	var even = true;
	for (var i = s.length-1; i >= 0; i--) {
		var ch = s.charAt(i);
		digit = parseInt(ch);
		if (!even) {
			digit *= 2;
			if (digit > 9)	digit -= 9;
		}
		sum += digit;
		even = !even;
	}
	return ((sum % 10) == 0);
};

String.prototype.isCCV = function(kind) {
	var len = (kind == 'F') ? 4 : 3;
	var s = this.trim();
	return (/^\d+$/.test(s) && s.length == len);
};


Number.prototype.toMoney = function() {
	var n = Math.floor(this.valueOf() * 100.0);
	var dollars = (n / 100).toString();
	var cents = (n % 100).toString();
	if (cents.length == 1) cents = '0' + cents;
	return dollars + '.' + cents;
};

function addClass(ctl, className) {
	if (!ctl) return;
	var s = ctl.className.trim();
	if (s.length == 0)
		ctl.className = className;
	else {
		var classes = s.split(' ');
		for (var i = 0; i < classes.length; ++i) {
			if (classes[i] == className) return;
		}
		ctl.className = s + ' ' + className;
	}
}

function removeClass(ctl, className) {
	if (!ctl) return;
	var s = ctl.className.trim();
	if (s.length == 0)
		return;
	var classes = s.split(' ');
	var newClasses = new Array();
	var found = false;
	for (var i = 0; i < classes.length; ++i) {
		if (classes[i] == className)
			found = true;
		else
			newClasses.push(classes[i]);
	}
	if (found) {
		if (newClasses.length > 0)
			ctl.className = newClasses.join(' ');
		else
			ctl.className = '';
	}
}

function hasClass(ctl, className) {
	if (!ctl) return;
	var s = ctl.className.trim();
	if (s.length == 0)
		return false;
	var classes = s.split(' ');
	for (var i = 0; i < classes.length; ++i) {
		if (classes[i] == className) {
			return true;
		}
	}
	return false;
}

function firstChildClass(cParent, className) {
	var node = cParent.firstChild;
	while (node) {
		if (node.nodeType == 1) {
			if (hasClass(node, className)) return node;
			var t = firstChildClass(node, className);
			if (t) return t;
		}
		node = node.nextSibling;
	}
	return null;
}

/**
 * Show/Hide Extra Details
 */
function toggleElementPosition(cell, elem, show, xOffset, yOffset) {
	if (!cell || !elem) return;

	// hide current element
	if (show != 'Y') {
		elem.style.display = 'none';
		return;
	}

	// fix up our offsets
	if (xOffset == undefined)
		var xOffset = 0;
	if (yOffset == undefined)
		var yOffset = 0;

	// get coordinates of current cell
	var coords = findPos(cell);
	var x = (coords[0] + xOffset) + 'px';
	var y = (coords[1] + yOffset) + 'px';

	// now show current element
	elem.style.left = x;
	elem.style.top = y;
	elem.style.display = 'block';

	return;
}

function popup(mylink, windowname, width, height, scrollbars) {
	if (! window.focus) return;
	var href;
	if (typeof(mylink) == 'string') href=mylink;
	else href=mylink.href;
	window.open(href, windowname, 'width=' + width + ',height=' + height + ',scrollbars=' + scrollbars);
	//return false;
}

function DoFail(ctl, msg) {
	ctl.focus();
	alert(msg);
	return false;
}

var alreadySubmitted = false; 

function LogInLoaded() {
	if (document.frmLogin && document.frmLogin.fvEmailLogIn && document.frmLogin.fvLoginPassword
		&& document.frmLogin.fvEmailLogIn.value.trim() != '') {
		document.frmLogin.fvLoginPassword.focus();
		return;
	}
	if (document.frmLoginNew && document.frmLoginNew.fvEmailNew) {
		document.frmLoginNew.fvEmailNew.focus();
		return;
	}
	if (document.frmLogin && document.frmLogin.fvEmailLogIn) {
		document.frmLogin.fvEmailLogIn.focus();
		return;
	}
}

function CanLogIn(frm) {
	if (alreadySubmitted) {
		alert("Please wait while we gather your information.");
		return false;
	}
	var s = frm.fvEmailLogIn.value.trim();
	if (s == '')
		return DoFail(frm.fvEmailLogIn, "You must enter your Email Address.");
	if (!s.isEmail())
		return DoFail(frm.fvEmailLogIn, "Your Email Address must be a valid email address.");
	s = frm.fvLoginPassword.value.trim();
	if (s == '')
		return DoFail(frm.fvLoginPassword, "You must enter your Password.");
	if (!s.isVBPassword())
		return DoFail(frm.fvLoginPassword, "Your Password must be between 5 and 10 alphanumeric characters.\n"
			+ "Please do not use spaces or punctuation - just letters and/or numbers.");
	alreadySubmitted = true;
	return true;
}

function CanLogInNew(frm) {
	if (alreadySubmitted) {
		alert("Please wait while we process your request.");
		return false;
	}
	var s = frm.fvEmailNew.value.trim();
	if (s == '')
		return DoFail(frm.fvEmailNew, "You must enter your Email Address.");
	if (!s.isEmail())
		return DoFail(frm.fvEmailNew, "Your Email Address must be a valid email address.");
	alreadySubmitted = true;
	return true;
}

function CanGetPassword(ctl) {
	var s = ctl.value.trim();
	if (s == '')
		return DoFail(ctl, "You must enter your email Address.");
	if (!s.isEmail())
		return DoFail(ctl, "Your email Address must be a valid email address.");
	return true;
}

function EmailOkay(ctl) {
	var s = ctl.value.trim();
	if (s == '')
		return DoFail(ctl, "You must enter your email Address.");
	if (!s.isEmail())
		return DoFail(ctl, "Your email Address must be a valid email address.");
	return true;
}

function StudentIDOkay(ctl) {
	var regex = /^.{1,16}$/;
	var s = ctl.value.trim();

	if (s == '')
		return DoFail(ctl, "You must enter your Student ID.");
	if (!regex.test(s))
		return DoFail(ctl, "Your Student ID must be 1-16 letters and/or numbers.");
	return true;
}

function CanBuybackSearch(frm) {
	var ctl = frm.isbns;
	if (ctl.value.trim() == '') {
		ctl.focus();
		alert('You have not entered any ISBNs to search for.');
		return false;
	}
	return true;
}

function CanUpdateBuybackCart(frm) {
	if (alreadySubmitted) {
		alert("Please wait while we process your request.\n"
			+ "If you got here by backing up, try using the My Buyback Cart "
			+ "menu item and make your updates there.");
		return false;
	}
	var ctl;
	var idx = 1;
	while (ctl = eval("frm.seq_" + idx)) {
		if (ctl.checked) {
			alreadySubmitted = true;
			return true;
		}
		idx++;
	}
	if (idx == 1)
		alert("Your cart is empty.");
	else
		alert("There is nothing to update.\n"
			+ "If you want to remove items from your cart, check the checkbox to the left of the item(s) and click UPDATE.");
	return false;
}

function CanBuybackFromResults(frm) {
	if (alreadySubmitted) {
		alert("Please wait while we process your request.\n"
			+ "If you got here by backing up, try using the My Buyback Cart "
			+ "menu item and search for your ISBN(s) again.");
		return false;
	}
	/* Even if no items checked, let them post to get back to the search form */
	alreadySubmitted = true;
	return true;
}

function CanBuybackFromHistory(frm) {
	if (alreadySubmitted) {
		alert("Please wait while we process your request.\n"
			+ "If you got here by backing up, try using the My Previous Purchases "
			+ "menu item.");
		return false;
	}
	/* nothing to check */
	alreadySubmitted = true;
	return true;
}

function CanAddQuotes(frm) {
	alert("Not yet.");
	return false;
}

function CanBuybackCheckout(frm) {
	var subtotal = parseFloat(frm.subtotal.value);
	if (minBuyback > 0.0 && subtotal < minBuyback) {
		if (!document.frmCart.seq_1)
			alert("Your buyback cart is empty.");
		else
			alert("You must have at least $" + minBuyback.toMoney() + " in your cart to check out.");
		return false;
	}
	return true;
}

function FocusBuybackMailing() {
	with (document.frmBBmailing) {
		if (FVFIRSTNAME.value.trim() == '')
			FVFIRSTNAME.focus();
		else if (FVLASTNAME.value.trim() == '')
			FVLASTNAME.focus();
		else if (FVADDRESS1.value.trim() == '')
			FVADDRESS1.focus();
		else if (FVCITY.value.trim() == '')
			FVCITY.focus();
		else if (FVSTATE.selectedIndex < 1)
			FVSTATE.focus();
		else if (FVZIPPOSTAL.value.trim() == '')
			FVZIPPOSTAL.focus();
		else {
			if (document.frmBBmailing.FVEMAIL) {
				if (FVEMAIL.value.trim() == '')
					FVEMAIL.focus();
				else
					FVPASSWORD.focus();
			}
			else
				FVACCEPT.focus();
		}
	}
}

function CanBuybackMailing(frm) {
	if (alreadySubmitted) {
		alert("Please wait while we process your request.\n"
			+ "If you got here by backing up, this message is just here to protect "
			+ "you from submitting your quote multiple times.");
		return false;
	}
	var ctl = frm.FVFIRSTNAME;
	if (ctl.value.trim() == '') {
		alert("Please enter your First Name.");
		ctl.focus();
		return false;
	}
	ctl = frm.FVLASTNAME;
	if (ctl.value.trim() == '') {
		alert("Please enter your Last Name.");
		ctl.focus();
		return false;
	}
	ctl = frm.FVADDRESS1;
	if (ctl.value.trim() == '') {
		alert("Please enter your street address.");
		ctl.focus();
		return false;
	}
	ctl = frm.FVCITY;
	if (ctl.value.trim() == '') {
		alert("Please enter your City.");
		ctl.focus();
		return false;
	}
	ctl = frm.FVSTATE;
	if (ctl.selectedIndex < 1) {
		alert("Please enter your State.");
		ctl.focus();
		return false;
	}
	ctl = frm.FVZIPPOSTAL;
	var s = ctl.value.trim();
	if (s == '') {
		alert("Please enter your Zip Code.");
		ctl.focus();
		return false;
	}
	if (!/^\d{5}([-]\d{4})?$/.test(s)) {
		alert("Please enter either a 5-digit number or 5+4 (eg: 12345-1234) for your Zip Code.");
		ctl.focus();
		return false;
	}
	ctl = frm.FVGRADYEAR;
	if (ctl.selectedIndex < 1) {
		alert("Please enter your Expected Graduation Year.");
		ctl.focus();
		return false;
	}
	ctl = frm.FVEMAIL;
	if (ctl) {
		s = ctl.value.trim();
		if (s == '') {
			alert("Please enter your Email Address.");
			ctl.focus();
			return false;
		}
		if (!s.isEmail()) {
			alert("Please enter a valid Email Address.");
			ctl.focus();
			return false;
		}
		ctl = frm.FVPASSWORD;
		s = ctl.value.trim();
		if (s == '') {
			alert("Please enter a Password for your account.");
			ctl.focus();
			return false;
		}
		if (!/^[a-zA-Z0-9]{5,10}$/.test(s)) {
			alert("The Password for your account must be 5-10 letters and/or numbers.");
			ctl.focus();
			return false;
		}
		if (s != frm.FVPASSWORD2.value.trim()) {
			alert("Please Confirm your password by entering the same value you did for Password.");
			frm.FVPASSWORD2.value = '';
			frm.FVPASSWORD2.focus();
			return false;
		}
	}

	if (frm.FVPAYMENT) {
		if (frm.FVPAYMENT[0] && frm.FVPAYMENT[0].type == 'radio') {
			if (!frm.FVPAYMENT[0].checked && !frm.FVPAYMENT[1].checked) {
				alert("Please select a Payment Option.");
				frm.FVPAYMENT[0].focus();
				return false;
			}
			else if (frm.FVPAYMENT[1].checked) {
				ctl = frm.FVPAYPALEMAIL;
				s = ctl.value.trim();
				if (s == '') {
					alert("Please enter your PayPal Email Address.");
					ctl.focus();
					return false;
				}
				else if (!s.isEmail()) {
					alert("Please enter a valid PayPal Email Address.");
					ctl.focus();
					return false;
				}

				ctl2 = frm.FVVERIFYPAYPALEMAIL;
				s2 = ctl2.value.trim();
				if (s2 == '') {
					alert("Please Verify your PayPal Email Address.");
					ctl2.focus();
					return false;
				}
				else if (s != s2) {
					alert("PayPal Email Addresses do not match. Please re-enter and verify your PayPal Email Address.");
					ctl.focus();
					return false;
				}
			}
		}
	}

	ctl = frm.FVACCEPT;
	if (!ctl.checked) {
		alert('You must accept the Terms and Conditions to sell books to Your School\'s Official Bookstore.\n' +
			'You accept by checking the "I accept the Terms and Conditions" checkbox before clicking Submit.');
		return false;
	}
	alreadySubmitted = true;
	return true;
}

function CookiesEnabled() {
	var tmp = new Date();
	tmp.setTime(tmp.getTime() + 24*60*60*1000);
	document.cookie = 'cookie-test=true; expires=' + tmp.toGMTString() + '; path=/';
	tmp = document.cookie.split(';');
	var tmp2 = new Date();
	tmp2.setTime(tmp2.getTime() - 24*60*60*1000);
	document.cookie = 'cookie-test=; expires=' + tmp2.toGMTString() + '; path=/';
	for (var i = 0; i < tmp.length; ++i) {
		var c = tmp[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf('cookie-test=') == 0) return true;
	}
	return false;
}

/* Do not double-post when submitting adoptions
 */
function CanAdopt(frm) {
	if (alreadySubmitted) {
		alert("Please wait while we process your request.\n"
			+ "If you got here by backing up, try using the Select Course(s) menu item instead.");
		return false;
	}
	alreadySubmitted = true;
	return true;
}

/* Do not double-post when submitting supplements
 */
function CanSupplement(frm) {
	if (alreadySubmitted) {
		alert("Please wait while we process your request.\n"
			+ "If you got here by backing up, try using the Select Course(s) menu item instead.");
		return false;
	}
	alreadySubmitted = true;
	return true;
}

/* Do what needs to be done when selecting given fillopt and shiptype. 
 *
 * Note that either parm can be null and the script will use the 
 * *currently selected* value for that. The currently selected 
 * value will NOT be the one the user is attempting to select, 
 * but rather the one that was selected prior to the event 
 * that calls this firing. 
 *
 */ 
function shipping_page_set_shiptype(fillopt, shiptype){

	// we weren't passed a fillopt parm. figure it out from selected value. 
	if( fillopt == null ){ 
		if( document.frmShipping.FILLOPTION ){
			if( document.frmShipping.FILLOPTION[0].checked )
				fillopt = 'D';
			else
				fillopt = 'A'; 
		}
		else{
			fillopt = 'O'; 
		}
	}
	
	// we weren't passed a shiptype parm. figure it out from selected value. 
	if(shiptype == null){
		if( document.frmShipping.SHIPTYPE ){
			for( var i=0; i < document.frmShipping.SHIPTYPE.length; i++ ){
				if(document.frmShipping.SHIPTYPE[i].checked){
					shiptype=document.frmShipping.SHIPTYPE[i].value;
					break;
				}
			}
		}
	}

	// if we have a fillopt and a shiptype (or no shipping), set value in order summary section.
	var ship_cost = 0;
	if( fillopt != null && (shiptype != null || ar_ship_cost.length == 0 ) ){

		if( shiptype != null ){
			ship_cost = ar_ship_cost[fillopt][shiptype];
		}
	}

	var elem=document.getElementById('shippingamountdisplay');
	if(elem){
		elem.innerHTML='$' + ship_cost.toFixed(2);
	}

	var total = subtotal + tax_amount + ship_cost;
	var total_voucherable = 0; 

	if( subtotal_voucherable != null ){

		var total_voucherable = subtotal_voucherable + tax_voucherable; 

		if (fillopt != null && shiptype != null ){
			if( ar_ship_covered[fillopt][shiptype] ){
				total_voucherable += shipping_amount_voucherable(ship_cost);
			}
		}
	}
	
	elem=document.getElementById('ordertotaldisplay');
	if(elem){
		elem.innerHTML='$' + total.toFixed(2);
	}

	elem=document.getElementById('vch_total');
	if(elem){
		elem.innerHTML='$' + total_voucherable.toFixed(2); 
	}

	elem=document.getElementById('amount_due');
	if(elem){

		if (total_voucherable >= total){
			elem.innerHTML='$0.00';

			document.getElementById('checkout_address_static').style.display = 'none'; 
			document.getElementById('bill_address_inputs').style.display = 'none'; 
			document.frmShipping.BILLINGINPUTS.value = '0';
			document.getElementById('cc_info').style.display = 'none';
		}else{
			var amt_due = total - total_voucherable; 
			elem.innerHTML='$' + amt_due.toFixed(2);
			
			if( ! have_billing_adr ){
				document.getElementById('checkout_address_static').style.display = 'none'; 
				document.getElementById('bill_address_inputs').style.display = 'block'; 
				document.frmShipping.BILLINGINPUTS.value = '1';
			}else{ 
				document.getElementById('bill_address_inputs').style.display = 'none'; 
				document.frmShipping.BILLINGINPUTS.value = '0';
				document.getElementById('checkout_address_static').style.display = 'block'; 
			}
			
			document.getElementById('cc_info').style.display = 'block';
		}
	}

	return true;
}

// assumes it's a type that can be covered. 
function shipping_amount_voucherable(ship_cost){

	if( typeof(ship_cost) == 'string' ) ship_cost = parseFloat(ship_cost);
	
	var covered=0;
	var ship_covered = 0;
	var i = 0; 
	for (i = 0; i < ar_vch_info.length ;i++ ){
		if( ar_vch_info[i]['PCT'] ){
			if(subtotal>0) 
				factor=(subtotal_voucherable/ar_vch_info[i]['PCT'])/subtotal;
			else 
				factor=1;
			covered=ship_cost*ar_vch_info[i]['PCT']*factor;
			covered=Math.round(covered*100)/100;
			if( ( ar_vch_info[i]['MAX'] > 0 ) && covered + ar_vch_scoreboard['pct_used'] > ar_vch_info[i]['MAX'] ) 
				covered = ar_vch_info[i]['MAX'] - ar_vch_scoreboard['pct_used'];
			ship_cost -= covered;
			ship_covered += covered; 
		}
		else{
			factor =(subtotal>0)?subtotal_voucherable / subtotal:1;
			covered=Math.round((ship_cost*factor)*100)/100; 
			if( !is_proxy )
				if( covered + ar_vch_scoreboard['reg_used'] > ar_vch_info[i]['AMT'] ) 
					covered = ar_vch_info[i]['AMT'] - ar_vch_scoreboard['reg_used'];
			ship_cost -= covered;
			ship_covered += covered; 
		}
	}
	
	// hmmm, something above causes ship_covered to be a string. should not, don't know what, so casting here. 
	ship_covered = parseFloat(ship_covered); 
	return ship_covered; 
}

function shipping_page_set_fillopt(fillopt){

	// display appropriate prices in ship types section. 
	for (shiptype in ar_ship_cost[fillopt] ){
		var elem=document.getElementById("cost_display_" + shiptype ); 
		if(elem){
			elem.innerHTML='$' + ar_ship_cost[fillopt][shiptype].toFixed(2);
		}

		elem=document.getElementById("delivery_display_" + shiptype ); 
		if(elem && allow_delivery_display==true){
			if(fillopt == 'D')
				elem.style.display = 'none';
			else
				elem.style.display = 'inline';
		}
	}

	// if ship type already selected, need to update summary shipping cost. 
	shipping_page_set_shiptype(fillopt,null); 

	return true;
}

function shipping_page_init(need_payment_info,need_billing_adr,show_static_billing){

	// hide appropriate divs based on parms
	if( ! need_payment_info ){
		document.getElementById('cc_info').style.display = 'none'; 
	}

	if( !show_static_billing ){
		document.getElementById('checkout_address_static').style.display = 'none';  
	}

	if( need_billing_adr ){
		document.getElementById('checkout_address_static').style.display = 'none'; 
	}else{
		document.getElementById('bill_address_inputs').style.display = 'none'; 
		document.frmShipping.BILLINGINPUTS.value = '0';
	}

	// do we have a fill option and/or a fill option checked? 
	// if so, set price display values for prices. 
	if( document.frmShipping.FILLOPTION ){
		if( document.frmShipping.FILLOPTION[0].checked )
			shipping_page_set_fillopt('D');
		else
			shipping_page_set_fillopt('A'); 
	}else{
		shipping_page_set_fillopt('O'); 
	}
	
	if( document.frmShipping.BILLSHIPPERACCOUNT ){
		if( ! document.frmShipping.BILLSHIPPERACCOUNT[1].checked ){
			document.getElementById('shipper_account').style.display='none'; 
		}
	}
	return true;

}

function set_shipping_account_vis( newvis ){

	document.getElementById('shipper_account').style.display=newvis;
	if (newvis != 'none' && document.frmShipping.SHIPTYPE){
		if(document.frmShipping.SHIPTYPE.length){
			for( i=0; i < document.frmShipping.SHIPTYPE.length; i++ ){
				if(document.frmShipping.SHIPTYPE[i].checked){
					document.frmShipping.SHIPTYPE[i].checked = false;
				}
				document.frmShipping.SHIPTYPE[i].disabled=true;
			}
		}
		else{
			if(document.frmShipping.SHIPTYPE.checked){
				document.frmShipping.SHIPTYPE.checked = false;
			}
			document.frmShipping.SHIPTYPE.disabled=true;
		}
		shipping_page_set_shiptype(null, null);
	}
	else if(document.frmShipping.SHIPTYPE){
		if(document.frmShipping.SHIPTYPE.length){
			for( i=0; i < document.frmShipping.SHIPTYPE.length; i++ ){
				document.frmShipping.SHIPTYPE[i].disabled=false;
			}
		}
		else{
			document.frmShipping.SHIPTYPE.disabled=false;
		}
	}
}

/**
 * Validate Address form data. 
 * 
 * (could smarten up a little with real validation of US ZIP codes). 
 */ 
function validate_frmAddress(frm){

	if( ! frm ) return false;

	if( frm.BILLINGINPUTS ){

		var checkBillCity = frm.BILLINGCITY.value.trim();
		checkBillCity = checkBillCity.toUpperCase();
		var checkBillState = frm.BILLINGSTATE[frm.BILLINGSTATE.selectedIndex].value.trim();

		if(frm.BILLINGFIRSTNAME.value.trim() == '' ){
			return DoFail( frm.BILLINGFIRSTNAME, "Please enter a Billing Address First Name" );
		}
		if(frm.BILLINGLASTNAME.value.trim() == '' ){
			return DoFail( frm.BILLINGLASTNAME, "Please enter a Billing Address Last Name" );
		}
		if(frm.BILLINGADDRESS1.value.trim() == '' ){
			return DoFail( frm.BILLINGADDRESS1, "Please enter a Billing Address Line 1" );
		}
		if(frm.BILLINGCITY.value.trim() == '' ){
			return DoFail( frm.BILLINGCITY, "Please enter a Billing Address City" );
		}
		if(frm.BILLINGCOUNTRY[frm.BILLINGCOUNTRY.selectedIndex].value.trim() == '' ){
			return DoFail( frm.BILLINGCOUNTRY, "Please select a Billing Address Country" );
		}
		if(checkBillCity == 'APO' || checkBillCity == 'FPO'){
			if(checkBillState!='AA' && checkBillState!='AE' && checkBillState!='AP'){
				return DoFail( frm.BILLINGSTATE, "Please select Billing Address State of Armed Forces (AA, AE, or AP) for APO/FPO addresses" );
			}
			if(frm.BILLINGCOUNTRY[frm.BILLINGCOUNTRY.selectedIndex].value != 'US'){
				return DoFail( frm.BILLINGCOUNTRY, "Please select Billing Address Country of United States for APO/FPO addresses" );
			}
		}
		else if(frm.BILLINGCOUNTRY[frm.BILLINGCOUNTRY.selectedIndex].value == 'US'){ 
			if(checkBillState==''){
				return DoFail( frm.BILLINGSTATE, "Please select a Billing Address State" );
			}
			if(frm.BILLINGZIP.value.trim() == '' ){
				return DoFail( frm.BILLINGZIP, "Please enter a Billing Address Zip/Postal Code" );
			}
		}
		else{ 
			if(checkBillState!=''){
				return DoFail( frm.BILLINGSTATE, "Please do NOT select a Billing Address State when Billing Address Country is not United States" );
			}
			if(frm.BILLINGZIP.value.trim() == '' ){
				/*
				if(!confirm("Billing Address Zip/Postal Code is missing. Continue without one?")){
					frm.BILLINGZIP.focus(); 
					return false; 
				}
				*/
				return DoFail( frm.BILLINGZIP, "Please enter a Billing Address Zip/Postal Code" );

			}
		} 
		
		var billhomephone = frm.BILLINGPHONEHOME.value.replace(/[^\d]+/g, '');
		if( billhomephone == '' )
			return DoFail( frm.BILLINGPHONEHOME, "Please enter a Billing Address Home Phone" );
		else{
			if(billhomephone.length > 11 )
				return DoFail( frm.BILLINGPHONEHOME, "Maximum length for Billing Address Home Phone is eleven digits" );
		}

		var billworkphone = frm.BILLINGPHONEWORK.value.replace(/[^\d]+/g, '');
		if(billworkphone.length > 11 )
			return DoFail( frm.BILLINGPHONEWORK, "Maximum length for Billing Address Work Phone is eleven digits" );

		var billworkphoneext = frm.BILLINGWORKEXT.value.replace(/[\(\) -]/g, '');  // This matches server-side validation. Non-sensical, maybe, but consistently so. 
		if( ! /^\d{0,5}$/.test(billworkphoneext) )
			return DoFail( frm.BILLINGWORKEXT, "Maximum length for Billing Address Work Phone Extention is five digits" );
	}

	if( frm.SHIPPINGINPUTS ){
		
		var validate_shipping = (frm.SHIPPINGINPUTS && ! frm.SHIPPINGS2B);
		var school_shipping = false;
		var store_shipping = false;

		if( frm.SHIPPINGS2B ){
			if( frm.SHIPPINGS2B[0] && frm.SHIPPINGS2B[0].type == 'radio' ){

				var ctrl = frm.SHIPPINGS2B;
				var shippingSelected = false;
				var radioCount = ctrl.length;

				for (var i=0; i < radioCount; i++) {
					if (ctrl[i].checked) {
						if (ctrl[i].value == 'sameasbill')
							validate_shipping = false;
						else if (ctrl[i].value == 'unlikebill')
							validate_shipping = true;
						else if (ctrl[i].value == 'school') {
							validate_shipping = true;
							school_shipping = true;
						}
						else if (ctrl[i].value == 'store') {
							validate_shipping = true;
							store_shipping = true;
						}

						shippingSelected = true;
						break;
					}
				}

				if (!shippingSelected)
					return DoFail( ctrl[0], "Please select a Shipping Location." );

			}
			else if( frm.SHIPPINGS2B.type == 'checkbox' ){
				validate_shipping = (frm.SHIPPINGS2B && ! frm.SHIPPINGS2B.checked);
			}
		}

		if( validate_shipping ){ // shipping to shipping address. 

			var checkShipCity = frm.SHIPPINGCITY.value.trim();
			checkShipCity = checkShipCity.toUpperCase();
			var checkShipState = frm.SHIPPINGSTATE[frm.SHIPPINGSTATE.selectedIndex].value.trim();

			if (school_shipping) {
				if(frm.SHIPPINGSTOREFIRSTNAME.value.trim() == '' )
					return DoFail( frm.SHIPPINGSTOREFIRSTNAME, "Please enter a Shipping Address First Name" );
				if(frm.SHIPPINGSTORELASTNAME.value.trim() == '' )
					return DoFail( frm.SHIPPINGSTORELASTNAME, "Please enter a Shipping Address Last Name" );
			}
			else if(!store_shipping) {
				if(frm.SHIPPINGFIRSTNAME.value.trim() == '' )
					return DoFail( frm.SHIPPINGFIRSTNAME, "Please enter a Shipping Address First Name" );
				if(frm.SHIPPINGLASTNAME.value.trim() == '' )
					return DoFail( frm.SHIPPINGLASTNAME, "Please enter a Shipping Address Last Name" );
			}

			if (!store_shipping && !school_shipping) {
				if(frm.SHIPPINGADDRESS1.value.trim() == '' ){
					return DoFail( frm.SHIPPINGADDRESS1, "Please enter a Shipping Address Line 1" );
				}
				if(checkShipCity == '' ){
					return DoFail( frm.SHIPPINGCITY, "Please enter a Shipping Address City" );
				}
				if(frm.SHIPPINGCOUNTRY[frm.SHIPPINGCOUNTRY.selectedIndex].value.trim() == '' ){
					return DoFail( frm.SHIPPINGCOUNTRY, "Please select a Shipping Address Country" );
				}
				if(checkShipCity == 'APO' || checkShipCity == 'FPO'){
					if(checkShipState!='AA' && checkShipState!='AE' && checkShipState!='AP'){
						return DoFail( frm.SHIPPINGSTATE, "Please select Shipping Address State of Armed Forces (AA, AE, or AP) for APO/FPO addresses" );
					}
					if(frm.SHIPPINGCOUNTRY[frm.SHIPPINGCOUNTRY.selectedIndex].value != 'US'){
						return DoFail( frm.SHIPPINGCOUNTRY, "Please select Shipping Address Country of United States for APO/FPO addresses" );
					}
				}
				else if(frm.SHIPPINGCOUNTRY[frm.SHIPPINGCOUNTRY.selectedIndex].value == 'US'){ 
					if(checkShipState==''){
						return DoFail( frm.SHIPPINGSTATE, "Please select a Shipping Address State" );
					}
					if(frm.SHIPPINGZIP.value.trim() == '' ){
						return DoFail( frm.SHIPPINGZIP, "Please enter a Shipping Address Zip/Postal Code" );
					}
				}
				else{ 
					if(checkShipState!=''){
						return DoFail( frm.SHIPPINGSTATE, "Please do NOT select a Shipping Address State when Shipping Address Country is not United States" );
					}
					if(frm.SHIPPINGZIP.value.trim() == '' ){
						/*
						if(!confirm("Shipping Address Zip/Postal Code is missing. Continue without one?")){
							frm.SHIPPINGZIP.focus(); 
							return false; 
						}
						*/
						return DoFail( frm.SHIPPINGZIP, "Please enter a Shipping Address Zip/Postal Code" );
					}
				}
			}
		}
	}

	if( frm.email && frm.email.type == 'text'){

		if (frm.emailoptional && frm.emailoptional.value == 'yes'){
			if( frm.email.value.trim() != '' && !frm.email.value.trim().isEmail()){
				return DoFail(frm.email, "Your Email Address must be a valid email address.");
			}
		}
		else{
			if( frm.email.value.trim() == '' ){
				return DoFail(frm.email, "You must enter your Email Address.");
			}
			if( !frm.email.value.trim().isEmail()){
				return DoFail(frm.email, "Your Email Address must be a valid email address.");
			}
		}
	}

	if( frm.PWORD ){
		if( ! frm.PWORD.value.isVBPassword() ){
			return DoFail( frm.PWORD, "Password must be 5-10 characters long and consist of letters and/or numbers");
		}

		if( frm.PWORD.value != frm.PWORDV.value){
			return DoFail( frm.PWORD, "Password and Password verification do not match");
		}
	}

	if( frm.NAMEINPUTS ){ 
		if(frm.BILLINGFIRSTNAME.value.trim() == '' ){
			return DoFail( frm.BILLINGFIRSTNAME, "Please enter a First Name" );
		}
		if(frm.BILLINGLASTNAME.value.trim() == '' ){
			return DoFail( frm.BILLINGLASTNAME, "Please enter a Last Name" );
		}
	}

	if( frm.fvvchid ){
		
		var vch_id_desc = frm.vch_id_desc.value.trim();

		if( vch_id_desc == '' ) vch_id_desc = 'Voucher ID'; 

		if (frm.fvvchid.value.trim() == '' )
			return DoFail(frm.fvvchid, "You must enter a " + vch_id_desc + ".");
	}

	return true; 
}

function validate_frmShipping(){
	if (alreadySubmitted) {
		alert("Please wait while we gather your information.");
		return false;
	}

	// need ship type selected 
	if( !document.frmShipping.BILLSHIPPERACCOUNT || (document.frmShipping.BILLSHIPPERACCOUNT && document.frmShipping.BILLSHIPPERACCOUNT[0].checked) ){
		if( document.frmShipping.SHIPTYPE ){

			if (document.frmShipping.SHIPTYPE[0]) {
				// check multiple radio buttons
				var shiptype_checked = false; 
				for (i=0; i < document.frmShipping.SHIPTYPE.length; i++) {
					if (document.frmShipping.SHIPTYPE[i].checked) { 
						shiptype_checked = true; 
						break; 
					}
				}
				if (!shiptype_checked) {
					DoFail( document.frmShipping.SHIPTYPE[0], "Please select a Shipping Method" );
					return false;
				}
			}
			else {
				// check one radio button or text
				var shiptype_checked = false;

				if (document.frmShipping.SHIPTYPE.type == 'radio') {
					if (document.frmShipping.SHIPTYPE.checked)
						shiptype_checked = true;
				}
				else if (document.frmShipping.SHIPTYPE.value.trim() != '')
					shiptype_checked = true;

				if (!shiptype_checked) {
					DoFail( document.frmShipping.SHIPTYPE, "Please select a Shipping Method" );
					return false;
				}
			}

		}
	}
	
	if( document.getElementById('cc_info').style.display != 'none' ){

		if( !document.frmShipping.CCPAYROLL || document.frmShipping.CCPAYROLL.checked == false){

			var ccType = document.frmShipping.CCTYPE.value.trim();
			if( ccType == '' ){
				DoFail( document.frmShipping.CCTYPE, "Please select a Credit Card type" );
				return false;
			}
			
			var ccNum = document.frmShipping.CCNUM.value.trim();
			if( ccNum == '' ){
				DoFail( document.frmShipping.CCNUM, "Please enter a Credit Card number" );
				return false;
			}
			else if (!ccNum.isCCN(ccType)) {
				var name = (ccType == 'V') ? 'Visa' : (ccType == 'M') ? 'Mastercard'
					: (ccType == 'D') ? 'Discover' : 'American Express';
				DoFail( document.frmShipping.CCNUM, "Please enter a valid " + name 
					+ " Credit Card number." );
				return false;
			}

			if( document.frmShipping.CCEXPMON.value.trim() == '' ){
				DoFail( document.frmShipping.CCEXPMON, "Please enter data for a non-expired credit card." );
				return false;
			}

			if( document.frmShipping.CCEXPYEAR.value.trim() == '' ){
				DoFail( document.frmShipping.CCEXPYEAR, "Please enter data for a non-expired credit card." );
				return false;
			}

			var month = parseInt(document.frmShipping.CCEXPMON.value.replace(/^0*(\d+)$/, '$1'));
			var year = parseInt(document.frmShipping.CCEXPYEAR.value);
			var d = new Date();
			expdate = d.getDate();
			var exp = new Date();
			exp.setFullYear(year, month-1, expdate);

			if (exp < d) {
				alert("Please enter data for a non-expired credit card.");
				return false;
			}

			if( document.frmShipping.CCCODE ) {
				var ccv = document.frmShipping.CCCODE.value.trim();
				if (ccv == '' )
				{
					DoFail( document.frmShipping.CCCODE, "Please enter a Credit Card Security Code" );
					return false;
				}
				if (! ccv.isCCV(ccType)) {
					DoFail( document.frmShipping.CCCODE, "Please enter a valid Credit Card Security Code" );
					return false;
				}
			}
		}
	}

	if( document.getElementById('bill_address_inputs').style.display == 'block' ){
		
		if(document.frmShipping.BILLINGFIRSTNAME.value.trim() == '' ){
			DoFail( document.frmShipping.BILLINGFIRSTNAME, "Please enter a Billing Address First Name" );
			return false;
		}
		if(document.frmShipping.BILLINGLASTNAME.value.trim() == '' ){
			DoFail( document.frmShipping.BILLINGLASTNAME, "Please enter a Billing Address Last Name" );
			return false;
		}
		if(document.frmShipping.BILLINGADDRESS1.value.trim() == '' ){
			DoFail( document.frmShipping.BILLINGADDRESS1, "Please enter a Billing Address Line 1" );
			return false;
		}
		if(document.frmShipping.BILLINGCITY.value.trim() == '' ){
			DoFail( document.frmShipping.BILLINGCITY, "Please enter a Billing Address City" );
			return false;
		}
		if(document.frmShipping.BILLINGSTATE.selectedIndex < 1 ){
			DoFail( document.frmShipping.BILLINGSTATE, "Please select a Billing Address State" );
			return false;
		}
		if(document.frmShipping.BILLINGCOUNTRY.selectedIndex < 1 ){
			DoFail( document.frmShipping.BILLINGCOUNTRY, "Please select a Billing Address Country" );
			return false;
		}
		if(document.frmShipping.BILLINGZIP.value.trim() == '' ){
			DoFail( document.frmShipping.BILLINGZIP, "Please enter a Billing Address Zip/Postal Code" );
			return false;
		}
		if( document.frmShipping.BILLINGPHONEHOME.value.trim() == '' ){
			DoFail( document.frmShipping.BILLINGPHONEHOME, "Please enter a Billing Address Home Phone" );
			return false;
		}
		else{
			if(frm.BILLINGPHONEHOME.value.trim().length > 11 ){
				return DoFail( frm.BILLINGPHONEHOME, "Maximum length for Billing Address Home Phone is eleven digits" );
			}
		}
	}

	if( document.frmShipping.BILLSHIPPERACCOUNT ){
		if( document.frmShipping.BILLSHIPPERACCOUNT[1].checked ){
			if( document.frmShipping.SHIPPERACCOUNT.value.trim() == '' ){
				DoFail( document.frmShipping.SHIPPERACCOUNT, "Please enter a Shipper Account Number" );
				return false;
			}
		}
	}

	alreadySubmitted = true;

	with (document.frmShipping) {
		if (CCNUM) {
			CCNUM_post.value = CCNUM.value;
			CCNUM.value = '';
		}
		if (CCCODE) {
			CCCODE_post.value = CCCODE.value;
			CCCODE.value = '';
		}
	}
	return true;

}

function CanSubmitRegion(frm) {
	var ctl = frm.FVCUSNO;
	for (var i = 0; i < ctl.length; i++)
		if (ctl[i].checked) return true;
	alert("Please select your region.");
	return false;
}

function CanUpdateShopCart(frm) {
	if (alreadySubmitted) {
		alert("Please wait while we process your request.\n"
			+ "If you got here by backing up, try using the My Cart "
			+ "menu item and make your changes there.");
		return false;
	}
	var idx = 1;
	while (eval("frm.FVNUM_" + idx)) {
		var ctl = eval("frm.FVSEQ_" + idx);
		if (ctl && ctl.checked) {
			alreadySubmitted = true;
			return true;
		}
		idx++;
	}

	alert("Nothing to do.");
	return false;
}

function CanModifyCreditCard(frm) {
	var ctl = frm.CTFULLNAME;
	if (ctl.value.trim() == '') {
		ctl.focus();
		alert('Please enter your Name (as it appears on card).');
		return false;
	}
	ctl = frm.CTTYPE;
	if (ctl.selectedIndex < 1) {
		ctl.focus();
		alert('Please select the Credit Card Type.');
		return false;
	}
	var ctype = ctl.value;
	var cName = ctl.options[ctl.selectedIndex].text.trim();
	ctl = frm.CTNUMBER;
	if (ctl.value.trim() == '') {
		ctl.focus();
		alert('Please enter your Credit Card Number.');
		return false;
	}
	if (!ctl.value.isCCN(ctype)) {
		ctl.focus();
		alert('Please enter a valid ' + cName + ' Credit Card Number.');
		return false;
	}
	ctl = frm.CTEXPMONTH;
	var month = parseInt(ctl.value);
	var year = parseInt(frm.CTEXPYEAR.value);
	var d = new Date();
	var expdate = d.getDate();
	var exp = new Date();
	exp.setFullYear(year, month-1, expdate);
	if (exp < d) {
		ctl.focus();
		alert("Please change your Credit Card Expiration date, or enter the data for an un-expired credit card.");
		return false;
	}
	return true;
}

function CanDeleteCreditCard(frm) {
	for (var i = 0; i < frm.delete_me.length; i++)
		if (frm.delete_me[i].checked)
			return true;
	alert('In order to delete your credit card information you must first check the "Yes, I want to delete it" radio button.'
		+ "\n"
		+ 'If you want to leave it on record and return to the previous screen you must first check the '
		+ '"No, I want to keep my credit card information" radio button.');
	return false;
}


function choose_payment_select_cc(frm){

	if( frm.vch_stu ){ 
		frm.vch_stu.disabled=true; 
	}
	frm.vch_id.disabled=true; 

}
function choose_payment_select_vch(frm){

	if( frm.vch_stu ){ 
		frm.vch_stu.disabled=false; 
	}
	frm.vch_id.disabled=false; 

}

function validate_vch_inputs(frm){ 

	if( frm.vch_stu ){
		if( frm.vch_stu.value.trim() == '' ){
			DoFail( frm.vch_stu, "Please enter your SFA information." );  
			return false; 
		}
	}
	if( frm.vch_id.value.trim() == '' ){
		DoFail( frm.vch_id, "Please enter your SFA information." );  
		return false; 
	}
	if (frm.vch_stu) {
		frm.vch_stu_post.value = frm.vch_stu.value;
		frm.vch_stu.value = '';
	}
	frm.vch_id_post.value = frm.vch_id.value;
	frm.vch_id.value = '';
	return true; 
}

function validate_choose_payment(frm){

	if( frm.vch_paytype.length ){
		if( !(frm.vch_paytype[0].checked || frm.vch_paytype[1].checked) ){
			DoFail( frm.vch_paytype[0], "Please select how you would like to pay for your order." );  
			return false;
		}
		if( frm.vch_paytype[0].checked ){
			return validate_vch_inputs(frm); 
		}
	}
	else{ 
		return validate_vch_inputs(frm);
	}
}

var max_unshrunk_lines = 5;
var num_shrunk_lines = 3;
var expandedMessages;

function RemoveLastWord(node) {
	// Return codes:
	//	0 = OK, removed a word and am done
	//	1 = OK, removed a word but remove node from it's parent
	//	-1 = Did not remove a word; remove node from it's parent
	if (!node || !node.nodeType) return -1;
	if (node.nodeType == 1) {	// an element
		while (node.lastChild) {
/*alert('Removing last word from element ' + node.tagName);*/
			var n = RemoveLastWord(node.lastChild);
			if (n == 0) return 0;
			if (n == 1) {
				node.removeChild(node.lastChild);
				if (!node.lastChild) return 1;
				else return 0;
			}
			else
				node.removeChild(node.lastChild);
		}
		return -1;
	}
	else if (node.nodeType == 3) {	// a text node
		var text = node.nodeValue;
		if (/\S+\s+\S+/.test(text)) {
			// has multiple words
			node.nodeValue = text.replace(/\s+\S+\s*$/, '').trim();
			return 0;
		}
		else if (/^\s*$/.test(text))
			// is all whitespace
			return -1;
		else
			// is a single word
			return 1;
	}
	else if (node.nodeType == 5) {	// an entity reference
		return 1;
	}
	else
		return -1;
}

function ShrinkMessages() {
	expandedMessages = new Array();
	if (!document.getElementsByTagName) return;
	var divs = document.getElementsByTagName('div');
	if (!divs || !divs.length) return;

	for (var i = 0; i < divs.length; i++) {
		var div = divs[i];
		if (/^.*Messaging$/.test(div.className)) {

			/* This is a message-type div; shrink it */

			var height = div.offsetHeight;
			var lines = Math.round(height / 12);	/* assumes a lineheight of 12px */

			if (height && lines > max_unshrunk_lines) {
				/* Hide the div so it is not visibly twitching on the screen */
				div.visibility = 'hidden';
				var expanded = div.innerHTML.trim();	/* this is the original content */
				var count = 0; /* just to be sure we do not end up with an endless loop */
				var retcode = -1;
				while (lines > num_shrunk_lines) {
					if (++count > 1000) break;	/* Only remove up to 1000 words */
					retcode = RemoveLastWord(div);
					if (retcode != 0) break;	/* For one reason or another could not remove last word */
					/* We have to add the '...click to expand' text so we can tell if it makes
					 * a wrap and adds another line. I am assuming that a textnode will have the
					 * same bounding box as that node wrapped inside an anchor tag.. */
					var node = document.createTextNode('... click to expand \xbb');
					div.appendChild(node);

					height = div.offsetHeight;
					lines = Math.round(height / 12);
					/* If it wrapped, remove the '... click to expand' text because we will keep
					 * looping. */
					div.removeChild(node);
				}
				if (lines > num_shrunk_lines || retcode != 0) {
					/* Cannot shrink the message */
				}
				else {
					/* Our data will be appended to expandedMessages[], so the index to our data
					 * is the current length of expandedMessages[] */
					var ofs = expandedMessages.length;
					/* Append a '... click to collapse' element */
					expanded += '<a class="expandor" href="javascript:ShrinkThisMessage(' + ofs + ')">... click to collapse \xbb</a>';
					var shrunk = div.innerHTML + '<a class="expandor" href="javascript:ExpandMessage(' + ofs + ')">... click to expand \xbb</a>';
					expandedMessages[ofs] = new Array(div, expanded, shrunk);
					div.innerHTML = shrunk;
				}
				div.visibility = 'visible';
			}
		}
	}
}

function ExpandMessage(idx) {
	if (idx >= 0 && expandedMessages[idx] && expandedMessages[idx][0] && expandedMessages[idx][1]) {
		var div = expandedMessages[idx][0];
		var markup = expandedMessages[idx][1];
		div.innerHTML = markup;
	}
}

function ShrinkThisMessage(idx) {
	if (idx >= 0 && expandedMessages[idx] && expandedMessages[idx][0] && expandedMessages[idx][2]) {
		var div = expandedMessages[idx][0];
		var markup = expandedMessages[idx][2];
		div.innerHTML = markup;
	}
}

/**
 * Validate Email update form data. 
 * 
 * 
 */ 
function validate_accountEmail(frm){

	if( ! frm ) return false;
	
	if(frm.fvOriginalEmailAddress.value.trim() == '' ){
		return DoFail( frm.fvOriginalEmailAddress, "Please enter your Current Email Address" );
	}
	if(frm.fvEmailAddress.value.trim() == '' ){
		return DoFail( frm.fvEmailAddress, "Please enter your New Email Address" );
	}
	if(frm.fvConfirmEmailAddress.value.trim() == '' ){
		return DoFail( frm.fvConfirmEmailAddress, "Please Confirm your New Email Address" );
	}
	if(frm.fvEmailAddress.value.trim() != frm.fvConfirmEmailAddress.value.trim()){
		return DoFail( frm.fvEmailAddress, "New Email Addresses do not match. Please re-enter your New Email Address" );
	}
	if( ! frm.fvEmailAddress.value.isEmail() ){
		return DoFail( frm.fvEmailAddress, "Please enter a valid email Address" );
	}

	return true;
}


/**
 * Validate Password update form data. 
 * 
 */ 
function validate_accountPassword(frm){

	if( ! frm ) return false;
	
	if(frm.fvOriginalPassword.value.trim() == '' ){
		return DoFail( frm.fvOriginalPassword, "Please enter your Current Password" );
	}
	if(frm.fvPassword.value.trim() == '' ){
		return DoFail( frm.fvPassword, "Please enter your New Password" );
	}
	if( !frm.fvPassword.value.isVBPassword() ){
		return DoFail( frm.fvPassword, "Your password must be 5-10 letters and/or numbers." );
	}
	if(frm.fvConfirmPassword.value.trim() == '' ){
		return DoFail( frm.fvConfirmPassword, "Please Confirm your New Password" );
	}
	if(frm.fvPassword.value.trim() != frm.fvConfirmPassword.value.trim()){
		return DoFail( frm.fvPassword, "New Passwords do not match. Please re-enter your New Password" );
	}

	return true;
}


/**
 * Validate Email Prefs form data. 
 * 
 */ 
function validate_accountEmailPrefs(frm){

	if( ! frm ) return false;

	if ( ! (frm.fvEmailType[0].checked || frm.fvEmailType[1].checked ) ){
		return DoFail( frm.fvEmailType[0], "Please select your Prefered Email type." );  
	}
	return true;
}


/**
 * Validate School Prefs form data. 
 * 
 * 
 */ 
function validate_accountSchoolPrefs(frm){

	if( ! frm ) return false;

	if( ! ValidateLevelOfStudy(frm) )
		return false;

	// Do not force Grad Year selection for Faculty
	if ( !(frm.fvEducationLevel.length == 11 && frm.fvEducationLevel[9].checked) ){
		if (frm.fvGradYear.value.trim() == ''){
			return DoFail(frm.fvGradYear,"Please select your Expected Graduation Year.");
		}
	}

	if ( ! ValidateMyInterests(frm) )
		return false;

	return true;
}

function ValidateLevelOfStudy(frm){
	var temp = 0;
	var temp2;
	
	for (i=0;i<11;i++){
		if ( frm.fvEducationLevel[i].checked == true){
			temp = temp + 1;
			temp2 = frm.fvEducationLevel[i].value;
		}
	}
	
	if (temp == 0){
		return DoFail(frm.fvEducationLevel[0], "Please select your Level of Study.");
	}

	if (temp2 == 'O'){
		if (frm.fvOther.value.trim() == ''){
			return DoFail(frm.fvOther, "Please provide a short description for your Level of Study.");
		}
	}

	return true;
}


function ValidateMyInterests(frm){

	if (frm.fvGeneralArea.value == -1){
		return DoFail(frm.fvGeneralArea, "Please select a General Area of Study or Interest.");
	}

	var item = "fvInterestArea" + frm.fvGeneralArea.value + "[]"; 
	var valid = false;
	for(i=0;i<frm[item].length;i++){
		if(frm[item][i].checked){
			valid = true;
			break;
		}
	}
	
	if( !valid ){
		return DoFail(frm[item][0], "Please choose a Specific Area of Study or Interest");
	}
	
	return true;
}

/**
 * Show/Hide Bundle Details
 */

var curDetailsShowing = false;
var curDetailsId = '';

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

function hideBundle(id) {
	if (!document.getElementById) return;
	var elem = document.getElementById(id);
	if (!elem) return;
	elem.style.display = 'none';
	curDetailsShowing = false;
}

function showBundle(cell, id) {
	if (curDetailsShowing) {
		if (id == curDetailsId) return;
		hideBundle(curDetailsId);
	}

	if (!document.getElementById) return;
	var elem = document.getElementById(id);
	if (!elem) return;
	
	var coords = findPos(cell);
	var x = coords[0];
	var y = coords[1] + cell.offsetHeight + 4;

	elem.style.left = x + 'px';
	elem.style.top = y + 'px';
	elem.style.display = 'block';

	curDetailsId = id;
	curDetailsShowing = true;
}

function confirm_logout(has_cart){

	var str = 'You have selected to log off of the Online Bookstore.\n';
	
	if (has_cart)
		str += 'All inventory you have reserved in your cart will be released.\n';
	
	str += '\nContinue?'; 

	if(confirm(str))		return true;
	else					return false;
}

function CanEmailVoucher(frm) {
	var ctl = document.email_voucher.stuid;
	if (ctl.value.trim().length == 0) {
		ctl.focus();
		alert("Please enter your " + student_id_name + ".");
		return false;
	}
	return true;
}

function checkProxyLogin(frm) {

	if (frm.fvStudentIDLogIn.value.trim() != "")
		return StudentIDOkay(frm.fvStudentIDLogIn);
	else if (frm.fvEmailLogIn.value.trim() != "") {
		return EmailOkay(frm.fvEmailLogIn);
	}

	return true;
}
