$(document).ready(function(){

    
    //If saveRenewal flag present, save the renewal (or rather, ATTEMPT to save the record)
    if( $('#SaveRenewal').val() == 1 )
    {
        $('#Edycjadanych').submit()
        
        return true
    }
    
    
    if( $('#searchType').val() == 'name') {
        
        $('#marketOptions').parent().show('slow');
        $('#statusOptions').parent().show('slow');
        $('#typeOptions').parent().show('slow');
        $('#yearOptions').parent().show('slow');
        
    } else {
        
        $('#marketOptions').parent().hide('slow');
        $('#statusOptions').parent().hide('slow');
        $('#typeOptions').parent().hide('slow');
        $('#yearOptions').parent().hide('slow');
        
    }
    
    
    
    $('#searchType').change(function(){
        
        if( (this).value == 'name') {
          
            $('#marketOptions').parent().show('slow');
            $('#statusOptions').parent().show('slow');
            $('#typeOptions').parent().show('slow');
            $('#yearOptions').parent().show('slow');
           
        } else {
          
            $('#marketOptions').parent().hide('slow');
            $('#statusOptions').parent().hide('slow');
            $('#typeOptions').parent().hide('slow');
            $('#yearOptions').parent().hide('slow');
          
        }
       
    });
 
   
    //Form
    $('#Edycjadanych').submit(function(eve){  
          
        //  opportunities_opYear vs. opportunities_startDate
        var startDate = $('#opportunities_startDate').val();
        var oppYear =   $('#opportunities_opYear').val();
        
        if(startDate){
            
            //get the 4-digit year
            startDate = startDate.substring(0,4);
            
            if( startDate != oppYear ){
                    
                var yearMsg = "The projected subscription start-date year (" + startDate + ") should usually match the year of the opportunity (" + oppYear + ") unless it is at the end of the year, so please check that is correct.\n";
                    
                yearMsg += "\nClick OK to save, otherwise click Cancel";
                  
                if ( !confirm( yearMsg )) {
                    
                    return false;
                }
            }
        }
    
        return true;
      
    });
    
    
    //New Record Button  
    $("#newRecord").click(function() {
        
        var editMode = $("#editMode").val()
       
        //Only for edit mode
        if( editMode )
        {
            var msg = "Are you sure you want to create a new record?\nAny unsaved changes (current record) will be lost\n";
                
            if ( !confirm( msg )) {
              
                return false
               
            }
        }
        
        var target = "/?p=list&t=deskforce_opportunityedit"
        window.location.replace( target )
        return true
      
    });
    
    
    //Refresh Button  
    $("#refresh").click(function() {
        
        var editMode = $("#editMode").val()
        var opID  = $("#opID").val()

        //Confirm only for edit mode
        if( editMode )
        {
          
            var msg = "Are you sure you want to refresh?\nAny unsaved changes will be lost\n"
            var target = "/?p=list&t=deskforce_opportunityedit&id="+opID
                    
            if ( !confirm( msg )) {
                
                return false
               
            }
            
        }
        else // view Mode
        {
            
            var target = "/?p=list&t=deskforce_opportunityview&id="+opID
            
        }
        

        window.location.replace( target )        
        return true
      
    });
    
    

    //Create Renewal Button  
    $("#duprec").click(function() {
        
        var editMode = $("#editMode").val()
        
        //Only for edit mode
        if( editMode )
        {
            
            var msg = "Are you sure you want to create a renewal?\nAny unsaved changes (current record) will be lost\n";
                
            if ( !confirm( msg ) )
            {
                
                return false
               
            }
        
        }
        
        var opID  = $("#opID").val()
        var target = "/?p=list&t=deskforce_opportunityedit&duprec=1&id="+opID    
        window.location.replace( target )
        
     
        return true
      
    });
    
    
    //View Mode Button  
    $("#viewMode").click(function() {
        
        var msg = "Are you sure you want to change to view mode?\nAny unsaved changes will be lost\n";
                
        if ( confirm( msg )) {
            
            var opID  = $("#opID").val()
            var target = "/?p=list&t=deskforce_opportunityview&id="+opID
            
            window.location.replace( target )
           
        }
        
        return false
      
    });
    
    
});