
// Compare two options within a list by VALUES
function compareOptionValues(a, b) 
{ 
  var sA = parseInt( a.value, 36 );  
  var sB = parseInt( b.value, 36 );  
  return sA - sB;
}

// Compare two options within a list by TEXT
function compareOptionText(a, b) 
{
  var sA = parseInt( a.text, 36 );  
  var sB = parseInt( b.text, 36 );  
  return sA - sB;
}

// Dual list move function
function moveDualList( srcList, destList, moveAll ) 
{
  if (  ( srcList.selectedIndex == -1 ) && ( moveAll == false )   )
    return;
  newDestList = new Array( destList.options.length );
  var len = 0;
  for( len = 0; len < destList.options.length; len++ )
  {
    if ( destList.options[ len ] != null )
      newDestList[ len ] = new Option( destList.options[ len ].text, destList.options[ len ].value, destList.options[ len ].defaultSelected, destList.options[ len ].selected );
  }

  for( var i = 0; i < srcList.options.length; i++ )
  {
    if ( srcList.options[i] != null && ( srcList.options[i].selected == true || moveAll ) )
    {
      newDestList[ len ] = new Option( srcList.options[i].text, srcList.options[i].value, srcList.options[i].defaultSelected, srcList.options[i].selected );
      len++;
    }
  }

  // Sort out the new destination list
  newDestList.sort( compareOptionValues );   // BY VALUES
  //newDestList.sort( compareOptionText );   // BY TEXT

  // Populate the destination with the items from the new array
  for ( var j = 0; j < newDestList.length; j++ )
  {
    if ( newDestList[ j ] != null )
      destList.options[ j ] = newDestList[ j ];
  }

  // Erase source list selected elements
  for( var i = srcList.options.length - 1; i >= 0; i-- )
  {
    if ( srcList.options[i] != null && ( srcList.options[i].selected == true || moveAll ) )
    {
       // Erase Source
       //srcList.options[i].value = "";
       //srcList.options[i].text  = "";
       srcList.options[i]       = null;
    }
  }

} // End of moveDualList()


  function SelectAllDestList(){
    //alert('here');
    var destlist=document.CTReportsMainEntryPage.SelectedFilterFieldValues;
    if (destlist==null)
      return;
    var i=0;
    var len=DestList.length;
    for(i=0 ; i<len;        i++)
      destlist.options[i].selected=true;}

   function testfunc(){
     alert(document.getElementById("testcol").innerText);
     return;
     alert(document.getElementById("RepRTMFNameLbl").innerText);
     document.getElementById("RepRTMFNameLbl").innerText="fff";
     alert(document.getElementById("RepRTMFNameLbl").innerText);
     return;
     alert(document.getElementById("testdiv").innerHTML);
     document.getElementById("testdiv").innerHTML="hhhhh";
     alert(document.getElementById("testdiv").innerHTML);
   }

   function SetCheckAllAccounts(val) {
    len=document.CTReportsMainEntryPage.elements.length;
    var elename;
    var i=0;
    for( i=0 ; i<len;      i++){
      elename=document.CTReportsMainEntryPage.elements[i].name;
          elename=elename.substring(0,3);
          if(elename=="row")
        document.CTReportsMainEntryPage.elements[i].checked=document.CTReportsMainEntryPage.checkuncheckall.checked;}}

  function EditCloneRepGroupClick(AUrl, RepAction){
    var TheURL=AUrl;
    TheURL=TheURL + "?RepRTMFname=" + document.CTReportsMainEntryPage.RepRTMFName.value;
    if (document.CTReportsMainEntryPage.UserDirName!=null)
      TheURL=TheURL + "&UserDirName=" + document.CTReportsMainEntryPage.UserDirName.value;
    TheURL=TheURL + "&CustomRep=" + document.CTReportsMainEntryPage.CustomRep.value;
    TheURL=TheURL + "&ReportAction="+RepAction;
    if (document.CTReportsMainEntryPage.ReportGroup!=null)
      TheURL=TheURL + "&ReportGroup=" + document.CTReportsMainEntryPage.ReportGroup.value;
    //alert(TheURL);
    //window.location=TheURL;
    var FeatureStr = "scrollbars=yes ,resizable=yes ,toolbar=no ,menubar=no ,statusbar=yes, ";
    FeatureStr = FeatureStr + "width=1100, height=875, top=75, left=60";
    window.open(TheURL, "", FeatureStr);
  }

  function EditRepClick(){
    var TheURL="CTReportsEditPage";
    TheURL=TheURL + "?RepRTMFname=" + document.CTReportsMainEntryPage.RepRTMFName.value;
    if (document.CTReportsMainEntryPage.UserDirName!=null)
      TheURL=TheURL + "&UserDirName=" + document.CTReportsMainEntryPage.UserDirName.value;
    TheURL=TheURL + "&CustomRep=" + document.CTReportsMainEntryPage.CustomRep.value;
    TheURL=TheURL + "&ReportAction=edit&ReportGroup=" + document.CTReportsMainEntryPage.ReportGroup.value;
//    alert(TheURL);
    window.location=TheURL;
    //window.open(TheURL, "", "scrollbars=yes ,resizable=yes ,toolbar=yes ,menubar=yes ,statusbar=yes");
  }

  function CloneRepClick(){
    var TheURL="CTReportsEditPage";
    TheURL=TheURL + "?RepRTMFname=" + document.CTReportsMainEntryPage.RepRTMFName.value;
    if (document.CTReportsMainEntryPage.UserDirName!=null)
      TheURL=TheURL + "&UserDirName=" + document.CTReportsMainEntryPage.UserDirName.value;
    TheURL=TheURL + "&CustomRep=" + document.CTReportsMainEntryPage.CustomRep.value;
    TheURL=TheURL + "&ReportAction=clone&ReportGroup=" + document.CTReportsMainEntryPage.ReportGroup.value;
    alert(TheURL);
    window.location=TheURL;
    //window.open(TheURL, "", "scrollbars=yes ,resizable=yes ,toolbar=yes ,menubar=yes ,statusbar=yes");
  }

  function DoEditFilter(FilterNo){
    document.CTReportsMainEntryPage.CurrFilterNo.value=FilterNo;
    __doPostBack('ChangeFilterBtn', 'CLICK');
  }

  function DoDeleteFilter(FilterNo){
    document.CTReportsMainEntryPage.CurrFilterNo.value=FilterNo;
    __doPostBack('DeleteFilterBtn', 'CLICK');
  }

  function InitializeHiddenVars(){
    if (document.CTReportsMainEntryPage.RepSetFound!=null && document.CTReportsMainEntryPage.RepSetFound.value=="true") {
      if (document.CTReportsMainEntryPage.iDeviceType!=null){
        //document.CTReportsMainEntryPage.rdReportFormat.value=document.CTReportsMainEntryPage.iDeviceType.value;
        //alert(document.CTReportsMainEntryPage.iDeviceType.value + " " + document.CTReportsMainEntryPage.rdReportFormat.value);
        setCheckedValue(document.CTReportsMainEntryPage.rdReportFormat, document.CTReportsMainEntryPage.iDeviceType.value);
      }
      if (document.CTReportsMainEntryPage.AutoResumeRep!=null && document.CTReportsMainEntryPage.AutoResumeRep.value=="true") {
        if (document.CTReportsMainEntryPage.SkipLastRunAcctsCBox!=null)
           document.CTReportsMainEntryPage.SkipLastRunAcctsCBox.checked=true;
        if (document.CTReportsMainEntryPage.SavePDFAfterGenerateCBox!=null)
           document.CTReportsMainEntryPage.SavePDFAfterGenerateCBox.checked=true;
        if (document.CTReportsMainEntryPage.SaveAfterGenerate!=null)
           document.CTReportsMainEntryPage.SaveAfterGenerate.checked=true;
        if (document.CTReportsMainEntryPage.ClearPrevArchFiles!=null){
           document.CTReportsMainEntryPage.ClearPrevArchFiles.checked=true;
           if (document.CTReportsMainEntryPage.AutoRunRep!=null && document.CTReportsMainEntryPage.AutoRunRep.value=="true")
             alert('Report is being run in Auto Resume mode to potentially recover from an error and will resume processing at the last processed account.');
        }
      }

      if (document.CTReportsMainEntryPage.AutoRunRep!=null && document.CTReportsMainEntryPage.AutoRunRep.value=="true") {
        __doPostBack('GenRepBtn', 'CLICK');
      //alert("hi");
      }
    }
    else {
      setCheckedValue(document.CTReportsMainEntryPage.rdReportFormat, document.CTReportsMainEntryPage.iDeviceType.value);
    }
    //DoSavePDFAfterGenCBoxClick();
    //DoEmailPDFAfterGenCBoxClick();
    window.focus();
  }

  function WarnForSkipRunAccts(){
    var theform = document.forms[0];
    var theSkipLastRunFlag = null;
    if (theform.SkipLastRunAcctsCBox != null){
      if (theform.SkipLastRunAcctsCBox.checked)
        theSkipLastRunFlag = "true";
      else
        theSkipLastRunFlag = "false"
      }

    var theClearPrevArchFilesFlag = null;
    if (theform.ClearPrevArchFiles != null){
      if (theform.ClearPrevArchFiles.checked)
        theClearPrevArchFilesFlag = "true";
      else
        theClearPrevArchFilesFlag = "false";
    }

    //alert("ff");
    if ((theSkipLastRunFlag == null) && (theClearPrevArchFilesFlag == null))
      return(1);
    else{
      var UserResult;
      var MsgStr="";
      if ((theSkipLastRunFlag != null) && (theSkipLastRunFlag == "true"))
        MsgStr = ' WARNING : Report is being run with the "Skip Previously Generated Accounts" option checked. This could result in a stale (previously saved Internal - RAF format) file being used.\n' +
                 ' Unless you are recovering from a partially completed report run(due to an error), you should uncheck this option.';

      if (MsgStr!=""){
        UserResult = confirm(MsgStr);
        if(UserResult=="0")
           return(0);
        else
           return(1);
      }
      else
           return(1);
    }
  }

  function ValidateAccountSel(){
    len=document.CTReportsMainEntryPage.elements.length;
    var elename;
    var AccountSelected=new Boolean(false);
    AccountSelected=false;
    for(var i=0; i<len; i++){
      elename=document.CTReportsMainEntryPage.elements[i].name;
          elename=elename.substring(0,3);
          if(elename=="row"){
            if(document.CTReportsMainEntryPage.elements[i].checked){
              AccountSelected=true;
              //alert("sel");
                  break;
            }
          }
    }
    //alert("Please");
    if (AccountSelected){
      var AUrl = "";
      if (DoSaveAndRunSchedRepNow(AUrl) == 0)
        return 0;
      else{
        if (document.CTReportsMainEntryPage.AutoResumeRep!=null && document.CTReportsMainEntryPage.AutoResumeRep.value=="true")
          return 1;
        else{
          if (WarnForSkipRunAccts() == 0)
            return 0;
          else
            return 1;
        }
      }
    }
    else{
      alert("Please select atleast one account number before generating report.");
      return 0;
    }
  }

  function MakeReportFName(iReportFName){
    document.getElementById("RepRTMFNameLbl").innerText=iReportFName;
  }

  function callTest(){
    document.CTReportsMainEntryPage.onSubmit="SelectAllDestList()"
  }

