﻿// JScript File

/*********************************************************************
       searchForVendorsToChangeType
*********************************************************************/
function searchForVendorsToChangeType(vendorNameIntputTagId, vendorSearchResultStatusTagId, vendorSearchResultDisplayDivId, vendorSearchResultDisplayType){
            var displayElements = new Array(2);
            displayElements[0]='changeVendorTypeInstructions';
            displayElements[1]='changeVendorTypeButton';

            searchForVendorsAndDisplayResults(displayElements, vendorNameIntputTagId, vendorSearchResultStatusTagId, vendorSearchResultDisplayDivId, vendorSearchResultDisplayType)
 }

 function doPost(form, url) {
     form.action = url;
     form.submit();
 }

/*********************************************************************
       toggleElementDisplay
*********************************************************************/ 
function toggleElementDisplay(displayElements, show){
            for(var i=0; i<displayElements.length; i++){
                if(show){
                    document.getElementById(displayElements[i]).style.display = 'block';
                }else{
                    document.getElementById(displayElements[i]).style.display = 'none';
                }                
            }
}
        

/*********************************************************************
       toggleDisplayBasedOnAjaxResults
**********************************************************************/        
 function toggleDisplayBasedOnAjaxResults(displayElements){
            if(document.getElementById('vendorSearchResultNumberFound').value.length > 0){
                if(document.getElementById('vendorSearchResultNumberFound').value > 0){
		    var elements = new Array(2);
		    elements = displayElements.split(",");
                    toggleElementDisplay(elements, true);
                    document.getElementById('vendorSearchResultNumberFound').value = '';
                }
            }else{
                setTimeout('toggleDisplayBasedOnAjaxResults(\'' + displayElements + '\')',500);
            }
  }
  
 
/*********************************************************************
       searchForVendors
**********************************************************************/   
 function searchForVendors(vendorNameIntputTagId, vendorSearchResultStatusTagId, vendorSearchResultDisplayDivId, vendorSearchResultDisplayType){
            var vendorName = document.getElementById(vendorNameIntputTagId).value;            
            setVendorSearchTags(vendorSearchResultStatusTagId, vendorSearchResultDisplayDivId, vendorSearchResultDisplayType);	        
            CreateCommand('12', vendorName);
 }
        
        
 /*********************************************************************
      setVendorSearchTags
**********************************************************************/       
 function setVendorSearchTags(vendorSearchResultStatusTagId, vendorSearchResultDisplayDivId, vendorSearchResultDisplayType){
            document.getElementById('vendorSearchStatusTag').value = vendorSearchResultStatusTagId;
            document.getElementById('vendorSearchResultDisplayDiv').value = vendorSearchResultDisplayDivId;
            document.getElementById('vendorSearchResultDisplayType').value = vendorSearchResultDisplayType;
  }
  
  
 /*********************************************************************
      ToggleFaxAffinityEditRow
**********************************************************************/    
  function ToggleFaxAffinityEditRow(show, divid) {
		    HideFaxAffinityMessageDisplay();
		    var canSee;
		    if(navigator.appName.indexOf("Microsoft") > -1){
			canSee = 'block';
		    } else {
			canSee = 'table-row';
		    }
		    var editRow = document.getElementById("faxAffinityEditRow" + divid);
		    var displayRow = document.getElementById("faxAffinityDisplayRow" + divid);
		    if (show) {
			editRow.style.display = canSee;
			editRow.style.visibility = 'visible';
			displayRow.style.display = 'none';
			displayRow.style.visibility = 'hidden';
		    } else {
			editRow.style.display = 'none';
			editRow.style.visibility = 'hidden';
			displayRow.style.display = 'block';
			displayRow.style.visibility = 'visible';
		    }
		}

/*********************************************************************
      ToggleFaxAffinityAddRow
**********************************************************************/  	
function ToggleFaxAffinityAddRow(show) {
		
		    HideFaxAffinityMessageDisplay();
		    var addRow = document.getElementById("faxAffinityAddRow");
		    if (show) {
			addRow.style.display = 'block';
			addRow.style.visibility = 'visible';
		    } else {
			addRow.style.display = 'none';
			addRow.style.visibility = 'hidden';
		    }
	
		}
		

/*********************************************************************
      HideFaxAffinityDisplayRow
**********************************************************************/  	
function HideFaxAffinityDisplayRow(divid) {
		    HideFaxAffinityMessageDisplay();
		    document.getElementById("faxAffinityDisplayRow" + divid).style.display = 'none';
		    document.getElementById("faxAffinityDisplayRow" + divid).style.visibility = 'hidden';
}
		

/*********************************************************************
      HideFaxAffinityMessageDisplay
**********************************************************************/  		
function HideFaxAffinityMessageDisplay() {
		    if(document.getElementById("faxAffinityMessageDisplay") != null)
		    {
			document.getElementById("faxAffinityMessageDisplay").style.visibility = 'hidden';
		    }
}


/*********************************************************************
      ToggleFaxServerAddRow
**********************************************************************/ 
		function ToggleFaxServerAddRow(show) {
		    			
		    HideFaxServerMessageDisplay();
		    var addRow = document.getElementById("faxServerAddRow");
		    if (show) {
			addRow.style.display = 'block';
			addRow.style.visibility = 'visible';
		    } else {
			addRow.style.display = 'none';
			addRow.style.visibility = 'hidden';
		    }
		}
		
		
/*********************************************************************
      HideFaxServerMessageDisplay
**********************************************************************/
		function HideFaxServerMessageDisplay() {
		    if(document.getElementById("faxServerMessageDisplay") != null)
		    {
			document.getElementById("faxServerMessageDisplay").style.visibility = 'hidden';
		    }
		}
		
		
/*********************************************************************
      HideFaxServerDisplayRow
**********************************************************************/
		function HideFaxServerDisplayRow(divid) {
		    HideFaxServerMessageDisplay();
		    document.getElementById("faxServerDisplayRow" + divid).style.display = 'none';
		    document.getElementById("faxServerDisplayRow" + divid).style.visibility = 'hidden';
		}		
		    

/*********************************************************************
      ToggleFaxServerEditRow
**********************************************************************/		
function ToggleFaxServerEditRow(show, divid) {
		    HideFaxServerMessageDisplay();
		    var canSee;
		    if(navigator.appName.indexOf("Microsoft") > -1){
			canSee = 'block';
		    } else {
			canSee = 'table-row';
		    }
		    var editRow = document.getElementById("faxServerEditRow" + divid);
		    var displayRow = document.getElementById("faxServerDisplayRow" + divid);
		    if (show) {
			editRow.style.display = canSee;
			editRow.style.visibility = 'visible';
			displayRow.style.display = 'none';
			displayRow.style.visibility = 'hidden';
		    } else {
			editRow.style.display = 'none';
			editRow.style.visibility = 'hidden';
			displayRow.style.display = 'block';
			displayRow.style.visibility = 'visible';
		    }
		}
		

/*********************************************************************
      doValidate
**********************************************************************/				
	function doValidate(form, action){
	    var oopenTime = new Date(document.SystemFunctions.openDate.value + " " + document.SystemFunctions.openTime.value);
	    var ddeliveryTime = new Date(document.SystemFunctions.deliveryDate.value + " " + document.SystemFunctions.deliveryTime.value);
	    var ccloseTime = new Date(document.SystemFunctions.closeDate.value + " " + document.SystemFunctions.closeTime.value);
	    var bErr = false;
	    var sMsg = "";
	    if (oopenTime>=ccloseTime){
		bErr=true;
		sMsg+="Open time must not be equal to or greater than close time.\n\n";
	    }
	    if(ccloseTime>=ddeliveryTime){
		bErr = true;
		sMsg+="Close time must not be equal to or greater than delivery time.\n\n";
	    }
	    if (ddeliveryTime.valueOf()-ccloseTime.valueOf()<3600000){
		bErr=true;
		sMsg+="Please note that the Close Time for Group Orders must be at least 1 hour before the Delivery Time";
	    }
	    
	    if(bErr){
		alert(sMsg);
	    }else{
		document.SystemFunctions.updateGroupOrder.value = "updateGroupOrder";
		form.action = 'UpdateSystemEmergency.m';
		form.submit();
	    }
	}	
	
	
/*********************************************************************
      searchVendors
**********************************************************************/	
 function searchVendors(vendorNameIntputTagId, vendorSearchResultStatusTagId, vendorSearchResultDisplayDivId, vendorSearchResultDisplayType){
            var displayElements = new Array(2);
            displayElements[0]='closeVendorInstructions';
            displayElements[1]='closeVendorButton';

            searchForVendorsAndDisplayResults(displayElements, vendorNameIntputTagId, vendorSearchResultStatusTagId, vendorSearchResultDisplayDivId, vendorSearchResultDisplayType)
   }
   
 
/*********************************************************************
      closeVendorLocations
**********************************************************************/	  
 function closeVendorLocations() {        
	    var resultVendorLocationIds = document.getElementsByName('resultVendorLocationId');
	    var selectedVendorLoactionId;
	    for(var i=0; i<resultVendorLocationIds.length; i++){
                if(resultVendorLocationIds[i].checked){
                    selectedVendorLoactionId = resultVendorLocationIds[i].value;
                    document.getElementById('vendorLocationId').value = selectedVendorLoactionId;                                        
                    var vendorLocationNames = document.getElementById("vendorLocationIDNames").value.split(",");
                    for(var i=0; i<vendorLocationNames.length; i++){
			if(selectedVendorLoactionId == vendorLocationNames[i]){
			    var vendorName = document.getElementById('vendorName').value;
			    document.getElementById('vendorName').value = vendorName + "-" + vendorLocationNames[i+1];
			    break;
			}                    
                    }                    
                    break;
                }
            }
                                 
            var elementArray = new Array(2);   
	    elementArray[0]='closeVendorLocations';
	    elementArray[1]='closeVendorLocationButton';
	    toggleElementDisplay(elementArray, false);
            document.getElementById('vendorLocationSearchResults').innerHTML = '';
 }
        
 
/*********************************************************************
     SetShowReviewValue
**********************************************************************/	       
 function SetShowReviewValue()
 {
	 document.getElementById("showReview").value = document.getElementById('showReviewCheckBox').checked;
 } 
 
 
/*********************************************************************
                   GetTotalOfPercentages
**********************************************************************/	 
 function GetTotalOfPercentages(){
	    var totalValues = 0;
	    for (var i = 0; i < guids.length; i++){
		var guid = guids[i];
		//total the value of each valueBox
		totalValues += parseInt(document.getElementById("txtSegmentSubGroupPercentage_" + guid).value);
	    }
	    return totalValues;
}


/*********************************************************************
                 RecalibrateUnLockedSliders
**********************************************************************/		
function RecalibrateUnLockedSliders(currentGuid){
	    var currentSliderValueBox = document.getElementById("txtSegmentSubGroupPercentage_" + currentGuid);
	    var numberOfUnlockedSliders = 0;
	    
	    var totalValues = GetTotalOfPercentages();
	    var slideDirectionOfOtherSliders = 0;
	    
	    if (totalValues > 100){
		slideDirectionOfOtherSliders = -1; //negative slide
	    }else if (totalValues < 0){
		slideDirectionOfOtherSliders = 1; //positive slide
	    }else if (totalValues == 100){
		//alert('total is 100');
		return;
	    }
	    for (var i = 0; i < guids.length; i++){
		var guid = guids[i];
		//count the number of unlocked valueBoxes
		if (currentGuid == null){
		    if (!IsLocked(guid, slideDirectionOfOtherSliders)){
			numberOfUnlockedSliders++;	
		    }
		}else{
		    if (currentGuid != guid){
			if (!IsLocked(guid, slideDirectionOfOtherSliders)){
			    numberOfUnlockedSliders++;	
			}
		    }
		}
		
	    }

	    //if the total of the valueBoxes was not equal to 100
	    //alert (numberOfUnlockedSliders + " unlocked sliders");
	    if (totalValues != 100){
		if (currentSliderValueBox != null){
		    //tweak the current Slider value so the difference is divisible evenly
		    for (var i = 0; i < 10; i++){
			if ((totalValues - parseInt(currentSliderValueBox.value)) % numberOfUnlockedSliders == 0){
			    break;
			}
			if (parseInt(currentSliderValueBox.value) > 10){
			    currentSliderValueBox.value = parseInt(currentSliderValueBox.value) - 1;
			    totalValues--;
			}else{
			    currentSliderValueBox.value = parseInt(currentSliderValueBox.value) + 1;
			    totalValues++;
			}
		    }
		}
		
		//decrement or increment the unlocked valuebox by the divided difference
		var slice = 0;
		
		slice = (100 - totalValues) / numberOfUnlockedSliders;
		slice = (100 - totalValues) / numberOfUnlockedSliders;
		slice = Math.round(slice);
		//alert ("slice: " + slice);
		var leftOvers = 0;
		for (var j = 0; j < 20; j++){
		    if (j != 0 && leftOvers == 0){
			break;
		    }else if (leftOvers != 0){
			slice = Math.round(leftOvers / numberOfUnlockedSliders);
		    }
		    for (var i = 0; i < guids.length; i++){
			var guid = guids[i];
			//if the valueBox is unlocked
			if (guid != currentGuid && !IsLocked(guid, slice)){
			    var previousValue = parseInt(document.getElementById("txtSegmentSubGroupPercentage_" + guid).value);
    			
			    if (previousValue + slice > 100){ 
				document.getElementById("txtSegmentSubGroupPercentage_" + guid).value = 100;
				leftOvers += previousValue + slice - 100;
			    }else if (previousValue + slice < 0){
				document.getElementById("txtSegmentSubGroupPercentage_" + guid).value = 0;
				leftOvers += slice - previousValue;
			    }else{
				document.getElementById("txtSegmentSubGroupPercentage_" + guid).value = previousValue + slice;
			    }			
    			
			}
		    }
		}
	    }
	    SyncAll(currentGuid);
	    
	    //check for illegal actions
	    if (currentSliderValueBox != null){
		var newTotal = GetTotalOfPercentages();
		if (newTotal > 100){
		    currentSliderValueBox.value = parseInt(currentSliderValueBox.value) - (newTotal - 100);
		    SyncAll(currentGuid);
		}else if (totalValues < 100){
		    currentSliderValueBox.value = parseInt(currentSliderValueBox.value) + (newTotal - 100);
		    SyncAll(currentGuid);
		}
	    }
}



/*********************************************************************
                         IsLocked
**********************************************************************/	
function IsLocked(guid, slideDirection){
	    if (document.getElementById("txtSegmentSubGroupLockStatus_" + guid).value == "True"){
		//alert(guid + " is locked1");
		return true;
	    }
	    
	    if (slideDirection != null){
		var currentValue = parseInt(document.getElementById("txtSegmentSubGroupPercentage_" + guid));
		if (currentValue >= 100 && slideDirection > 0){
		    //alert(guid + " is locked2");
		    return true;
		}else if (currentValue <= 0 && slideDirection < 0){
		    //alert(guid + " is locked3");
		    return true;
    		
		}
	    }
	    
	   
	    var numberOfOtherUnlockedSliders = 0;
	    for (var i = 0; i < guids.length; i++){
		var otherGuid = guids[i];
		if (otherGuid != guid){
		    if (document.getElementById("txtSegmentSubGroupLockStatus_" + otherGuid).value != "True"){
			numberOfOtherUnlockedSliders++;
		    }
		}
	    }
	    if (numberOfOtherUnlockedSliders < 1){
		//alert(guid + " is locked4");
		return true;
	    }
	    
	    
	    return false;
}


/*********************************************************************
                         ShowTotal
**********************************************************************/	
function ShowTotal(){
	    document.getElementById("lblTotalPercentage").innerHTML = GetTotalOfPercentages() + "%";
}


/*********************************************************************
                        ToggleLock
**********************************************************************/	
function ToggleLock(image, guid){
    	    var lockBox = document.getElementById("txtSegmentSubGroupLockStatus_" + guid);
    	    if (lockBox.value == "True"){
    		image.src = "../images/LockButton.jpg";
    		lockBox.value = "False";
    	    }else{
    		image.src = "../images/LockButton_Pressed.jpg";
    		lockBox.value = "True";
    	    }
  }
  
 
 /*********************************************************************
                       validateField
**********************************************************************/	 
 function validateField(oChanged, oStatic) {
	if (oChanged.value == "") {
	    oChanged.value = "None";
	}

	if (oChanged.value != "None") {
	    oStatic.value = "None";
	}	
 }
    

 /*********************************************************************
                      ResetDistrFields
**********************************************************************/	 
 function ResetDistrFields() {
	document.getElementById('Recipients').value = "None";
    	document.getElementById('FileDestination').value = "None";
 }
 
 
 /*********************************************************************
                      fieldType_changed
**********************************************************************/	
function fieldType_changed(type){
					    document.getElementById("getList").style.display='none';
					    document.getElementById("getChecked").style.display='none';
					    if(type=='checkBox'){
						document.getElementById("getChecked").style.display='';
						document.getElementById("getDefault").style.display='none';
					    } else {
						document.getElementById("getDefault").style.display='';
						if(type=='dropDown'|type=='radio'){
						    document.getElementById("getList").style.display='';
						}
					    }
}


/*********************************************************************
                      deleteElement
**********************************************************************/	
function deleteElement(elementName){
					    document.getElementById("customFields").FieldName.value=elementName;
					    document.getElementById("customFields").action='DeleteCustomField.m';
					    document.getElementById("customFields").submit();
}


/*********************************************************************
                      populateElementFields
**********************************************************************/
function populateElementFields(fieldName, valueList, checkBoxChecked, defaultValue, fieldType, filter) {
				            document.getElementById("FieldName").value = fieldName;
				            document.getElementById("ValueList").value = valueList;
				            if(checkBoxChecked='Y'){
				                document.getElementById("CheckBoxChecked").Checked = true;
				            } else {
				                document.getElementById("CheckBoxChecked").Checked = false;
				            }
				            document.getElementById("DefaultValue").value = defaultValue;
				            document.getElementById("FieldType").value = fieldType;
				            document.getElementById("Filter").value = filter;
				            document.getElementById("submitCustomFields").value="Update";
				            fieldType_changed(document.getElementById("FieldType").value);
}


/*********************************************************************
                      alertUnableToAddFirms
**********************************************************************/
function alertUnableToAddFirms(alertList) {
	if (alertList != "") {
	    alert("Unable to add the following firms because they weren't in the discount vendor(s) service area. \n" + alertList + "\n\nNOTE: The listed firms have been removed from the list.  Submit the page again to save or test the promotion.");
	}   
 }
    

/*********************************************************************
                     CheckForDiscount
**********************************************************************/   
function CheckForDiscount() {
	if (document.forms["VLP"].item("DiscountId").value == "UNSET") {
	    alert("Please select a discount from the drop down list first.");
	    return false;
	}	
 }
 

/*********************************************************************
                   RemoveQuestion
**********************************************************************/  
function RemoveQuestion(){
	if (confirm("Are you sure you want to remove this question?")){
	    document.getElementById('SurveyQuestions').remove(document.getElementById('SurveyQuestions').selectedIndex);
	    EnableOrDisableQuestionEditorButtons();
	}
}



/*********************************************************************
                   SelectAllQuestions
**********************************************************************/  
function SelectAllQuestions(){
	var listbox = document.getElementById("SurveyQuestions");
	for (var i = 0; i < listbox.options.length; i++) {
	    listbox.options[i].selected = true;
	}
}


/*********************************************************************
                  MoveQuestion
**********************************************************************/  
function MoveQuestion(direction){
      var listbox = document.getElementById("SurveyQuestions");
      var index = listbox.selectedIndex;
      if (index == -1) {
	alert("You must first select the item to reorder.");
      }else {
	var nextIndex = index + (direction == "Up" ? -1 : 1);
	if (nextIndex < 0) {
	    nextIndex = listbox.length - 1;
	}
	if (nextIndex >= listbox.length) {
	    nextIndex = 0;
	}
	var oldVal = listbox[index].value;
	var oldText = listbox[index].text;
	listbox[index].value = listbox[nextIndex].value;
	listbox[index].text = listbox[nextIndex].text;
	listbox[nextIndex].value = oldVal;
	listbox[nextIndex].text = oldText;
	listbox.selectedIndex = nextIndex;
        
	EnableOrDisableQuestionEditorButtons();
      }
}



/*********************************************************************
                EnableOrDisableQuestionEditorButtons
**********************************************************************/  
function EnableOrDisableQuestionEditorButtons(){
        
	var listbox = document.getElementById("SurveyQuestions");
	var itemIsSelected = false;
	if (listbox.selectedIndex >= 0){
	    itemIsSelected = true;
	}
        
	document.getElementById("MoveQuestionUpButton").disabled = !itemIsSelected;
	document.getElementById("MoveQuestionDownButton").disabled = !itemIsSelected;
	document.getElementById("RemoveQuestionButton").disabled = !itemIsSelected;
        
	if (listbox.selectedIndex == listbox.options.length - 1){
	    //last item in the list can't be moved down
	    document.getElementById("MoveQuestionDownButton").disabled = true;
	}
	if (listbox.selectedIndex == 0){
	    //first item in the list can't be moved up
	    document.getElementById("MoveQuestionUpButton").disabled = true;
	}
        
    }



/*********************************************************************
                EnableOrDisableQuestionAddButton
**********************************************************************/ 
function EnableOrDisableQuestionAddButton(){
	var dropDown = document.getElementById("AddQuestionId");
	document.getElementById("AddQuestionButton").disabled = dropDown.selectedIndex <= 0;
 }


/*********************************************************************
              AddNewQuestion
**********************************************************************/
function AddNewQuestion(){
	var option = document.createElement("OPTION");
	var dropDown = document.getElementById("AddQuestionId");
	option.text = dropDown[dropDown.selectedIndex].text;
	option.value = dropDown[dropDown.selectedIndex].value;
	dropDown.selectedIndex = 0;
	document.getElementById("SurveyQuestions").options.add(option);
	EnableOrDisableQuestionEditorButtons();
	EnableOrDisableQuestionAddButton();
 }
  
  
/*********************************************************************
              RemoveOption
**********************************************************************/  
function RemoveOption(){
    if (confirm("Are you sure you want to remove this option?")){
	document.getElementById('QuestionOptions').remove(document.getElementById('QuestionOptions').selectedIndex);
	EnableOrDisableOptionEditorButtons();
    }
}


/*********************************************************************
              SelectAllOptions
**********************************************************************/ 
function SelectAllOptions(){
    var listbox = document.getElementById("QuestionOptions");
    for (var i = 0; i < listbox.options.length; i++) {
	listbox.options[i].selected = true;
    }
}


/*********************************************************************
              ToggleOptionEditor
**********************************************************************/ 
function ToggleOptionEditor(){
    var questionTypeDropdown = document.getElementById("QuestionType");
    var selectedValue = questionTypeDropdown.options[questionTypeDropdown.selectedIndex].value;
    var visible = selectedValue == "Single" || selectedValue == "Multiple";
    var optionEditor = document.getElementById("optionRow");
    SetVisibility(optionEditor, visible);
    var followupSurveyField = document.getElementById("FollowupSurveyPanel");
    if (selectedValue == "Single"){
	SetVisibility(followupSurveyField, true);	
    }else{
	SetVisibility(followupSurveyField, false);	
    }
    EnableOrDisableOptionEditorButtons();
} 



/*********************************************************************
             SetVisibility
**********************************************************************/ 
function SetVisibility(element, visibility){
    if (visibility){
	element.style.visibility = 'visible';
	element.style.display = 'block';
    }else{
	element.style.visibility = 'hidden';
	element.style.display = 'none';
    }
}


/*********************************************************************
              MoveOption
**********************************************************************/
function MoveOption(direction){
  var listbox = document.getElementById("QuestionOptions");
  var index = listbox.selectedIndex;
  if (index == -1) {
    alert("You must first select the item to reorder.");
  }else {
    var nextIndex = index + (direction == "Up" ? -1 : 1);
    if (nextIndex < 0) {
	nextIndex = listbox.length - 1;
    }
    if (nextIndex >= listbox.length) {
	nextIndex = 0;
    }
    var oldVal = listbox[index].value;
    var oldText = listbox[index].text;
    listbox[index].value = listbox[nextIndex].value;
    listbox[index].text = listbox[nextIndex].text;
    listbox[nextIndex].value = oldVal;
    listbox[nextIndex].text = oldText;
    listbox.selectedIndex = nextIndex;
    
    EnableOrDisableOptionEditorButtons();
  }
}


/*********************************************************************
             EnableOrDisableOptionEditorButtons
**********************************************************************/
function EnableOrDisableOptionEditorButtons(){
    
    var listbox = document.getElementById("QuestionOptions");
    var itemIsSelected = false;
    if (listbox.selectedIndex >= 0){
	itemIsSelected = true;
    }
    
    document.getElementById("MoveOptionUpButton").disabled = !itemIsSelected;
    document.getElementById("MoveOptionDownButton").disabled = !itemIsSelected;
    document.getElementById("RemoveOptionButton").disabled = !itemIsSelected;
    
    if (listbox.selectedIndex == listbox.options.length - 1){
	//last item in the list can't be moved down
	document.getElementById("MoveOptionDownButton").disabled = true;
    }
    if (listbox.selectedIndex == 0){
	//first item in the list can't be moved up
	document.getElementById("MoveOptionUpButton").disabled = true;
    }
    
}


/*********************************************************************
            EnableOrDisableOptionAddButton
**********************************************************************/
function EnableOrDisableOptionAddButton(){
    var textBox = document.getElementById("AddOption");
    document.getElementById("AddOptionButton").disabled = textBox.value == "";
}



/*********************************************************************
            AddNewOption
**********************************************************************/
function AddNewOption(){
    var option = document.createElement("OPTION");
    var textBox = document.getElementById("AddOption");
    var followupSurveyDropdown = document.getElementById("FollowupSurveyId");
    

    
    var optionText = textBox.value;
    var questionType = document.getElementById("QuestionType").options[document.getElementById("QuestionType").selectedIndex].value;
    var followupSurveyName = "";
    var followupSurveyId = "";
	
    if (questionType == "Single" && followupSurveyDropdown.selectedIndex >= 1){
	followupSurveyName = " (Followup Survey: " + followupSurveyDropdown.options[followupSurveyDropdown.selectedIndex].text + ")";
	followupSurveyId = followupSurveyDropdown.options[followupSurveyDropdown.selectedIndex].value;
    }
    
    option.text = optionText + followupSurveyName;
    option.value = "~" + optionText.replace(",", "&comma;") + "~" + followupSurveyId;
    

    //clear the form
    textBox.value = "";
    followupSurveyDropdown.selectedIndex = 0;
    
    document.getElementById("QuestionOptions").options.add(option);
    EnableOrDisableOptionEditorButtons();
} 


/*********************************************************************
            searchForVendorSource
**********************************************************************/		
function searchForVendorSource() {
				var vendorName = document.getElementById('sourceSearchVendor').value;
				document.getElementById('searchType').value = "VendorSource";
				CreateCommand('10', vendorName);
}


/*********************************************************************
            searchForVendorDest
**********************************************************************/				
function searchForVendorDest() {
				var vendorName = document.getElementById('destSearchVendor').value;
				document.getElementById('searchType').value = "VendorDest";
				CreateCommand('10', vendorName);
}


/*********************************************************************
            uiCopyMenu
**********************************************************************/			
function uiCopyMenu() {
				addSrc();
				addDest();				
				var srcVId = document.getElementById('srcChoice');
				var destVId = document.getElementById('destChoice');
				if (srcVId.value != '' || destVId.value != '') {
				    disableSearchButtons();
				    CreateCommand('11', srcVId);
				} else {
				    alert('Please select the source and destination vendors to perform the menu copy');
				}			
}


/*********************************************************************
            disableSearchButtons
**********************************************************************/				
function disableSearchButtons() {
				document.menuCopyForm.destSearchButton.disabled = true;
				document.menuCopyForm.sourceSearchButton.disabled = true;
				document.menuCopyForm.copyButton.disabled = true;
}


/*********************************************************************
            enableSearchButtons
**********************************************************************/				
function enableSearchButtons() {
				document.menuCopyForm.destSearchButton.disabled = false;
				document.menuCopyForm.sourceSearchButton.disabled = false;
				document.menuCopyForm.copyButton.disabled = false;
}

			
/*********************************************************************
            addSrc
**********************************************************************/				
function addSrc() {
				var vendorId = '';
				var sourceChoices = document.getElementsByName('SourceChoices');
				for (var i=0; i < sourceChoices.length; i++) {
					if (sourceChoices[i].checked) {
						vendorId = sourceChoices[i].value;
						break;
					}
				}
				document.getElementById('srcChoice').value = vendorId;
}

			
/*********************************************************************
            addDest
**********************************************************************/				
function addDest() {
			    var vendorIds = '';
    			
			    var DestinationChoices = document.getElementsByName('DestinationChoices');
			    for(var i=0; i < DestinationChoices.length; i++) {
				if(DestinationChoices[i].checked) {
				    if (vendorIds.length>0) {
					vendorIds += ",";
				    }
				    vendorIds += DestinationChoices[i].value;
				}
			    }
			    document.getElementById('destChoice').value = vendorIds;
}

			

/*********************************************************************
            disableTargetVendor
**********************************************************************/	
function disableTargetVendor(vendorId){
			    var destinations=document.getElementsByName('DestinationChoices');
			    for(var i=0; i < destinations.length; i++){
				if(destinations[i].id=='destChoice_'+vendorId){
				    destinations[i].checked=false;
				    destinations[i].disabled=true;
				} else {
    				    destinations[i].disabled=false;
    				}
			    }
}


/*********************************************************************
            searchForVendorsToClose
**********************************************************************/	
function searchForVendorsToClose(vendorNameIntputTagId, vendorSearchResultStatusTagId, vendorSearchResultDisplayDivId, vendorSearchResultDisplayType){
            var displayElements = new Array(2);
            displayElements[0]='closeVendorInstructions';
            displayElements[1]='closeVendorButton';

            searchForVendorsAndDisplayResults(displayElements, vendorNameIntputTagId, vendorSearchResultStatusTagId, vendorSearchResultDisplayDivId, vendorSearchResultDisplayType)
 }

 

/*********************************************************************
            sendNotificationEmail
**********************************************************************/	     
function sendNotificationEmail(){
            var orderid = document.getElementById('orderId').value;
	        CreateCommand('4',orderid);
}
        
 
/*********************************************************************
            changeVendorType
**********************************************************************/	        
function changeVendorType(){
           var resultVendorIds = document.getElementsByName('resultVendorId');
          
            var selectedVendorId;
            for(var i=0; i<resultVendorIds.length; i++){
                if(resultVendorIds[i].checked){
                    selectedVendorId = resultVendorIds[i].value;
                    break;
                }
            }
	        CreateCommand('6',selectedVendorId);
}

        
/*********************************************************************
            cancelOrder
**********************************************************************/	        
function cancelOrder() {
	    	var orderId = document.getElementById('cancelOrderId').value;
	    	CreateCommand('7', orderId);
 }


/*********************************************************************
            adjustOrder
**********************************************************************/	
function adjustOrder() {
		var orderId = document.getElementById('adjustOrderId').value;
		CreateCommand('8', orderId);
}
 
 
/*********************************************************************
            viewOrder
**********************************************************************/	        
 function viewOrder() {
	    	var orderid = document.getElementById('orderNumber').value;
	    	CreateCommand('9', orderid);
  }
  
 
 /*********************************************************************
           SetUsesPerUser
**********************************************************************/	  
 function SetUsesPerUser(checkBoxBeingClicked){
		if(checkBoxBeingClicked.checked == true){
		    document.pageForm.usesPerUser.value = 1;
		}
 }
 
 
/*********************************************************************
           saveChanges
**********************************************************************/	 
function saveChanges() {
            doPost(document.pageForm, 'UpdateManageTags.m');
}
        
 
/*********************************************************************
           copyValuesToFields
**********************************************************************/	         
function copyValuesToFields(type,description,expiration,id) {
            document.pageForm.tagId.value = id;
            document.pageForm.tagDescription.value = description;
            document.pageForm.tagExpirationDate.value = expiration;
            
            var selectBox = document.pageForm.tagTypeSelection;
            for (var i=0; i<selectBox.options.length; i++) {
                if (selectBox.options[i].value == type) {
                    selectBox.options[i].selected = true;
                    i = selectBox.options.length;
                }
            }
 }
        
 
/*********************************************************************
           expireTag
**********************************************************************/	        
function expireTag(type,description,id) {
            copyValuesToFields(type,description, '<%=DateTimeType.Today.AddDays(-1).ToString()%>',id);
            doPost(document.pageForm, 'UpdateManageTags.m');
 }
 
 

/*********************************************************************
           SubmitForm
**********************************************************************/	 
function SubmitForm(RequestType) {
	document.myForm.RequestType.value = RequestType;
	document.forms[0].submit;
}


/*********************************************************************
           Back
**********************************************************************/
function Back() {
		document.forms[0].action="EditVendorAgreementGroup.m";
		document.forms[0].submit();		
}
	

/*********************************************************************
           SubmitPage
**********************************************************************/	
function SubmitPage() {
		document.forms[0].AddingVendors.value="Y";
		document.forms[0].submit();		
}


/*********************************************************************
           ViewAccepted
**********************************************************************/	
function ViewAccepted() {
			document.location.href = "VendorAgreementAssignedVendors.m?AgreementId=<%=model.AgreementId%>&mode=ViewAccepted&ReturnTo=EditVendorAgreement";
	    
}
	    
/*********************************************************************
           ViewDeclined
**********************************************************************/	    
function ViewDeclined() {
			document.location.href = "VendorAgreementAssignedVendors.m?AgreementId=<%=model.AgreementId%>&mode=ViewDeclined&ReturnTo=EditVendorAgreement";
	    
}


/*********************************************************************
           searchForVendorsAndDisplayResults
**********************************************************************/
function searchForVendorsAndDisplayResults(displayElements, vendorNameIntputTagId, vendorSearchResultStatusTagId, vendorSearchResultDisplayDivId, vendorSearchResultDisplayType){
            toggleElementDisplay(displayElements, false);
            searchForVendors(vendorNameIntputTagId, vendorSearchResultStatusTagId, vendorSearchResultDisplayDivId, vendorSearchResultDisplayType)
            setTimeout('toggleDisplayBasedOnAjaxResults(\'' + displayElements + '\')',500);
 }