// 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 and subtotal
var RefreshCartInfo = function(){
	// Save the CF Loading... message HTML
	var Orig_cf_loadingtexthtml = _cf_loadingtexthtml;
	
	// Temporarily clear the CF Loading... message HTML
	_cf_loadingtexthtml="";
	// Refresh the backpack item count and subtotal display div binding
	ColdFusion.navigate('cart_info.cfm','divCartItemInfo');
	// 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 window
	ColdFusion.Window.destroy('LoginWarningWindow', true);
}

// Returns a complete product attribute string. When CartItemID and/or OrderItemID are not needed, an empty string should be passed in. No longer used.
var GetProdAttribStr = function(FormName, CartItemID, OrderItemID){
	// Create/init the return value
	var SelectedItemAttribs = '';
	// Form object
	var objForm = eval('document.' + FormName);
	// Get any list of attribute type IDs
	var ProdAttribTypeIDList = objForm.PRODATTRIBTYPE_IDList.value;
	
	if (ProdAttribTypeIDList != '') {
		var AttribType = '';
		var AttribValue = '';
		var objAttribCtrl;
		var ProdAttribTypeTypeList = objForm.PRODATTRIBTYPE_TypeList.value;
		var arrProdAttribTypeIDList = ProdAttribTypeIDList.split(',');
		var arrProdAttribTypeTypeList = ProdAttribTypeTypeList.split(',');
		// Loop through the attributes and assemble the SelectedItemAttribs string, which is encoded as a comma-separated list of 
		// PRODATTRIBTYPE_ID|PRODATTRIBTYPE_Type|AttribValue, where AttribValue depends 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
			objAttribCtrl = eval('objForm.PRODATTRIBTYPE_ID' + arrProdAttribTypeIDList[i] + CartItemID + OrderItemID);
			// Init the attribute value
			AttribValue = '';
			
			// Get the attribute value - how that's done varies by attribute type
			// Select-type attribute
			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;
				}
			}
			// Yes/No-type attribute, which can be displayed as either a pair of radio buttons or a checkbox
			else if (AttribType == 'Y'){
				// Radio buttons
				if (objAttribCtrl.type == 'radio') {
					AttribValue = getSelectedRadio(objAttribCtrl);
				// Checkbox
				} else {
					if (objAttribCtrl.checked == true)
						AttribValue = '1'
					else
						AttribValue = '0';
				}
			}
			// Free-form attribute
			else if (AttribType == 'F') {
				AttribValue = objAttribCtrl.value;
			}
			// Checkbox group attribute
			else if (AttribType == 'C') {
				AttribValue = GetCBGroupValues(objAttribCtrl,'~');
			}
			
			// Add the attribute info to the cart item attributes string
			if (SelectedItemAttribs != '')
				SelectedItemAttribs = SelectedItemAttribs + ',';
				
			SelectedItemAttribs = SelectedItemAttribs + arrProdAttribTypeIDList[i] + '|' + AttribType + '|' + AttribValue;
			
		}
	}
	
	return SelectedItemAttribs;
}

var AFeaItemSectProdID = '';
var DoShowAdded = 1;
var AddBtnId = 0;
var CartSummaryTimer;

// No longer used
var HideAddedToBackpack = function(){
	document.getElementById('AddedToBackpack').style.display = 'none';
}

var RestoreAddBtn = function() {
	document.getElementById(AddBtnId).src = 'images/btn_Add_new.gif';
}

var HideCartSummary = function() {
	ColdFusion.navigate('blank.cfm', 'divCartSummary');
}

// Add items to the cart
var AddToCart = function(FormName,Bypass){
	// Save the CF Loading... message HTML
	var Orig_cf_loadingtexthtml = _cf_loadingtexthtml;
	
	if (typeof(Bypass) == 'undefined')
		Bypass = '';
	// Temporarily clear the CF Loading... message HTML
	_cf_loadingtexthtml="";

	ColdFusion.navigate('cart_info.cfm?Bypass=' + Bypass, 'divCartItemInfo', callback, myErrorHandler, 'POST', FormName);
	
	// Restore the CF Loading... message HTML
	_cf_loadingtexthtml = Orig_cf_loadingtexthtml;
}

// Called after something has been added to the backpack
var AddToCartResult = function(ErrorMsg, ShowAdded, FormName, idAddBtn){
	// Only stop/play the add to cart sound if there wasn't an error druing the add to cart
	if (ErrorMsg == '') {
		
		// DHTMLSound('AddToCartWav', 'media/kerchunk.wav');
		
		if (ShowAdded == 1) {
			// relPosition('AddedToBackpack',idAddBtn,95,5,false);
			document.getElementById(idAddBtn).src = 'images/btn_added.gif';
			AddBtnId = idAddBtn;
			
			// Hide the "added to backpack" indicator in 1 second
			// setTimeout('HideAddedToBackpack()',1000);
			setTimeout('RestoreAddBtn()',1000);
		}
	} else {
		// Show any error
		alert(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');
	} else {
		// Show the backpack summary
		ColdFusion.navigate('cart_summary.cfm', 'divCartSummary');
		
		// Hide the cart summary in 3 seconds
		CartSummaryTimer = setTimeout('HideCartSummary()',3000);
	}
}

var OnMouseOverCartSummary = function(){
	if (CartSummaryTimer)
		clearTimeout(CartSummaryTimer);
}

var OnMouseOutCartSummary = function(){
	// Hide the cart summary in 1 second
	CartSummaryTimer = setTimeout('HideCartSummary()',1000);
}

// 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 = ''; 
	}
}

var GetRentalPrice = function(RentPerIDPrice,Formatted){
	var arrRentPerIDPrice = RentPerIDPrice.split('|');
	var Price = arrRentPerIDPrice[1];
	
	if (Formatted == 1)
		Price = '$' + CurrencyFormat(Price);
		
	return Price;
}

// Handles autosuggest for site search
var SearchAutoSuggest = function() {
	// Get the current search value
	var autosuggestvalue = document.SearchForm.SearchKeywords.value;
	
	// Only show autosuggest results when the search keyword length is greater than 3
	if (autosuggestvalue.length > 3) {
		
		// Suppress the normal CF loading icon/text
		var orig_cf_loadingtexthtml = _cf_loadingtexthtml;
		_cf_loadingtexthtml = '';
		
		// Get the y position of the search bar
		var SearchBarTop = findPosY(document.getElementById('SearchBox'));
		// Set the top of the autosuggest
		document.getElementById('divSearchAutoSuggest').style.top = SearchBarTop + 47 + 'px';
		// Make sure the autosuggest div is visible
		document.getElementById('divSearchAutoSuggest').style.display = '';
		// Load the search autosuggest results into the autosuggest div
		ColdFusion.navigate('search_autosuggest.cfm?s=' + escape(autosuggestvalue), 'divSearchAutoSuggest');
		// Restore the normal CF loading icon/text
		_cf_loadingtexthtml = orig_cf_loadingtexthtml;
		
	} else {
		// Hide the autosuggest div when the search keywords are shorter than 3 char
		document.getElementById('divSearchAutoSuggest').style.display = 'none';
	}
}

var ScrollCartSummToBot = function() {
	ScrollDivToBottom('divCartSummItems');
}

var CartWarningWindowOnHide = function(){
	// Destroy the window
	ColdFusion.Window.destroy('CartWarningWindow', true);
}

var AdjustCartSummaryDivClass = function(){
	var objCartSummItemsDiv = document.getElementById('divCartSummItems');
	
	if (objCartSummItemsDiv) {
		if (objCartSummItemsDiv.clientHeight < objCartSummItemsDiv.scrollHeight)
			objCartSummItemsDiv.className = 'VScrollingDiv'
		else
			objCartSummItemsDiv.className = '';
	}
}

