// Auto-clears the Email field in the login form
var LoginFormEmailOnFocus = function(objFld){
	if (objFld.value == 'Email address')
		objFld.value = '';
}

// "Auto-clears" the Password field in the login form - it actually swaps between a 
// fake and real password field so everything will work properly in IE
var LoginFormFakeOnFocus = function(objFld){
	if (objFld.value == 'Password') {
		objFld.style.display = 'none';
		document.LoginForm.LoginPassword.style.display = '';
		document.LoginForm.LoginPassword.focus();
	}
}

// Displays a semester's subjects
var ToggleSemSubjs = function(SemesterID){
	var TheDiv = document.getElementById('Sem' + SemesterID + 'Subjs');
	var CurrDisplayVal = TheDiv.style.display;
	
	if (CurrDisplayVal == '')
		TheDiv.style.display = 'none';
	else
		TheDiv.style.display = '';
}

// Displays a subject's courses
var ToggleSubjCourses = function(SemSubjID){
	var TheDiv = document.getElementById('Subj' + SemSubjID + 'Courses');
	var CurrDisplayVal = TheDiv.style.display;
	
	if (CurrDisplayVal == '')
		TheDiv.style.display = 'none';
	else
		TheDiv.style.display = '';
}

// Redirects to a page that shows the products for the specified semester/subject/course  
var GoToSemSubjCourse = function(SemesterID,SemSubjID,CourseID){
	window.location.href = 'school_prods.cfm?semester_id=' + SemesterID + '&semsubj_id=' + SemSubjID + '&course_id=' + CourseID;
}

// Redirects to a page that shows the products for the specified semester/subject  
var GoToSemSubj = function(SemesterID,SemSubjID){
	window.location.href = 'school_prods.cfm?semester_id=' + SemesterID + '&semsubj_id=' + SemSubjID;
}

// Refreshes the cart (backpack) item count
var RefreshCartItemCnt = function(){
	// Save the CF Loading... message HTML
	var Orig_cf_loadingtexthtml = _cf_loadingtexthtml;
	
	// Temporarily clear the CF Loading... message HTML
	_cf_loadingtexthtml="";
	// URL version of calling the CFC binding
	ColdFusion.navigate('CartRemote.cfc?method=GetCartItemCntStr','divCartItemCnt');
	// Restore the CF Loading... message HTML
	_cf_loadingtexthtml = Orig_cf_loadingtexthtml;
}

// Refreshes the cart (backpack) subtotal
var RefreshCartSubtotal = function(){
	// Save the CF Loading... message HTML
	var Orig_cf_loadingtexthtml = _cf_loadingtexthtml;
	
	// Temporarily clear the CF Loading... message HTML
	_cf_loadingtexthtml="";
	// URL version of calling the CFC binding
	ColdFusion.navigate('CartRemote.cfc?method=GetCartSubtotalStr','divCartSubtotal');
	// Restore the CF Loading... message HTML
	_cf_loadingtexthtml = Orig_cf_loadingtexthtml;
}

// Refreshes the Sign In/Sign Out and Sign Up/My Account areas at the top of every page
var RefreshSignInOut = function(){
	// Save the CF Loading... message HTML
	var Orig_cf_loadingtexthtml = _cf_loadingtexthtml;
	
	// Temporarily clear the CF Loading... message HTML
	_cf_loadingtexthtml="";
	// URL version of calling the CFC binding
	ColdFusion.navigate('CartRemote.cfc?method=GetSignInOutStr','divSignInOut');
	ColdFusion.navigate('CartRemote.cfc?method=GetSignUpMyAcctStr','divSignUpMyAcct');
	// Restore the CF Loading... message HTML
	_cf_loadingtexthtml = Orig_cf_loadingtexthtml;
}

var LoginWarningWindowOnHide = function(){
	// Destroy the PO window
	ColdFusion.Window.destroy('LoginWarningWindow', true);
}

var AFeaItemSectProdID = '';
var DoShowAdded = 1;

// Add items to the cart
var AddToCart = function(CustID,ProdID,SemSubjID,CourseID,FeaItemSectProdID,BTSAIsActive,ShowAdded){
	if (!FeaItemSectProdID)
		FeaItemSectProdID = '';
	if (!ShowAdded)
		DoShowAdded = 1
	else
		DoShowAdded = ShowAdded;
	AFeaItemSectProdID = FeaItemSectProdID;
	// Default for new qty
	var QtyNew = 0;
	// Default for used qty
	var QtyUsed = 0;
	// Default for rental qty
	var QtyRental = 0;
	var RentPerID = 0;
	var CartItemAttribs = '';
	
	// Get the qty of new product the user wants, if such a field exists
	var objQtyNew = document.getElementById('QtyNew' + SemSubjID + CourseID + ProdID + FeaItemSectProdID);
	if (objQtyNew)
		QtyNew = objQtyNew.value;
	
	// Get the qty of used product the user wants, if such a field exists
	var objQtyUsed = document.getElementById('QtyUsed' + SemSubjID + CourseID + ProdID + FeaItemSectProdID);
	if (objQtyUsed)
		QtyUsed = objQtyUsed.value;
	
	// Get the qty of product the user wants to rent, if such a field exists
	var objQtyRental = document.getElementById('QtyRental' + SemSubjID + CourseID + ProdID + FeaItemSectProdID);
	if (objQtyRental) {
		QtyRental = objQtyRental.value;
		if (IsNumeric(QtyRental) && QtyRental > 0) {
			// Get the rental period ID
			var objRentPerIDPrice = document.getElementById('RentalPer' + SemSubjID + CourseID + ProdID + FeaItemSectProdID);
			var RentPerIDPrice = objRentPerIDPrice[objRentPerIDPrice.selectedIndex].value;
			var arrRentPerIDPrice = RentPerIDPrice.split('|');
			RentPerID = arrRentPerIDPrice[0];
		}
	}
	
	// Only logged-in customers can add to the cart during BTSA
	if (BTSAIsActive==0 || (BTSAIsActive==1 && CustID > 0)) {

		if (IsNumeric(QtyNew) && QtyNew > 0) {
			// Clear the Qty field
			objQtyNew.value = '';
			
			// Get any attributes
			var ProdAttribTypeIDList = eval('document.Add' + SemSubjID + CourseID + ProdID + FeaItemSectProdID + '.PRODATTRIBTYPE_IDList.value');
			
			if (ProdAttribTypeIDList != '') {
				var AttribType = '';
				var AttribValue = '';
				var AttribCtrl = '';
				var objAttribCtrl;
				var ProdAttribTypeTypeList = eval('document.Add' + SemSubjID + CourseID + ProdID + FeaItemSectProdID + '.PRODATTRIBTYPE_TypeList.value');
				var ProdAttribDefValueList = eval('document.Add' + SemSubjID + CourseID + ProdID + FeaItemSectProdID + '.ProdAttribDefValueList.value');
				var arrProdAttribTypeIDList = ProdAttribTypeIDList.split(',');
				var arrProdAttribTypeTypeList = ProdAttribTypeTypeList.split(',');
				var arrProdAttribDefValueList = ProdAttribDefValueList.split(',');
				// Loop through the attributes and assemble the CartItemAttribs string, which is encoded as a comma-separated list of 
				// PRODATTRIBTYPE_ID|PRODATTRIBTYPE_Type|AttribValue, where AttribValue is PRODATTRIBSELOPT_ID, PRODATTRIB_YesNoValue, or a free-form value
				// depending upon the attribute type
				for (var i = 0; i < arrProdAttribTypeIDList.length; i++) {
					// Get the attribute type
					AttribType = arrProdAttribTypeTypeList[i];
					// Get the attribute form control object
					AttribCtrl = 'document.Add' + SemSubjID + CourseID + ProdID + FeaItemSectProdID + '.PRODATTRIBTYPE_ID' + SemSubjID + CourseID + ProdID + FeaItemSectProdID + arrProdAttribTypeIDList[i];
					objAttribCtrl = eval(AttribCtrl);
					
					// Get the attribute value - how you do that varies by attribute type
					if (AttribType == 'S'){
						// Do we have a hidden field from an image select?
						if (objAttribCtrl.type == 'hidden') {
							AttribValue = objAttribCtrl.value;
						// Regular select
						} else {
							AttribValue = objAttribCtrl[objAttribCtrl.selectedIndex].value;
						}
					} else if (AttribType == 'Y'){
						AttribValue = getSelectedRadio(objAttribCtrl);
					} else {
						AttribValue = objAttribCtrl.value;
						// Reset the product attribute to it's default
						objAttribCtrl.value = '';
					}
					
					if (CartItemAttribs != '')
						CartItemAttribs = CartItemAttribs + ',';
						
					// Add the attribute info to the cart item attributes string
					CartItemAttribs = CartItemAttribs + arrProdAttribTypeIDList[i] + '|' + AttribType + '|' + AttribValue;
					
				}
			}

			// create an instance of the proxy. 
			var e = new CartRemote();
			e.setCallbackHandler(AddToCartResult);
			e.setErrorHandler(myErrorHandler);
			e.AddCartItem(ProdID,SemSubjID,CourseID,'N',QtyNew,'S',0,CartItemAttribs);
		}
	
		if (IsNumeric(QtyUsed) && QtyUsed > 0) {
			// Clear the Qty field
			objQtyUsed.value = '';
			// create an instance of the proxy. 
			var e = new CartRemote();
			e.setCallbackHandler(AddToCartResult);
			e.setErrorHandler(myErrorHandler);
			e.AddCartItem(ProdID,SemSubjID,CourseID,'U',QtyUsed,'S');
		}
	
		if (IsNumeric(QtyRental) && QtyRental > 0) {
			// Clear the Qty field
			objQtyRental.value = '';
			// create an instance of the proxy. 
			var e = new CartRemote();
			e.setCallbackHandler(AddToCartResult);
			e.setErrorHandler(myErrorHandler);
			// Rentals are added to the cart as new items, and potentially later split and changed to used if that is available
			e.AddCartItem(ProdID,SemSubjID,CourseID,'N',QtyRental,'R',RentPerID);
		}
	
	} else {

		if (IsNumeric(QtyNew) && QtyNew > 0) {
			var ProdAttribTypeIDList = eval('document.Add' + SemSubjID + CourseID + ProdID + FeaItemSectProdID + '.PRODATTRIBTYPE_IDList.value');
			
			if (ProdAttribTypeIDList != '') {
				var AttribType = '';
				var AttribValue = '';
				var AttribCtrl = '';
				var objAttribCtrl;
				var ProdAttribTypeTypeList = eval('document.Add' + SemSubjID + CourseID + ProdID + FeaItemSectProdID + '.PRODATTRIBTYPE_TypeList.value');
				var ProdAttribDefValueList = eval('document.Add' + SemSubjID + CourseID + ProdID + FeaItemSectProdID + '.ProdAttribDefValueList.value');
				var arrProdAttribTypeIDList = ProdAttribTypeIDList.split(',');
				var arrProdAttribTypeTypeList = ProdAttribTypeTypeList.split(',');
				var arrProdAttribDefValueList = ProdAttribDefValueList.split(',');
				// Loop through the attributes and assemble the CartItemAttribs string, which is encoded as a comma-separated list of 
				// PRODATTRIBTYPE_ID|PRODATTRIBTYPE_Type|AttribValue, where AttribValue is PRODATTRIBSELOPT_ID, PRODATTRIB_YesNoValue, or a free-form value
				// depending upon the attribute type
				for (var i = 0; i < arrProdAttribTypeIDList.length; i++) {
					// Get the attribute type
					AttribType = arrProdAttribTypeTypeList[i];
					// Get the attribute form control object
					AttribCtrl = 'document.Add' + SemSubjID + CourseID + ProdID + FeaItemSectProdID + '.PRODATTRIBTYPE_ID' + SemSubjID + CourseID + ProdID + FeaItemSectProdID + arrProdAttribTypeIDList[i];
					objAttribCtrl = eval(AttribCtrl);
					
					// Get the attribute value - how you do that varies by attribute type
					if (AttribType == 'S'){
						// Do we have a hidden field from an image select?
						if (objAttribCtrl.type == 'hidden') {
							AttribValue = objAttribCtrl.value;
						// Regular select
						} else {
							AttribValue = objAttribCtrl[objAttribCtrl.selectedIndex].value;
						}
					} else if (AttribType == 'Y'){
						AttribValue = getSelectedRadio(objAttribCtrl);
					} else {
						AttribValue = objAttribCtrl.value;
						// Reset the product attribute to it's default
						objAttribCtrl.value = '';
					}
					
					// Special separator (^ rather than ,) because the attribute list will be combined with another comma-separated pipe-delimited list
					if (CartItemAttribs != '')
						CartItemAttribs = CartItemAttribs + '^';
						
					// Add the attribute info to the cart item attributes string - 
					// Special separator (~ rather than |) because the attribute list will be combined with another comma-separated pipe-delimited list
					CartItemAttribs = CartItemAttribs + arrProdAttribTypeIDList[i] + '~' + AttribType + '~' + AttribValue;
					
				}
			}
		}

		ColdFusion.Window.create('LoginWarningWindow', 'Account/Login Required', 
			'acct_login_req_warning.cfm?prod_id=' + ProdID + '&semsubj_id=' + SemSubjID + '&course_id=' + CourseID + '&qtynew=' + QtyNew + '&qtyused=' + QtyUsed + '&qtyrental=' + QtyRental + '&rentper_id=' + RentPerID + '&CartItemAttribs=' + CartItemAttribs,
			{x:100,y:100,height:190,width:400,modal:true,closable:true,
			draggable:true,resizable:false,center:true,initshow:true,
			refreshonshow:true,bodystyle:'color:#000000; background-color:#ffffff; padding: 5px;'});
		document.getElementById('LoginWarningWindow_title').className = 'cfWindowTitleBar';
		ColdFusion.Window.onHide('LoginWarningWindow', LoginWarningWindowOnHide);
	
	}
}

var HideAddedToBackpack = function(){
	document.getElementById('AddedToBackpack').style.display = 'none';
}

// Res is a structure containing the number-of-items-in-the-cart string (so we can update the cart display with it), an error message, and some of the original values
var AddToCartResult = function(Res){
	// Only stop/play the add to cart sound if there wasn't an error druing the add to cart
	if (Res.ERRORMSG == '') {
		// Stop any sound that might be playing
		//StopSound('AddToCartWav');
		// Play a sound to give some indication that the item was added to the cart
		//PlaySound('AddToCartWav', 'media/kerchunk.wav');
		DHTMLSound('AddToCartWav', 'media/kerchunk.wav')
		if (DoShowAdded == 1) {
			relPosition('AddedToBackpack','AddBtn' + Res.SEMSUBJ_ID + Res.COURSE_ID + Res.PROD_ID + AFeaItemSectProdID,95,2,false);
			// Hide the "added to backpack" indicator in 1 second
			setTimeout('HideAddedToBackpack()',1000);
		}
	}
	// Update the cart item count
	document.getElementById('divCartItemCnt').innerHTML = Res.ITEMCNTSTR;
	// Update the cart subtotal
	RefreshCartSubtotal();
	// If there is a non-zero NetAvailUsedInv returned, display it as the used qty value
	//if (Res.NETAVAILUSEDINV > 0)
		//document.getElementById('QtyUsed' + Res.SEMSUBJ_ID + Res.COURSE_ID + Res.PROD_ID).value = Res.NETAVAILUSEDINV;
	// Show any error
	if (Res.ERRORMSG != '')
		alert(Res.ERRORMSG);
		
	// Get the current page file name
	var CurrDocument = returnDocument();
	
	// If we're on the backpack page or any of the subsequent pages in the checkout process, goto/refresh the backpack page.
	if (CurrDocument == 'backpack.cfm' || 
		CurrDocument == 'checkout.cfm' || 
		CurrDocument == 'checkout_new1.cfm' || 
		CurrDocument == 'checkout_new2.cfm' || 
		CurrDocument == 'checkout.cfm#1' || 
		CurrDocument == 'checkout.cfm#2')
		GoToURL('backpack.cfm');
		
	// If we're on the recommended items page and just added the first rental item to the backpack,
	// send the customer back to the backpack page so they can accept the rental agreement
	//if (CurrDocument == 'recomm_items.cfm' && Res.RENTALCNTBEFORE == 0 && Res.RENTALCNTAFTER > 0)
	  //GoToURL('backpack.cfm');
	  
	// If we're on the recommended items page and didn't get redirected above, then just refresh the recommended items
	// page so the item that was just added to the cart will disappear
	//if (CurrDocument == 'recomm_items.cfm')
	  //GoToURL('recomm_items.cfm');
}

// OnHide for VBSMismatch
var VBSMismatchWindowOnHide = function(){
	ColdFusion.Window.destroy('VBSMismatchWindow', true)
}

// Handle a click on the Deal link next to a quote in the list on cust_detail_trans_doclist.cfm page
var VBSMismatch = function(){
	ColdFusion.Window.create('VBSMismatchWindow', 'School Bookstore Mismatch', 'vbsmismatch.cfm',
						{x:100,y:100,height:100,width:380,modal:true,closable:true,
						draggable:true,resizable:false,center:true,initshow:true,
						refreshonshow:true,bodystyle:'color:#000000; background-color:#ffffff; padding: 5px;'});
	document.getElementById('VBSMismatchWindow_title').className = 'cfWindowTitleBar';
	ColdFusion.Window.onHide('VBSMismatchWindow', VBSMismatchWindowOnHide);
}

// Not used
var onErrorClearField = function(form, ctrlName, value, message) {
	alert(message);
	document.getElementById(ctrlName).value = '';
	document.getElementById(ctrlName).focus();
   }

// OnHide for BTSALackOfPurchWarning
var BTSALackOfPurchWarningWindowOnHide = function(){
	ColdFusion.Window.destroy('BTSALackOfPurchWarningWindow', true)
}

// Display a BTSA product group lack of purchase warning
var BTSALackOfPurchWarning = function(WarningCopy, RedirectURL){
	ColdFusion.Window.create('BTSALackOfPurchWarningWindow', 'Warning', 'lack_of_purchase_warning.cfm?warningcopy=' + WarningCopy + '&redirecturl=' + RedirectURL,
						{x:100,y:100,height:200,width:400,modal:true,closable:true,
						draggable:true,resizable:false,center:true,initshow:true,
						refreshonshow:true,bodystyle:'color:#000000; background-color:#ffffff; padding: 5px;'});
	document.getElementById('BTSALackOfPurchWarningWindow_title').className = 'cfWindowTitleBar';
	ColdFusion.Window.onHide('BTSALackOfPurchWarningWindow', BTSALackOfPurchWarningWindowOnHide);
}

var SemesterOnChange = function(SemesterID){
	// create an instance of the proxy. 
	var e = new Schools();
	e.setCallbackHandler(SemesterOnChangeResult);
	e.setErrorHandler(myErrorHandler);
	e.GetSubjectCnt(SemesterID);
}

var SemesterOnChangeResult = function(Res){
	if (Res == 1) { 
		document.getElementById('lblSubject').style.display = 'none'; 
		document.CourseSelectForm.SEMSUBJ_ID.style.display = 'none'; 
	} else { 
		document.getElementById('lblSubject').style.display = '';
		document.CourseSelectForm.SEMSUBJ_ID.style.display = ''; 
	}
}