var tmpURLValue = ''; 

function changeType ( sType )
{
  try {
  switch ( sType )
  {
    case 1:
      document.itemForm.ResourceTypeID[0].checked = true;
      document.itemForm.File1.disabled = document.itemForm.File2.disabled = document.itemForm.File3.disabled = document.itemForm.File4.disabled = document.itemForm.File5.disabled= true;
      document.itemForm.Url.value = tmpURLValue;
      document.itemForm.Url.disabled = false;
      document.getElementById("WebCastProducer").style.display = 'none';
    break;
    case 2:
      document.itemForm.ResourceTypeID[1].checked = true;
      document.itemForm.File1.disabled = document.itemForm.File2.disabled = document.itemForm.File3.disabled = document.itemForm.File4.disabled = document.itemForm.File5.disabled= false;
      if (!tmpURLValue) tmpURLValue = document.itemForm.Url.value;
      document.itemForm.Url.value = '';
      document.itemForm.Url.disabled = true;
      document.getElementById("WebCastProducer").style.display = 'none';
    break;
    case 4:
      document.itemForm.File1.disabled = document.itemForm.File2.disabled = document.itemForm.File3.disabled = document.itemForm.File4.disabled = document.itemForm.File5.disabled= true;
      if (!tmpURLValue) tmpURLValue = document.itemForm.Url.value;
      document.itemForm.Url.value = '';
      document.itemForm.Url.disabled = true;
      document.getElementById("WebCastProducer").style.display = 'block';
    break;
  }
  }catch(e){}

}
  function trim(s)
  {
     var temp = s;
     return temp.replace(/^s+/, "").replace(/s+$/,"");
  }
  
  function validateExtension( fn ) 
  {   
    if (fn != undefined) 
    {   
      fn = fn.replace(/^\s+|\s+$/g,""); // trim
      // this shouldnt be hardcoded
      if (fn.match(/\.(doc|htm|html|pdf|ppt|ppz|pps|pot|pub|rtf|txt|xls|xlc|xll|xlm|xlw|wpd|gif|jpg|jpeg|png|tif|tiff|docx|dotx|ppsx|potx|pptx|xlsx|xltx|zip)$/i)) 
      {    
        return true;            
      } 
    return false;
    }
  }  

  function getFileExtension( fn ) 
  {
    return fn.substring( fn.lastIndexOf("."), fn.length );
  }      

  function arrHasDupes( arr )
  {
    // Returns an associative array with values as the
    // number of instances.
    var i, count;
    var arrResult = new Array();
    count = 0;    
    
    // Initalize the associative array.
    for (i=0; i<arr.length; i++ )
    {
      arrResult[arr[i]] = 0;
    }
    for (i=0; i<arr.length; i++)
    {
      // Increment count if found in array.
      if ( arr.indexOf( arr[i] ) > -1 )
      {
        arrResult[arr[i]]++;
        count++;      
      }
    }             
    return arrResult;      
  }                        
  
  var DupeList = "";
  
  $("form#itemForm").submit( 
  function () 
  {  
  // Check that at least 1 folder is selected.
  if ( $("select#FolderID").length > 0 && $("select#FolderID").val() == null )
  {
    alert("A folder must be selected to submit the resource.");                
    return false;
  } 
    
  // Check if there's checkboxes to delete - means they've already uploaded.
  var isThereFilesOnServer = $("input[name^='delete']").length;    
  
  // If something was inserted into the FCKEditor, then count that as an HTML file. This will be
  // added to the array to later check for dupes. Check if FCKEditor is being used first on the page.
  var FCKEdited = false;
  if ( typeof FCKeditorAPI != "undefined" ) 
  {     
    if ( trim( FCKeditorAPI.GetInstance("HTMLContent").GetXHTML(true) ).length == 0 )
    {
      FCKEdited = true;         
    }       
  }      
  
  // Check for duplicate and valid filetypes...         
  if ( $("#type_File:checked").val() == 2 )
  {
    var filesubmitted = 0;
      
    // Check that at least one file has been submitted.      
    for(j=1; j<=$("#FileNamesList li").length; j++) 
    {      
      var filename = $("#File"+j).val();      
      if (( $("#File"+j).val().length == 0  ) )  
      {
        filesubmitted++;  
      }
      
      // Check if they've browsed for at least 1 file, if there's a file on the server, if they've entered 
      // text in the FCKEditor and take into account for only 1 html file (via file browser or FCKEditor).
      if ($("#FileNamesList li").length == filesubmitted && !isThereFilesOnServer  &&  (FCKEdited || getFileExtension ( filename ).match(/\.(html|htm)$/) ) ) 
      {
        alert( "You must submit at least one file." );
        return false; 
      }         
    }          
                
    // Check that the filenames are of valid extension.      
    for(j=1; j<=$("#FileNamesList li").length; j++) 
    {
      filename = $("#File"+j).val();      
      if (( $("#File"+j).val().length != 0  ) && ( !validateExtension ( filename ) ))  
      {
        alert( filename + " is not a valid file." ); 
        return false;        
      }   
    }
    
    var arrFileExtension = [];
    var arrFileNames     = [];
    var arrAllFileNames  = [];        
     
    // Get filenames from the file textboxes and put in array.    
    for (j=0; j<=$("#FileNamesList li").length; j++) 
    {
      // Check that the field is defined AND strlength of filename is > 0 to be put in array. 
      if( $("#File"+(j+1)).val() != undefined  &&  $("#File"+(j+1)).val().length != 0 ) 
      {         
        arrFileNames[j]     = $("#File"+(j+1)).val();                              
      }
    }        
    
    
    // Get the file extensions of files.     
    if ( arrAllFileNames.length )
    {
      for (j=0; j<arrAllFileNames.length; j++) 
      {         
        arrFileExtension[j] = getFileExtension( arrAllFileNames[j] );                                   
      }
    }    
    
    // Get a list of duplicate extensions.  
    arrFileDupes = arrHasDupes( arrFileExtension );          
      
    if ( FCKEdited )
    {
      arrFileDupes.push(".html");
    }         
            
    // Put together a list of duplicate files to show user.              
    for(i in arrFileDupes)
    {      
      if ( arrFileDupes[i] > 1 ) 
      {        
        DupeList = DupeList + i + ",";        
      }      
    }
    
    // Show message to user. 
    if ( DupeList.length > 0 )
    { 
      DupeList = DupeList.substring( 0, DupeList.length-1 );
      alert("You must upload only one type of file when you upload.\nYou have duplicate " + DupeList + " file(s).");      
      DupeList         = "";        
      return false;
    }        
   } 
  }    
);  
