﻿// JScript File

var HiddenAllAdminsArray = new Array();
var HiddenAllGenreArray = new Array();
function ExchangeListBoxItems(objSourceElement, objTargetElement, exchangeType)    
{   var aryTempSourceOptions = new Array();        
    var x = 0; 
    var updateIDs = "";               
    //looping through source element to find selected options        
    for (var i = 0; i < objSourceElement.length; i++) 
    {            
        if (objSourceElement.options[i].selected) 
        {                //need to move this option to target element                
            var intTargetLen = objTargetElement.length++;                
            objTargetElement.options[intTargetLen].text = objSourceElement.options[i].text;                
            objTargetElement.options[intTargetLen].value = objSourceElement.options[i].value;            
        }            
        else 
        {                //storing options that stay to recreate select element                
            var objTempValues = new Object();                
            objTempValues.text = objSourceElement.options[i].text;                
            objTempValues.value = objSourceElement.options[i].value;                
            aryTempSourceOptions[x] = objTempValues;                
            x++;            
       }        
   }                //resetting length of source        
   objSourceElement.length = aryTempSourceOptions.length;        
   //looping through temp array to recreate source select element        
   for (var i = 0; i < aryTempSourceOptions.length; i++) 
   {            
        objSourceElement.options[i].text = aryTempSourceOptions[i].text;            
        objSourceElement.options[i].value = aryTempSourceOptions[i].value;            
        objSourceElement.options[i].selected = false;  
        if ( exchangeType == "RemoveGenreFromJudge" || exchangeType == "RemoveJudgeFromGenre" )
        {
            if (updateIDs == "")
                updateIDs = aryTempSourceOptions[i].value;
            else
                updateIDs = updateIDs + "," + aryTempSourceOptions[i].value;    
        }   
   }
   
   if ( exchangeType == "AddGenreToJudge" || exchangeType == "AddJudgeToGenre" )
   {
        updateIDs = "";
        for (var i = 0; i < objTargetElement.options.length; i++) 
        { 
            if (updateIDs == "")
                 updateIDs = objTargetElement[i].value;
            else
                 updateIDs = updateIDs + "," + objTargetElement[i].value; 
        }        
        
   }        

//   if (HiddenEditRoundIDObj.value == "0")
//       HiddenEditRoundIDObj.value = 1;  
//   alert( HiddenEditGenreIDObj.value);
//   //alert( HiddenEditAdminIDObj.value);
//   alert( HiddenEditRoundIDObj.value);
//     alert( updateIDs);
//   alert( exchangeType );
   
   //return;
   switch (exchangeType)
   {
  	    case "RemoveGenreFromJudge": 
  	    if (parseInt(HiddenEditAdminIDObj.value) == 0)
  	    {
  	        alert("Please hit ‘Save’ and then return to assign this Judge’s genres.");
  	        return;
  	    }
  	    CDBB.Web.UI.Services.AdminService.AddUpdateAdminRound(parseInt(HiddenEditAdminIDObj.value), parseInt(HiddenEditRoundIDObj.value), updateIDs, CallBackDoNothing); 
  	    break;
  	    case "AddGenreToJudge":  
  	    if (parseInt(HiddenEditAdminIDObj.value) == 0)
  	    {
  	        alert("Please hit ‘Save’ and then return to assign this Judge’s genres.");
  	        return;
  	    }
  	    CDBB.Web.UI.Services.AdminService.AddUpdateAdminRound(parseInt(HiddenEditAdminIDObj.value), parseInt(HiddenEditRoundIDObj.value), updateIDs, CallBackDoNothing);
  	    break;
  	    case "RemoveJudgeFromGenre":
  	    CDBB.Web.UI.Services.AdminService.AddUpdateGenreRound(parseInt(HiddenEditGenreIDObj.value), parseInt(HiddenEditRoundIDObj.value), updateIDs, CallBackDoNothing);  
  	    break;
  	    case "AddJudgeToGenre": 
  	    CDBB.Web.UI.Services.AdminService.AddUpdateGenreRound(parseInt(HiddenEditGenreIDObj.value), parseInt(HiddenEditRoundIDObj.value), updateIDs, CallBackDoNothing); 
  	    break;
  	    case "RemoveGenreFromSignUp": 
  	    if (parseInt(HiddenEditSignUpIDObj.value) == 0)
  	    {
  	        alert("Sign up user not found.");
  	        return;
  	    }
  	    //maybe not need at all
  	    //CDBB.Web.UI.Services.AdminService.UpdateSignUpGenres(parseInt(HiddenEditSignUpIDObj.value), parseInt(HiddenEditRoundIDObj.value), updateIDs, CallBackDoNothing); 
  	    break;
  	    case "AddGenreToSignUp":  
  	    if (parseInt(HiddenEditSignUpIDObj.value) == 0)
  	    {
  	        alert("Sign up user not found.");
  	        return;
  	    }
  	    //maybe not need at all
  	    //CDBB.Web.UI.Services.AdminService.UpdateSignUpGenres(parseInt(HiddenEditSignUpIDObj.value), parseInt(HiddenEditRoundIDObj.value), updateIDs, CallBackDoNothing);
  	    break;
   }  
}

function CallBackDoNothing(result)
{
    return;
}
function RemoveTargetHaveSameSource(objSourceElement, objTargetElement)    
{   var aryTempSourceOptions = new Array();        
    var x = 0;                
    //looping through source element to find selected options        
    for (var i = 0; i < objTargetElement.length; i++) 
    {            
             //storing options that stay to recreate select element
        if (IfItemExist( objTargetElement.options[i].value, objSourceElement) == false)
        {                    
            var objTempValues = new Object();                
            objTempValues.text = objTargetElement.options[i].text;                
            objTempValues.value = objTargetElement.options[i].value;                
            aryTempSourceOptions[x] = objTempValues;                
            x++; 
        }           
       
   }                //resetting length of source        
   objTargetElement.length = aryTempSourceOptions.length;        
   //looping through temp array to recreate source select element        
   for (var i = 0; i < aryTempSourceOptions.length; i++) 
   {            
        objTargetElement.options[i].text = aryTempSourceOptions[i].text;            
        objTargetElement.options[i].value = aryTempSourceOptions[i].value;            
        objTargetElement.options[i].selected = false;        
   }    
}

function FillInAdminListBoxForGenre(editGenreID, roundID)
{
    HiddenEditRoundIDObj.value = roundID;
    CDBB.Web.UI.Services.AdminService.GetAdminsForGenreRound(editGenreID, roundID,GetAdminsForGenreResults_SucceededCallback);
}
function GetAdminsForGenreResults_SucceededCallback(result)
{
    loadAllAdmins();
    if(result != null && result != "[]" && result != "")
    {
        var res=Sys.Serialization.JavaScriptSerializer.deserialize(result);
        GenreJudgeListObj.options.length = 0;
        GenreJudgeListObj.options.length = res.length;
        for(var i=0;i<res.length;i++)
        {  
            var rs = res[i];
            if(rs != null)
            {
                GenreJudgeListObj.options[i].text = rs.FirstName + " " + rs.LastName;            
                GenreJudgeListObj.options[i].value = rs.ID;
            }
        }
        RemoveTargetHaveSameSource(GenreJudgeListObj,SourceJudgeListObj);
    }
    else
        GenreJudgeListObj.options.length = 0;
}

function IfItemExist(val, objSourceElement)
{
   var itemExist = false;
   for (var i = 0; i < objSourceElement.length; i++) 
   {            
        if (val == objSourceElement.options[i].value)
        {
            itemExist = true;
            break;
        }           
   }  
   return itemExist; 
}


function loadAllAdmins()
{
    HiddenAllAdminsArray = HiddenAllAdminsList.split('^^');
    SourceJudgeListObj.options.length = 0;
    SourceJudgeListObj.options.length = HiddenAllAdminsArray.length;
    for (var i = 0; i < HiddenAllAdminsArray.length; i++) 
    {            
        var OneAdminInfo = new Array();
        OneAdminInfo = HiddenAllAdminsArray[i].split('^');
        //alert(OneAdminInfo[0]);
        SourceJudgeListObj.options[i].text = OneAdminInfo[0];            
        SourceJudgeListObj.options[i].value = OneAdminInfo[1];            
        SourceJudgeListObj.options[i].selected = false;        
    }    
}

function FillInGenreListBoxForAdmin(editAdminID, roundID)
{
    HiddenEditRoundIDObj.value = roundID;
    CDBB.Web.UI.Services.AdminService.GetAllGenresSimpleWithAdminCount(roundID,GetAllGenresSimpleWithAdminCountResults_SucceededCallback);
    //CDBB.Web.UI.Services.AdminService.GetGenresForAdminRound(editAdminID, roundID,GetGenresForAdminResults_SucceededCallback);
}

function GetGenresForAdminResults_SucceededCallback(result)
{
    //loadAllGenres();
    if(result != null && result != "[]" && result != "")
    {
        var res=Sys.Serialization.JavaScriptSerializer.deserialize(result);
        JudgeGenreListObj.options.length = 0;
        JudgeGenreListObj.options.length = res.length;
        for(var i=0;i<res.length;i++)
        {  
            var rs = res[i];
            if(rs != null)
            {
                JudgeGenreListObj.options[i].text = rs.Name;            
                JudgeGenreListObj.options[i].value = rs.ID;
            }
        }
        RemoveTargetHaveSameSource(JudgeGenreListObj, SourceGenreListObj);
    }
    else
        JudgeGenreListObj.options.length = 0;
}

function loadAllGenres()
{
    HiddenAllGenreArray = HiddenAllGenresList.split('^^');
    SourceGenreListObj.options.length = 0;
    SourceGenreListObj.options.length = HiddenAllGenreArray.length;
    for (var i = 0; i < HiddenAllGenreArray.length; i++) 
    {            
        var OneInfo = new Array();
        OneInfo = HiddenAllGenreArray[i].split('^');
        SourceGenreListObj.options[i].text = OneInfo[0];            
        SourceGenreListObj.options[i].value = OneInfo[1];            
        SourceGenreListObj.options[i].selected = false;        
    }    
}

function GetAllGenresSimpleWithAdminCountResults_SucceededCallback(result)
{
    if(result != null && result != "[]" && result != "")
    {
        var res=Sys.Serialization.JavaScriptSerializer.deserialize(result);
        SourceGenreListObj.options.length = 0;
        SourceGenreListObj.options.length = res.length;
        
        for (var i = 0; i < res.length; i++) 
        {            
            var rs = res[i];
            if(rs != null)
            {
                SourceGenreListObj.options[i].text = rs.Name;            
                SourceGenreListObj.options[i].value = rs.ID;            
                SourceGenreListObj.options[i].selected = false;      
            }  
        }    
    }
    else
        JudgeGenreListObj.options.length = 0;
    CDBB.Web.UI.Services.AdminService.GetGenresForAdminRound(parseInt(HiddenEditAdminIDObj.value), parseInt(HiddenEditRoundIDObj.value), GetGenresForAdminResults_SucceededCallback);    
}