﻿// JScript File

//to limit number of selections in multiple select list box
function limitOptions(oSel, howmany) {
	
	var oList1 = document.getElementById(oSel);
	var iCount = 0;
	for (i =0; i<oList1.options.length;i++) {
		if (oList1.options[i].selected)
		{
			iCount = iCount + 1;
			if (iCount > howmany) {
				oList1.options[i].selected = false;
				alert('Maximum ' + howmany + ' items are allowed to select');
				return false;
			}
		}
	}
}


function AddListBoxFromSourceToDestination(SourceSelectbox, DestinationSelectBox, HiddenSource, HiddenDestination, MaxAllowedCountInDestination)
{
    var SSelectBox= document.getElementById(SourceSelectbox);
    var DSelectBox= document.getElementById(DestinationSelectBox);
    var HSource=document.getElementById(HiddenSource);
    var HDestination=document.getElementById(HiddenDestination);
  
    HSource.value='';
    HDestination.value = '';

    var i;
    var iCount = 0;    
    // validation check - max count allowed in destination listbox
    if (MaxAllowedCountInDestination != 0 ) {
    	
    	if (MaxAllowedCountInDestination == DSelectBox.options.length)
    		return 'Maximum ' + MaxAllowedCountInDestination + ' choices are allowed to select';

    	for (i = SSelectBox.options.length - 1; i >= 0; i--) {
    		if (SSelectBox.options[i].selected) {
    			iCount = iCount + 1;

    			if ((iCount + DSelectBox.options.length) > MaxAllowedCountInDestination)
    				return 'Maximum ' + MaxAllowedCountInDestination + ' choices are allowed to select';
    		}
    	}
    }
    
    for(i=SSelectBox.options.length-1;i>=0;i--)
    {
        if(SSelectBox.options[i].selected) {        	
            var optn = document.createElement("OPTION");
            optn.text = SSelectBox.options[i].text;
            optn.value = SSelectBox.options[i].value;
            DSelectBox.options.add(optn);          
        }
    }
    
    for(i=SSelectBox.options.length-1;i>=0;i--)
    {
        if(SSelectBox.options[i].selected)
        {
            SSelectBox.remove(i);
           
        }
        
   }
    for(i=SSelectBox.options.length-1;i>=0;i--)
    {
        if (HSource.value=='')
                HSource.value=SSelectBox.options[i].value;
        else
            HSource.value=HSource.value + ',' + SSelectBox.options[i].value;
    }
    for(i=DSelectBox.options.length-1;i>=0;i--)
    {
         if (HDestination.value=='')
                HDestination.value=DSelectBox.options[i].value;
            else
                HDestination.value=HDestination.value + ',' + DSelectBox.options[i].value;
    }
   //alert('Destination  ' + HDestination.value);
   //alert('Source  ' + HSource.value);
   sortlist(DSelectBox);
   sortlist(SSelectBox);
   return '';
}
function RemoveItemFromDestination(SourceSelectbox,DestinationSelectBox,HiddenSource,HiddenDestination)
{
    var SSelectBox= document.getElementById(SourceSelectbox);
    var DSelectBox= document.getElementById(DestinationSelectBox);
    var HSource=document.getElementById(HiddenSource);
    var HDestination=document.getElementById(HiddenDestination);
  
    HSource.value='';
    HDestination.value='';
    var i;
    for(i=DSelectBox.options.length-1;i>=0;i--)
    {
        if(DSelectBox.options[i].selected)
        {
            var optn = document.createElement("OPTION");
            optn.text = DSelectBox.options[i].text;
            optn.value = DSelectBox.options[i].value;
            SSelectBox.options.add(optn);          
        }
    }
    
    for(i=DSelectBox.options.length-1;i>=0;i--)
    {
        if(DSelectBox.options[i].selected)
        {
            DSelectBox.remove(i);
        }
   }
    for(i=SSelectBox.options.length-1;i>=0;i--)
    {
        if (HSource.value=='')
                HSource.value=SSelectBox.options[i].value;
        else
            HSource.value=HSource.value + ',' + SSelectBox.options[i].value;
    }
    for(i=DSelectBox.options.length-1;i>=0;i--)
    {
         if (HDestination.value=='')
                HDestination.value=DSelectBox.options[i].value;
            else
                HDestination.value=HDestination.value + ',' + DSelectBox.options[i].value;
    }
   
    //alert('Destination  ' + HDestination.value);
   //alert('Source  ' + HSource.value);
   sortlist(DSelectBox);
   sortlist(SSelectBox);
}

/*
Checks if the item was selected by the user
*/
function IsItemSelected(lstBoxID)
{
    var bFound = false;
    var i;
    var lstBox = document.getElementById(lstBoxID);
    for(i=lstBox.options.length-1;i>=0;i--)
    {
        if(lstBox.options[i].selected)
        {
            bFound = true;
            break;
        }
    }

    return bFound;    
}

function OnAdd(SourceSelectbox, DestinationSelectBox, HiddenSource, HiddenDestination, isPost, AlertMessage)
{
    if(IsItemSelected(SourceSelectbox)) {
    	var MaxAllowedCountInDestination = 0;
    	if (AlertMessage != '' && AlertMessage.indexOf('#') != -1)
    	{
    		MaxAllowedCountInDestination = parseInt((AlertMessage.split('#'))[1]);
    		AlertMessage = (AlertMessage.split('#'))[0]
    	}

    	if (MaxAllowedCountInDestination != 0 && DestinationSelectBox.length == MaxAllowedCountInDestination) {
    		alert('Maximum ' + MaxAllowedCountInDestination + ' choices are allowed to select');
		    return false;
	    }
	    var sMsg = AddListBoxFromSourceToDestination(SourceSelectbox, DestinationSelectBox, HiddenSource, HiddenDestination, MaxAllowedCountInDestination);
	    if (sMsg != null && sMsg != '') {
	    	alert(sMsg);
	    	return false;
	    }
        if(isPost != "")
            __doPostBack("UpdateRolesSource","");  
    }
    else
    	alert(AlertMessage);
        
  return false;
}
function OnRemove(SourceSelectbox,DestinationSelectBox,HiddenSource,HiddenDestination,isPost,AlertMessage)
{
if(IsItemSelected(DestinationSelectBox))
{
   RemoveItemFromDestination(SourceSelectbox,DestinationSelectBox,HiddenSource,HiddenDestination);
   if(isPost != "")
       __doPostBack("UpdateRolesSourceDestination","");  
}
else
    alert(AlertMessage);
    
   return false;
}

function sortlist(ListBoxID)
{
     //////////////////////////////////////////////////
   
    var lb=ListBoxID;

    arrTexts = new Array();

    for(i=0; i<lb.length; i++) {
        arrTexts[i] = lb.options[i].text+':'+lb.options[i].value;
    }
    arrTexts.sort();
    for(i=0; i<lb.length; i++) 
    {
        el = arrTexts[i].split(':');
        lb.options[i].text = el[0];
        lb.options[i].value = el[1];     
    }
    //////////////////////////////////////////
    
   
}
function checkListBoxCount(source, args) 
{
    var sourceId = source.id;
    if(sourceId == "ctl00_ContentPlaceHolder1_lstFunctionalArea_cvDestination" || 
        sourceId == "ctl00_ContentPlaceHolder1_lstRole_cvDestination" ||
        sourceId == "ctl00_ContentPlaceHolder1_lstJobLevel_cvDestination" || 
        sourceId == "ctl00_ContentPlaceHolder1_lstCountry_cvDestination" ||
        sourceId == "ctl00_ContentPlaceHolder1_lstCity_cvDestination" ||
        sourceId == "ctl00_ContentPlaceHolder1_lstSpecialization_cvDestination")
    {
       var lstdestination;
       var isCountrySeleted = 0;
       switch(sourceId)
       {
           case "ctl00_ContentPlaceHolder1_lstFunctionalArea_cvDestination":
                lstdestination = "ctl00_ContentPlaceHolder1_lstFunctionalArea_lstDestination";
                break;
           case "ctl00_ContentPlaceHolder1_lstRole_cvDestination":
                lstdestination = "ctl00_ContentPlaceHolder1_lstRole_lstDestination";
                break;
           case "ctl00_ContentPlaceHolder1_lstJobLevel_cvDestination":
                lstdestination = "ctl00_ContentPlaceHolder1_lstJobLevel_lstDestination";
                break;
          case "ctl00_ContentPlaceHolder1_lstCountry_cvDestination":
                lstdestination = "ctl00_ContentPlaceHolder1_lstCountry_lstDestination";
                break;
          case "ctl00_ContentPlaceHolder1_lstCity_cvDestination":
                lstdestination = "ctl00_ContentPlaceHolder1_lstCity_lstDestination";
                break;
         case "ctl00_ContentPlaceHolder1_lstSpecialization_cvDestination":
                lstdestination = "ctl00_ContentPlaceHolder1_lstSpecialization_lstDestination";
                break;
       }
       
       var ctlToValidate = document.getElementById(lstdestination);  
      
       if(lstdestination == "ctl00_ContentPlaceHolder1_lstCity_lstDestination")
       {
           var rbCity = document.getElementById("ctl00_ContentPlaceHolder1_rdbCity");
           
           if(rbCity.checked)
           {
               if(ctlToValidate.options.length > 0)
                   args.IsValid = true;  
               else
                   args.IsValid = false;  
           }
           else
               args.IsValid = true; 
       }                
       else if(lstdestination == "ctl00_ContentPlaceHolder1_lstCountry_lstDestination")
       { 
           var rbCountry = document.getElementById("ctl00_ContentPlaceHolder1_rdbCountry");
                
           if(rbCountry.checked)
           {
               if(ctlToValidate.options.length > 0)
                   args.IsValid = true;  
               else
                   args.IsValid = false;  
           }
           else
               args.IsValid = true; 
       }  
       else if(lstdestination == "ctl00_ContentPlaceHolder1_lstSpecialization_lstDestination")
       {
           var rbSpecific = document.getElementById("ctl00_ContentPlaceHolder1_rdbSpecific");
                     
           if(rbSpecific.checked)
           {
               if(ctlToValidate.options.length > 0)
                   args.IsValid = true;  
               else
                   args.IsValid = false;  
           }
           else
               args.IsValid = true; 
       }           
       else
       {
           if(ctlToValidate.options.length > 0)
               args.IsValid = true;  
           else
               args.IsValid = false;  
       }            
       return args.IsValid;
    }
    return true;
}
