﻿


/*************************************************************************
Logic for [root]/account.aspx && [root]/createaccount.aspx
----------------------------------------------------------
- Manages Category (School) dual listbox selector functionality
*************************************************************************/

//------------------------------------------------------------------------------------------------------------------------------------------------
function availableSchoolsListFocus(ctl) {
	var Avail = ctl;
	var btn = document.getElementById('btnAdd');
	if (btn) {
		btn.className = 'disabled';
		for (var i = Avail.options.length - 1; i >= 0; i--) {
			if ((Avail.options[i].selected) && (Avail.options[i].value != '-1')) {
				btn.className = 'enabled';
				return;
			}
		}
	}
}
function availableSchoolsListBlur(ctl) {
	var btn = document.getElementById('btnAdd');
	if (btn) { btn.className = 'disabled'; }
}
//------------------------------------------------------------------------------------------------------------------------------------------------
function customerSchoolsListFocus(ctl) {
	var CustSel = ctl;
	var btn = document.getElementById('btnRemove');
	if (btn) {
		btn.className = 'disabled';
		for (var i = CustSel.options.length - 1; i >= 0; i--) {
			if ((CustSel.options[i].selected) && (CustSel.options[i].value != '-1')) {
				btn.className = 'enabled';
				return;
			}
		}
	}
}
function customerSchoolsListBlur(ctl) {
	var btn = document.getElementById('btnRemove');
	if (btn) { btn.className = 'disabled'; }
}
//------------------------------------------------------------------------------------------------------------------------------------------------
function addToCustomerList(ctl, avail_ListID, cust_ListID, clientavail_ListID, clientcust_ListID) {
//    if (document.getElementById(cust_ListID).options.length > 0) {
//        document.getElementById('schoolMessage').style.display = '';
//        return;
//    }
	var src = document.getElementById(avail_ListID);
	var dst = document.getElementById(cust_ListID);
	for (var i = src.options.length - 1; i >= 0; i--) {
		if (src.options[i].selected == true) {
			var theAList = document.getElementById(clientavail_ListID);
			var theCList = document.getElementById(clientcust_ListID);
			var itemStr = src.options[i].text + '~' + src.options[i].value + '|';
			if (theCList.value.indexOf(itemStr) == -1) {
				//remove item from hidden Available list
				theAList.value = theAList.value.replace(itemStr, '');
				//add item to Customer list
				dst.options[dst.options.length] = new Option(src.options[i].text, src.options[i].value);
				//add item to hidden Customer list
				theCList.value = theCList.value + itemStr;
				//remove item from Available list
				src.remove(i);
			}
		}
	}
	 //if Available list IS empty then add 'no schools' item
	 if (src.options.length === 0) { src.options[src.options.length] = new Option('No Schools Currently Available', '-1'); }
	 //if Customer list WAS empty then remove 'no schools' item from list
	 if (dst.options.length > 1) { for (var i = dst.options.length - 1; i >= 0; i--) { if (dst.options[i].value === '-1') { dst.remove(i); } } }
}
//------------------------------------------------------------------------------------------------------------------------------------------------
function removeFromCustomerList(ctl, avail_ListID, cust_ListID, clientavail_ListID, clientcust_ListID) {
	var src = document.getElementById(cust_ListID);
	var dst = document.getElementById(avail_ListID);
	for (var i = src.options.length - 1; i >= 0; i--) {
		if (src.options[i].selected == true) {
			var theCList = document.getElementById(clientcust_ListID);
			var theAList = document.getElementById(clientavail_ListID);
			var itemStr = src.options[i].text + '~' + src.options[i].value + '|';
			if (theAList.value.indexOf(itemStr) == -1) {
				//remove item from hidden Customer list
				theCList.value = theCList.value.replace(itemStr, '');
				//add item to Available list
				dst.options[dst.options.length] = new Option(src.options[i].text, src.options[i].value);
				//add item to hidden Available list
				theAList.value = theAList.value + itemStr;
				//remove item from Customer list
				src.remove(i);
			}
		}
	}
	 //if Available list WAS empty then remove 'no schools' item from list
	 if (dst.options.length > 1) { for (var i = dst.options.length - 1; i >= 0; i--) { if (dst.options[i].value === '-1') { dst.remove(i); } } }
	 //if Customer list IS empty then add 'no schools' item
	 if (src.options.length === 0) { src.options[src.options.length] = new Option('No Schools Currently Selected', '-1'); }
}

